ansys-pre-commit-hooks 0.2.4__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.4
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.4"
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",
@@ -28,6 +28,7 @@ A license header consists of the Ansys copyright statement and licensing informa
28
28
  import argparse
29
29
  from datetime import date as dt
30
30
  import filecmp
31
+ import fileinput
31
32
  import json
32
33
  import os
33
34
  import pathlib
@@ -36,7 +37,7 @@ import sys
36
37
  from tempfile import NamedTemporaryFile
37
38
 
38
39
  import git
39
- from reuse import header, lint, project
40
+ from reuse import _util, header, lint, project
40
41
 
41
42
  DEFAULT_TEMPLATE = "ansys"
42
43
  """Default template to use for license headers."""
@@ -44,6 +45,7 @@ DEFAULT_COPYRIGHT = "ANSYS, Inc. and/or its affiliates."
44
45
  """Default copyright line for license headers."""
45
46
  DEFAULT_LICENSE = "MIT"
46
47
  """Default license for headers."""
48
+ DEFAULT_START_YEAR = dt.today().year
47
49
 
48
50
 
49
51
  def set_lint_args(parser: argparse.ArgumentParser) -> argparse.Namespace:
@@ -83,6 +85,13 @@ def set_lint_args(parser: argparse.ArgumentParser) -> argparse.Namespace:
83
85
  help="Default license for headers.",
84
86
  default=DEFAULT_LICENSE,
85
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
+ )
86
95
  # Ignore license check by default is False when action='store_true'
87
96
  parser.add_argument("--ignore_license_check", action="store_true")
88
97
  parser.add_argument("--parser")
@@ -203,7 +212,12 @@ def list_noncompliant_files(args: argparse.Namespace, proj: project.Project) ->
203
212
 
204
213
 
205
214
  def set_header_args(
206
- 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,
207
221
  ) -> argparse.Namespace:
208
222
  """
209
223
  Set arguments for `REUSE <https://reuse.software/>`_.
@@ -228,7 +242,10 @@ def set_header_args(
228
242
  """
229
243
  # Provide values for license header arguments
230
244
  args = parser.parse_args([file_path])
231
- 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)]
232
249
  args.copyright_style = "string-c"
233
250
  args.copyright = [copyright]
234
251
  args.merge_copyrights = True
@@ -273,7 +290,8 @@ def check_exists(
273
290
  ``1`` if ``REUSE`` changed all noncompliant files.
274
291
  """
275
292
  files = values["files"]
276
- year = values["year"]
293
+ start_year = values["start_year"]
294
+ current_year = values["current_year"]
277
295
  copyright = values["copyright"]
278
296
  template = values["template"]
279
297
 
@@ -282,7 +300,7 @@ def check_exists(
282
300
  if files[i] in missing_headers:
283
301
  changed_headers = 1
284
302
  # Run REUSE on the file
285
- args = set_header_args(parser, year, files[i], copyright, template)
303
+ args = set_header_args(parser, start_year, current_year, files[i], copyright, template)
286
304
  if not args.ignore_license_check:
287
305
  args.license = [values["license"]]
288
306
  header.run(args, proj)
@@ -297,7 +315,9 @@ def check_exists(
297
315
  # Update the header
298
316
  # tmp captures the stdout of the header.run() function
299
317
  with NamedTemporaryFile(mode="w", delete=True) as tmp:
300
- 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
+ )
301
321
  header.run(args, proj, tmp)
302
322
 
303
323
  # Check if the file before add-license-headers was run is the same as the one
@@ -313,6 +333,8 @@ def check_exists(
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
@@ -355,48 +377,41 @@ def add_hook_changes(before_hook: str, after_hook: str) -> None:
355
377
  after_hook: str
356
378
  Path to file after add-license-headers was run.
357
379
  """
358
- # Compare each line of the file before and after add-license-headers was run,
359
- # and combine the files into one
360
- with open(before_hook, "r") as before_edit, open(
361
- after_hook, "r"
362
- ) as after_edit, NamedTemporaryFile(mode="w", delete=False) as tmp:
363
- for before_edit_line, after_edit_line2 in zip(before_edit, after_edit):
364
- # If the lines are different,
365
- if before_edit_line != after_edit_line2:
366
- # If the after_edit_line is empty, and the before_edit_line is the
367
- # same as the next after_edit_line, remove the space by writing the
368
- # before_edit_line. For example:
369
- #
370
- # before_edit file after_edit file
371
- # line 1: """ line 1:
372
- # line 2: print("example") line 2: """
373
- # We want to remove the blank space, so we take line 1 from the before_edit file.
374
- if not after_edit_line2.strip() and (before_edit_line == next(after_edit)):
375
- tmp.write(before_edit_line)
376
- # If the before_edit_line is empty, write that line and the next line in
377
- # the before_edit file. For example:
378
- #
379
- # before_edit file after_edit file
380
- # line 1: line 1: print("example")
381
- # line 2: print("example") line 2: print("example 2")
382
- # We want to keep the blank space, so we take lines 1 & 2 from the before_edit file.
383
- elif not before_edit_line.strip():
384
- tmp.write(f"{before_edit_line}{next(before_edit)}")
385
- # Otherwise, write the line from the after_edit file
386
- else:
387
- tmp.write(after_edit_line2)
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())
409
+ break
410
+ # Copy the header lines before reuse information is found
388
411
  else:
389
- tmp.write(before_edit_line)
390
-
391
- # Close the tmp file after writing to it
392
- tmp.close()
393
-
394
- # Copy the file with combined changes to the file that was
395
- # edited by the add-license-headers hook
396
- shutil.copyfile(tmp.name, after_hook)
397
-
398
- # Delete temporary file
399
- os.remove(tmp.name)
412
+ count += 1
413
+ print(line.rstrip())
414
+ fileinput.close()
400
415
 
401
416
 
402
417
  def get_full_paths(file_list: list) -> list:
@@ -468,6 +483,19 @@ def find_files_missing_header() -> int:
468
483
  # Set changed_headers to zero by default
469
484
  changed_headers = 0
470
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
+
471
499
  # Create dictionary containing the committed files, custom copyright,
472
500
  # template, license, changed_headers, year, and git_repo
473
501
  values = {
@@ -475,7 +503,8 @@ def find_files_missing_header() -> int:
475
503
  "copyright": args.custom_copyright,
476
504
  "template": args.custom_template,
477
505
  "license": args.custom_license,
478
- "year": dt.today().year,
506
+ "start_year": args.start_year,
507
+ "current_year": dt.today().year,
479
508
  "git_repo": git_repo,
480
509
  }
481
510