Skip to content

Commit e520359

Browse files
committed
Pre-commit: Fix copyright_headers.py to run on all relevant files
It was only running on the first file passed by pre-commit, instead of all. Fixes compatibility with Windows paths to get the basename.
1 parent 89f70e9 commit e520359

File tree

2 files changed

+51
-47
lines changed

2 files changed

+51
-47
lines changed

.pre-commit-config.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ repos:
3939
entry: python3 misc/scripts/copyright_headers.py
4040
exclude: |
4141
(?x)^(
42-
tests/python_build.*|
4342
.*thirdparty.*|
44-
.*platform/android/java/lib/src/com.*|
4543
.*-so_wrap.*|
44+
core/math/bvh_.*\.inc$|
45+
platform/android/java/lib/src/com.*|
4646
platform/android/java/lib/src/org/godotengine/godot/gl/GLSurfaceView.*|
4747
platform/android/java/lib/src/org/godotengine/godot/gl/EGLLogWrapper.*|
4848
platform/android/java/lib/src/org/godotengine/godot/utils/ProcessPhoenix.*

misc/scripts/copyright_headers.py

+49-45
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
33

4+
import os
45
import sys
56

67
header = """\
@@ -35,58 +36,61 @@
3536
/**************************************************************************/
3637
"""
3738

38-
fname = sys.argv[1]
39+
if len(sys.argv) < 2:
40+
print("Invalid usage of copyright_headers.py, it should be called with a path to one or multiple files.")
41+
sys.exit(1)
3942

40-
# Handle replacing $filename with actual filename and keep alignment
41-
fsingle = fname.strip()
42-
if fsingle.find("/") != -1:
43-
fsingle = fsingle[fsingle.rfind("/") + 1 :]
44-
rep_fl = "$filename"
45-
rep_fi = fsingle
46-
len_fl = len(rep_fl)
47-
len_fi = len(rep_fi)
48-
# Pad with spaces to keep alignment
49-
if len_fi < len_fl:
50-
for x in range(len_fl - len_fi):
51-
rep_fi += " "
52-
elif len_fl < len_fi:
53-
for x in range(len_fi - len_fl):
54-
rep_fl += " "
55-
if header.find(rep_fl) != -1:
56-
text = header.replace(rep_fl, rep_fi)
57-
else:
58-
text = header.replace("$filename", fsingle)
59-
text += "\n"
43+
for f in sys.argv[1:]:
44+
fname = f
6045

61-
# We now have the proper header, so we want to ignore the one in the original file
62-
# and potentially empty lines and badly formatted lines, while keeping comments that
63-
# come after the header, and then keep everything non-header unchanged.
64-
# To do so, we skip empty lines that may be at the top in a first pass.
65-
# In a second pass, we skip all consecutive comment lines starting with "/*",
66-
# then we can append the rest (step 2).
46+
# Handle replacing $filename with actual filename and keep alignment
47+
fsingle = os.path.basename(fname.strip())
48+
rep_fl = "$filename"
49+
rep_fi = fsingle
50+
len_fl = len(rep_fl)
51+
len_fi = len(rep_fi)
52+
# Pad with spaces to keep alignment
53+
if len_fi < len_fl:
54+
for x in range(len_fl - len_fi):
55+
rep_fi += " "
56+
elif len_fl < len_fi:
57+
for x in range(len_fi - len_fl):
58+
rep_fl += " "
59+
if header.find(rep_fl) != -1:
60+
text = header.replace(rep_fl, rep_fi)
61+
else:
62+
text = header.replace("$filename", fsingle)
63+
text += "\n"
6764

68-
with open(fname.strip(), "r") as fileread:
69-
line = fileread.readline()
70-
header_done = False
65+
# We now have the proper header, so we want to ignore the one in the original file
66+
# and potentially empty lines and badly formatted lines, while keeping comments that
67+
# come after the header, and then keep everything non-header unchanged.
68+
# To do so, we skip empty lines that may be at the top in a first pass.
69+
# In a second pass, we skip all consecutive comment lines starting with "/*",
70+
# then we can append the rest (step 2).
7171

72-
while line.strip() == "": # Skip empty lines at the top
72+
with open(fname.strip(), "r") as fileread:
7373
line = fileread.readline()
74+
header_done = False
7475

75-
if line.find("/**********") == -1: # Godot header starts this way
76-
# Maybe starting with a non-Godot comment, abort header magic
77-
header_done = True
76+
while line.strip() == "": # Skip empty lines at the top
77+
line = fileread.readline()
7878

79-
while not header_done: # Handle header now
80-
if line.find("/*") != 0: # No more starting with a comment
79+
if line.find("/**********") == -1: # Godot header starts this way
80+
# Maybe starting with a non-Godot comment, abort header magic
8181
header_done = True
82-
if line.strip() != "":
83-
text += line
84-
line = fileread.readline()
8582

86-
while line != "": # Dump everything until EOF
87-
text += line
88-
line = fileread.readline()
83+
while not header_done: # Handle header now
84+
if line.find("/*") != 0: # No more starting with a comment
85+
header_done = True
86+
if line.strip() != "":
87+
text += line
88+
line = fileread.readline()
89+
90+
while line != "": # Dump everything until EOF
91+
text += line
92+
line = fileread.readline()
8993

90-
# Write
91-
with open(fname.strip(), "w", encoding="utf-8", newline="\n") as filewrite:
92-
filewrite.write(text)
94+
# Write
95+
with open(fname.strip(), "w", encoding="utf-8", newline="\n") as filewrite:
96+
filewrite.write(text)

0 commit comments

Comments
 (0)