blob: d54c16419e97b5cb6ac1551898207d5d13662b29 [file] [log] [blame]
Nick Terrell1f144352020-03-26 16:57:48 -07001#!/usr/bin/env python3
2
3# ################################################################
Elliott Hughes44aba642023-09-12 20:18:59 +00004# Copyright (c) Meta Platforms, Inc. and affiliates.
Nick Terrell1f144352020-03-26 16:57:48 -07005# All rights reserved.
6#
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# ################################################################
12
Nick Terrell1f144352020-03-26 16:57:48 -070013import enum
14import glob
15import os
Nick Terrella4943082021-03-29 14:23:36 -070016import re
Nick Terrell1f144352020-03-26 16:57:48 -070017import sys
18
Nick Terrell1f144352020-03-26 16:57:48 -070019ROOT = os.path.join(os.path.dirname(__file__), "..")
20
21RELDIRS = [
22 "doc",
23 "examples",
24 "lib",
25 "programs",
26 "tests",
Nick Terrella4943082021-03-29 14:23:36 -070027 "contrib/linux-kernel",
Nick Terrell1f144352020-03-26 16:57:48 -070028]
29
Nick Terrella4943082021-03-29 14:23:36 -070030REL_EXCLUDES = [
31 "contrib/linux-kernel/test/include",
32]
Nick Terrell1f144352020-03-26 16:57:48 -070033
Nick Terrella4943082021-03-29 14:23:36 -070034def to_abs(d):
35 return os.path.normpath(os.path.join(ROOT, d)) + "/"
Nick Terrell1f144352020-03-26 16:57:48 -070036
Nick Terrella4943082021-03-29 14:23:36 -070037DIRS = [to_abs(d) for d in RELDIRS]
38EXCLUDES = [to_abs(d) for d in REL_EXCLUDES]
39
40SUFFIXES = [
41 ".c",
42 ".h",
43 "Makefile",
44 ".mk",
45 ".py",
Nick Terrellc7b03c22022-01-07 09:35:27 -080046 ".S",
Nick Terrella4943082021-03-29 14:23:36 -070047]
Nick Terrell1f144352020-03-26 16:57:48 -070048
49# License should certainly be in the first 10 KB.
50MAX_BYTES = 10000
51MAX_LINES = 50
52
53LICENSE_LINES = [
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.",
58]
59
60COPYRIGHT_EXCEPTIONS = {
61 # From zstdmt
62 "threading.c",
63 "threading.h",
64 # From divsufsort
65 "divsufsort.c",
66 "divsufsort.h",
67}
68
69LICENSE_EXCEPTIONS = {
70 # From divsufsort
71 "divsufsort.c",
72 "divsufsort.h",
Nick Terrella4943082021-03-29 14:23:36 -070073 # License is slightly different because it references GitHub
74 "linux_zstd.h",
Nick Terrell1f144352020-03-26 16:57:48 -070075}
76
77
78def valid_copyright(lines):
Nick Terrella4943082021-03-29 14:23:36 -070079 YEAR_REGEX = re.compile("\d\d\d\d|present")
Nick Terrell1f144352020-03-26 16:57:48 -070080 for line in lines:
81 line = line.strip()
82 if "Copyright" not in line:
83 continue
84 if "present" in line:
85 return (False, f"Copyright line '{line}' contains 'present'!")
Elliott Hughes44aba642023-09-12 20:18:59 +000086 if "Meta Platforms, Inc" not in line:
87 return (False, f"Copyright line '{line}' does not contain 'Meta Platforms, Inc'")
Nick Terrella4943082021-03-29 14:23:36 -070088 year = YEAR_REGEX.search(line)
89 if year is not None:
90 return (False, f"Copyright line '{line}' contains {year.group(0)}; it should be yearless")
Nick Terrell1f144352020-03-26 16:57:48 -070091 if " (c) " not in line:
92 return (False, f"Copyright line '{line}' does not contain ' (c) '!")
93 return (True, "")
94 return (False, "Copyright not found!")
95
96
97def valid_license(lines):
98 for b in range(len(lines)):
99 if LICENSE_LINES[0] not in lines[b]:
100 continue
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}!
104Expected: '{LICENSE_LINES[l]}'
105Actual: '{lines[b + l]}'"""
106 return (False, message)
107 return (True, "")
108 return (False, "License not found!")
109
110
111def valid_file(filename):
112 with open(filename, "r") as f:
113 lines = f.readlines(MAX_BYTES)
114 lines = lines[:min(len(lines), MAX_LINES)]
Nick Terrella4943082021-03-29 14:23:36 -0700115
Nick Terrell1f144352020-03-26 16:57:48 -0700116 ok = True
117 if os.path.basename(filename) not in COPYRIGHT_EXCEPTIONS:
118 c_ok, c_msg = valid_copyright(lines)
119 if not c_ok:
Nick Terrella4943082021-03-29 14:23:36 -0700120 print(f"{filename}: {c_msg}", file=sys.stderr)
Nick Terrell1f144352020-03-26 16:57:48 -0700121 ok = False
122 if os.path.basename(filename) not in LICENSE_EXCEPTIONS:
123 l_ok, l_msg = valid_license(lines)
124 if not l_ok:
Nick Terrella4943082021-03-29 14:23:36 -0700125 print(f"{filename}: {l_msg}", file=sys.stderr)
Nick Terrell1f144352020-03-26 16:57:48 -0700126 ok = False
127 return ok
128
129
Nick Terrella4943082021-03-29 14:23:36 -0700130def exclude(filename):
131 for x in EXCLUDES:
132 if filename.startswith(x):
133 return True
134 return False
135
Nick Terrell1f144352020-03-26 16:57:48 -0700136def main():
137 invalid_files = []
138 for directory in DIRS:
Nick Terrella4943082021-03-29 14:23:36 -0700139 for suffix in SUFFIXES:
140 files = set(glob.glob(f"{directory}/**/*{suffix}", recursive=True))
Nick Terrell1f144352020-03-26 16:57:48 -0700141 for filename in files:
Nick Terrella4943082021-03-29 14:23:36 -0700142 if exclude(filename):
143 continue
Nick Terrell1f144352020-03-26 16:57:48 -0700144 if not valid_file(filename):
145 invalid_files.append(filename)
146 if len(invalid_files) > 0:
Nick Terrella4943082021-03-29 14:23:36 -0700147 print("Fail!", file=sys.stderr)
148 for f in invalid_files:
149 print(f)
150 return 1
Nick Terrell1f144352020-03-26 16:57:48 -0700151 else:
Nick Terrella4943082021-03-29 14:23:36 -0700152 print("Pass!", file=sys.stderr)
153 return 0
Nick Terrell1f144352020-03-26 16:57:48 -0700154
155if __name__ == "__main__":
Nick Terrell66e811d2021-01-04 17:53:52 -0500156 sys.exit(main())