-
Notifications
You must be signed in to change notification settings - Fork 6
/
check_copyright.py
executable file
·122 lines (105 loc) · 3.98 KB
/
check_copyright.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env python3
# This file is part of python-project-template.
# Copyright 2024 Marco Favorito
#
# python-project-template is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# python-project-template is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with python-project-template. If not, see <https://www.gnu.org/licenses/>.
#
"""Check that all the Python files of the repository have the copyright notice.
In particular:
- (optional) the Python shebang
- the encoding header;
- the copyright and license notices;
It is assumed the script is run from the repository root.
"""
import itertools
import re
import sys
from pathlib import Path
COPYRIGHT_NOTICE = "Copyright 2024 Marco Favorito"
HEADER_REGEX = re.compile(
rf"""(#!/usr/bin/env python3
)?# This file is part of python-project-template\.
# {COPYRIGHT_NOTICE}
#
# python-project-template is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# \(at your option\) any later version\.
#
# python-project-template is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE\. See the
# GNU General Public License for more details\.
#
# You should have received a copy of the GNU General Public License
# along with python-project-template\. If not, see <https://www\.gnu\.org/licenses/>\.
#""",
re.MULTILINE,
)
def check_copyright(file: Path) -> bool:
"""Given a file, check if the header stuff is in place.
Return True if the files has the encoding header and the copyright notice,
optionally prefixed by the shebang. Return False otherwise.
:param file: the file to check.
:return: True if the file is compliant with the checks, False otherwise.
"""
content = file.read_text()
return re.match(HEADER_REGEX, content) is not None
def check_copyright_in_readme() -> bool:
"""Check if the README.md contains the right copyright notice."""
readme_filepath = Path("README.md").resolve()
if not readme_filepath.exists():
msg = f"README file {readme_filepath} does not exist"
raise ValueError(msg)
readme_content = readme_filepath.read_text()
matches = re.findall("Copyright .*", readme_content)
if len(matches) == 0:
return False
for match in matches:
if match != COPYRIGHT_NOTICE:
return False
print("README is OK.")
return True
def check_copyright_headers() -> bool:
"""Check copyright headers are correct."""
exclude_files = {Path("scripts", "whitelist.py")}
python_files = filter(
lambda x: x not in exclude_files,
itertools.chain(
Path("python_project_template").glob("**/*.py"),
Path("tests").glob("**/*.py"),
Path("scripts").glob("**/*.py"),
),
)
bad_files = []
for filepath in python_files:
print(f"Checking file {filepath}...", end=" ")
result = check_copyright(filepath)
if result:
print("OK")
else:
print("FAIL")
bad_files.append(filepath)
if len(bad_files) > 0:
print("The following files are not well formatted:")
print("\n".join(map(str, bad_files)))
return False
return True
if __name__ == "__main__":
result: bool = check_copyright_headers()
result = result and check_copyright_in_readme()
if not result:
sys.exit(1)
print("All checks have passed!")
sys.exit(0)