Skip to content

Commit

Permalink
Support filename in license headers
Browse files Browse the repository at this point in the history
  • Loading branch information
calcmogul committed Nov 14, 2024
1 parent 7c04d3b commit 4b3fda4
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions wpiformat/wpiformat/licenseupdate.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ def should_process_file(config_file, name):
or name.endswith(".java")
) and not license_regex.search(name)

def __try_regex(self, lines, last_year, license_template):
def __try_regex(self, repo_relative_name, lines, last_year, license_template):
"""Try finding license with regex of license template.
Keyword arguments:
repo_relative_name -- repo-relative filename
lines -- lines of file
last_year -- last year in copyright range
license_template -- license_template string
Expand All @@ -44,6 +45,7 @@ def __try_regex(self, lines, last_year, license_template):
.replace(")", r"\)")
.replace("{year}", r"(?P<year>[0-9]+)(-[0-9]+)?")
.replace("{padding}", "[ ]*")
.replace("{filename}", "")
)
license_rgx = regex.compile(license_rgxstr, regex.M)

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

repo_relative_name = name[len(Task.get_repo_root()) + 1 :]

_, license_template = Config.read_file(
os.path.dirname(os.path.abspath(name)), ".styleguide-license"
)
Expand All @@ -160,7 +164,7 @@ def run_pipeline(self, config_file, name, lines):
last_year = str(date.today().year)

success, first_year, appendix = self.__try_regex(
lines, last_year, license_template
repo_relative_name, lines, last_year, license_template
)
if not success:
success, first_year, appendix = self.__try_string_search(
Expand All @@ -179,6 +183,9 @@ def run_pipeline(self, config_file, name, lines):
# Insert copyright year range
line = line.replace("{year}", year_range)

# Insert filename
line = line.replace("{filename}", repo_relative_name)

# Insert padding which expands to the 80th column. If there is more
# than one padding token, the line may contain fewer than 80
# characters due to rounding during the padding width calculation.
Expand Down

0 comments on commit 4b3fda4

Please sign in to comment.