blob: 69d2d00e339d75e8c4a3184569c34090e7f10e68 [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
Dan Albertffa5cbe2021-02-03 16:44:37 -080048def is_copyright_end(line: str, first_line_was_hash: bool) -> bool:
49 endings = [
50 " $FreeBSD: ",
51 "$Citrus$",
52 "$FreeBSD$",
53 "*/",
54 "From: @(#)",
55 # OpenBSD likes to say where stuff originally came from:
56 "Original version ID:",
57 "\t$Citrus: ",
58 "\t$NetBSD: ",
59 "\t$OpenBSD: ",
60 "\t@(#)",
61 "\tcitrus Id: ",
62 "\tfrom: @(#)",
63 "from OpenBSD:",
64 ]
65 if first_line_was_hash and not line:
66 return True
67
68 for ending in endings:
69 if ending in line:
70 return True
71
72 return False
73
74
75def extract_copyright_at(lines: Sequence[str], i: int) -> int:
76 first_line_was_hash = lines[i].startswith("#")
Elliott Hughes387d4b72012-08-09 15:17:46 -070077
Elliott Hughes261e2232012-08-14 15:04:05 -070078 # Do we need to back up to find the start of the copyright header?
79 start = i
Dan Albertffa5cbe2021-02-03 16:44:37 -080080 if not first_line_was_hash:
Elliott Hughes261e2232012-08-14 15:04:05 -070081 while start > 0:
82 if "/*" in lines[start - 1]:
83 break
84 start -= 1
85
Elliott Hughes387d4b72012-08-09 15:17:46 -070086 # Read comment lines until we hit something that terminates a
87 # copyright header.
Elliott Hughes387d4b72012-08-09 15:17:46 -070088 while i < len(lines):
Dan Albertffa5cbe2021-02-03 16:44:37 -080089 if is_copyright_end(lines[i], first_line_was_hash):
Elliott Hughesbfa582d2014-05-05 14:58:17 -070090 break
Elliott Hughes387d4b72012-08-09 15:17:46 -070091 i += 1
92
93 end = i
94
95 # Trim trailing cruft.
96 while end > 0:
Dan Albertffa5cbe2021-02-03 16:44:37 -080097 line = lines[end - 1]
98 if line not in {
99 " *", " * ===================================================="
100 }:
Elliott Hughes387d4b72012-08-09 15:17:46 -0700101 break
102 end -= 1
103
104 # Remove C/assembler comment formatting, pulling out just the text.
105 clean_lines = []
106 for line in lines[start:end]:
107 line = line.replace("\t", " ")
108 line = line.replace("/* ", "")
Dan Albertffa5cbe2021-02-03 16:44:37 -0800109 line = re.sub(r"^ \* ", "", line)
Elliott Hughes387d4b72012-08-09 15:17:46 -0700110 line = line.replace("** ", "")
111 line = line.replace("# ", "")
Elliott Hughesab528072018-07-24 00:01:52 +0000112 if "SPDX-License-Identifier:" in line:
113 continue
Elliott Hughes387d4b72012-08-09 15:17:46 -0700114 if line.startswith("++Copyright++"):
115 continue
116 line = line.replace("--Copyright--", "")
117 line = line.rstrip()
118 # These come last and take care of "blank" comment lines.
Dan Albertffa5cbe2021-02-03 16:44:37 -0800119 if line in {"#", " *", "**", "-"}:
Elliott Hughes387d4b72012-08-09 15:17:46 -0700120 line = ""
121 clean_lines.append(line)
122
123 # Trim blank lines from head and tail.
124 while clean_lines[0] == "":
125 clean_lines = clean_lines[1:]
126 while clean_lines[len(clean_lines) - 1] == "":
127 clean_lines = clean_lines[0:(len(clean_lines) - 1)]
128
Dan Albertffa5cbe2021-02-03 16:44:37 -0800129 copyrights.add("\n".join(clean_lines))
Elliott Hughes387d4b72012-08-09 15:17:46 -0700130
131 return i
132
Elliott Hughes387d4b72012-08-09 15:17:46 -0700133
Dan Albertffa5cbe2021-02-03 16:44:37 -0800134def do_file(path: str) -> None:
135 raw = Path(path).read_bytes()
136 try:
137 content = raw.decode("utf-8")
138 except UnicodeDecodeError:
139 warn("bad UTF-8 in %s" % path)
140 content = raw.decode("iso-8859-1")
Elliott Hughes387d4b72012-08-09 15:17:46 -0700141
Elliott Hughesaac7c3a2017-07-14 10:00:32 -0700142 lines = content.split("\n")
143
144 if len(lines) <= 4:
145 warn_verbose("ignoring short file %s" % path)
146 return
147
Elliott Hughesaac7c3a2017-07-14 10:00:32 -0700148 if not "Copyright" in content:
149 if "public domain" in content.lower():
Elliott Hughesc5db38a2020-06-15 17:26:58 -0700150 warn_verbose("ignoring public domain file %s" % path)
Elliott Hughesaac7c3a2017-07-14 10:00:32 -0700151 return
Dan Albertffa5cbe2021-02-03 16:44:37 -0800152 warn('no copyright notice found in "%s" (%d lines)' %
153 (path, len(lines)))
Elliott Hughesaac7c3a2017-07-14 10:00:32 -0700154 return
155
Dan Albertffa5cbe2021-02-03 16:44:37 -0800156 # Manually iterate because extract_copyright_at tells us how many lines to
157 # skip.
Elliott Hughesaac7c3a2017-07-14 10:00:32 -0700158 i = 0
159 while i < len(lines):
160 if "Copyright" in lines[i] and not "@(#) Copyright" in lines[i]:
161 i = extract_copyright_at(lines, i)
162 else:
163 i += 1
164
165
Dan Albertffa5cbe2021-02-03 16:44:37 -0800166def do_dir(arg):
Elliott Hughes387d4b72012-08-09 15:17:46 -0700167 for directory, sub_directories, filenames in os.walk(arg):
168 if ".git" in sub_directories:
169 sub_directories.remove(".git")
170 sub_directories = sorted(sub_directories)
171
172 for filename in sorted(filenames):
173 path = os.path.join(directory, filename)
Elliott Hughesaac7c3a2017-07-14 10:00:32 -0700174 if is_interesting(path):
175 do_file(path)
Elliott Hughes387d4b72012-08-09 15:17:46 -0700176
Elliott Hughes387d4b72012-08-09 15:17:46 -0700177
Dan Albertffa5cbe2021-02-03 16:44:37 -0800178def main() -> None:
179 args = sys.argv[1:]
180 if len(args) == 0:
181 args = ["."]
Elliott Hughes387d4b72012-08-09 15:17:46 -0700182
Dan Albertffa5cbe2021-02-03 16:44:37 -0800183 for arg in args:
184 if os.path.isdir(arg):
185 do_dir(arg)
186 else:
187 do_file(arg)
Elliott Hughes387d4b72012-08-09 15:17:46 -0700188
Dan Albertffa5cbe2021-02-03 16:44:37 -0800189 for notice in sorted(copyrights):
190 print(notice)
191 print()
192 print("-" * 67)
193 print()
Elliott Hughes387d4b72012-08-09 15:17:46 -0700194
Dan Albertffa5cbe2021-02-03 16:44:37 -0800195
196if __name__ == "__main__":
197 main()