Skip to content

Commit fad76f5

Browse files
maint: bump reuse to v3.0.2 in add-license-headers (#163)
Co-authored-by: pyansys-ci-bot <[email protected]>
1 parent b6315e3 commit fad76f5

File tree

10 files changed

+36
-97
lines changed

10 files changed

+36
-97
lines changed

.github/workflows/ci_cd.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ jobs:
4545
- uses: ansys/actions/doc-style@v5
4646
with:
4747
token: ${{ secrets.GITHUB_TOKEN }}
48+
vale-version: "3.4.1"
4849

4950
smoke-tests:
5051
name: "Build and Smoke tests"

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2024 ANSYS, Inc. and/or its affiliates.
3+
Copyright (c) 2023 - 2024 ANSYS, Inc. and/or its affiliates.
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy of
66
this software and associated documentation files (the "Software"), to deal in

doc/.vale.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,5 @@ Vocab = ANSYS
2626

2727
# Apply the following styles
2828
BasedOnStyles = Vale, Google
29+
30+
Vale.Terms = NO

doc/changelog.d/163.changed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
maint: bump reuse to v3.0.2 in add-license-headers

doc/styles/.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
*
2-
!Vocab
3-
!Vocab/**
2+
!vocabularies
3+
!vocabularies/**
44
!.gitignore

doc/styles/Vocab/ANSYS/accept.txt renamed to doc/styles/config/vocabularies/ANSYS/accept.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ API
22
ANSYS
33
Ansys
44
ansys
5+
maint
File renamed without changes.

pyproject.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,14 @@ classifiers = [
2828
]
2929
dependencies = [
3030
"importlib-metadata==7.1.0",
31-
"reuse==2.1.0",
31+
"reuse==3.0.2",
3232
"GitPython==3.1.43",
3333
]
3434

3535
[project.optional-dependencies]
3636
tests = [
3737
"pytest==8.1.1",
3838
"pytest-cov==5.0.0",
39-
"reuse==2.1.0",
40-
"GitPython==3.1.43",
4139
]
4240
doc = [
4341
"ansys-sphinx-theme[autoapi]==0.15.2",

src/ansys/pre_commit_hooks/add_license_headers.py

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
from tempfile import NamedTemporaryFile
3939

4040
import git
41-
from reuse import _util, header, lint, project
41+
from reuse import _annotate, _util, lint, project
4242

4343
DEFAULT_TEMPLATE = "ansys"
4444
"""Default template to use for license headers."""
@@ -200,12 +200,6 @@ def list_noncompliant_files(args: argparse.Namespace, proj: project.Project) ->
200200
missing_licensing_info = set(lint_json["non_compliant"]["missing_licensing_info"])
201201
missing_headers = missing_headers.union(missing_licensing_info)
202202

203-
if lint_json["non_compliant"]["missing_licenses"]:
204-
missing_licenses = set(
205-
lint_json["non_compliant"]["missing_licenses"][args.custom_license]
206-
)
207-
missing_headers = missing_headers.union(missing_licenses)
208-
209203
# Remove temporary file
210204
os.remove(filename)
211205

@@ -244,9 +238,9 @@ def set_header_args(
244238
# Provide values for license header arguments
245239
args = parser.parse_args([file_path])
246240
if start_year == current_year:
247-
args.year = [str(current_year)]
241+
args.year = [current_year]
248242
else:
249-
args.year = [start_year, str(current_year)]
243+
args.year = [int(start_year), current_year]
250244
args.copyright_style = "string-c"
251245
args.copyright = [copyright]
252246
args.merge_copyrights = True
@@ -296,48 +290,41 @@ def check_exists(
296290
copyright = values["copyright"]
297291
template = values["template"]
298292

299-
if i < len(files):
293+
for file in files:
294+
args = set_header_args(parser, start_year, current_year, file, copyright, template)
300295
# If the committed file is in missing_headers
301-
if (files[i] in missing_headers) or (os.path.getsize(files[i]) == 0):
296+
if (file in missing_headers) or (os.path.getsize(file) == 0):
302297
changed_headers = 1
303298
# Run REUSE on the file
304-
args = set_header_args(parser, start_year, current_year, files[i], copyright, template)
305299
if not args.ignore_license_check:
306300
args.license = [values["license"]]
307-
header.run(args, proj)
308301

309-
# Check if the next file is in missing_headers
310-
return check_exists(changed_headers, parser, values, proj, missing_headers, i + 1)
302+
_annotate.run(args, proj)
311303
else:
312-
# Save current copy of files[i]
304+
# Save current copy of file
313305
before_hook = NamedTemporaryFile(mode="w", delete=False).name
314-
shutil.copyfile(files[i], before_hook)
306+
shutil.copyfile(file, before_hook)
315307

316308
# Update the header
317309
# tmp captures the stdout of the header.run() function
318310
with NamedTemporaryFile(mode="w", delete=True) as tmp:
319-
args = set_header_args(
320-
parser, start_year, current_year, files[i], copyright, template
321-
)
322-
header.run(args, proj, tmp)
311+
_annotate.run(args, proj, tmp)
323312

324313
# Check if the file before add-license-headers was run is the same as the one
325314
# after add-license-headers was run. If not, apply the syntax changes
326315
# from other hooks before add-license-headers was run to the file
327-
if check_same_content(before_hook, files[i]) == False:
328-
add_hook_changes(before_hook, files[i])
316+
if check_same_content(before_hook, file) == False:
317+
add_hook_changes(before_hook, file)
329318

330319
# Check if the file content before add-license-headers was run has been changed
331320
# Assuming the syntax was fixed in the above if statement, this check is
332321
# solely for the file's content
333-
if check_same_content(before_hook, files[i]) == False:
322+
if check_same_content(before_hook, file) == False:
334323
changed_headers = 1
335-
print(f"Successfully changed header of {files[i]}")
324+
print(f"Successfully changed header of {file}")
336325

337326
os.remove(before_hook)
338327

339-
return check_exists(changed_headers, parser, values, proj, missing_headers, i + 1)
340-
341328
return changed_headers
342329

343330

@@ -634,7 +621,8 @@ def find_files_missing_header() -> int:
634621
# year, style, copyright-style, template, exclude-year, merge-copyrights, single-line,
635622
# multi-line, explicit-license, force-dot-license, recursive, no-replace,
636623
# skip-unrecognized, and skip-existing
637-
header.add_arguments(parser)
624+
# header.add_arguments(parser)
625+
_annotate.add_arguments(parser)
638626

639627
# Link the default template and/or license from the assets folder to your git repo.
640628
link_assets(assets, os_git_root, args)

tests/test_reuse.py

Lines changed: 11 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
git_repo = git.Repo(os.getcwd(), search_parent_directories=True)
3636
REPO_PATH = git_repo.git.rev_parse("--show-toplevel")
37+
START_YEAR = "2023"
3738

3839

3940
def set_up_repo(tmp_path, template_path, template_name, license_path, license_name):
@@ -143,7 +144,7 @@ def test_custom_start_year(tmp_path: pytest.TempPathFactory):
143144

144145
# Set up git repository in tmp_path with temporary file
145146
repo, tmp_file = set_up_repo(tmp_path, template_path, template_name, license_path, license_name)
146-
custom_args = [tmp_file, "--start_year=2023"]
147+
custom_args = [tmp_file, f"--start_year={START_YEAR}"]
147148

148149
# Assert the hook fails because it added the header
149150
assert add_argv_run(repo, tmp_file, custom_args) == 1
@@ -155,7 +156,7 @@ def test_custom_start_year(tmp_path: pytest.TempPathFactory):
155156
# Assert the copyright line's time range is
156157
# from 2023 to the current year
157158
if count == 1:
158-
assert f"2023 - {dt.today().year}" in line
159+
assert f"{START_YEAR} - {dt.today().year}" in line
159160
if count > 1:
160161
break
161162
file.close()
@@ -282,8 +283,9 @@ def test_main_fails(tmp_path: pytest.TempPathFactory):
282283

283284
# Set up git repository in tmp_path with temporary file
284285
repo, tmp_file = set_up_repo(tmp_path, template_path, template_name, license_path, license_name)
286+
custom_args = [tmp_file, f"--start_year={START_YEAR}"]
285287

286-
assert add_argv_run(repo, tmp_file, [tmp_file]) == 1
288+
assert add_argv_run(repo, tmp_file, custom_args) == 1
287289

288290
check_ansys_header(tmp_file)
289291

@@ -292,7 +294,7 @@ def test_main_fails(tmp_path: pytest.TempPathFactory):
292294

293295
def test_main_passes():
294296
"""Test all files are compliant."""
295-
sys.argv[1:] = []
297+
sys.argv[1:] = [f"--start_year={START_YEAR}"]
296298

297299
# Assert main runs successfully
298300
assert hook.main() == 0
@@ -442,60 +444,6 @@ def test_update_changed_header(tmp_path: pytest.TempPathFactory):
442444
os.chdir(REPO_PATH)
443445

444446

445-
def test_missing_licenses(tmp_path: pytest.TempPathFactory):
446-
"""Test that LICENSES folder is required."""
447-
# Set template and license names
448-
template_name = "test_template.jinja2"
449-
license_name = "ECL-1.0.txt"
450-
template_path = os.path.join(REPO_PATH, "tests", "test_reuse_files", "templates", template_name)
451-
license_path = os.path.join(REPO_PATH, "tests", "test_reuse_files", "LICENSES", license_name)
452-
453-
# Set up git repository in tmp_path with temporary file
454-
repo, tmp_file = set_up_repo(tmp_path, template_path, template_name, license_path, license_name)
455-
456-
# Remove LICENSES/ECL-1.0.txt file from tmp_path
457-
os.remove(os.path.join(tmp_path, "LICENSES", license_name))
458-
459-
custom_args = [
460-
tmp_file,
461-
"--custom_template=test_template",
462-
"--custom_license=ECL-1.0",
463-
]
464-
465-
# Add header to tmp_file
466-
add_argv_run(repo, tmp_file, custom_args)
467-
468-
custom_args = [
469-
tmp_file,
470-
"--custom_template=test_template",
471-
"--custom_license=ECL-1.0",
472-
]
473-
474-
# If LICENSES/license_name.txt file is missing, then it will fail
475-
# and add another SPDX line. This shows you need the
476-
# license_name.txt (MIT.txt, for example) in LICENSES or else it
477-
# will fail
478-
assert add_argv_run(repo, tmp_file, custom_args) == 1
479-
480-
# Assert two SPDX-License lines are found in the file if
481-
# the LICENSES/ECL-1.0.txt file does not exist.
482-
file = open(tmp_file, "r")
483-
count = 0
484-
for line in file:
485-
count += 1
486-
if count == 6:
487-
assert "SPDX-License" in line
488-
# Ensure header was updated correctly and didn't add
489-
# an extra SPDX-Identifier line
490-
if count == 7:
491-
assert "SPDX-License" in line
492-
if count > 7:
493-
break
494-
file.close()
495-
496-
os.chdir(REPO_PATH)
497-
498-
499447
def test_copy_assets(tmp_path: pytest.TempPathFactory):
500448
"""Test .reuse and LICENSES folders are copied."""
501449
# List of files to be git added
@@ -588,7 +536,7 @@ def test_index_exception(tmp_path: pytest.TempPathFactory):
588536
"--custom_copyright=ANSYS, Inc. Unauthorized use, distribution, \
589537
or duplication is prohibited.",
590538
"--ignore_license_check",
591-
"--start_year=2023",
539+
f"--start_year={START_YEAR}",
592540
]
593541

594542
# Assert the hook failed
@@ -652,16 +600,16 @@ def test_license_year_update(tmp_path: pytest.TempPathFactory):
652600
values = {
653601
"copyright": args.custom_copyright,
654602
"license": args.custom_license,
655-
"start_year": args.start_year,
603+
"start_year": START_YEAR,
656604
"current_year": dt.today().year,
657605
"git_repo": git_repo,
658606
}
659607

660608
# Years to update the LICENSE file
661-
years = [values["start_year"], "2022", dt.today().year]
609+
years = ["2022", dt.today().year, values["start_year"]]
662610

663-
# Check the copyright line has "2023 - {current_year}", "2022 - {current_year}",
664-
# and "{current_year}"
611+
# Check the copyright line has "2022 - {current_year}", "{current_year}"
612+
# and "2023 - {current_year}"
665613
for year in years:
666614
values["start_year"] = year
667615
hook.update_license_file(values)

0 commit comments

Comments
 (0)