-
Notifications
You must be signed in to change notification settings - Fork 6
/
CheckLicensesInFiles.py
66 lines (46 loc) · 2.07 KB
/
CheckLicensesInFiles.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
#!/usr/bin/env python
# Copyright (C) 2024 ISIS Rutherford Appleton Laboratory UKRI
# SPDX - License - Identifier: GPL-3.0-or-later
from __future__ import annotations
# Checks the license comment block at the top of files
#
# Reads the license content from .licenserc.json and checks if the files passed in as arguments have matching
# license lines. Written primarily for use as a pre-commit check, so the pre-commit configuration limits the check to
# only Python files and the passed in arguments will be the files staged for commit.
import json
import sys
def load_copyright_header() -> dict[str, str]:
return json.load(open(".licenserc.json"))
def find_files_with_incorrect_license_headers(filepaths: list[str], copyright_text: str) -> list[str]:
copyright_lines = copyright_text.split("\n")
incorrect_files = []
for filepath in filepaths:
lines = open(filepath, encoding="utf8").readlines()
if len(lines) > 0:
if has_shebang_line(lines):
del lines[0]
if not has_correct_copyright_lines(lines, copyright_lines):
incorrect_files.append(filepath)
return incorrect_files
def has_shebang_line(file_lines: list[str]) -> bool:
return file_lines[0].startswith("#!")
def has_correct_copyright_lines(file_lines: list[str], copyright_lines: list[str]) -> bool:
if len(file_lines) < len(copyright_lines):
return False
for i in range(len(copyright_lines)):
if file_lines[i].strip() != copyright_lines[i].strip():
return False
return True
if __name__ == "__main__":
copyright_strings = load_copyright_header()
files_to_check = sys.argv[1:]
files_in_error = set()
for copyright_string in copyright_strings.values():
files_in_error.update(find_files_with_incorrect_license_headers(files_to_check, copyright_string))
if len(files_in_error) > 0:
print('The following files contain errors in their licenses:\n')
print('\n'.join(files_in_error))
exit(1)
else:
print("All files have correct licenses")
exit(0)