blob: e4318b2b55fa0071beea29b3c8f297325e3e5967 [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",
36 ]
Dan Albertffa5cbe2021-02-03 16:44:37 -080037 if path.suffix in uninteresting_extensions:
Elliott Hughesaac7c3a2017-07-14 10:00:32 -070038 return False
Dan Albertffa5cbe2021-02-03 16:44:37 -080039 if path.name in {"notice", "readme", "pylintrc"}:
Elliott Hughesaac7c3a2017-07-14 10:00:32 -070040 return False
41 return True
42
Dan Albertffa5cbe2021-02-03 16:44:37 -080043
Elliott Hughesaac7c3a2017-07-14 10:00:32 -070044def is_auto_generated(content):
Elliott Hughes22a0d6f2014-03-06 15:10:22 -080045 if "Generated by gensyscalls.py" in content or "generated by genserv.py" in content:
Elliott Hughes387d4b72012-08-09 15:17:46 -070046 return True
47 if "This header was automatically generated from a Linux kernel header" in content:
48 return True
49 return False
50
Elliott Hughes387d4b72012-08-09 15:17:46 -070051
Dan Albertffa5cbe2021-02-03 16:44:37 -080052def is_copyright_end(line: str, first_line_was_hash: bool) -> bool:
53 endings = [
54 " $FreeBSD: ",
55 "$Citrus$",
56 "$FreeBSD$",
57 "*/",
58 "From: @(#)",
59 # OpenBSD likes to say where stuff originally came from:
60 "Original version ID:",
61 "\t$Citrus: ",
62 "\t$NetBSD: ",
63 "\t$OpenBSD: ",
64 "\t@(#)",
65 "\tcitrus Id: ",
66 "\tfrom: @(#)",
67 "from OpenBSD:",
68 ]
69 if first_line_was_hash and not line:
70 return True
71
72 for ending in endings:
73 if ending in line:
74 return True
75
76 return False
77
78
79def extract_copyright_at(lines: Sequence[str], i: int) -> int:
80 first_line_was_hash = lines[i].startswith("#")
Elliott Hughes387d4b72012-08-09 15:17:46 -070081
Elliott Hughes261e2232012-08-14 15:04:05 -070082 # Do we need to back up to find the start of the copyright header?
83 start = i
Dan Albertffa5cbe2021-02-03 16:44:37 -080084 if not first_line_was_hash:
Elliott Hughes261e2232012-08-14 15:04:05 -070085 while start > 0:
86 if "/*" in lines[start - 1]:
87 break
88 start -= 1
89
Elliott Hughes387d4b72012-08-09 15:17:46 -070090 # Read comment lines until we hit something that terminates a
91 # copyright header.
Elliott Hughes387d4b72012-08-09 15:17:46 -070092 while i < len(lines):
Dan Albertffa5cbe2021-02-03 16:44:37 -080093 if is_copyright_end(lines[i], first_line_was_hash):
Elliott Hughesbfa582d2014-05-05 14:58:17 -070094 break
Elliott Hughes387d4b72012-08-09 15:17:46 -070095 i += 1
96
97 end = i
98
99 # Trim trailing cruft.
100 while end > 0:
Dan Albertffa5cbe2021-02-03 16:44:37 -0800101 line = lines[end - 1]
102 if line not in {
103 " *", " * ===================================================="
104 }:
Elliott Hughes387d4b72012-08-09 15:17:46 -0700105 break
106 end -= 1
107
108 # Remove C/assembler comment formatting, pulling out just the text.
109 clean_lines = []
110 for line in lines[start:end]:
111 line = line.replace("\t", " ")
112 line = line.replace("/* ", "")
Dan Albertffa5cbe2021-02-03 16:44:37 -0800113 line = re.sub(r"^ \* ", "", line)
Elliott Hughes387d4b72012-08-09 15:17:46 -0700114 line = line.replace("** ", "")
115 line = line.replace("# ", "")
Elliott Hughesab528072018-07-24 00:01:52 +0000116 if "SPDX-License-Identifier:" in line:
117 continue
Elliott Hughes387d4b72012-08-09 15:17:46 -0700118 if line.startswith("++Copyright++"):
119 continue
120 line = line.replace("--Copyright--", "")
121 line = line.rstrip()
122 # These come last and take care of "blank" comment lines.
Dan Albertffa5cbe2021-02-03 16:44:37 -0800123 if line in {"#", " *", "**", "-"}:
Elliott Hughes387d4b72012-08-09 15:17:46 -0700124 line = ""
125 clean_lines.append(line)
126
127 # Trim blank lines from head and tail.
128 while clean_lines[0] == "":
129 clean_lines = clean_lines[1:]
130 while clean_lines[len(clean_lines) - 1] == "":
131 clean_lines = clean_lines[0:(len(clean_lines) - 1)]
132
Dan Albertffa5cbe2021-02-03 16:44:37 -0800133 copyrights.add("\n".join(clean_lines))
Elliott Hughes387d4b72012-08-09 15:17:46 -0700134
135 return i
136
Elliott Hughes387d4b72012-08-09 15:17:46 -0700137
Dan Albertffa5cbe2021-02-03 16:44:37 -0800138def do_file(path: str) -> None:
139 raw = Path(path).read_bytes()
140 try:
141 content = raw.decode("utf-8")
142 except UnicodeDecodeError:
143 warn("bad UTF-8 in %s" % path)
144 content = raw.decode("iso-8859-1")
Elliott Hughes387d4b72012-08-09 15:17:46 -0700145
Elliott Hughesaac7c3a2017-07-14 10:00:32 -0700146 lines = content.split("\n")
147
148 if len(lines) <= 4:
149 warn_verbose("ignoring short file %s" % path)
150 return
151
152 if is_auto_generated(content):
153 warn_verbose("ignoring auto-generated file %s" % path)
154 return
155
156 if not "Copyright" in content:
157 if "public domain" in content.lower():
Elliott Hughesc5db38a2020-06-15 17:26:58 -0700158 warn_verbose("ignoring public domain file %s" % path)
Elliott Hughesaac7c3a2017-07-14 10:00:32 -0700159 return
Dan Albertffa5cbe2021-02-03 16:44:37 -0800160 warn('no copyright notice found in "%s" (%d lines)' %
161 (path, len(lines)))
Elliott Hughesaac7c3a2017-07-14 10:00:32 -0700162 return
163
Dan Albertffa5cbe2021-02-03 16:44:37 -0800164 # Manually iterate because extract_copyright_at tells us how many lines to
165 # skip.
Elliott Hughesaac7c3a2017-07-14 10:00:32 -0700166 i = 0
167 while i < len(lines):
168 if "Copyright" in lines[i] and not "@(#) Copyright" in lines[i]:
169 i = extract_copyright_at(lines, i)
170 else:
171 i += 1
172
173
Dan Albertffa5cbe2021-02-03 16:44:37 -0800174def do_dir(arg):
Elliott Hughes387d4b72012-08-09 15:17:46 -0700175 for directory, sub_directories, filenames in os.walk(arg):
176 if ".git" in sub_directories:
177 sub_directories.remove(".git")
178 sub_directories = sorted(sub_directories)
179
180 for filename in sorted(filenames):
181 path = os.path.join(directory, filename)
Elliott Hughesaac7c3a2017-07-14 10:00:32 -0700182 if is_interesting(path):
183 do_file(path)
Elliott Hughes387d4b72012-08-09 15:17:46 -0700184
Elliott Hughes387d4b72012-08-09 15:17:46 -0700185
Dan Albertffa5cbe2021-02-03 16:44:37 -0800186def main() -> None:
187 args = sys.argv[1:]
188 if len(args) == 0:
189 args = ["."]
Elliott Hughes387d4b72012-08-09 15:17:46 -0700190
Dan Albertffa5cbe2021-02-03 16:44:37 -0800191 for arg in args:
192 if os.path.isdir(arg):
193 do_dir(arg)
194 else:
195 do_file(arg)
Elliott Hughes387d4b72012-08-09 15:17:46 -0700196
Dan Albertffa5cbe2021-02-03 16:44:37 -0800197 for notice in sorted(copyrights):
198 print(notice)
199 print()
200 print("-" * 67)
201 print()
Elliott Hughes387d4b72012-08-09 15:17:46 -0700202
Dan Albertffa5cbe2021-02-03 16:44:37 -0800203
204if __name__ == "__main__":
205 main()