From 26c97ac1ce67363a7294f19b2272ca0724d0c7ad Mon Sep 17 00:00:00 2001 From: Tyler Veness Date: Mon, 20 Dec 2021 12:14:22 -0800 Subject: [PATCH] Support filename in license headers Fixes #223. --- wpiformat/wpiformat/licenseupdate.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/wpiformat/wpiformat/licenseupdate.py b/wpiformat/wpiformat/licenseupdate.py index b45d34ca..e2c1b664 100644 --- a/wpiformat/wpiformat/licenseupdate.py +++ b/wpiformat/wpiformat/licenseupdate.py @@ -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 @@ -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[0-9]+)(-[0-9]+)?").replace("{padding}", "[ ]*") + "{year}", r"(?P[0-9]+)(-[0-9]+)?").replace( + "{padding}", "[ ]*").replace("{filename}", "") license_rgx = regex.compile(license_rgxstr, regex.M) first_year = last_year @@ -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") @@ -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) @@ -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.