Skip to content

Commit 2434ab5

Browse files
committed
Scripts and tools: Fix devtools/copyright_header.py to always honor exclusions
This script compared paths relative to the report directory to test for exclusion, meaning the directory exclusions did not work properly, as they were relative to the project root. Fix this by creating absolute paths through the combination of: 'git ls-files --full-name' and 'git rev-parse --show-toplevel'
1 parent 72bd4ab commit 2434ab5

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

contrib/devtools/copyright_header.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,22 @@ def applies_to_file(filename):
4848
# obtain list of files in repo according to INCLUDE and EXCLUDE
4949
################################################################################
5050

51-
GIT_LS_CMD = 'git ls-files'
51+
GIT_LS_CMD = 'git ls-files --full-name'.split(' ')
52+
GIT_TOPLEVEL_CMD = 'git rev-parse --show-toplevel'.split(' ')
5253

53-
def call_git_ls():
54-
out = subprocess.check_output(GIT_LS_CMD.split(' '))
54+
def call_git_ls(base_directory):
55+
out = subprocess.check_output([*GIT_LS_CMD, base_directory])
5556
return [f for f in out.decode("utf-8").split('\n') if f != '']
5657

57-
def get_filenames_to_examine():
58-
filenames = call_git_ls()
59-
return sorted([filename for filename in filenames if
58+
def call_git_toplevel():
59+
"Returns the absolute path to the project root"
60+
return subprocess.check_output(GIT_TOPLEVEL_CMD).strip().decode("utf-8")
61+
62+
def get_filenames_to_examine(base_directory):
63+
"Returns an array of absolute paths to any project files in the base_directory that pass the include/exclude filters"
64+
root = call_git_toplevel()
65+
filenames = call_git_ls(base_directory)
66+
return sorted([os.path.join(root, filename) for filename in filenames if
6067
applies_to_file(filename)])
6168

6269
################################################################################
@@ -146,7 +153,7 @@ def file_has_without_c_style_copyright_for_holder(contents, holder_name):
146153
################################################################################
147154

148155
def read_file(filename):
149-
return open(os.path.abspath(filename), 'r', encoding="utf8").read()
156+
return open(filename, 'r', encoding="utf8").read()
150157

151158
def gather_file_info(filename):
152159
info = {}
@@ -260,12 +267,9 @@ def print_report(file_infos, verbose):
260267
print(SEPARATOR)
261268

262269
def exec_report(base_directory, verbose):
263-
original_cwd = os.getcwd()
264-
os.chdir(base_directory)
265-
filenames = get_filenames_to_examine()
270+
filenames = get_filenames_to_examine(base_directory)
266271
file_infos = [gather_file_info(f) for f in filenames]
267272
print_report(file_infos, verbose)
268-
os.chdir(original_cwd)
269273

270274
################################################################################
271275
# report cmd
@@ -325,13 +329,13 @@ def get_most_recent_git_change_year(filename):
325329
################################################################################
326330

327331
def read_file_lines(filename):
328-
f = open(os.path.abspath(filename), 'r', encoding="utf8")
332+
f = open(filename, 'r', encoding="utf8")
329333
file_lines = f.readlines()
330334
f.close()
331335
return file_lines
332336

333337
def write_file_lines(filename, file_lines):
334-
f = open(os.path.abspath(filename), 'w', encoding="utf8")
338+
f = open(filename, 'w', encoding="utf8")
335339
f.write(''.join(file_lines))
336340
f.close()
337341

@@ -399,11 +403,8 @@ def update_updatable_copyright(filename):
399403
"Copyright updated! -> %s" % last_git_change_year)
400404

401405
def exec_update_header_year(base_directory):
402-
original_cwd = os.getcwd()
403-
os.chdir(base_directory)
404-
for filename in get_filenames_to_examine():
406+
for filename in get_filenames_to_examine(base_directory):
405407
update_updatable_copyright(filename)
406-
os.chdir(original_cwd)
407408

408409
################################################################################
409410
# update cmd

0 commit comments

Comments
 (0)