Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support filename in license headers #224

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading