ansys-pre-commit-hooks 0.6.0__tar.gz → 0.7.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {ansys_pre_commit_hooks-0.6.0/src/ansys_pre_commit_hooks.egg-info → ansys_pre_commit_hooks-0.7.1}/PKG-INFO +6 -6
- {ansys_pre_commit_hooks-0.6.0 → ansys_pre_commit_hooks-0.7.1}/pyproject.toml +8 -7
- {ansys_pre_commit_hooks-0.6.0 → ansys_pre_commit_hooks-0.7.1}/setup.py +5 -5
- ansys_pre_commit_hooks-0.7.1/src/ansys/pre_commit_hooks/VERSION +1 -0
- {ansys_pre_commit_hooks-0.6.0 → ansys_pre_commit_hooks-0.7.1}/src/ansys/pre_commit_hooks/add_license_headers.py +213 -44
- {ansys_pre_commit_hooks-0.6.0 → ansys_pre_commit_hooks-0.7.1}/src/ansys/pre_commit_hooks/assets/.reuse/templates/ansys.jinja2 +12 -0
- ansys_pre_commit_hooks-0.7.1/src/ansys/pre_commit_hooks/assets/LICENSES/Apache-2.0.txt +202 -0
- {ansys_pre_commit_hooks-0.6.0 → ansys_pre_commit_hooks-0.7.1/src/ansys_pre_commit_hooks.egg-info}/PKG-INFO +6 -6
- {ansys_pre_commit_hooks-0.6.0 → ansys_pre_commit_hooks-0.7.1}/src/ansys_pre_commit_hooks.egg-info/SOURCES.txt +1 -0
- {ansys_pre_commit_hooks-0.6.0 → ansys_pre_commit_hooks-0.7.1}/src/ansys_pre_commit_hooks.egg-info/requires.txt +5 -5
- {ansys_pre_commit_hooks-0.6.0 → ansys_pre_commit_hooks-0.7.1}/tests/test_add_license_headers.py +230 -0
- ansys_pre_commit_hooks-0.6.0/src/ansys/pre_commit_hooks/VERSION +0 -1
- {ansys_pre_commit_hooks-0.6.0 → ansys_pre_commit_hooks-0.7.1}/LICENSE +0 -0
- {ansys_pre_commit_hooks-0.6.0 → ansys_pre_commit_hooks-0.7.1}/MANIFEST.in +0 -0
- {ansys_pre_commit_hooks-0.6.0 → ansys_pre_commit_hooks-0.7.1}/README.rst +0 -0
- {ansys_pre_commit_hooks-0.6.0 → ansys_pre_commit_hooks-0.7.1}/setup.cfg +0 -0
- {ansys_pre_commit_hooks-0.6.0 → ansys_pre_commit_hooks-0.7.1}/src/ansys/pre_commit_hooks/__init__.py +0 -0
- {ansys_pre_commit_hooks-0.6.0 → ansys_pre_commit_hooks-0.7.1}/src/ansys/pre_commit_hooks/assets/LICENSES/MIT.txt +0 -0
- {ansys_pre_commit_hooks-0.6.0 → ansys_pre_commit_hooks-0.7.1}/src/ansys/pre_commit_hooks/assets/licenses.json +0 -0
- {ansys_pre_commit_hooks-0.6.0 → ansys_pre_commit_hooks-0.7.1}/src/ansys/pre_commit_hooks/tech_review.py +0 -0
- {ansys_pre_commit_hooks-0.6.0 → ansys_pre_commit_hooks-0.7.1}/src/ansys/pre_commit_hooks/templates/AUTHORS +0 -0
- {ansys_pre_commit_hooks-0.6.0 → ansys_pre_commit_hooks-0.7.1}/src/ansys/pre_commit_hooks/templates/CODE_OF_CONDUCT.md +0 -0
- {ansys_pre_commit_hooks-0.6.0 → ansys_pre_commit_hooks-0.7.1}/src/ansys/pre_commit_hooks/templates/CONTRIBUTING.md +0 -0
- {ansys_pre_commit_hooks-0.6.0 → ansys_pre_commit_hooks-0.7.1}/src/ansys/pre_commit_hooks/templates/CONTRIBUTORS.md +0 -0
- {ansys_pre_commit_hooks-0.6.0 → ansys_pre_commit_hooks-0.7.1}/src/ansys/pre_commit_hooks/templates/LICENSE +0 -0
- {ansys_pre_commit_hooks-0.6.0 → ansys_pre_commit_hooks-0.7.1}/src/ansys/pre_commit_hooks/templates/README.md +0 -0
- {ansys_pre_commit_hooks-0.6.0 → ansys_pre_commit_hooks-0.7.1}/src/ansys/pre_commit_hooks/templates/README.rst +0 -0
- {ansys_pre_commit_hooks-0.6.0 → ansys_pre_commit_hooks-0.7.1}/src/ansys/pre_commit_hooks/templates/dependabot.yml +0 -0
- {ansys_pre_commit_hooks-0.6.0 → ansys_pre_commit_hooks-0.7.1}/src/ansys_pre_commit_hooks.egg-info/dependency_links.txt +0 -0
- {ansys_pre_commit_hooks-0.6.0 → ansys_pre_commit_hooks-0.7.1}/src/ansys_pre_commit_hooks.egg-info/entry_points.txt +0 -0
- {ansys_pre_commit_hooks-0.6.0 → ansys_pre_commit_hooks-0.7.1}/src/ansys_pre_commit_hooks.egg-info/top_level.txt +0 -0
- {ansys_pre_commit_hooks-0.6.0 → ansys_pre_commit_hooks-0.7.1}/tests/test_metadata.py +0 -0
- {ansys_pre_commit_hooks-0.6.0 → ansys_pre_commit_hooks-0.7.1}/tests/test_tech_review.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: ansys-pre-commit-hooks
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.7.1
|
|
4
4
|
Home-page: https://github.com/ansys/pre-commit-hooks
|
|
5
5
|
Author: ANSYS, Inc.
|
|
6
6
|
Author-email: "ANSYS, Inc." <pyansys.core@ansys.com>
|
|
@@ -25,10 +25,10 @@ Requires-Python: >=3.10,<4
|
|
|
25
25
|
Description-Content-Type: text/x-rst
|
|
26
26
|
License-File: LICENSE
|
|
27
27
|
Requires-Dist: GitPython==3.1.46
|
|
28
|
-
Requires-Dist: importlib-metadata==
|
|
28
|
+
Requires-Dist: importlib-metadata==9.0.0
|
|
29
29
|
Requires-Dist: Jinja2==3.1.6
|
|
30
30
|
Requires-Dist: reuse==6.2.0
|
|
31
|
-
Requires-Dist: requests==2.
|
|
31
|
+
Requires-Dist: requests==2.33.1
|
|
32
32
|
Requires-Dist: semver==3.0.4
|
|
33
33
|
Requires-Dist: toml==0.10.2
|
|
34
34
|
Provides-Extra: doc
|
|
@@ -36,10 +36,10 @@ Requires-Dist: ansys-sphinx-theme[autoapi]==1.7.2; extra == "doc"
|
|
|
36
36
|
Requires-Dist: numpydoc==1.10.0; extra == "doc"
|
|
37
37
|
Requires-Dist: sphinx==8.2.3; extra == "doc"
|
|
38
38
|
Requires-Dist: sphinx-autodoc-typehints==3.1.0; extra == "doc"
|
|
39
|
-
Requires-Dist: sphinx-copybutton==0.5.
|
|
39
|
+
Requires-Dist: sphinx-copybutton==0.5.2; extra == "doc"
|
|
40
40
|
Provides-Extra: tests
|
|
41
|
-
Requires-Dist: pytest==9.0.
|
|
42
|
-
Requires-Dist: pytest-cov==7.
|
|
41
|
+
Requires-Dist: pytest==9.0.3; extra == "tests"
|
|
42
|
+
Requires-Dist: pytest-cov==7.1.0; extra == "tests"
|
|
43
43
|
Dynamic: author
|
|
44
44
|
Dynamic: classifier
|
|
45
45
|
Dynamic: home-page
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
[build-system]
|
|
2
2
|
requires = [
|
|
3
|
-
"setuptools>=
|
|
3
|
+
"setuptools>=65.5.1",
|
|
4
4
|
"GitPython==3.1.46",
|
|
5
5
|
"Jinja2==3.1.6",
|
|
6
6
|
"reuse==6.2.0",
|
|
7
|
-
"requests==2.
|
|
7
|
+
"requests==2.33.1",
|
|
8
8
|
"semver==3.0.4",
|
|
9
9
|
"toml==0.10.2",
|
|
10
10
|
"wheel",
|
|
@@ -21,10 +21,10 @@ authors = [{ name = "ANSYS, Inc.", email = "pyansys.core@ansys.com" }]
|
|
|
21
21
|
maintainers = [{ name = "ANSYS, Inc.", email = "pyansys.core@ansys.com" }]
|
|
22
22
|
dependencies = [
|
|
23
23
|
"GitPython==3.1.46",
|
|
24
|
-
"importlib-metadata==
|
|
24
|
+
"importlib-metadata==9.0.0",
|
|
25
25
|
"Jinja2==3.1.6",
|
|
26
26
|
"reuse==6.2.0",
|
|
27
|
-
"requests==2.
|
|
27
|
+
"requests==2.33.1",
|
|
28
28
|
"semver==3.0.4",
|
|
29
29
|
"toml==0.10.2",
|
|
30
30
|
]
|
|
@@ -42,6 +42,7 @@ classifiers = {file = """
|
|
|
42
42
|
"Programming Language :: Python :: 3.11",
|
|
43
43
|
"Programming Language :: Python :: 3.12",
|
|
44
44
|
"Programming Language :: Python :: 3.13",
|
|
45
|
+
"Programming Language :: Python :: 3.14",
|
|
45
46
|
"""}
|
|
46
47
|
|
|
47
48
|
[project.optional-dependencies]
|
|
@@ -50,11 +51,11 @@ doc = [
|
|
|
50
51
|
"numpydoc==1.10.0",
|
|
51
52
|
"sphinx==8.2.3",
|
|
52
53
|
"sphinx-autodoc-typehints==3.1.0",
|
|
53
|
-
"sphinx-copybutton==0.5.
|
|
54
|
+
"sphinx-copybutton==0.5.2",
|
|
54
55
|
]
|
|
55
56
|
tests = [
|
|
56
|
-
"pytest==9.0.
|
|
57
|
-
"pytest-cov==7.
|
|
57
|
+
"pytest==9.0.3",
|
|
58
|
+
"pytest-cov==7.1.0",
|
|
58
59
|
]
|
|
59
60
|
|
|
60
61
|
[project.urls]
|
|
@@ -40,10 +40,10 @@ setup(
|
|
|
40
40
|
python_requires=">=3.10,<4",
|
|
41
41
|
install_requires=[
|
|
42
42
|
"GitPython==3.1.46",
|
|
43
|
-
"importlib-metadata==
|
|
43
|
+
"importlib-metadata==9.0.0",
|
|
44
44
|
"Jinja2==3.1.6",
|
|
45
45
|
"reuse==6.2.0",
|
|
46
|
-
"requests==2.
|
|
46
|
+
"requests==2.33.1",
|
|
47
47
|
"semver==3.0.4",
|
|
48
48
|
"toml==0.10.2",
|
|
49
49
|
],
|
|
@@ -53,12 +53,12 @@ setup(
|
|
|
53
53
|
"numpydoc==1.10.0",
|
|
54
54
|
"sphinx==8.2.3",
|
|
55
55
|
"sphinx-autodoc-typehints==3.1.0",
|
|
56
|
-
"sphinx-copybutton==0.5.
|
|
56
|
+
"sphinx-copybutton==0.5.2",
|
|
57
57
|
"sphinx_design==0.6.1",
|
|
58
58
|
],
|
|
59
59
|
"tests": [
|
|
60
|
-
"pytest==9.0.
|
|
61
|
-
"pytest-cov==7.
|
|
60
|
+
"pytest==9.0.3",
|
|
61
|
+
"pytest-cov==7.1.0",
|
|
62
62
|
],
|
|
63
63
|
},
|
|
64
64
|
project_urls={
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
0.7.1
|
|
@@ -125,9 +125,45 @@ def get_full_paths(file_list: list) -> list:
|
|
|
125
125
|
return full_path_files
|
|
126
126
|
|
|
127
127
|
|
|
128
|
-
|
|
128
|
+
# Mapping from license identifier to a string that uniquely identifies that license
|
|
129
|
+
# in the body of the LICENSE file.
|
|
130
|
+
_LICENSE_FINGERPRINTS = {
|
|
131
|
+
"MIT": "MIT License",
|
|
132
|
+
"Apache-2.0": "Apache License, Version 2.0",
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def _detect_license_mismatch(existing_content: str, requested_license: str) -> bool:
|
|
137
|
+
"""Return ``True`` if the existing LICENSE file does not match the requested license type.
|
|
138
|
+
|
|
139
|
+
Parameters
|
|
140
|
+
----------
|
|
141
|
+
existing_content: str
|
|
142
|
+
The full text of the existing LICENSE file.
|
|
143
|
+
requested_license: str
|
|
144
|
+
The SPDX license identifier that should be in the LICENSE file (e.g. ``"MIT"`` or
|
|
145
|
+
``"Apache-2.0"``).
|
|
146
|
+
|
|
147
|
+
Returns
|
|
148
|
+
-------
|
|
149
|
+
bool
|
|
150
|
+
``True`` if the file needs to be fully regenerated because its license type
|
|
151
|
+
differs from ``requested_license``.
|
|
152
|
+
``False`` if the file already uses the requested license and only the year
|
|
153
|
+
may need updating.
|
|
154
|
+
"""
|
|
155
|
+
expected_fingerprint = _LICENSE_FINGERPRINTS.get(requested_license)
|
|
156
|
+
if expected_fingerprint is None:
|
|
157
|
+
# Unknown license — assume no mismatch, fall back to year-update logic
|
|
158
|
+
return False
|
|
159
|
+
return expected_fingerprint not in existing_content
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def update_license_file(
|
|
163
|
+
repo_license_path: Path, year_span: str, license: str = DEFAULT_LICENSE
|
|
164
|
+
) -> int:
|
|
129
165
|
"""
|
|
130
|
-
Update the LICENSE file to match
|
|
166
|
+
Update the LICENSE file to match the license template, adjusting the year span for each repo.
|
|
131
167
|
|
|
132
168
|
Parameters
|
|
133
169
|
----------
|
|
@@ -135,6 +171,8 @@ def update_license_file(repo_license_path: Path, year_span: str) -> int:
|
|
|
135
171
|
Path to the LICENSE file in the repository.
|
|
136
172
|
year_span: str
|
|
137
173
|
The user start year to the current year. If they are the same, default to the current year.
|
|
174
|
+
license: str
|
|
175
|
+
The license identifier to use. Defaults to ``DEFAULT_LICENSE`` (MIT).
|
|
138
176
|
|
|
139
177
|
Returns
|
|
140
178
|
-------
|
|
@@ -150,13 +188,23 @@ def update_license_file(repo_license_path: Path, year_span: str) -> int:
|
|
|
150
188
|
temp_file = NamedTemporaryFile(mode="w", delete=False).name
|
|
151
189
|
shutil.copyfile(repo_license_path, temp_file)
|
|
152
190
|
|
|
153
|
-
#
|
|
154
|
-
|
|
191
|
+
# Detect whether the existing LICENSE file matches the requested license type.
|
|
192
|
+
# If the license has changed (e.g. MIT → Apache-2.0), regenerate the file
|
|
193
|
+
# from the template rather than just updating the year.
|
|
194
|
+
with Path(repo_license_path).open(encoding="utf-8", newline="", mode="r") as f:
|
|
195
|
+
existing_content = f.read()
|
|
155
196
|
|
|
156
|
-
|
|
157
|
-
lines = file.readlines()
|
|
158
|
-
content = "".join(lines)
|
|
197
|
+
license_mismatch = _detect_license_mismatch(existing_content, license)
|
|
159
198
|
|
|
199
|
+
if license_mismatch:
|
|
200
|
+
hook_loc = Path(__file__).parent.resolve()
|
|
201
|
+
template_parent_dir = hook_loc / "assets" / "LICENSES"
|
|
202
|
+
generate_license_file(template_parent_dir, year_span, repo_license_path, license)
|
|
203
|
+
else:
|
|
204
|
+
# The year regex to match the year or year range in the LICENSE file
|
|
205
|
+
year_regex = r"(\d{4}) - (\d{4})|\d{4}"
|
|
206
|
+
|
|
207
|
+
content = existing_content
|
|
160
208
|
# Get the first instance of the year range, either one year or a range of years
|
|
161
209
|
year_range_match = re.search(year_regex, content)
|
|
162
210
|
# Get the group from the year_range_match
|
|
@@ -170,7 +218,7 @@ def update_license_file(repo_license_path: Path, year_span: str) -> int:
|
|
|
170
218
|
with repo_license_path.open(encoding="utf-8", newline="", mode="w") as file:
|
|
171
219
|
file.write(content)
|
|
172
220
|
|
|
173
|
-
# If the
|
|
221
|
+
# If the file changed, print a message that the LICENSE file was changed
|
|
174
222
|
if not check_same_content(temp_file, repo_license_path):
|
|
175
223
|
changed = 1
|
|
176
224
|
print(f"Successfully updated year in {repo_license_path}")
|
|
@@ -206,27 +254,29 @@ def link_assets(assets: dict, git_root: str, args: argparse.Namespace) -> None:
|
|
|
206
254
|
repo_asset_dir = Path(git_root) / value["path"]
|
|
207
255
|
|
|
208
256
|
# If key is .reuse and the custom template is being used
|
|
257
|
+
# If key is LICENSES and ignore_license_check is False, link the appropriate license file.
|
|
209
258
|
if key == ".reuse" and args.custom_template == DEFAULT_TEMPLATE:
|
|
210
259
|
mkdirs_and_link(value["path"], hook_asset_dir, repo_asset_dir, value["default_file"])
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
and args.custom_license == DEFAULT_LICENSE
|
|
216
|
-
and not args.ignore_license_check
|
|
217
|
-
):
|
|
218
|
-
hook_license_file = hook_loc / "assets" / "LICENSES" / f"{DEFAULT_LICENSE}.txt"
|
|
260
|
+
# Supports both the default (MIT) and any other recognized license (e.g. Apache-2.0).
|
|
261
|
+
if key == "LICENSES" and not args.ignore_license_check:
|
|
262
|
+
custom_license = args.custom_license
|
|
263
|
+
hook_license_file = hook_loc / "assets" / "LICENSES" / f"{custom_license}.txt"
|
|
219
264
|
repo_license_file = repo_asset_dir / value["default_file"]
|
|
220
265
|
if not repo_asset_dir.is_dir():
|
|
221
266
|
repo_asset_dir.mkdir(parents=True)
|
|
222
|
-
if not repo_license_file.is_file():
|
|
223
|
-
generate_license_file(
|
|
267
|
+
if hook_license_file.is_file() and not repo_license_file.is_file():
|
|
268
|
+
generate_license_file(
|
|
269
|
+
hook_license_file.parent, dt.today().year, repo_license_file, custom_license
|
|
270
|
+
)
|
|
224
271
|
|
|
225
272
|
|
|
226
273
|
def generate_license_file(
|
|
227
|
-
template_parent_dir: Path,
|
|
274
|
+
template_parent_dir: Path,
|
|
275
|
+
year_span: Union[int, str],
|
|
276
|
+
license_file_name: Path,
|
|
277
|
+
license: str = DEFAULT_LICENSE,
|
|
228
278
|
) -> None:
|
|
229
|
-
"""Generate the
|
|
279
|
+
"""Generate the license file from the assets/LICENSES/<license>.txt template.
|
|
230
280
|
|
|
231
281
|
Parameters
|
|
232
282
|
----------
|
|
@@ -237,13 +287,15 @@ def generate_license_file(
|
|
|
237
287
|
For example, "2024" or "2023 - 2025".
|
|
238
288
|
license_file_name: Path
|
|
239
289
|
Path to the license file in the repository to generate.
|
|
290
|
+
license: str
|
|
291
|
+
The license identifier. Defaults to ``DEFAULT_LICENSE`` (MIT).
|
|
240
292
|
"""
|
|
241
293
|
from jinja2 import Environment, FileSystemLoader
|
|
242
294
|
|
|
243
295
|
loader = FileSystemLoader(searchpath=template_parent_dir)
|
|
244
296
|
env = Environment(loader=loader) # nosec
|
|
245
|
-
# Get the template for the specified file
|
|
246
|
-
template = env.get_template(f"{
|
|
297
|
+
# Get the template for the specified license file
|
|
298
|
+
template = env.get_template(f"{license}.txt")
|
|
247
299
|
# Generate the file content from the template
|
|
248
300
|
file_content = template.render(year_span=year_span)
|
|
249
301
|
|
|
@@ -288,7 +340,13 @@ def mkdirs_and_link(
|
|
|
288
340
|
dest.unlink()
|
|
289
341
|
|
|
290
342
|
# Make symbolic links to files within the assets folder
|
|
291
|
-
|
|
343
|
+
# Fall back to copying if symlinks are not permitted (e.g. Windows without Developer Mode)
|
|
344
|
+
try:
|
|
345
|
+
dest.symlink_to(src)
|
|
346
|
+
except OSError:
|
|
347
|
+
import shutil
|
|
348
|
+
|
|
349
|
+
shutil.copy2(src, dest)
|
|
292
350
|
|
|
293
351
|
|
|
294
352
|
def _has_current_header(file_path: str, copyright: list, years: str) -> bool:
|
|
@@ -323,6 +381,80 @@ def _has_current_header(file_path: str, copyright: list, years: str) -> bool:
|
|
|
323
381
|
return copyright[0] in head and years in head
|
|
324
382
|
|
|
325
383
|
|
|
384
|
+
def _file_has_license(file_path: str, license: str) -> bool:
|
|
385
|
+
"""Check if the file's header already contains the requested SPDX-License-Identifier.
|
|
386
|
+
|
|
387
|
+
Parameters
|
|
388
|
+
----------
|
|
389
|
+
file_path: str
|
|
390
|
+
Path to the file to check.
|
|
391
|
+
license: str
|
|
392
|
+
License SPDX identifier string.
|
|
393
|
+
For example, ``"MIT"`` or ``"Apache-2.0"``.
|
|
394
|
+
|
|
395
|
+
Returns
|
|
396
|
+
-------
|
|
397
|
+
bool
|
|
398
|
+
``True`` if the file header already contains the expected license identifier.
|
|
399
|
+
``False`` if the license identifier is missing or different.
|
|
400
|
+
"""
|
|
401
|
+
try:
|
|
402
|
+
with Path(file_path).open(encoding="utf-8", errors="ignore") as f:
|
|
403
|
+
head = f.read(1024)
|
|
404
|
+
except OSError:
|
|
405
|
+
return False
|
|
406
|
+
|
|
407
|
+
return f"SPDX-License-Identifier: {license[0]}" in head
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
def _strip_reuse_header(file_path: str) -> None:
|
|
411
|
+
"""Remove the existing REUSE header block (copyright + SPDX lines + license text) from a file.
|
|
412
|
+
|
|
413
|
+
This is used before re-annotating a file with a different license so that the old
|
|
414
|
+
``SPDX-License-Identifier`` is not left behind. The header is identified as the
|
|
415
|
+
leading block of comment lines that contains at least one ``SPDX`` marker. Any
|
|
416
|
+
blank lines immediately following the header block are also removed.
|
|
417
|
+
|
|
418
|
+
Parameters
|
|
419
|
+
----------
|
|
420
|
+
file_path: str
|
|
421
|
+
Path to the file whose REUSE header should be stripped.
|
|
422
|
+
"""
|
|
423
|
+
from reuse import extract
|
|
424
|
+
|
|
425
|
+
with Path(file_path).open(encoding="utf-8", newline="", mode="r") as f:
|
|
426
|
+
lines = f.readlines()
|
|
427
|
+
|
|
428
|
+
# Walk through comment lines at the top of the file and collect the header block.
|
|
429
|
+
# A comment line is any line whose stripped content starts with a comment character
|
|
430
|
+
# or is blank (blank lines are allowed within the comment block).
|
|
431
|
+
# We keep scanning until we hit a non-comment, non-blank line.
|
|
432
|
+
comment_chars = ("#", "//", "*", "/*", "*/", "--", "%", ";", "!")
|
|
433
|
+
header_end = 0
|
|
434
|
+
in_header = False
|
|
435
|
+
|
|
436
|
+
for i, line in enumerate(lines):
|
|
437
|
+
stripped = line.strip()
|
|
438
|
+
is_comment = stripped == "" or any(stripped.startswith(c) for c in comment_chars)
|
|
439
|
+
if is_comment:
|
|
440
|
+
if extract.contains_reuse_info(line):
|
|
441
|
+
in_header = True
|
|
442
|
+
header_end = i + 1
|
|
443
|
+
else:
|
|
444
|
+
break
|
|
445
|
+
|
|
446
|
+
if not in_header:
|
|
447
|
+
# No REUSE header found — nothing to strip
|
|
448
|
+
return
|
|
449
|
+
|
|
450
|
+
# Skip any blank lines immediately after the header
|
|
451
|
+
while header_end < len(lines) and lines[header_end].strip() == "":
|
|
452
|
+
header_end += 1
|
|
453
|
+
|
|
454
|
+
with Path(file_path).open(encoding="utf-8", newline="", mode="w") as f:
|
|
455
|
+
f.writelines(lines[header_end:])
|
|
456
|
+
|
|
457
|
+
|
|
326
458
|
def non_recursive_file_check(
|
|
327
459
|
changed_headers: int, obj: "common.ClickObj", values: dict, args: argparse.Namespace
|
|
328
460
|
) -> int:
|
|
@@ -353,11 +485,14 @@ def non_recursive_file_check(
|
|
|
353
485
|
)
|
|
354
486
|
|
|
355
487
|
for file in pre_commit_files:
|
|
356
|
-
#
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
#
|
|
488
|
+
# If license isn't specified, set it as True, otherwise check the content of the file
|
|
489
|
+
# for the existing license
|
|
490
|
+
existing_license_matches = True if not license else _file_has_license(file, license)
|
|
491
|
+
|
|
492
|
+
# Fast check: skip files that already have the correct header (copyright,
|
|
493
|
+
# year range, AND the expected license identifier).
|
|
494
|
+
if _has_current_header(file, copyright, years) and existing_license_matches:
|
|
495
|
+
continue # Get the reuse information of the file
|
|
361
496
|
file_reuse_info = project.reuse_info_of(file)
|
|
362
497
|
|
|
363
498
|
# If the file is empty or does not contain reuse information
|
|
@@ -365,9 +500,22 @@ def non_recursive_file_check(
|
|
|
365
500
|
changed_headers = 1
|
|
366
501
|
add_header(copyright, license, years, file, template, commented, sys.stdout)
|
|
367
502
|
else:
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
503
|
+
# Check whether the existing SPDX-License-Identifier differs from the
|
|
504
|
+
# requested license. If so, strip the old header and write a fresh one
|
|
505
|
+
# so that the old identifier is fully replaced rather than merged.
|
|
506
|
+
if not existing_license_matches:
|
|
507
|
+
before_hook = NamedTemporaryFile(mode="w", delete=False).name
|
|
508
|
+
shutil.copyfile(file, before_hook)
|
|
509
|
+
_strip_reuse_header(file)
|
|
510
|
+
add_header(copyright, license, years, file, template, commented, sys.stdout)
|
|
511
|
+
if not check_same_content(before_hook, file):
|
|
512
|
+
changed_headers = 1
|
|
513
|
+
print(f"Successfully changed header of {file}")
|
|
514
|
+
Path(before_hook).unlink()
|
|
515
|
+
else:
|
|
516
|
+
changed_headers = update_header(
|
|
517
|
+
changed_headers, file, copyright, license, years, template, commented
|
|
518
|
+
)
|
|
371
519
|
|
|
372
520
|
return changed_headers
|
|
373
521
|
|
|
@@ -415,6 +563,7 @@ def update_header(
|
|
|
415
563
|
years: str,
|
|
416
564
|
template: str,
|
|
417
565
|
commented: bool,
|
|
566
|
+
force_license: bool = False,
|
|
418
567
|
) -> int:
|
|
419
568
|
"""Update the license header of the file.
|
|
420
569
|
|
|
@@ -435,6 +584,11 @@ def update_header(
|
|
|
435
584
|
The template to use for the header. For example, "ansys" for "ansys.jinja2".
|
|
436
585
|
commented: bool
|
|
437
586
|
Whether the template is commented or not.
|
|
587
|
+
force_license: bool
|
|
588
|
+
When ``True``, pass the full license list to :func:`add_header` so that a
|
|
589
|
+
mismatched ``SPDX-License-Identifier`` (e.g. MIT when Apache-2.0 is
|
|
590
|
+
requested) is fully replaced instead of being left unchanged.
|
|
591
|
+
Defaults to ``False``.
|
|
438
592
|
|
|
439
593
|
Returns
|
|
440
594
|
-------
|
|
@@ -442,18 +596,26 @@ def update_header(
|
|
|
442
596
|
``0`` if all files contain headers and are up to date.
|
|
443
597
|
``1`` if ``REUSE`` changed all noncompliant files.
|
|
444
598
|
"""
|
|
445
|
-
#
|
|
446
|
-
#
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
599
|
+
# When force_license is True, keep the license list populated so that
|
|
600
|
+
# add_header replaces the existing (wrong) SPDX-License-Identifier.
|
|
601
|
+
# Otherwise, clear the list to avoid adding the identifier a second time.
|
|
602
|
+
effective_license = (
|
|
603
|
+
license if force_license else []
|
|
604
|
+
) # If the license type is changing, strip the old header first so REUSE
|
|
605
|
+
# doesn't merge the old SPDX-License-Identifier with the new one.
|
|
606
|
+
if force_license:
|
|
607
|
+
_strip_reuse_header(file)
|
|
608
|
+
|
|
609
|
+
# Save the current state of the file (post-strip when force_license, otherwise
|
|
610
|
+
# original) so that apply_hook_changes can preserve any non-header edits made
|
|
611
|
+
# by earlier hooks.
|
|
450
612
|
before_hook = NamedTemporaryFile(mode="w", delete=False).name
|
|
451
613
|
shutil.copyfile(file, before_hook)
|
|
452
614
|
|
|
453
615
|
# Update the header
|
|
454
616
|
# tmp captures the stdout of the header.run() function
|
|
455
617
|
with NamedTemporaryFile(mode="w", delete=True) as tmp:
|
|
456
|
-
add_header(copyright,
|
|
618
|
+
add_header(copyright, effective_license, years, file, template, commented, tmp)
|
|
457
619
|
|
|
458
620
|
# Check if the file before add-license-headers was run is the same as the one
|
|
459
621
|
# after add-license-headers was run. If not, apply the syntax changes
|
|
@@ -530,12 +692,17 @@ def add_header(
|
|
|
530
692
|
)
|
|
531
693
|
|
|
532
694
|
# Add or update the header in the file with the REUSE information.
|
|
695
|
+
comment_style = get_comment_style(file)
|
|
696
|
+
if comment_style is None:
|
|
697
|
+
print(f"Skipping {file}: no comment style found for this file type.", file=out)
|
|
698
|
+
return
|
|
699
|
+
|
|
533
700
|
add_header_to_file(
|
|
534
701
|
path=file,
|
|
535
702
|
reuse_info=reuse_info,
|
|
536
703
|
template=template,
|
|
537
704
|
template_is_commented=commented,
|
|
538
|
-
style=f"{
|
|
705
|
+
style=f"{comment_style.SHORTHAND}",
|
|
539
706
|
merge_copyrights=True,
|
|
540
707
|
out=out,
|
|
541
708
|
)
|
|
@@ -794,7 +961,6 @@ def main():
|
|
|
794
961
|
"current_year": dt.today().year,
|
|
795
962
|
"git_repo": git_repo,
|
|
796
963
|
}
|
|
797
|
-
|
|
798
964
|
# Dictionary containing the asset folder information
|
|
799
965
|
assets = {
|
|
800
966
|
".reuse": {
|
|
@@ -803,7 +969,7 @@ def main():
|
|
|
803
969
|
},
|
|
804
970
|
"LICENSES": {
|
|
805
971
|
"path": "LICENSES",
|
|
806
|
-
"default_file": f"{
|
|
972
|
+
"default_file": f"{args.custom_license}.txt",
|
|
807
973
|
},
|
|
808
974
|
}
|
|
809
975
|
|
|
@@ -815,8 +981,11 @@ def main():
|
|
|
815
981
|
# Get the path to the LICENSE file in the repository
|
|
816
982
|
repo_license_path = git_root / "LICENSE"
|
|
817
983
|
|
|
818
|
-
# Update the
|
|
819
|
-
|
|
984
|
+
# Update the LICENSE file if it exists and a known license template is available.
|
|
985
|
+
# This handles both year-only updates and full license replacement (e.g. MIT → Apache-2.0).
|
|
986
|
+
hook_loc = Path(__file__).parent.resolve()
|
|
987
|
+
license_template = hook_loc / "assets" / "LICENSES" / f"{args.custom_license}.txt"
|
|
988
|
+
if repo_license_path.is_file() and license_template.is_file():
|
|
820
989
|
# Create a year span based on user input and the current year
|
|
821
990
|
user_start_year = str(values["start_year"])
|
|
822
991
|
current_year = str(values["current_year"])
|
|
@@ -826,8 +995,8 @@ def main():
|
|
|
826
995
|
else current_year
|
|
827
996
|
)
|
|
828
997
|
|
|
829
|
-
# Update
|
|
830
|
-
license_return_code = update_license_file(repo_license_path, year_span)
|
|
998
|
+
# Update (or regenerate) the LICENSE file
|
|
999
|
+
license_return_code = update_license_file(repo_license_path, year_span, args.custom_license)
|
|
831
1000
|
|
|
832
1001
|
from reuse.cli import common
|
|
833
1002
|
|
|
@@ -24,4 +24,16 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
24
24
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
25
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
26
|
SOFTWARE.
|
|
27
|
+
{% elif "Apache-2.0" in spdx_expressions %}
|
|
28
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
29
|
+
you may not use this file except in compliance with the License.
|
|
30
|
+
You may obtain a copy of the License at
|
|
31
|
+
|
|
32
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
33
|
+
|
|
34
|
+
Unless required by applicable law or agreed to in writing, software
|
|
35
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
36
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
37
|
+
See the License for the specific language governing permissions and
|
|
38
|
+
limitations under the License.
|
|
27
39
|
{% endif %}
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright {{ year_span }} ANSYS, Inc. and/or its affiliates.
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: ansys-pre-commit-hooks
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.7.1
|
|
4
4
|
Home-page: https://github.com/ansys/pre-commit-hooks
|
|
5
5
|
Author: ANSYS, Inc.
|
|
6
6
|
Author-email: "ANSYS, Inc." <pyansys.core@ansys.com>
|
|
@@ -25,10 +25,10 @@ Requires-Python: >=3.10,<4
|
|
|
25
25
|
Description-Content-Type: text/x-rst
|
|
26
26
|
License-File: LICENSE
|
|
27
27
|
Requires-Dist: GitPython==3.1.46
|
|
28
|
-
Requires-Dist: importlib-metadata==
|
|
28
|
+
Requires-Dist: importlib-metadata==9.0.0
|
|
29
29
|
Requires-Dist: Jinja2==3.1.6
|
|
30
30
|
Requires-Dist: reuse==6.2.0
|
|
31
|
-
Requires-Dist: requests==2.
|
|
31
|
+
Requires-Dist: requests==2.33.1
|
|
32
32
|
Requires-Dist: semver==3.0.4
|
|
33
33
|
Requires-Dist: toml==0.10.2
|
|
34
34
|
Provides-Extra: doc
|
|
@@ -36,10 +36,10 @@ Requires-Dist: ansys-sphinx-theme[autoapi]==1.7.2; extra == "doc"
|
|
|
36
36
|
Requires-Dist: numpydoc==1.10.0; extra == "doc"
|
|
37
37
|
Requires-Dist: sphinx==8.2.3; extra == "doc"
|
|
38
38
|
Requires-Dist: sphinx-autodoc-typehints==3.1.0; extra == "doc"
|
|
39
|
-
Requires-Dist: sphinx-copybutton==0.5.
|
|
39
|
+
Requires-Dist: sphinx-copybutton==0.5.2; extra == "doc"
|
|
40
40
|
Provides-Extra: tests
|
|
41
|
-
Requires-Dist: pytest==9.0.
|
|
42
|
-
Requires-Dist: pytest-cov==7.
|
|
41
|
+
Requires-Dist: pytest==9.0.3; extra == "tests"
|
|
42
|
+
Requires-Dist: pytest-cov==7.1.0; extra == "tests"
|
|
43
43
|
Dynamic: author
|
|
44
44
|
Dynamic: classifier
|
|
45
45
|
Dynamic: home-page
|
|
@@ -9,6 +9,7 @@ src/ansys/pre_commit_hooks/add_license_headers.py
|
|
|
9
9
|
src/ansys/pre_commit_hooks/tech_review.py
|
|
10
10
|
src/ansys/pre_commit_hooks/assets/licenses.json
|
|
11
11
|
src/ansys/pre_commit_hooks/assets/.reuse/templates/ansys.jinja2
|
|
12
|
+
src/ansys/pre_commit_hooks/assets/LICENSES/Apache-2.0.txt
|
|
12
13
|
src/ansys/pre_commit_hooks/assets/LICENSES/MIT.txt
|
|
13
14
|
src/ansys/pre_commit_hooks/templates/AUTHORS
|
|
14
15
|
src/ansys/pre_commit_hooks/templates/CODE_OF_CONDUCT.md
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
GitPython==3.1.46
|
|
2
|
-
importlib-metadata==
|
|
2
|
+
importlib-metadata==9.0.0
|
|
3
3
|
Jinja2==3.1.6
|
|
4
4
|
reuse==6.2.0
|
|
5
|
-
requests==2.
|
|
5
|
+
requests==2.33.1
|
|
6
6
|
semver==3.0.4
|
|
7
7
|
toml==0.10.2
|
|
8
8
|
|
|
@@ -11,8 +11,8 @@ ansys-sphinx-theme[autoapi]==1.7.2
|
|
|
11
11
|
numpydoc==1.10.0
|
|
12
12
|
sphinx==8.2.3
|
|
13
13
|
sphinx-autodoc-typehints==3.1.0
|
|
14
|
-
sphinx-copybutton==0.5.
|
|
14
|
+
sphinx-copybutton==0.5.2
|
|
15
15
|
|
|
16
16
|
[tests]
|
|
17
|
-
pytest==9.0.
|
|
18
|
-
pytest-cov==7.
|
|
17
|
+
pytest==9.0.3
|
|
18
|
+
pytest-cov==7.1.0
|
{ansys_pre_commit_hooks-0.6.0 → ansys_pre_commit_hooks-0.7.1}/tests/test_add_license_headers.py
RENAMED
|
@@ -22,6 +22,7 @@
|
|
|
22
22
|
|
|
23
23
|
import argparse
|
|
24
24
|
from datetime import date as dt
|
|
25
|
+
import io
|
|
25
26
|
import os
|
|
26
27
|
from pathlib import Path
|
|
27
28
|
import shutil
|
|
@@ -496,6 +497,108 @@ def test_copy_assets(tmp_path: pytest.TempPathFactory):
|
|
|
496
497
|
check_ansys_header(tmp_file)
|
|
497
498
|
|
|
498
499
|
|
|
500
|
+
@pytest.mark.add_license_headers
|
|
501
|
+
def test_mkdirs_and_link_creates_symlink(tmp_path):
|
|
502
|
+
"""Test that mkdirs_and_link creates a symlink when permissions allow."""
|
|
503
|
+
src_dir = tmp_path / "hook_assets"
|
|
504
|
+
src_dir.mkdir()
|
|
505
|
+
src_file = src_dir / "ansys.jinja2"
|
|
506
|
+
src_file.write_text("template content")
|
|
507
|
+
|
|
508
|
+
dest_dir = tmp_path / "repo" / ".reuse" / "templates"
|
|
509
|
+
asset_dir = str(dest_dir)
|
|
510
|
+
|
|
511
|
+
hook.mkdirs_and_link(asset_dir, str(src_dir), str(dest_dir), "ansys.jinja2")
|
|
512
|
+
|
|
513
|
+
dest_file = dest_dir / "ansys.jinja2"
|
|
514
|
+
assert dest_file.exists()
|
|
515
|
+
assert dest_file.read_text() == "template content"
|
|
516
|
+
|
|
517
|
+
|
|
518
|
+
@pytest.mark.add_license_headers
|
|
519
|
+
def test_mkdirs_and_link_fallback_copy_on_oserror(tmp_path, monkeypatch):
|
|
520
|
+
"""Test that mkdirs_and_link falls back to copying when symlink raises OSError."""
|
|
521
|
+
src_dir = tmp_path / "hook_assets"
|
|
522
|
+
src_dir.mkdir()
|
|
523
|
+
src_file = src_dir / "ansys.jinja2"
|
|
524
|
+
src_file.write_text("template content")
|
|
525
|
+
|
|
526
|
+
dest_dir = tmp_path / "repo" / ".reuse" / "templates"
|
|
527
|
+
asset_dir = str(dest_dir)
|
|
528
|
+
|
|
529
|
+
# Simulate Windows symlink privilege error
|
|
530
|
+
monkeypatch.setattr(
|
|
531
|
+
Path,
|
|
532
|
+
"symlink_to",
|
|
533
|
+
lambda *_args, **_kwargs: (_ for _ in ()).throw(
|
|
534
|
+
OSError(1314, "A required privilege is not held by the client")
|
|
535
|
+
),
|
|
536
|
+
)
|
|
537
|
+
|
|
538
|
+
hook.mkdirs_and_link(asset_dir, str(src_dir), str(dest_dir), "ansys.jinja2")
|
|
539
|
+
|
|
540
|
+
dest_file = dest_dir / "ansys.jinja2"
|
|
541
|
+
assert dest_file.exists()
|
|
542
|
+
assert not dest_file.is_symlink()
|
|
543
|
+
assert dest_file.read_text() == "template content"
|
|
544
|
+
|
|
545
|
+
|
|
546
|
+
@pytest.mark.add_license_headers
|
|
547
|
+
def test_mkdirs_and_link_skips_existing_symlink(tmp_path):
|
|
548
|
+
"""Test that mkdirs_and_link skips creation when symlink points to the correct source."""
|
|
549
|
+
src_dir = tmp_path / "hook_assets"
|
|
550
|
+
src_dir.mkdir()
|
|
551
|
+
src_file = src_dir / "ansys.jinja2"
|
|
552
|
+
src_file.write_text("template content")
|
|
553
|
+
|
|
554
|
+
dest_dir = tmp_path / "repo" / ".reuse" / "templates"
|
|
555
|
+
dest_dir.mkdir(parents=True)
|
|
556
|
+
dest_file = dest_dir / "ansys.jinja2"
|
|
557
|
+
|
|
558
|
+
try:
|
|
559
|
+
dest_file.symlink_to(src_file)
|
|
560
|
+
except OSError:
|
|
561
|
+
pytest.skip("Symlinks not supported on this platform/environment")
|
|
562
|
+
|
|
563
|
+
mtime_before = src_file.stat().st_mtime
|
|
564
|
+
|
|
565
|
+
hook.mkdirs_and_link(str(dest_dir), str(src_dir), str(dest_dir), "ansys.jinja2")
|
|
566
|
+
|
|
567
|
+
# File should still exist and be unchanged
|
|
568
|
+
assert dest_file.is_symlink()
|
|
569
|
+
assert dest_file.read_text() == "template content"
|
|
570
|
+
assert src_file.stat().st_mtime == mtime_before
|
|
571
|
+
|
|
572
|
+
|
|
573
|
+
@pytest.mark.add_license_headers
|
|
574
|
+
def test_add_header_skips_unknown_comment_style(tmp_path, monkeypatch):
|
|
575
|
+
"""Test add_header skips files when no comment style can be inferred."""
|
|
576
|
+
test_file = tmp_path / "unknown.ext"
|
|
577
|
+
test_file.write_text("content\n", encoding="utf-8")
|
|
578
|
+
|
|
579
|
+
called = {"add_header_to_file": False}
|
|
580
|
+
|
|
581
|
+
def _fake_add_header_to_file(**_kwargs):
|
|
582
|
+
called["add_header_to_file"] = True
|
|
583
|
+
|
|
584
|
+
monkeypatch.setattr("reuse.cli.annotate.get_comment_style", lambda _path: None)
|
|
585
|
+
monkeypatch.setattr("reuse.cli.annotate.add_header_to_file", _fake_add_header_to_file)
|
|
586
|
+
|
|
587
|
+
out = io.StringIO()
|
|
588
|
+
hook.add_header(
|
|
589
|
+
[DEFAULT_COPYRIGHT],
|
|
590
|
+
["MIT"],
|
|
591
|
+
f"{dt.today().year}",
|
|
592
|
+
str(test_file),
|
|
593
|
+
"ansys.jinja2",
|
|
594
|
+
True,
|
|
595
|
+
out,
|
|
596
|
+
)
|
|
597
|
+
|
|
598
|
+
assert not called["add_header_to_file"]
|
|
599
|
+
assert "Skipping" in out.getvalue()
|
|
600
|
+
|
|
601
|
+
|
|
499
602
|
@pytest.mark.add_license_headers
|
|
500
603
|
def test_bad_chars(tmp_path: pytest.TempPathFactory):
|
|
501
604
|
# Set template and license names
|
|
@@ -761,6 +864,133 @@ def get_line_endings(tmp_file):
|
|
|
761
864
|
return "Mac"
|
|
762
865
|
|
|
763
866
|
|
|
867
|
+
def check_apache_header(file_name):
|
|
868
|
+
"""Check file contains all Apache-2.0 copyright and license header components."""
|
|
869
|
+
with open(file_name, "r", encoding="utf8") as file:
|
|
870
|
+
content = file.read()
|
|
871
|
+
assert "ANSYS, Inc. and/or its affiliates" in content
|
|
872
|
+
assert "Apache-2.0" in content
|
|
873
|
+
assert "Apache License, Version 2.0" in content
|
|
874
|
+
assert "http://www.apache.org/licenses/LICENSE-2.0" in content
|
|
875
|
+
# Confirm there is no MIT text in the header
|
|
876
|
+
assert "Permission is hereby granted" not in content
|
|
877
|
+
|
|
878
|
+
|
|
879
|
+
@pytest.mark.add_license_headers
|
|
880
|
+
def test_apache_header_added(tmp_path: pytest.TempPathFactory):
|
|
881
|
+
"""Test that a file with no header gets an Apache-2.0 header when it's the custom license."""
|
|
882
|
+
# Set template and license names (use default ansys template; Apache-2.0 is in assets)
|
|
883
|
+
template_name = "ansys.jinja2"
|
|
884
|
+
license_name = "Apache-2.0.txt"
|
|
885
|
+
template_path = Path(REPO_PATH) / ".reuse" / "templates" / template_name
|
|
886
|
+
license_path = (
|
|
887
|
+
Path(REPO_PATH)
|
|
888
|
+
/ "src"
|
|
889
|
+
/ "ansys"
|
|
890
|
+
/ "pre_commit_hooks"
|
|
891
|
+
/ "assets"
|
|
892
|
+
/ "LICENSES"
|
|
893
|
+
/ license_name
|
|
894
|
+
)
|
|
895
|
+
|
|
896
|
+
# Set up git repository in tmp_path with temporary file
|
|
897
|
+
repo, tmp_file = set_up_repo(tmp_path, template_path, template_name, license_path, license_name)
|
|
898
|
+
custom_args = [tmp_file, "--custom_license=Apache-2.0"]
|
|
899
|
+
|
|
900
|
+
# Assert the hook fails because it added the header
|
|
901
|
+
assert add_argv_run(repo, tmp_file, custom_args) == 1
|
|
902
|
+
|
|
903
|
+
check_apache_header(tmp_file)
|
|
904
|
+
|
|
905
|
+
os.chdir(REPO_PATH)
|
|
906
|
+
|
|
907
|
+
|
|
908
|
+
@pytest.mark.add_license_headers
|
|
909
|
+
def test_mit_header_replaced_with_apache(tmp_path: pytest.TempPathFactory):
|
|
910
|
+
"""Test that a file with an MIT header is fully replaced with Apache-2.0 when requested."""
|
|
911
|
+
# Set template and license names
|
|
912
|
+
template_name = "ansys.jinja2"
|
|
913
|
+
license_name = "Apache-2.0.txt"
|
|
914
|
+
template_path = Path(REPO_PATH) / ".reuse" / "templates" / template_name
|
|
915
|
+
license_path = (
|
|
916
|
+
Path(REPO_PATH)
|
|
917
|
+
/ "src"
|
|
918
|
+
/ "ansys"
|
|
919
|
+
/ "pre_commit_hooks"
|
|
920
|
+
/ "assets"
|
|
921
|
+
/ "LICENSES"
|
|
922
|
+
/ license_name
|
|
923
|
+
)
|
|
924
|
+
|
|
925
|
+
# Set up git repository in tmp_path with temporary file
|
|
926
|
+
repo, tmp_file = set_up_repo(tmp_path, template_path, template_name, license_path, license_name)
|
|
927
|
+
|
|
928
|
+
# First, add an MIT header to the file
|
|
929
|
+
mit_args = [tmp_file]
|
|
930
|
+
add_argv_run(repo, tmp_file, mit_args)
|
|
931
|
+
repo.index.add([tmp_file])
|
|
932
|
+
|
|
933
|
+
# Verify the MIT header was added
|
|
934
|
+
with open(tmp_file, "r") as f:
|
|
935
|
+
content = f.read()
|
|
936
|
+
assert "MIT" in content
|
|
937
|
+
assert "Permission is hereby granted" in content
|
|
938
|
+
|
|
939
|
+
# Now re-run with Apache-2.0 — the MIT header should be fully replaced
|
|
940
|
+
apache_args = [tmp_file, "--custom_license=Apache-2.0"]
|
|
941
|
+
assert add_argv_run(repo, tmp_file, apache_args) == 1
|
|
942
|
+
|
|
943
|
+
check_apache_header(tmp_file)
|
|
944
|
+
|
|
945
|
+
# Ensure the old MIT license text is gone
|
|
946
|
+
with open(tmp_file, "r") as f:
|
|
947
|
+
new_content = f.read()
|
|
948
|
+
assert "Permission is hereby granted" not in new_content
|
|
949
|
+
|
|
950
|
+
os.chdir(REPO_PATH)
|
|
951
|
+
|
|
952
|
+
|
|
953
|
+
@pytest.mark.add_license_headers
|
|
954
|
+
def test_mit_license_file_replaced_with_apache(tmp_path: pytest.TempPathFactory):
|
|
955
|
+
"""Test that LICENSE file is regenerated as Apache-2.0 when it's the custom license."""
|
|
956
|
+
template_name = "ansys.jinja2"
|
|
957
|
+
license_name = "Apache-2.0.txt"
|
|
958
|
+
template_path = Path(REPO_PATH) / ".reuse" / "templates" / template_name
|
|
959
|
+
license_path = (
|
|
960
|
+
Path(REPO_PATH)
|
|
961
|
+
/ "src"
|
|
962
|
+
/ "ansys"
|
|
963
|
+
/ "pre_commit_hooks"
|
|
964
|
+
/ "assets"
|
|
965
|
+
/ "LICENSES"
|
|
966
|
+
/ license_name
|
|
967
|
+
)
|
|
968
|
+
|
|
969
|
+
# Set up git repository in tmp_path with temporary file (this copies a MIT LICENSE file)
|
|
970
|
+
repo, tmp_file = set_up_repo(tmp_path, template_path, template_name, license_path, license_name)
|
|
971
|
+
tmp_license = Path(tmp_path) / "LICENSE"
|
|
972
|
+
|
|
973
|
+
# Confirm the starting LICENSE file is MIT
|
|
974
|
+
with open(tmp_license, "r") as f:
|
|
975
|
+
starting_content = f.read()
|
|
976
|
+
assert "Permission is hereby granted" in starting_content
|
|
977
|
+
|
|
978
|
+
custom_args = [tmp_file, "--custom_license=Apache-2.0"]
|
|
979
|
+
add_argv_run(repo, tmp_file, custom_args)
|
|
980
|
+
|
|
981
|
+
# Confirm the LICENSE file is now Apache-2.0
|
|
982
|
+
with open(tmp_license, "r") as f:
|
|
983
|
+
new_content = f.read()
|
|
984
|
+
|
|
985
|
+
assert "Apache License" in new_content
|
|
986
|
+
assert "Version 2.0" in new_content
|
|
987
|
+
assert "http://www.apache.org/licenses/LICENSE-2.0" in new_content
|
|
988
|
+
# MIT text must be gone
|
|
989
|
+
assert "Permission is hereby granted" not in new_content
|
|
990
|
+
|
|
991
|
+
os.chdir(REPO_PATH)
|
|
992
|
+
|
|
993
|
+
|
|
764
994
|
@pytest.mark.add_license_headers
|
|
765
995
|
def test_line_endings(tmp_path: pytest.TempPathFactory):
|
|
766
996
|
"""Test line endings remain the same before and after running the hook."""
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
0.6.0
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{ansys_pre_commit_hooks-0.6.0 → ansys_pre_commit_hooks-0.7.1}/src/ansys/pre_commit_hooks/__init__.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|