-
Notifications
You must be signed in to change notification settings - Fork 297
/
Copy pathverify_license.py
executable file
·139 lines (122 loc) · 4.01 KB
/
verify_license.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env python
#
# SPDX-License-Identifier: Apache-2.0
# Copyright Contributors to the OpenTimelineIO project
__doc__ = """The utility script checks to make sure that all of the source
files in the OpenTimelineIO project have the correct license header."""
import argparse
import os
import sys
LICENSES = {
".py": """# SPDX-License-Identifier: Apache-2.0
# Copyright Contributors to the OpenTimelineIO project
""",
".cpp": """// SPDX-License-Identifier: Apache-2.0
// Copyright Contributors to the OpenTimelineIO project
""",
".c": """// SPDX-License-Identifier: Apache-2.0
// Copyright Contributors to the OpenTimelineIO project
""",
".h": """// SPDX-License-Identifier: Apache-2.0
// Copyright Contributors to the OpenTimelineIO project
""",
".swift": """// SPDX-License-Identifier: Apache-2.0
// Copyright Contributors to the OpenTimelineIO project
""",
".mm": """// SPDX-License-Identifier: Apache-2.0
// Copyright Contributors to the OpenTimelineIO project
"""
}
# dependencies and build dir do not need to be checked
SKIP_DIRS = [
os.path.join("src", "deps"),
"build",
".git",
".venv",
]
def _parsed_args():
""" parse commandline arguments with argparse """
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument(
'-s',
'--start-dir',
default='.',
type=str,
help=("Directory to start searching for files in.")
)
parser.add_argument(
'-f',
'--fix',
default=False,
action="store_true",
help="Fix licenses in place when possible"
)
return parser.parse_args()
def main():
correct_license = 0
incorrect_license = 0
total = 0
args = _parsed_args()
for root, dirs, files in os.walk(args.start_dir):
for filename in files:
# make sure the dependencies aren't checked
if any(d in root for d in SKIP_DIRS):
continue
fullpath = os.path.join(root, filename)
for ext, lic in LICENSES.items():
if filename.endswith(ext):
total += 1
try:
content = open(fullpath).read()
except Exception as ex:
sys.stderr.write(
"ERROR: Unable to read file: {}\n{}".format(
fullpath,
ex
)
)
continue
if len(content) > 0 and lic not in content:
print(f"MISSING: {os.path.relpath(fullpath)}")
if args.fix:
content = LICENSES[os.path.splitext(fullpath)[1]]
with open(fullpath) as fi:
content += fi.read()
with open(fullpath, 'w') as fo:
fo.write(content)
print(
"...FIXED: {}".format(
os.path.relpath(fullpath)
)
)
incorrect_license += 1
else:
correct_license += 1
print(
"{} of {} files have the correct license.".format(
correct_license,
total
)
)
if incorrect_license != 0:
if not args.fix:
raise RuntimeError(
"ERROR: {} files do NOT have the correct license.\n".format(
incorrect_license
)
)
else:
print(
"{} files had the correct license added.".format(
incorrect_license
)
)
if __name__ == "__main__":
try:
main()
except RuntimeError as err:
sys.stderr.write(err.args[0])
sys.exit(1)