ansys-pre-commit-hooks 0.1.0__py3-none-any.whl

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.
@@ -0,0 +1,30 @@
1
+ # Copyright (C) 2023 ANSYS, Inc. and/or its affiliates.
2
+ # SPDX-License-Identifier: MIT
3
+ #
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in all
13
+ # copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ # SOFTWARE.
22
+
23
+ """Module for initializing package-level imports for Ansys pre-commit hooks."""
24
+
25
+ try:
26
+ import importlib.metadata as importlib_metadata
27
+ except ModuleNotFoundError: # pragma: no cover
28
+ import importlib_metadata
29
+
30
+ __version__ = importlib_metadata.version(__name__.replace(".", "-").replace("_", "-"))
@@ -0,0 +1,217 @@
1
+ #!/usr/bin/env python
2
+ # Copyright (C) 2023 ANSYS, Inc. and/or its affiliates.
3
+ # SPDX-License-Identifier: MIT
4
+ #
5
+ #
6
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ # of this software and associated documentation files (the "Software"), to deal
8
+ # in the Software without restriction, including without limitation the rights
9
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ # copies of the Software, and to permit persons to whom the Software is
11
+ # furnished to do so, subject to the following conditions:
12
+ #
13
+ # The above copyright notice and this permission notice shall be included in all
14
+ # copies or substantial portions of the Software.
15
+ #
16
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ # SOFTWARE.
23
+
24
+ """
25
+ Module for running `REUSE <https://reuse.software/>`_ to add missing license headers to files.
26
+
27
+ A license header consists of the Ansys copyright statement and licensing information.
28
+ """
29
+ import argparse
30
+ from datetime import date as dt
31
+ import json
32
+ import os
33
+ from tempfile import NamedTemporaryFile
34
+
35
+ import git
36
+ from reuse import header, lint, project
37
+
38
+
39
+ def set_lint_args(parser):
40
+ """
41
+ Add lint arguments to the parser for `REUSE <https://reuse.software/>`_.
42
+
43
+ Parameters
44
+ ----------
45
+ parser: argparse.ArgumentParser
46
+ Parser without any lint arguments.
47
+
48
+ Returns
49
+ -------
50
+ argparse.Namespace
51
+ Parser namespace containing lint arguments.
52
+ """
53
+ parser.add_argument("--loc", type=str, help="Path to repository location", default="src")
54
+ parser.add_argument("--parser")
55
+ parser.add_argument("--no_multiprocessing", action="store_true")
56
+ lint.add_arguments(parser)
57
+
58
+ return parser.parse_args()
59
+
60
+
61
+ def list_noncompliant_files(args, proj):
62
+ """
63
+ Get a list of the files that are missing license headers.
64
+
65
+ Parameters
66
+ ----------
67
+ args: argparse.Namespace
68
+ Namespace of arguments with their values.
69
+ proj: project.Project
70
+ Project to run `REUSE <https://reuse.software/>`_ on.
71
+
72
+ Returns
73
+ -------
74
+ list
75
+ List of the files that are missing license headers.
76
+ """
77
+ # Create a temporary file containing lint.run json output
78
+ filename = None
79
+ with NamedTemporaryFile(mode="w", delete=False) as tmp:
80
+ args.json = True
81
+ lint.run(args, proj, tmp)
82
+ filename = tmp.name
83
+
84
+ # Open the temporary file, load the JSON file, and find files that
85
+ # are missing license headers.
86
+ lint_json = None
87
+ with open(filename, "rb") as file:
88
+ lint_json = json.load(file)
89
+
90
+ missing_headers = set(
91
+ lint_json["non_compliant"]["missing_copyright_info"]
92
+ + lint_json["non_compliant"]["missing_licensing_info"]
93
+ )
94
+
95
+ # Remove temporary file
96
+ os.remove(filename)
97
+
98
+ return missing_headers
99
+
100
+
101
+ def find_files_missing_header():
102
+ """
103
+ Find files that are missing license headers and run `REUSE <https://reuse.software/>`_ on them.
104
+
105
+ Returns
106
+ -------
107
+ int
108
+ ``1`` if ``REUSE`` changed all noncompliant files.
109
+
110
+ ``2`` if the ``.reuse`` directory does not exist in the root path of the
111
+ GitHub repository.
112
+ """
113
+ # Set up argparse for location, parser, and lint
114
+ # Lint contains four arguments: quiet, json, plain, and no_multiprocessing
115
+ parser = argparse.ArgumentParser()
116
+ args = set_lint_args(parser)
117
+ proj = project.Project(rf"{args.loc}")
118
+
119
+ missing_headers = list_noncompliant_files(args, proj)
120
+
121
+ # Get current year for license file
122
+ year = dt.today().year
123
+
124
+ # If there are files missing headers, run REUSE and return 1
125
+ if missing_headers:
126
+ # Returns 1 if REUSE changes all noncompliant files
127
+ # Returns 2 if .reuse directory does not exist in root of git repository
128
+ return run_reuse(parser, year, args.loc, missing_headers)
129
+
130
+ return 0
131
+
132
+
133
+ def check_reuse_dir():
134
+ """
135
+ Check if the ``.reuse`` directory exists in the root path of the git repository.
136
+
137
+ Returns
138
+ -------
139
+ str
140
+ Root path of the git repository.
141
+ int
142
+ ``1`` if the ``.reuse`` directory does not exist in the root path
143
+ of the git repository.
144
+ """
145
+ # Get root directory of current git repository
146
+ git_repo = git.Repo(os.getcwd(), search_parent_directories=True)
147
+ git_root = git_repo.git.rev_parse("--show-toplevel")
148
+
149
+ # If .reuse folder does not exist in root of git repository, return 1
150
+ if not os.path.isdir(os.path.join(git_root, ".reuse")):
151
+ print(f"Ensure that the .reuse directory is in {git_root}.")
152
+ return 1
153
+
154
+ return git_root
155
+
156
+
157
+ def run_reuse(parser, year, loc, missing_headers):
158
+ """
159
+ Run `REUSE <https://reuse.software/>`_ on files that are missing license headers.
160
+
161
+ Parameters
162
+ ----------
163
+ parser: argparse.ArgumentParser
164
+ Parser containing the previously set arguments.
165
+ year: int
166
+ Current year for the license header.
167
+ loc: str
168
+ Location to search for files that are missing license headers.
169
+ missing_headers: list
170
+ List of files that are missing license headers.
171
+
172
+ Returns
173
+ -------
174
+ int
175
+ ``1`` if the pre-commit hook fails.
176
+
177
+ ``2`` if the ``.reuse`` directory does not exist in the root path
178
+ of the GitHub repository.
179
+ """
180
+ # Add header arguments to parser. Arguments are: copyright, license, contributor,
181
+ # year, style, copyright-style, template, exclude-year, merge-copyrights, single-line,
182
+ # multi-line, explicit-license, force-dot-license, recursive, no-replace,
183
+ # skip-unrecognized, and skip-existing.
184
+ header.add_arguments(parser)
185
+
186
+ git_root = check_reuse_dir()
187
+ if git_root == 1:
188
+ # Previous check_reuse_dir() function returned error because .reuse
189
+ # directory does not exist... returning 2
190
+ return 2
191
+
192
+ # Add missing license header to each file in the list
193
+ for file in missing_headers:
194
+ args = parser.parse_args([rf"--loc={loc}", file])
195
+ args.year = [str(year)]
196
+ args.copyright_style = "string-c"
197
+ args.copyright = ["ANSYS, Inc. and/or its affiliates."]
198
+ args.merge_copyrights = True
199
+ args.template = "ansys"
200
+ args.license = ["MIT"]
201
+ args.skip_unrecognised = True
202
+ args.parser = parser
203
+
204
+ # Requires .reuse directory to be in git_root directory
205
+ proj = project.Project(git_root)
206
+ header.run(args, proj)
207
+
208
+ return 1
209
+
210
+
211
+ def main():
212
+ """Find files missing license headers and run `REUSE <https://reuse.software/>`_ on them."""
213
+ return find_files_missing_header()
214
+
215
+
216
+ if __name__ == "__main__":
217
+ raise SystemExit(main()) # pragma: no cover
@@ -0,0 +1,243 @@
1
+ Metadata-Version: 2.1
2
+ Name: ansys-pre-commit-hooks
3
+ Version: 0.1.0
4
+ Summary: A Python wrapper to create Ansys-tailored pre-commit hooks
5
+ Author-email: "ANSYS, Inc." <pyansys.core@ansys.com>
6
+ Maintainer-email: "ANSYS, Inc." <pyansys.core@ansys.com>
7
+ Requires-Python: >=3.8,<4
8
+ Description-Content-Type: text/x-rst
9
+ Classifier: Development Status :: 4 - Beta
10
+ Classifier: Programming Language :: Python :: 3.8
11
+ Classifier: Programming Language :: Python :: 3.9
12
+ Classifier: Programming Language :: Python :: 3.10
13
+ Classifier: Programming Language :: Python :: 3.11
14
+ Classifier: License :: OSI Approved :: MIT License
15
+ Classifier: Operating System :: OS Independent
16
+ Requires-Dist: importlib-metadata >=4.0
17
+ Requires-Dist: reuse>=2
18
+ Requires-Dist: GitPython>=3
19
+ Requires-Dist: ansys-sphinx-theme==0.10.4 ; extra == "doc"
20
+ Requires-Dist: numpydoc==1.5.0 ; extra == "doc"
21
+ Requires-Dist: sphinx==7.2.4 ; extra == "doc"
22
+ Requires-Dist: sphinx-autoapi==2.1.1 ; extra == "doc"
23
+ Requires-Dist: sphinx-autodoc-typehints==1.24.0 ; extra == "doc"
24
+ Requires-Dist: sphinx-copybutton==0.5.1 ; extra == "doc"
25
+ Requires-Dist: pytest==7.3.0 ; extra == "tests"
26
+ Requires-Dist: pytest-cov==4.0.0 ; extra == "tests"
27
+ Requires-Dist: reuse==2.1.0 ; extra == "tests"
28
+ Requires-Dist: GitPython==3.1.32 ; extra == "tests"
29
+ Project-URL: Documentation, https://pre-commit-hooks.docs.ansys.com
30
+ Project-URL: Homepage, https://github.com/ansys/pre-commit-hooks
31
+ Project-URL: Source, https://github.com/ansys/pre-commit-hooks
32
+ Project-URL: Tracker, https://github.com/ansys/pre-commit-hooks/issues
33
+ Provides-Extra: doc
34
+ Provides-Extra: tests
35
+
36
+ Ansys pre-commit hooks
37
+ ======================
38
+ |pyansys| |python| |pypi| |GH-CI| |MIT| |black|
39
+
40
+ .. |pyansys| image:: https://img.shields.io/badge/Py-Ansys-ffc107.svg?logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAABDklEQVQ4jWNgoDfg5mD8vE7q/3bpVyskbW0sMRUwofHD7Dh5OBkZGBgW7/3W2tZpa2tLQEOyOzeEsfumlK2tbVpaGj4N6jIs1lpsDAwMJ278sveMY2BgCA0NFRISwqkhyQ1q/Nyd3zg4OBgYGNjZ2ePi4rB5loGBhZnhxTLJ/9ulv26Q4uVk1NXV/f///////69du4Zdg78lx//t0v+3S88rFISInD59GqIH2esIJ8G9O2/XVwhjzpw5EAam1xkkBJn/bJX+v1365hxxuCAfH9+3b9/+////48cPuNehNsS7cDEzMTAwMMzb+Q2u4dOnT2vWrMHu9ZtzxP9vl/69RVpCkBlZ3N7enoDXBwEAAA+YYitOilMVAAAAAElFTkSuQmCC
41
+ :target: https://docs.pyansys.com/
42
+ :alt: PyAnsys
43
+
44
+ .. |python| image:: https://img.shields.io/pypi/pyversions/ansys-pre-commit-hooks?logo=pypi
45
+ :target: https://pypi.org/project/ansys-pre-commit-hooks/
46
+ :alt: Python
47
+
48
+ .. |pypi| image:: https://img.shields.io/pypi/v/ansys-pre-commit-hooks.svg?logo=python&logoColor=white
49
+ :target: https://pypi.org/project/ansys-pre-commit-hooks
50
+ :alt: PyPI
51
+
52
+ .. |GH-CI| image:: https://github.com/ansys/pre-commit-hooks/actions/workflows/ci_cd.yml/badge.svg
53
+ :target: https://github.com/ansys/pre-commit-hooks/actions/workflows/ci_cd.yml
54
+ :alt: GH-CI
55
+
56
+ .. |MIT| image:: https://img.shields.io/badge/License-MIT-yellow.svg
57
+ :target: https://opensource.org/licenses/MIT
58
+ :alt: MIT
59
+
60
+ .. |black| image:: https://img.shields.io/badge/code%20style-black-000000.svg?style=flat
61
+ :target: https://github.com/psf/black
62
+ :alt: Black
63
+
64
+
65
+ This Ansys repository contains `pre-commit`_ hooks for different purposes.
66
+ Currently, these hooks are available:
67
+
68
+ * ``add-license-headers``: Add missing license headers to files by using
69
+ `REUSE <https://reuse.software/>`_ . To use this hook, you must
70
+ have ``REUSE`` implemented in your repository.
71
+
72
+
73
+ How to install
74
+ --------------
75
+
76
+ The following sections provide instructions for installing the ``ansys-pre-commit-hooks``
77
+ package in two installation modes: user and developer.
78
+
79
+ For users
80
+ ^^^^^^^^^
81
+
82
+ Before installing the package, to ensure that you
83
+ have the latest version of `pip`_, run this command:
84
+
85
+ .. code:: bash
86
+
87
+ python -m pip install -U pip
88
+
89
+ Then, to install the package, run this command:
90
+
91
+ .. code:: bash
92
+
93
+ python -m pip install ansys-pre-commit-hooks
94
+
95
+ For developers
96
+ ^^^^^^^^^^^^^^
97
+
98
+ Installing the package in developer mode allows you to modify and
99
+ enhance the source code.
100
+
101
+ Before contributing to the project, ensure that you are familiar with
102
+ the `PyAnsys Developer's Guide`_.
103
+
104
+ For a developer installation, you must follow these steps:
105
+
106
+ #. Clone the repository with this command:
107
+
108
+ .. code:: bash
109
+
110
+ git clone https://github.com/ansys/pre-commit-hooks
111
+
112
+ #. Create a fresh-clean Python environment and activate it with these commands:
113
+
114
+ .. code:: bash
115
+
116
+ # Create a virtual environment
117
+ python -m venv .venv
118
+
119
+ # Activate it in a POSIX system
120
+ source .venv/bin/activate
121
+
122
+ # Activate it in Windows CMD environment
123
+ .venv\Scripts\activate.bat
124
+
125
+ # Activate it in Windows Powershell
126
+ .venv\Scripts\Activate.ps1
127
+
128
+ #. Ensure that you have the latest required build system tools by
129
+ running this command:
130
+
131
+ .. code:: bash
132
+
133
+ python -m pip install -U pip flit tox twine
134
+
135
+
136
+ #. Install the project in editable mode by running one of these commands:
137
+
138
+ .. code:: bash
139
+
140
+ # Install the minimum requirements
141
+ python -m pip install -e .
142
+
143
+ # Install the minimum + tests requirements
144
+ python -m pip install -e .[tests]
145
+
146
+ # Install the minimum + doc requirements
147
+ python -m pip install -e .[doc]
148
+
149
+ # Install all requirements
150
+ python -m pip install -e .[tests,doc]
151
+
152
+ #. Verify your development installation by running this command:
153
+
154
+ .. code:: bash
155
+
156
+ tox
157
+
158
+
159
+ How to test it
160
+ --------------
161
+
162
+ This project takes advantage of `tox`_. This tool automates common
163
+ development tasks (similar to Makefile), but it is oriented towards
164
+ Python development.
165
+
166
+ Using ``tox``
167
+ ^^^^^^^^^^^^^
168
+
169
+ While Makefile has rules, ``tox`` has environments. In fact, ``tox`` creates its
170
+ own virtual environment so that anything being tested is isolated from the project
171
+ to guarantee the project's integrity.
172
+
173
+ These environment commands are provided:
174
+
175
+ - **tox -e style**: Checks for coding style quality.
176
+ - **tox -e py**: Checks for unit tests.
177
+ - **tox -e py-coverage**: Checks for unit testing and code coverage.
178
+ - **tox -e doc**: Checks for successfully building the documentation.
179
+
180
+
181
+ Raw testing
182
+ ^^^^^^^^^^^
183
+
184
+ If required, you can always call style commands, such as `black`_, `isort`_,
185
+ and `flake8`_, or unit testing commands, such as `pytest`_, from the command line.
186
+ However, calling these commands does not guarantee that your project is
187
+ being tested in an isolated environment, which is the reason why tools like
188
+ ``tox`` exist.
189
+
190
+
191
+ A note on ``pre-commit``
192
+ ^^^^^^^^^^^^^^^^^^^^^^^^
193
+
194
+ The style checks take advantage of `pre-commit`_. Developers are not forced but
195
+ encouraged to install this tool by running this command:
196
+
197
+ .. code:: bash
198
+
199
+ python -m pip install pre-commit && pre-commit install
200
+
201
+
202
+ Documentation
203
+ -------------
204
+
205
+ For building documentation, you can run the usual rules provided in the
206
+ `Sphinx`_ Makefile with a command that is formatted like this:
207
+
208
+ .. code:: bash
209
+
210
+ make -C doc/ html && your_browser_name doc/html/index.html
211
+
212
+ However, the recommended way of checking documentation integrity is by
213
+ running ``tox`` with a command that is formatted like this:
214
+
215
+ .. code:: bash
216
+
217
+ tox -e doc && your_browser_name .tox/doc_out/index.html
218
+
219
+
220
+ Distributing
221
+ ------------
222
+
223
+ If you would like to create either source or wheel files, install
224
+ the building requirements and then execute the build module with these commands:
225
+
226
+ .. code:: bash
227
+
228
+ python -m pip install .
229
+ python -m build
230
+ python -m twine check dist/*
231
+
232
+
233
+ .. LINKS AND REFERENCES
234
+ .. _black: https://github.com/psf/black
235
+ .. _flake8: https://flake8.pycqa.org/en/latest/
236
+ .. _isort: https://github.com/PyCQA/isort
237
+ .. _pip: https://pypi.org/project/pip/
238
+ .. _pre-commit: https://pre-commit.com/
239
+ .. _PyAnsys Developer's Guide: https://dev.docs.pyansys.com/
240
+ .. _pytest: https://docs.pytest.org/en/stable/
241
+ .. _Sphinx: https://www.sphinx-doc.org/en/master/
242
+ .. _tox: https://tox.wiki/
243
+
@@ -0,0 +1,6 @@
1
+ ansys/pre_commit_hooks/__init__.py,sha256=KnLstnvKQ17jSdR2IgSpXhf3RHktf6ehXPZLlKVJzxU,1451
2
+ ansys/pre_commit_hooks/add_license_headers.py,sha256=zZYj8RXv50HWCWwH1YpFAez0OkPLZv43VXCWwP4aOcw,7024
3
+ ansys_pre_commit_hooks-0.1.0.dist-info/entry_points.txt,sha256=N9pvhGBJtJDJVoxXTMC6ZXkT0CNdvEyrl8xPj5A_Oc0,87
4
+ ansys_pre_commit_hooks-0.1.0.dist-info/WHEEL,sha256=EZbGkh7Ie4PoZfRQ8I0ZuP9VklN_TvcZ6DSE5Uar4z4,81
5
+ ansys_pre_commit_hooks-0.1.0.dist-info/METADATA,sha256=hZ0QetpImAbzFMP-4ZrVn31iUYL53OBsKtEABz7Bdn8,7804
6
+ ansys_pre_commit_hooks-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: flit 3.9.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,3 @@
1
+ [console_scripts]
2
+ add-license-headers=ansys.pre_commit_hooks.add_license_headers:main
3
+