-
Notifications
You must be signed in to change notification settings - Fork 209
/
Copy pathcheck_copyright.py
242 lines (206 loc) · 8.77 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# This code is part of a Qiskit project.
#
# (C) Copyright IBM 2020, 2023.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.
""" Fix copyright year in header """
from typing import Tuple, Union, List
import builtins
import sys
import os
import datetime
import argparse
import subprocess
import traceback
class CopyrightChecker:
"""Check copyright"""
_UTF_STRING = "# -*- coding: utf-8 -*-"
_COPYRIGHT_STRING = "# (C) Copyright IBM "
def __init__(self, root_dir: str, check: bool) -> None:
self._root_dir = root_dir
self._check = check
self._current_year = datetime.datetime.now().year
self._changed_files = self._get_changed_files()
@staticmethod
def _exception_to_string(excp: Exception) -> str:
stack = traceback.extract_stack()[:-3] + traceback.extract_tb(excp.__traceback__)
pretty = traceback.format_list(stack)
return "".join(pretty) + f"\n {excp.__class__} {excp}"
@staticmethod
def _get_year_from_date(date) -> int:
if not date or len(date) < 4:
return None
return int(date[:4])
def _cmd_execute(self, args: List[str]) -> Tuple[str, Union[None, str]]:
# execute command
env = {}
for k in ["SYSTEMROOT", "PATH"]:
v = os.environ.get(k)
if v is not None:
env[k] = v
# LANGUAGE is used on win32
env["LANGUAGE"] = "C"
env["LANG"] = "C"
env["LC_ALL"] = "C"
with subprocess.Popen(
args,
cwd=self._root_dir,
env=env,
stdin=subprocess.DEVNULL,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
) as popen:
out, err = popen.communicate()
popen.wait()
out_str = out.decode("utf-8").strip()
err_str = err.decode("utf-8").strip()
err_str = err_str if err_str else None
return out_str, err_str
def _get_changed_files(self) -> List[str]:
out_str, err_str = self._cmd_execute(["git", "diff", "--name-only", "HEAD"])
if err_str:
raise builtins.Exception(err_str)
return out_str.splitlines()
def _get_file_last_year(self, relative_path: str) -> int:
last_year = None
errors = []
try:
out_str, err_str = self._cmd_execute(
["git", "log", "-1", "--format=%cI", relative_path]
)
last_year = CopyrightChecker._get_year_from_date(out_str)
if err_str:
errors.append(err_str)
except Exception as ex: # pylint: disable=broad-except
errors.append(f"'{relative_path}' Last year: {str(ex)}")
if errors:
raise ValueError(" - ".join(errors))
return last_year
def check_copyright(self, file_path) -> Tuple[bool, bool, bool]:
"""check copyright for a file"""
file_with_utf8 = False
file_with_invalid_year = False
file_has_header = False
try:
new_line = "# (C) Copyright IBM "
idx_utf8 = -1
idx_new_line = -1
file_lines = None
with open(file_path, "rt", encoding="utf8") as file:
file_lines = file.readlines()
for idx, line in enumerate(file_lines):
relative_path = os.path.relpath(file_path, self._root_dir)
if line.startswith(CopyrightChecker._UTF_STRING):
if self._check:
print(f"File contains utf-8 header: '{relative_path}'")
file_with_utf8 = True
idx_utf8 = idx
if not line.startswith(CopyrightChecker._COPYRIGHT_STRING):
continue
file_has_header = True
curr_years = []
for word in line.strip().split():
for year in word.strip().split(","):
if year.startswith("20") and len(year) >= 4:
try:
curr_years.append(int(year[0:4]))
except ValueError:
pass
header_start_year = None
header_last_year = None
if len(curr_years) > 1:
header_start_year = curr_years[0]
header_last_year = curr_years[1]
elif len(curr_years) == 1:
header_start_year = header_last_year = curr_years[0]
if relative_path in self._changed_files:
self._changed_files.remove(relative_path)
last_year = self._current_year
else:
last_year = self._get_file_last_year(relative_path)
if last_year and header_last_year != last_year:
if header_start_year and header_start_year != last_year:
new_line += f"{header_start_year}, "
new_line += f"{self._current_year}.\n"
if self._check:
print(
f"Wrong Copyright Year:'{relative_path}': ",
f"Current:'{line[:-1]}' Correct:'{new_line[:-1]}'",
)
file_with_invalid_year = True
idx_new_line = idx
break
if not self._check and (idx_utf8 >= 0 or idx_new_line >= 0):
if idx_new_line >= 0:
file_lines[idx_new_line] = new_line
if idx_utf8 >= 0:
del file_lines[idx_utf8]
with open(file_path, "w", encoding="utf8") as file:
file.writelines(file_lines)
if idx_new_line >= 0:
file_with_invalid_year = False
print(f"Fixed copyright year for {relative_path}.")
if idx_utf8 >= 0:
file_with_utf8 = False
print(f"Removed utf-8 header for {relative_path}.")
except UnicodeDecodeError:
return file_with_utf8, file_with_invalid_year, file_has_header
return file_with_utf8, file_with_invalid_year, file_has_header
def check(self) -> Tuple[int, int, int]:
"""check copyright"""
return self._check_copyright(self._root_dir)
def _check_copyright(self, path: str) -> Tuple[int, int, int]:
files_with_utf8 = 0
files_with_invalid_year = 0
files_with_header = 0
for item in os.listdir(path):
fullpath = os.path.join(path, item)
if os.path.isdir(fullpath):
if not item.startswith("."):
files = self._check_copyright(fullpath)
files_with_utf8 += files[0]
files_with_invalid_year += files[1]
files_with_header += files[2]
continue
if os.path.isfile(fullpath):
# check copyright year
(
file_with_utf8,
file_with_invalid_year,
file_has_header,
) = self.check_copyright(fullpath)
if file_with_utf8:
files_with_utf8 += 1
if file_with_invalid_year:
files_with_invalid_year += 1
if file_has_header:
files_with_header += 1
return files_with_utf8, files_with_invalid_year, files_with_header
def check_path(path):
"""valid path argument"""
if not path or os.path.isdir(path):
return path
raise argparse.ArgumentTypeError(f"readable_dir:{path} is not a valid path")
if __name__ == "__main__":
PARSER = argparse.ArgumentParser(description="Check Copyright Tool")
PARSER.add_argument("-path", type=check_path, metavar="path", help="Root path of project.")
PARSER.add_argument(
"-check",
required=False,
action="store_true",
help="Just check copyright, without fixing it.",
)
ARGS = PARSER.parse_args()
if not ARGS.path:
ARGS.path = os.getcwd()
ARGS.path = os.path.abspath(os.path.realpath(os.path.expanduser(ARGS.path)))
INVALID_UTF8, INVALID_YEAR, HAS_HEADER = CopyrightChecker(ARGS.path, ARGS.check).check()
print(f"{INVALID_UTF8} files have utf8 headers.")
print(f"{INVALID_YEAR} of {HAS_HEADER} files with copyright header have wrong years.")
sys.exit(0 if INVALID_UTF8 == 0 and INVALID_YEAR == 0 else 1)