-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathis_copyright_uptodate.py
executable file
·68 lines (61 loc) · 2.2 KB
/
is_copyright_uptodate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python3
# SPDX-License-Identifier: AGPL-3.0-or-later
# Copyright (C) 2022-2024 Colin B. Macdonald
# Copyright (C) 2023-2024 Andrew Rechnitzer
# Copyright (C) 2023 Natalie Balashov
"""Output the names of files given as args that are not copyright in a given year.
Gives non-zero exit code if there were any that need updates.
Also, snitches on files without apparent copyright headers, although
in some cases, maybe they have to be like that!
"""
import re
from pathlib import Path
import sys
from datetime import datetime, timezone
year = datetime.now(timezone.utc).year
p = re.compile(f".*Copyright.*{year}.*")
p2 = re.compile(".*Copyright.*")
re_alt = re.compile(".*creativecommons.org/(licenses|publicdomain)/.*")
# Some files don't have copyright info: can consider whether this is ok,
# but for now we can avoid hearing about them by listing globs:
ok_no_copyright = [
"CHANGELOG*",
"CONTRIBUTORS*",
"*README.md",
"*/uiFiles/ui_*.py",
".mailmap",
]
if __name__ == "__main__":
at_least_one = False
files = set(sys.argv[1:])
print(f"Checking copyright headers for {year} in {len(files)} files...")
for f in files:
f = Path(f)
if any(f.match(x) for x in ok_no_copyright):
print(f" matches the allow list, skip: {f}")
continue
try:
with open(f, "r") as fh:
data = fh.read().replace("\n", "")
except UnicodeDecodeError:
print(f" Skipping binary (?) file: {f}")
continue
except FileNotFoundError:
print(f" Skipping deleted (?) file: {f}")
continue
if re_alt.match(data):
print(f" File has creativecommons licence url: {f}")
continue
if not p2.match(data):
print(f"[!] File without copyright header: {f}")
# TODO: or leave as False to just skip these
at_least_one = True
continue
if not p.match(data):
at_least_one = True
print(f"[w] Needs copyright header update: {f}")
if at_least_one:
print("At least one file needs updated, see list above")
sys.exit(1)
print("No files need updates")
sys.exit(0)