diff --git a/.github/workflows/ci_cd.yml b/.github/workflows/ci_cd.yml index 6d32c007..a4891053 100644 --- a/.github/workflows/ci_cd.yml +++ b/.github/workflows/ci_cd.yml @@ -45,6 +45,7 @@ jobs: - uses: ansys/actions/doc-style@v5 with: token: ${{ secrets.GITHUB_TOKEN }} + vale-version: "3.4.1" smoke-tests: name: "Build and Smoke tests" diff --git a/LICENSE b/LICENSE index 0add1578..46903eb5 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2024 ANSYS, Inc. and/or its affiliates. +Copyright (c) 2023 - 2024 ANSYS, Inc. and/or its affiliates. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in diff --git a/doc/.vale.ini b/doc/.vale.ini index 4f0b6709..9071ecda 100644 --- a/doc/.vale.ini +++ b/doc/.vale.ini @@ -26,3 +26,5 @@ Vocab = ANSYS # Apply the following styles BasedOnStyles = Vale, Google + +Vale.Terms = NO diff --git a/doc/changelog.d/163.changed.md b/doc/changelog.d/163.changed.md new file mode 100644 index 00000000..2db3188e --- /dev/null +++ b/doc/changelog.d/163.changed.md @@ -0,0 +1 @@ +maint: bump reuse to v3.0.2 in add-license-headers \ No newline at end of file diff --git a/doc/styles/.gitignore b/doc/styles/.gitignore index 080f12aa..f98a973c 100644 --- a/doc/styles/.gitignore +++ b/doc/styles/.gitignore @@ -1,4 +1,4 @@ * -!Vocab -!Vocab/** +!vocabularies +!vocabularies/** !.gitignore \ No newline at end of file diff --git a/doc/styles/Vocab/ANSYS/accept.txt b/doc/styles/config/vocabularies/ANSYS/accept.txt similarity index 81% rename from doc/styles/Vocab/ANSYS/accept.txt rename to doc/styles/config/vocabularies/ANSYS/accept.txt index c1cf25a4..40090fd8 100644 --- a/doc/styles/Vocab/ANSYS/accept.txt +++ b/doc/styles/config/vocabularies/ANSYS/accept.txt @@ -2,3 +2,4 @@ API ANSYS Ansys ansys +maint \ No newline at end of file diff --git a/doc/styles/Vocab/ANSYS/reject.txt b/doc/styles/config/vocabularies/ANSYS/reject.txt similarity index 100% rename from doc/styles/Vocab/ANSYS/reject.txt rename to doc/styles/config/vocabularies/ANSYS/reject.txt diff --git a/pyproject.toml b/pyproject.toml index 9d0bb07c..0b7537dd 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -28,7 +28,7 @@ classifiers = [ ] dependencies = [ "importlib-metadata==7.1.0", - "reuse==2.1.0", + "reuse==3.0.2", "GitPython==3.1.43", ] @@ -36,8 +36,6 @@ dependencies = [ tests = [ "pytest==8.1.1", "pytest-cov==5.0.0", - "reuse==2.1.0", - "GitPython==3.1.43", ] doc = [ "ansys-sphinx-theme[autoapi]==0.15.2", diff --git a/src/ansys/pre_commit_hooks/add_license_headers.py b/src/ansys/pre_commit_hooks/add_license_headers.py index 2593fc53..5ec6757a 100644 --- a/src/ansys/pre_commit_hooks/add_license_headers.py +++ b/src/ansys/pre_commit_hooks/add_license_headers.py @@ -38,7 +38,7 @@ from tempfile import NamedTemporaryFile import git -from reuse import _util, header, lint, project +from reuse import _annotate, _util, lint, project DEFAULT_TEMPLATE = "ansys" """Default template to use for license headers.""" @@ -200,12 +200,6 @@ def list_noncompliant_files(args: argparse.Namespace, proj: project.Project) -> missing_licensing_info = set(lint_json["non_compliant"]["missing_licensing_info"]) missing_headers = missing_headers.union(missing_licensing_info) - if lint_json["non_compliant"]["missing_licenses"]: - missing_licenses = set( - lint_json["non_compliant"]["missing_licenses"][args.custom_license] - ) - missing_headers = missing_headers.union(missing_licenses) - # Remove temporary file os.remove(filename) @@ -244,9 +238,9 @@ def set_header_args( # Provide values for license header arguments args = parser.parse_args([file_path]) if start_year == current_year: - args.year = [str(current_year)] + args.year = [current_year] else: - args.year = [start_year, str(current_year)] + args.year = [int(start_year), current_year] args.copyright_style = "string-c" args.copyright = [copyright] args.merge_copyrights = True @@ -296,48 +290,41 @@ def check_exists( copyright = values["copyright"] template = values["template"] - if i < len(files): + for file in files: + args = set_header_args(parser, start_year, current_year, file, copyright, template) # If the committed file is in missing_headers - if (files[i] in missing_headers) or (os.path.getsize(files[i]) == 0): + if (file in missing_headers) or (os.path.getsize(file) == 0): changed_headers = 1 # Run REUSE on the file - args = set_header_args(parser, start_year, current_year, files[i], copyright, template) if not args.ignore_license_check: args.license = [values["license"]] - header.run(args, proj) - # Check if the next file is in missing_headers - return check_exists(changed_headers, parser, values, proj, missing_headers, i + 1) + _annotate.run(args, proj) else: - # Save current copy of files[i] + # Save current copy of file before_hook = NamedTemporaryFile(mode="w", delete=False).name - shutil.copyfile(files[i], before_hook) + shutil.copyfile(file, before_hook) # Update the header # tmp captures the stdout of the header.run() function with NamedTemporaryFile(mode="w", delete=True) as tmp: - args = set_header_args( - parser, start_year, current_year, files[i], copyright, template - ) - header.run(args, proj, tmp) + _annotate.run(args, proj, tmp) # Check if the file before add-license-headers was run is the same as the one # after add-license-headers was run. If not, apply the syntax changes # from other hooks before add-license-headers was run to the file - if check_same_content(before_hook, files[i]) == False: - add_hook_changes(before_hook, files[i]) + if check_same_content(before_hook, file) == False: + add_hook_changes(before_hook, file) # Check if the file content before add-license-headers was run has been changed # Assuming the syntax was fixed in the above if statement, this check is # solely for the file's content - if check_same_content(before_hook, files[i]) == False: + if check_same_content(before_hook, file) == False: changed_headers = 1 - print(f"Successfully changed header of {files[i]}") + print(f"Successfully changed header of {file}") os.remove(before_hook) - return check_exists(changed_headers, parser, values, proj, missing_headers, i + 1) - return changed_headers @@ -634,7 +621,8 @@ def find_files_missing_header() -> int: # year, style, copyright-style, template, exclude-year, merge-copyrights, single-line, # multi-line, explicit-license, force-dot-license, recursive, no-replace, # skip-unrecognized, and skip-existing - header.add_arguments(parser) + # header.add_arguments(parser) + _annotate.add_arguments(parser) # Link the default template and/or license from the assets folder to your git repo. link_assets(assets, os_git_root, args) diff --git a/tests/test_reuse.py b/tests/test_reuse.py index 91c5a6af..d6cfc3a9 100644 --- a/tests/test_reuse.py +++ b/tests/test_reuse.py @@ -34,6 +34,7 @@ git_repo = git.Repo(os.getcwd(), search_parent_directories=True) REPO_PATH = git_repo.git.rev_parse("--show-toplevel") +START_YEAR = "2023" 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): # Set up git repository in tmp_path with temporary file repo, tmp_file = set_up_repo(tmp_path, template_path, template_name, license_path, license_name) - custom_args = [tmp_file, "--start_year=2023"] + custom_args = [tmp_file, f"--start_year={START_YEAR}"] # Assert the hook fails because it added the header assert add_argv_run(repo, tmp_file, custom_args) == 1 @@ -155,7 +156,7 @@ def test_custom_start_year(tmp_path: pytest.TempPathFactory): # Assert the copyright line's time range is # from 2023 to the current year if count == 1: - assert f"2023 - {dt.today().year}" in line + assert f"{START_YEAR} - {dt.today().year}" in line if count > 1: break file.close() @@ -282,8 +283,9 @@ def test_main_fails(tmp_path: pytest.TempPathFactory): # Set up git repository in tmp_path with temporary file repo, tmp_file = set_up_repo(tmp_path, template_path, template_name, license_path, license_name) + custom_args = [tmp_file, f"--start_year={START_YEAR}"] - assert add_argv_run(repo, tmp_file, [tmp_file]) == 1 + assert add_argv_run(repo, tmp_file, custom_args) == 1 check_ansys_header(tmp_file) @@ -292,7 +294,7 @@ def test_main_fails(tmp_path: pytest.TempPathFactory): def test_main_passes(): """Test all files are compliant.""" - sys.argv[1:] = [] + sys.argv[1:] = [f"--start_year={START_YEAR}"] # Assert main runs successfully assert hook.main() == 0 @@ -442,60 +444,6 @@ def test_update_changed_header(tmp_path: pytest.TempPathFactory): os.chdir(REPO_PATH) -def test_missing_licenses(tmp_path: pytest.TempPathFactory): - """Test that LICENSES folder is required.""" - # Set template and license names - template_name = "test_template.jinja2" - license_name = "ECL-1.0.txt" - template_path = os.path.join(REPO_PATH, "tests", "test_reuse_files", "templates", template_name) - license_path = os.path.join(REPO_PATH, "tests", "test_reuse_files", "LICENSES", license_name) - - # Set up git repository in tmp_path with temporary file - repo, tmp_file = set_up_repo(tmp_path, template_path, template_name, license_path, license_name) - - # Remove LICENSES/ECL-1.0.txt file from tmp_path - os.remove(os.path.join(tmp_path, "LICENSES", license_name)) - - custom_args = [ - tmp_file, - "--custom_template=test_template", - "--custom_license=ECL-1.0", - ] - - # Add header to tmp_file - add_argv_run(repo, tmp_file, custom_args) - - custom_args = [ - tmp_file, - "--custom_template=test_template", - "--custom_license=ECL-1.0", - ] - - # If LICENSES/license_name.txt file is missing, then it will fail - # and add another SPDX line. This shows you need the - # license_name.txt (MIT.txt, for example) in LICENSES or else it - # will fail - assert add_argv_run(repo, tmp_file, custom_args) == 1 - - # Assert two SPDX-License lines are found in the file if - # the LICENSES/ECL-1.0.txt file does not exist. - file = open(tmp_file, "r") - count = 0 - for line in file: - count += 1 - if count == 6: - assert "SPDX-License" in line - # Ensure header was updated correctly and didn't add - # an extra SPDX-Identifier line - if count == 7: - assert "SPDX-License" in line - if count > 7: - break - file.close() - - os.chdir(REPO_PATH) - - def test_copy_assets(tmp_path: pytest.TempPathFactory): """Test .reuse and LICENSES folders are copied.""" # List of files to be git added @@ -588,7 +536,7 @@ def test_index_exception(tmp_path: pytest.TempPathFactory): "--custom_copyright=ANSYS, Inc. Unauthorized use, distribution, \ or duplication is prohibited.", "--ignore_license_check", - "--start_year=2023", + f"--start_year={START_YEAR}", ] # Assert the hook failed @@ -652,16 +600,16 @@ def test_license_year_update(tmp_path: pytest.TempPathFactory): values = { "copyright": args.custom_copyright, "license": args.custom_license, - "start_year": args.start_year, + "start_year": START_YEAR, "current_year": dt.today().year, "git_repo": git_repo, } # Years to update the LICENSE file - years = [values["start_year"], "2022", dt.today().year] + years = ["2022", dt.today().year, values["start_year"]] - # Check the copyright line has "2023 - {current_year}", "2022 - {current_year}", - # and "{current_year}" + # Check the copyright line has "2022 - {current_year}", "{current_year}" + # and "2023 - {current_year}" for year in years: values["start_year"] = year hook.update_license_file(values)