Skip to content

Commit 4b3fda4

Browse files
committed
Support filename in license headers
Fixes wpilibsuite#223.
1 parent 7c04d3b commit 4b3fda4

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

wpiformat/wpiformat/licenseupdate.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ def should_process_file(config_file, name):
2121
or name.endswith(".java")
2222
) and not license_regex.search(name)
2323

24-
def __try_regex(self, lines, last_year, license_template):
24+
def __try_regex(self, repo_relative_name, lines, last_year, license_template):
2525
"""Try finding license with regex of license template.
2626
2727
Keyword arguments:
28+
repo_relative_name -- repo-relative filename
2829
lines -- lines of file
2930
last_year -- last year in copyright range
3031
license_template -- license_template string
@@ -44,6 +45,7 @@ def __try_regex(self, lines, last_year, license_template):
4445
.replace(")", r"\)")
4546
.replace("{year}", r"(?P<year>[0-9]+)(-[0-9]+)?")
4647
.replace("{padding}", "[ ]*")
48+
.replace("{filename}", "")
4749
)
4850
license_rgx = regex.compile(license_rgxstr, regex.M)
4951

@@ -135,6 +137,8 @@ def __try_string_search(self, lines, last_year, license_template):
135137
def run_pipeline(self, config_file, name, lines):
136138
linesep = super().get_linesep(lines)
137139

140+
repo_relative_name = name[len(Task.get_repo_root()) + 1 :]
141+
138142
_, license_template = Config.read_file(
139143
os.path.dirname(os.path.abspath(name)), ".styleguide-license"
140144
)
@@ -160,7 +164,7 @@ def run_pipeline(self, config_file, name, lines):
160164
last_year = str(date.today().year)
161165

162166
success, first_year, appendix = self.__try_regex(
163-
lines, last_year, license_template
167+
repo_relative_name, lines, last_year, license_template
164168
)
165169
if not success:
166170
success, first_year, appendix = self.__try_string_search(
@@ -179,6 +183,9 @@ def run_pipeline(self, config_file, name, lines):
179183
# Insert copyright year range
180184
line = line.replace("{year}", year_range)
181185

186+
# Insert filename
187+
line = line.replace("{filename}", repo_relative_name)
188+
182189
# Insert padding which expands to the 80th column. If there is more
183190
# than one padding token, the line may contain fewer than 80
184191
# characters due to rounding during the padding width calculation.

0 commit comments

Comments
 (0)