ansys-pre-commit-hooks 0.2.3__tar.gz → 0.2.5__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: ansys-pre-commit-hooks
3
- Version: 0.2.3
3
+ Version: 0.2.5
4
4
  Summary: A Python wrapper to create Ansys-tailored pre-commit hooks
5
5
  Author-email: "ANSYS, Inc." <pyansys.core@ansys.com>
6
6
  Maintainer-email: "ANSYS, Inc." <pyansys.core@ansys.com>
@@ -16,7 +16,7 @@ Classifier: Operating System :: OS Independent
16
16
  Requires-Dist: importlib-metadata >=4.0
17
17
  Requires-Dist: reuse>=2
18
18
  Requires-Dist: GitPython>=3
19
- Requires-Dist: ansys-sphinx-theme==0.12.5 ; extra == "doc"
19
+ Requires-Dist: ansys-sphinx-theme==0.13.0 ; extra == "doc"
20
20
  Requires-Dist: numpydoc==1.6.0 ; extra == "doc"
21
21
  Requires-Dist: sphinx==7.2.6 ; extra == "doc"
22
22
  Requires-Dist: sphinx-autoapi==3.0.0 ; extra == "doc"
@@ -119,10 +119,10 @@ Set custom arguments
119
119
  .. code:: yaml
120
120
 
121
121
  - repo: https://github.com/ansys/pre-commit-hooks
122
- rev: v0.2.0
122
+ rev: v0.2.5
123
123
  hooks:
124
124
  - id: add-license-headers
125
- args: ["--custom_copyright", "custom copyright phrase", "--custom_template", "template_name", "--custom_license", "license_name", "--ignore_license_check"]
125
+ args: ["--custom_copyright", "custom copyright phrase", "--custom_template", "template_name", "--custom_license", "license_name", "--ignore_license_check", "--start_year", "2023"]
126
126
 
127
127
  ``args`` can also be formatted as follows:
128
128
 
@@ -133,6 +133,7 @@ Set custom arguments
133
133
  - --custom_template=template_name
134
134
  - --custom_license=license_name
135
135
  - --ignore_license_check
136
+ - --start_year=2023
136
137
 
137
138
  * ``custom copyright phrase`` is the copyright line you want to include in the license
138
139
  header. By default, it uses ``"ANSYS, Inc. and/or its affiliates."``.
@@ -145,6 +146,12 @@ Set custom arguments
145
146
  it is ``False``, meaning the files are checked for both the copyright and licensing information
146
147
  in the header. Add ``--ignore_license_check`` to ignore checking for licensing information
147
148
  in the files.
149
+ * ``start_year`` is the start year of the copyright statement. By default, the ``start_year`` is
150
+ the current year, making the copyright statement
151
+ "Copyright (C) 2024 ANSYS, Inc. and/or its affiliates." If you are adding license headers
152
+ to packages released before the current year, add the ``start_year`` argument with the year your
153
+ package was released. For example, if ``start_year`` is 2023, the copyright statement would be
154
+ "Copyright (C) 2023 - 2024 ANSYS, Inc. and/or its affiliates." assuming the current year is 2024.
148
155
 
149
156
  Specify directories to run the hook on
150
157
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -157,7 +164,7 @@ the hook should run on, add the necessary regex to the ``files`` line in your
157
164
  .. code:: yaml
158
165
 
159
166
  - repo: https://github.com/ansys/pre-commit-hooks
160
- rev: v0.2.0
167
+ rev: v0.2.5
161
168
  hooks:
162
169
  - id: add-license-headers
163
170
  files: '(src|examples|tests|newFolder)/.*\.(py|newExtension)|\.(proto|newExtension)'
@@ -170,7 +177,7 @@ In .pre-commit-config.yaml:
170
177
  .. code:: yaml
171
178
 
172
179
  - repo: https://github.com/ansys/pre-commit-hooks
173
- rev: v0.2.0
180
+ rev: v0.2.5
174
181
  hooks:
175
182
  - id: add-license-headers
176
183
  exclude: |
@@ -84,10 +84,10 @@ Set custom arguments
84
84
  .. code:: yaml
85
85
 
86
86
  - repo: https://github.com/ansys/pre-commit-hooks
87
- rev: v0.2.0
87
+ rev: v0.2.5
88
88
  hooks:
89
89
  - id: add-license-headers
90
- args: ["--custom_copyright", "custom copyright phrase", "--custom_template", "template_name", "--custom_license", "license_name", "--ignore_license_check"]
90
+ args: ["--custom_copyright", "custom copyright phrase", "--custom_template", "template_name", "--custom_license", "license_name", "--ignore_license_check", "--start_year", "2023"]
91
91
 
92
92
  ``args`` can also be formatted as follows:
93
93
 
@@ -98,6 +98,7 @@ Set custom arguments
98
98
  - --custom_template=template_name
99
99
  - --custom_license=license_name
100
100
  - --ignore_license_check
101
+ - --start_year=2023
101
102
 
102
103
  * ``custom copyright phrase`` is the copyright line you want to include in the license
103
104
  header. By default, it uses ``"ANSYS, Inc. and/or its affiliates."``.
@@ -110,6 +111,12 @@ Set custom arguments
110
111
  it is ``False``, meaning the files are checked for both the copyright and licensing information
111
112
  in the header. Add ``--ignore_license_check`` to ignore checking for licensing information
112
113
  in the files.
114
+ * ``start_year`` is the start year of the copyright statement. By default, the ``start_year`` is
115
+ the current year, making the copyright statement
116
+ "Copyright (C) 2024 ANSYS, Inc. and/or its affiliates." If you are adding license headers
117
+ to packages released before the current year, add the ``start_year`` argument with the year your
118
+ package was released. For example, if ``start_year`` is 2023, the copyright statement would be
119
+ "Copyright (C) 2023 - 2024 ANSYS, Inc. and/or its affiliates." assuming the current year is 2024.
113
120
 
114
121
  Specify directories to run the hook on
115
122
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -122,7 +129,7 @@ the hook should run on, add the necessary regex to the ``files`` line in your
122
129
  .. code:: yaml
123
130
 
124
131
  - repo: https://github.com/ansys/pre-commit-hooks
125
- rev: v0.2.0
132
+ rev: v0.2.5
126
133
  hooks:
127
134
  - id: add-license-headers
128
135
  files: '(src|examples|tests|newFolder)/.*\.(py|newExtension)|\.(proto|newExtension)'
@@ -135,7 +142,7 @@ In .pre-commit-config.yaml:
135
142
  .. code:: yaml
136
143
 
137
144
  - repo: https://github.com/ansys/pre-commit-hooks
138
- rev: v0.2.0
145
+ rev: v0.2.5
139
146
  hooks:
140
147
  - id: add-license-headers
141
148
  exclude: |
@@ -5,7 +5,7 @@ build-backend = "flit_core.buildapi"
5
5
  [project]
6
6
  # Check https://flit.readthedocs.io/en/latest/pyproject_toml.html for all available sections
7
7
  name = "ansys-pre-commit-hooks"
8
- version = "0.2.3"
8
+ version = "0.2.5"
9
9
  description = "A Python wrapper to create Ansys-tailored pre-commit hooks"
10
10
  readme = "README.rst"
11
11
  requires-python = ">=3.9,<4"
@@ -40,7 +40,7 @@ tests = [
40
40
  "GitPython==3.1.40",
41
41
  ]
42
42
  doc = [
43
- "ansys-sphinx-theme==0.12.5",
43
+ "ansys-sphinx-theme==0.13.0",
44
44
  "numpydoc==1.6.0",
45
45
  "sphinx==7.2.6",
46
46
  "sphinx-autoapi==3.0.0",
@@ -27,8 +27,8 @@ A license header consists of the Ansys copyright statement and licensing informa
27
27
  """
28
28
  import argparse
29
29
  from datetime import date as dt
30
- import difflib
31
30
  import filecmp
31
+ import fileinput
32
32
  import json
33
33
  import os
34
34
  import pathlib
@@ -37,7 +37,7 @@ import sys
37
37
  from tempfile import NamedTemporaryFile
38
38
 
39
39
  import git
40
- from reuse import header, lint, project
40
+ from reuse import _util, header, lint, project
41
41
 
42
42
  DEFAULT_TEMPLATE = "ansys"
43
43
  """Default template to use for license headers."""
@@ -45,6 +45,7 @@ DEFAULT_COPYRIGHT = "ANSYS, Inc. and/or its affiliates."
45
45
  """Default copyright line for license headers."""
46
46
  DEFAULT_LICENSE = "MIT"
47
47
  """Default license for headers."""
48
+ DEFAULT_START_YEAR = dt.today().year
48
49
 
49
50
 
50
51
  def set_lint_args(parser: argparse.ArgumentParser) -> argparse.Namespace:
@@ -84,6 +85,13 @@ def set_lint_args(parser: argparse.ArgumentParser) -> argparse.Namespace:
84
85
  help="Default license for headers.",
85
86
  default=DEFAULT_LICENSE,
86
87
  )
88
+ # Get custom license
89
+ parser.add_argument(
90
+ "--start_year",
91
+ type=str,
92
+ help="Start year for copyright line in headers.",
93
+ default=DEFAULT_START_YEAR,
94
+ )
87
95
  # Ignore license check by default is False when action='store_true'
88
96
  parser.add_argument("--ignore_license_check", action="store_true")
89
97
  parser.add_argument("--parser")
@@ -204,7 +212,12 @@ def list_noncompliant_files(args: argparse.Namespace, proj: project.Project) ->
204
212
 
205
213
 
206
214
  def set_header_args(
207
- parser: argparse.ArgumentParser, year: int, file_path: str, copyright: str, template: str
215
+ parser: argparse.ArgumentParser,
216
+ start_year: str,
217
+ current_year: int,
218
+ file_path: str,
219
+ copyright: str,
220
+ template: str,
208
221
  ) -> argparse.Namespace:
209
222
  """
210
223
  Set arguments for `REUSE <https://reuse.software/>`_.
@@ -229,7 +242,10 @@ def set_header_args(
229
242
  """
230
243
  # Provide values for license header arguments
231
244
  args = parser.parse_args([file_path])
232
- args.year = [str(year)]
245
+ if start_year == current_year:
246
+ args.year = [str(current_year)]
247
+ else:
248
+ args.year = [start_year, str(current_year)]
233
249
  args.copyright_style = "string-c"
234
250
  args.copyright = [copyright]
235
251
  args.merge_copyrights = True
@@ -274,7 +290,8 @@ def check_exists(
274
290
  ``1`` if ``REUSE`` changed all noncompliant files.
275
291
  """
276
292
  files = values["files"]
277
- year = values["year"]
293
+ start_year = values["start_year"]
294
+ current_year = values["current_year"]
278
295
  copyright = values["copyright"]
279
296
  template = values["template"]
280
297
 
@@ -283,7 +300,7 @@ def check_exists(
283
300
  if files[i] in missing_headers:
284
301
  changed_headers = 1
285
302
  # Run REUSE on the file
286
- args = set_header_args(parser, year, files[i], copyright, template)
303
+ args = set_header_args(parser, start_year, current_year, files[i], copyright, template)
287
304
  if not args.ignore_license_check:
288
305
  args.license = [values["license"]]
289
306
  header.run(args, proj)
@@ -298,29 +315,34 @@ def check_exists(
298
315
  # Update the header
299
316
  # tmp captures the stdout of the header.run() function
300
317
  with NamedTemporaryFile(mode="w", delete=True) as tmp:
301
- args = set_header_args(parser, year, files[i], copyright, template)
318
+ args = set_header_args(
319
+ parser, start_year, current_year, files[i], copyright, template
320
+ )
302
321
  header.run(args, proj, tmp)
303
322
 
304
- # check diffs between files and do modifications afterwards
305
- add_hook_changes(before_hook, files[i])
306
-
307
- # Compare the tempfile with the updated file
308
- # same_content is True if the two files that are being compared are the same
309
- same_content = filecmp.cmp(before_hook, files[i], shallow=False)
323
+ # Check if the file before add-license-headers was run is the same as the one
324
+ # after add-license-headers was run. If not, apply the syntax changes
325
+ # from other hooks before add-license-headers was run to the file
326
+ if check_same_content(before_hook, files[i]) == False:
327
+ add_hook_changes(before_hook, files[i])
310
328
 
311
- # Print header was successfully changed if it was modified
312
- if same_content == False:
329
+ # Check if the file content before add-license-headers was run has been changed
330
+ # Assuming the syntax was fixed in the above if statement, this check is
331
+ # solely for the file's content
332
+ if check_same_content(before_hook, files[i]) == False:
313
333
  changed_headers = 1
314
334
  print(f"Successfully changed header of {files[i]}")
315
335
 
336
+ os.remove(before_hook)
337
+
316
338
  return check_exists(changed_headers, parser, values, proj, missing_headers, i + 1)
317
339
 
318
340
  return changed_headers
319
341
 
320
342
 
321
- def add_hook_changes(before_hook: str, after_hook: str) -> None:
343
+ def check_same_content(before_hook, after_hook):
322
344
  """
323
- Add earlier hook changes to updated file with header.
345
+ Check if file before the hook ran is the same as after the hook ran.
324
346
 
325
347
  Parameters
326
348
  ----------
@@ -328,41 +350,68 @@ def add_hook_changes(before_hook: str, after_hook: str) -> None:
328
350
  Path to file before add-license-headers was run.
329
351
  after_hook: str
330
352
  Path to file after add-license-headers was run.
353
+
354
+ Returns
355
+ -------
356
+ bool
357
+ ``True`` if the files have the same content.
358
+ ``False`` if the files have different content.
331
359
  """
332
- # Open files
333
- before_file = open(before_hook, "r")
334
- after_file = open(after_hook, "r")
360
+ # Check if the files have the same content
361
+ same_files = filecmp.cmp(before_hook, after_hook, shallow=False)
362
+ # If the files are different, return False. Otherwise, return True
363
+ if same_files == False:
364
+ return False
365
+ else:
366
+ return True
335
367
 
336
- # Compare the files before and after the hook ran
337
- diff = difflib.ndiff(after_file.readlines(), before_file.readlines())
338
368
 
339
- before_file.close()
340
- after_file.close()
369
+ def add_hook_changes(before_hook: str, after_hook: str) -> None:
370
+ """
371
+ Add earlier hook changes to updated file with header.
341
372
 
342
- # Combine the changes from before the hook with the changes after the hook
343
- with NamedTemporaryFile(mode="w", delete=False) as tmp:
344
- while True:
345
- try:
346
- line = str(next(diff))
347
- if "+" in line[0] and line[2:] == "\n":
348
- tmp.write(line[2:])
349
- elif "-" in line[0]:
350
- tmp.write(line[2:])
351
- elif "?" in line[0]:
352
- continue
353
- elif " " in line[0]:
354
- tmp.write(line[2:])
355
- except StopIteration:
373
+ Parameters
374
+ ----------
375
+ before_hook: str
376
+ Path to file before add-license-headers was run.
377
+ after_hook: str
378
+ Path to file after add-license-headers was run.
379
+ """
380
+ count = 0
381
+ before_hook_file = open(before_hook, "r", encoding="utf8")
382
+ before_hook_lines = before_hook_file.readlines()
383
+ found_reuse_info = False
384
+
385
+ # Copy file content before add-license-header was run into
386
+ # the file after add-license-header was run.
387
+ # stdout is redirected into the file if inplace is True
388
+ for line in fileinput.input(after_hook, inplace=True, encoding="utf8"):
389
+ # Copy the new reuse lines into the file
390
+ if _util.contains_reuse_info(line):
391
+ count += 1
392
+ found_reuse_info = True
393
+ print(line.rstrip())
394
+ else:
395
+ if found_reuse_info:
396
+ try:
397
+ # Check the lines after the reuse info are the same
398
+ # If not, print the line after the reuse info
399
+ # This happens when a comment changes from one line to
400
+ # multiline
401
+ if line != before_hook_lines[count]:
402
+ print(line.rstrip())
403
+ except IndexError:
404
+ pass
405
+
406
+ # Copy the rest of the file after the reuse information
407
+ for line_after_reuse_info in before_hook_lines[count:]:
408
+ print(line_after_reuse_info.rstrip())
356
409
  break
357
-
358
- tmp.close()
359
-
360
- # Copy the file with combined changes to the file that was
361
- # edited by the add-license-headers hook
362
- shutil.copyfile(tmp.name, after_hook)
363
-
364
- # Delete temporary file
365
- os.remove(tmp.name)
410
+ # Copy the header lines before reuse information is found
411
+ else:
412
+ count += 1
413
+ print(line.rstrip())
414
+ fileinput.close()
366
415
 
367
416
 
368
417
  def get_full_paths(file_list: list) -> list:
@@ -434,6 +483,19 @@ def find_files_missing_header() -> int:
434
483
  # Set changed_headers to zero by default
435
484
  changed_headers = 0
436
485
 
486
+ # Check start_year is valid
487
+ try:
488
+ # Check the start year is not later than the current year
489
+ if int(args.start_year) > dt.today().year:
490
+ print("Please provide a start year less than or equal to the current year.")
491
+ exit(1)
492
+ # Check the start year isn't earlier than when computers were created :)
493
+ if int(args.start_year) < 1942:
494
+ print("Please provide a start year greater than or equal to 1942.")
495
+ exit(1)
496
+ except ValueError:
497
+ print("Please ensure the start year is a number.")
498
+
437
499
  # Create dictionary containing the committed files, custom copyright,
438
500
  # template, license, changed_headers, year, and git_repo
439
501
  values = {
@@ -441,7 +503,8 @@ def find_files_missing_header() -> int:
441
503
  "copyright": args.custom_copyright,
442
504
  "template": args.custom_template,
443
505
  "license": args.custom_license,
444
- "year": dt.today().year,
506
+ "start_year": args.start_year,
507
+ "current_year": dt.today().year,
445
508
  "git_repo": git_repo,
446
509
  }
447
510