blob: 505708a761a89b16bc26a066c5db7d1f823765dc [file] [log] [blame]
Elliott Hughes6b586e72021-04-15 13:39:08 -07001#!/usr/bin/env python3
Dan Albertffa5cbe2021-02-03 16:44:37 -08002# Run with directory arguments from any directory, with no special setup
3# required.
Elliott Hughes387d4b72012-08-09 15:17:46 -07004
Elliott Hughes387d4b72012-08-09 15:17:46 -07005import os
Dan Albertffa5cbe2021-02-03 16:44:37 -08006from pathlib import Path
Elliott Hughes387d4b72012-08-09 15:17:46 -07007import re
Elliott Hughes387d4b72012-08-09 15:17:46 -07008import sys
Dan Albertffa5cbe2021-02-03 16:44:37 -08009from typing import Sequence
Elliott Hughes387d4b72012-08-09 15:17:46 -070010
Elliott Hughesaac7c3a2017-07-14 10:00:32 -070011VERBOSE = False
Elliott Hughes387d4b72012-08-09 15:17:46 -070012
Dan Albertffa5cbe2021-02-03 16:44:37 -080013copyrights = set()
14
15
Elliott Hughesaac7c3a2017-07-14 10:00:32 -070016def warn(s):
17 sys.stderr.write("warning: %s\n" % s)
18
Dan Albertffa5cbe2021-02-03 16:44:37 -080019
Elliott Hughesaac7c3a2017-07-14 10:00:32 -070020def warn_verbose(s):
21 if VERBOSE:
22 warn(s)
23
Dan Albertffa5cbe2021-02-03 16:44:37 -080024
25def is_interesting(path_str: str) -> bool:
26 path = Path(path_str.lower())
Elliott Hughesaac7c3a2017-07-14 10:00:32 -070027 uninteresting_extensions = [
28 ".bp",
29 ".map",
Elliott Hughesc5db38a2020-06-15 17:26:58 -070030 ".md",
Elliott Hughesaac7c3a2017-07-14 10:00:32 -070031 ".mk",
32 ".py",
33 ".pyc",
34 ".swp",
35 ".txt",
Christopher Ferris852f9b02023-06-02 16:34:28 -070036 ".xml",
Elliott Hughesaac7c3a2017-07-14 10:00:32 -070037 ]
Dan Albertffa5cbe2021-02-03 16:44:37 -080038 if path.suffix in uninteresting_extensions:
Elliott Hughesaac7c3a2017-07-14 10:00:32 -070039 return False
Dan Albertffa5cbe2021-02-03 16:44:37 -080040 if path.name in {"notice", "readme", "pylintrc"}:
Elliott Hughesaac7c3a2017-07-14 10:00:32 -070041 return False
Dan Albert77d976c2021-04-19 14:05:59 -070042 # Backup files for some editors.
43 if path.match("*~"):
44 return False
Elliott Hughesaac7c3a2017-07-14 10:00:32 -070045 return True
46
Dan Albertffa5cbe2021-02-03 16:44:37 -080047
Elliott Hughesaac7c3a2017-07-14 10:00:32 -070048def is_auto_generated(content):
Elliott Hughes22a0d6f2014-03-06 15:10:22 -080049 if "Generated by gensyscalls.py" in content or "generated by genserv.py" in content:
Elliott Hughes387d4b72012-08-09 15:17:46 -070050 return True
51 if "This header was automatically generated from a Linux kernel header" in content:
52 return True
53 return False
54
Elliott Hughes387d4b72012-08-09 15:17:46 -070055
Dan Albertffa5cbe2021-02-03 16:44:37 -080056def is_copyright_end(line: str, first_line_was_hash: bool) -> bool:
57 endings = [
58 " $FreeBSD: ",
59 "$Citrus$",
60 "$FreeBSD$",
61 "*/",
62 "From: @(#)",
63 # OpenBSD likes to say where stuff originally came from:
64 "Original version ID:",
65 "\t$Citrus: ",
66 "\t$NetBSD: ",
67 "\t$OpenBSD: ",
68 "\t@(#)",
69 "\tcitrus Id: ",
70 "\tfrom: @(#)",
71 "from OpenBSD:",
72 ]
73 if first_line_was_hash and not line:
74 return True
75
76 for ending in endings:
77 if ending in line:
78 return True
79
80 return False
81
82
83def extract_copyright_at(lines: Sequence[str], i: int) -> int:
84 first_line_was_hash = lines[i].startswith("#")
Elliott Hughes387d4b72012-08-09 15:17:46 -070085
Elliott Hughes261e2232012-08-14 15:04:05 -070086 # Do we need to back up to find the start of the copyright header?
87 start = i
Dan Albertffa5cbe2021-02-03 16:44:37 -080088 if not first_line_was_hash:
Elliott Hughes261e2232012-08-14 15:04:05 -070089 while start > 0:
90 if "/*" in lines[start - 1]:
91 break
92 start -= 1
93
Elliott Hughes387d4b72012-08-09 15:17:46 -070094 # Read comment lines until we hit something that terminates a
95 # copyright header.
Elliott Hughes387d4b72012-08-09 15:17:46 -070096 while i < len(lines):
Dan Albertffa5cbe2021-02-03 16:44:37 -080097 if is_copyright_end(lines[i], first_line_was_hash):
Elliott Hughesbfa582d2014-05-05 14:58:17 -070098 break
Elliott Hughes387d4b72012-08-09 15:17:46 -070099 i += 1
100
101 end = i
102
103 # Trim trailing cruft.
104 while end > 0:
Dan Albertffa5cbe2021-02-03 16:44:37 -0800105 line = lines[end - 1]
106 if line not in {
107 " *", " * ===================================================="
108 }:
Elliott Hughes387d4b72012-08-09 15:17:46 -0700109 break
110 end -= 1
111
112 # Remove C/assembler comment formatting, pulling out just the text.
113 clean_lines = []
114 for line in lines[start:end]:
115 line = line.replace("\t", " ")
116 line = line.replace("/* ", "")
Dan Albertffa5cbe2021-02-03 16:44:37 -0800117 line = re.sub(r"^ \* ", "", line)
Elliott Hughes387d4b72012-08-09 15:17:46 -0700118 line = line.replace("** ", "")
119 line = line.replace("# ", "")
Elliott Hughesab528072018-07-24 00:01:52 +0000120 if "SPDX-License-Identifier:" in line:
121 continue
Elliott Hughes387d4b72012-08-09 15:17:46 -0700122 if line.startswith("++Copyright++"):
123 continue
124 line = line.replace("--Copyright--", "")
125 line = line.rstrip()
126 # These come last and take care of "blank" comment lines.
Dan Albertffa5cbe2021-02-03 16:44:37 -0800127 if line in {"#", " *", "**", "-"}:
Elliott Hughes387d4b72012-08-09 15:17:46 -0700128 line = ""
129 clean_lines.append(line)
130
131 # Trim blank lines from head and tail.
132 while clean_lines[0] == "":
133 clean_lines = clean_lines[1:]
134 while clean_lines[len(clean_lines) - 1] == "":
135 clean_lines = clean_lines[0:(len(clean_lines) - 1)]
136
Dan Albertffa5cbe2021-02-03 16:44:37 -0800137 copyrights.add("\n".join(clean_lines))
Elliott Hughes387d4b72012-08-09 15:17:46 -0700138
139 return i
140
Elliott Hughes387d4b72012-08-09 15:17:46 -0700141
Dan Albertffa5cbe2021-02-03 16:44:37 -0800142def do_file(path: str) -> None:
143 raw = Path(path).read_bytes()
144 try:
145 content = raw.decode("utf-8")
146 except UnicodeDecodeError:
147 warn("bad UTF-8 in %s" % path)
148 content = raw.decode("iso-8859-1")
Elliott Hughes387d4b72012-08-09 15:17:46 -0700149
Elliott Hughesaac7c3a2017-07-14 10:00:32 -0700150 lines = content.split("\n")
151
152 if len(lines) <= 4:
153 warn_verbose("ignoring short file %s" % path)
154 return
155
156 if is_auto_generated(content):
157 warn_verbose("ignoring auto-generated file %s" % path)
158 return
159
160 if not "Copyright" in content:
161 if "public domain" in content.lower():
Elliott Hughesc5db38a2020-06-15 17:26:58 -0700162 warn_verbose("ignoring public domain file %s" % path)
Elliott Hughesaac7c3a2017-07-14 10:00:32 -0700163 return
Dan Albertffa5cbe2021-02-03 16:44:37 -0800164 warn('no copyright notice found in "%s" (%d lines)' %
165 (path, len(lines)))
Elliott Hughesaac7c3a2017-07-14 10:00:32 -0700166 return
167
Dan Albertffa5cbe2021-02-03 16:44:37 -0800168 # Manually iterate because extract_copyright_at tells us how many lines to
169 # skip.
Elliott Hughesaac7c3a2017-07-14 10:00:32 -0700170 i = 0
171 while i < len(lines):
172 if "Copyright" in lines[i] and not "@(#) Copyright" in lines[i]:
173 i = extract_copyright_at(lines, i)
174 else:
175 i += 1
176
177
Dan Albertffa5cbe2021-02-03 16:44:37 -0800178def do_dir(arg):
Elliott Hughes387d4b72012-08-09 15:17:46 -0700179 for directory, sub_directories, filenames in os.walk(arg):
180 if ".git" in sub_directories:
181 sub_directories.remove(".git")
182 sub_directories = sorted(sub_directories)
183
184 for filename in sorted(filenames):
185 path = os.path.join(directory, filename)
Elliott Hughesaac7c3a2017-07-14 10:00:32 -0700186 if is_interesting(path):
187 do_file(path)
Elliott Hughes387d4b72012-08-09 15:17:46 -0700188
Elliott Hughes387d4b72012-08-09 15:17:46 -0700189
Dan Albertffa5cbe2021-02-03 16:44:37 -0800190def main() -> None:
191 args = sys.argv[1:]
192 if len(args) == 0:
193 args = ["."]
Elliott Hughes387d4b72012-08-09 15:17:46 -0700194
Dan Albertffa5cbe2021-02-03 16:44:37 -0800195 for arg in args:
196 if os.path.isdir(arg):
197 do_dir(arg)
198 else:
199 do_file(arg)
Elliott Hughes387d4b72012-08-09 15:17:46 -0700200
Dan Albertffa5cbe2021-02-03 16:44:37 -0800201 for notice in sorted(copyrights):
202 print(notice)
203 print()
204 print("-" * 67)
205 print()
Elliott Hughes387d4b72012-08-09 15:17:46 -0700206
Dan Albertffa5cbe2021-02-03 16:44:37 -0800207
208if __name__ == "__main__":
209 main()