Skip to content

Commit

Permalink
Support filename in license headers
Browse files Browse the repository at this point in the history
Fixes #223.
  • Loading branch information
calcmogul committed Dec 20, 2021
1 parent 04157fc commit 26c97ac
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions wpiformat/wpiformat/licenseupdate.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ def should_process_file(config_file, name):
return (config_file.is_c_file(name) or config_file.is_cpp_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 @@ -37,8 +39,8 @@ def __try_regex(self, lines, last_year, license_template):
license_rgxstr = "^" + linesep.join(license_template)
license_rgxstr = license_rgxstr.replace("*", r"\*").replace(
".", r"\.").replace("(", r"\(").replace(")", r"\)").replace(
"{year}",
r"(?P<year>[0-9]+)(-[0-9]+)?").replace("{padding}", "[ ]*")
"{year}", r"(?P<year>[0-9]+)(-[0-9]+)?").replace(
"{padding}", "[ ]*").replace("{filename}", "")
license_rgx = regex.compile(license_rgxstr, regex.M)

first_year = last_year
Expand Down Expand Up @@ -128,6 +130,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 @@ -150,7 +154,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(
lines, last_year, license_template)
Expand All @@ -167,6 +171,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 26c97ac

Please sign in to comment.