-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathcheck_copyright_notice.py
More file actions
86 lines (71 loc) · 2.69 KB
/
check_copyright_notice.py
File metadata and controls
86 lines (71 loc) · 2.69 KB
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
# -------------------------------------------------------
# Copyright (c) 2020-2021 Arm Limited. All rights reserved.
#
# SPDX-License-Identifier: Apache-2.0
# -------------------------------------------------------
"""
Checks the presence of copyright notice in the files
"""
from typing import Optional, Sequence
import argparse
import os
import sys
import re
import magic
from comment_parser import comment_parser
COPYRIGHT_TEXT = "Copyright (c) <ValidYear>"
LICENSE_TEXT = "SPDX-License-Identifier: Apache-2.0"
def check_file(filename: str, copyright_reg_exp: re.Pattern) -> int:
"""
Checks a file for the presence of a comment in the form of a copyright
and license notice.
@param filename: The name of the file to check.
@param copyright_reg_exp A regular expression giving the format of the
copyright notice (exclusing language-specific comment chars).
@return 0 If the copyright & license notice are found, otherwise 1.
"""
if os.path.getsize(filename) == 0:
return 0
mime_type = magic.from_file(filename, mime=True)
if mime_type == "text/plain":
mime_type = "text/x-c++"
copyrightfound=False
licensefound=False
comments = ""
for comment in comment_parser.extract_comments(filename,
mime=mime_type):
comments += comment.text() + '\n'
if copyright_reg_exp.search(comments):
copyrightfound=True
if comments.find(LICENSE_TEXT) != -1:
licensefound=True
if copyrightfound and licensefound:
return 0
errstr = ""
if not copyrightfound:
errstr = "\n\t # Missing or invalid copyright text. Please follow format: " + COPYRIGHT_TEXT
if not licensefound:
errstr += "\n\t # Missing or invalid license text. Please write : " + LICENSE_TEXT
print(f"# Copyright check error(s) in : {filename} {errstr}")
return 1
def main(argv: Optional[Sequence[str]] = None) -> int:
"""
Entry point that checks for copyright notices being present in all the
files supplied on the command-line.
@param argv: The names of the files to check.
@return Non-zero if one or more of the passed files was missing a copyright
notice.
"""
parser = argparse.ArgumentParser()
parser.add_argument('filenames', nargs='*')
args = parser.parse_args(argv)
print("Checking copyright headers...")
ret = 0
copyright_reg_exp=re.compile(r"(Copyright\s\(c\)\s(19|20)[0-9][0-9][^0-9])")
for filename in args.filenames:
ret |= check_file(filename, copyright_reg_exp)
if ret != 0:
print(">> error: Files are missing a valid copyright header")
return ret
if __name__ == '__main__':
sys.exit(main())