3 # ################################################################
4 # Copyright (c) Meta Platforms, Inc. and affiliates.
7 # This source code is licensed under both the BSD-style license (found in the
8 # LICENSE file in the root directory of this source tree) and the GPLv2 (found
9 # in the COPYING file in the root directory of this source tree).
10 # You may select, at your option, one of the above-listed licenses.
11 # ################################################################
19 ROOT = os.path.join(os.path.dirname(__file__), "..")
27 "contrib/linux-kernel",
31 "contrib/linux-kernel/test/include",
35 return os.path.normpath(os.path.join(ROOT, d)) + "/"
37 DIRS = [to_abs(d) for d in RELDIRS]
38 EXCLUDES = [to_abs(d) for d in REL_EXCLUDES]
49 # License should certainly be in the first 10 KB.
54 "This source code is licensed under both the BSD-style license (found in the",
55 "LICENSE file in the root directory of this source tree) and the GPLv2 (found",
56 "in the COPYING file in the root directory of this source tree).",
57 "You may select, at your option, one of the above-listed licenses.",
60 COPYRIGHT_EXCEPTIONS = {
69 LICENSE_EXCEPTIONS = {
73 # License is slightly different because it references GitHub
78 def valid_copyright(lines):
79 YEAR_REGEX = re.compile("\d\d\d\d|present")
82 if "Copyright" not in line:
85 return (False, f"Copyright line '{line}' contains 'present'!")
86 if "Meta Platforms, Inc" not in line:
87 return (False, f"Copyright line '{line}' does not contain 'Meta Platforms, Inc'")
88 year = YEAR_REGEX.search(line)
90 return (False, f"Copyright line '{line}' contains {year.group(0)}; it should be yearless")
91 if " (c) " not in line:
92 return (False, f"Copyright line '{line}' does not contain ' (c) '!")
94 return (False, "Copyright not found!")
97 def valid_license(lines):
98 for b in range(len(lines)):
99 if LICENSE_LINES[0] not in lines[b]:
101 for l in range(len(LICENSE_LINES)):
102 if LICENSE_LINES[l] not in lines[b + l]:
103 message = f"""Invalid license line found starting on line {b + l}!
104 Expected: '{LICENSE_LINES[l]}'
105 Actual: '{lines[b + l]}'"""
106 return (False, message)
108 return (False, "License not found!")
111 def valid_file(filename):
112 with open(filename, "r") as f:
113 lines = f.readlines(MAX_BYTES)
114 lines = lines[:min(len(lines), MAX_LINES)]
117 if os.path.basename(filename) not in COPYRIGHT_EXCEPTIONS:
118 c_ok, c_msg = valid_copyright(lines)
120 print(f"{filename}: {c_msg}", file=sys.stderr)
122 if os.path.basename(filename) not in LICENSE_EXCEPTIONS:
123 l_ok, l_msg = valid_license(lines)
125 print(f"{filename}: {l_msg}", file=sys.stderr)
130 def exclude(filename):
132 if filename.startswith(x):
138 for directory in DIRS:
139 for suffix in SUFFIXES:
140 files = set(glob.glob(f"{directory}/**/*{suffix}", recursive=True))
141 for filename in files:
142 if exclude(filename):
144 if not valid_file(filename):
145 invalid_files.append(filename)
146 if len(invalid_files) > 0:
147 print("Fail!", file=sys.stderr)
148 for f in invalid_files:
152 print("Pass!", file=sys.stderr)
155 if __name__ == "__main__":