Skip to content

Commit

Permalink
Remove .reuse and LICENSES folders (#95)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Roberto Pastor Muela <[email protected]>
  • Loading branch information
3 people committed Nov 15, 2023
1 parent 787b939 commit 0167d2c
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 174 deletions.
27 changes: 0 additions & 27 deletions .reuse/templates/ansys.jinja2

This file was deleted.

21 changes: 0 additions & 21 deletions LICENSES/MIT.txt

This file was deleted.

48 changes: 10 additions & 38 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,16 @@ Currently, these hooks are available:
Add required directories
^^^^^^^^^^^^^^^^^^^^^^^^

If you are using any custom templates or licenses, copy the .reuse and LICENSES directories from this repository
into the target repository. If you are using the default Ansys template and MIT.txt license, skip this step. By default,
the hook will copy the LICENSES/MIT.txt, .reuse/templates/ansys.jinja2, and .reuse/dep5 into the target repository when
the hook runs.
If you are using the ansys.jinja2 template and MIT.txt license, skip this step. By default, the hook will make symbolic links
from its "assets" directory containing LICENSES/MIT.txt and .reuse/templates/ansys.jinja2
to your repository when the hook runs. The .reuse and LICENSES directories will be deleted once the hook is
done running.

The LICENSES and .reuse folders are required for the hook to run correctly. Your project should have the following layout:
If you are using a custom template, create a directory named ``.reuse``, and if you are using a custom license, create a directory
named ``LICENSES`` in the root of your repository. The custom template cannot be named ``ansys.jinja2``, otherwise it will be removed
after the hook is done running. The custom license cannot be named ``MIT.txt`` for the same reason. The ``.reuse`` and/or ``LICENSES``
directories will have to be committed to your repository and will not be removed once the hook is done running as long as there
are custom templates or licenses in those directories. Your project should have the following layout:

::

Expand All @@ -60,7 +64,6 @@ The LICENSES and .reuse folders are required for the hook to run correctly. Your
├── .reuse
│ └── templates
│ └── template_name.jinja2
│ └── dep5
├── src
├── examples
├── tests
Expand All @@ -75,21 +78,6 @@ Licenses that are supported by ``REUSE`` can be found in the
`spdx/license-list-data <https://github.com/spdx/license-list-data/tree/main/text>`_ repository.
Please select a license text file from that repository, and copy it to the LICENSES directory.

Configure the .reuse/dep5 file
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

If the default template and license are being used, run the hook to acquire the
.reuse and LICENSES directories. After running the hook, if files in your project
contain headers that should not, configure the .reuse/dep5 file to match the file
structure within your repository and run the hook again.

If you are manually setting up the .reuse and LICENSES directories,
ensure the .reuse/dep5 entries match the file structure within your repository.
The dep5 file contains files & directories that should not be given license headers.

* Ensure all files and directories you want to ignore are in this file.
* See step #5 for examples of how to ignore specific files in dep5.

Set custom arguments
^^^^^^^^^^^^^^^^^^^^

Expand Down Expand Up @@ -142,9 +130,7 @@ the hook should run on, add the necessary regex to the ``files`` line in your
Ignore specific files or file types
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

There are two different ways to ignore specific files or file types:

In .pre-commit-config.yaml
In .pre-commit-config.yaml:

.. code:: yaml
Expand All @@ -167,20 +153,6 @@ In .pre-commit-config.yaml
* ``.*\.js`` excludes all .js files in all directories.
* ``\..*`` excludes all hidden files.

In .reuse/dep5

.. code:: debcontrol
Files: path/to/file1.py
Copyright: 2023 ANSYS, Inc. and/or its affiliates.
License: MIT
Files: path/to/*.py
Copyright: 2023 ANSYS, Inc. and/or its affiliates.
License: MIT
* ``path/to/file1.py`` excludes the stated file.
* ``path/to/*.py`` excludes all .py files in the ``path/to`` directory.

How to install
--------------
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ version = "0.2.1"
description = "A Python wrapper to create Ansys-tailored pre-commit hooks"
readme = "README.rst"
requires-python = ">=3.8,<4"
license = {file = "LICENSES/MIT.txt"}
license = {file = "LICENSE"}
authors = [
{name = "ANSYS, Inc.", email = "[email protected]"},
]
Expand Down
137 changes: 99 additions & 38 deletions src/ansys/pre_commit_hooks/add_license_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,48 +93,65 @@ def set_lint_args(parser: argparse.ArgumentParser) -> argparse.Namespace:
return parser.parse_args()


def copy_assets(proj_root: str, args: argparse.Namespace) -> None:
def link_assets(assets: dict, git_root: str, args: argparse.Namespace) -> None:
"""
Copy .reuse and LICENSES folders from assets directory.
Link the default template and/or license from the assets folder to your git repo.
Parameters
----------
proj_root: str
assets: dict
Dictionary containing the asset folder information.
git_root: str
Full path of the repository's root directory.
args: argparse.Namespace
Namespace of arguments with their values.
"""
# Unlink default files & remove .reuse and LICENSES folders if empty
cleanup(assets, git_root)

hook_loc = pathlib.Path(__file__).parent.resolve()
directories = [".reuse"]

# If ignore_license_check is False, copy LICENSES folder to
# the root of the repository
if not args.ignore_license_check:
directories.append("LICENSES")

for dir in directories:
src = os.path.join(hook_loc, "assets", dir)
dest = os.path.join(proj_root, dir)

# If .reuse or LICENSES exists in the root of the repository,
# only replace .reuse/templates/ansys.jinja2 and LICENSES/MIT.txt
if os.path.isdir(dest):
if ".reuse" in dest:
src_file = os.path.join(src, "templates", "ansys.jinja2")
dest_file = os.path.join(dest, "templates", "ansys.jinja2")
elif "LICENSES" in dest:
src_file = os.path.join(src, "MIT.txt")
dest_file = os.path.join(dest, "MIT.txt")

if os.path.isfile(dest_file):
# Remove destination file & replace with
# a new copy from source
os.remove(dest_file)
shutil.copyfile(src_file, dest_file)
else:
# If destination directory does not exist, copy the entire
# folder from src to dest
shutil.copytree(src, dest)
for key, value in assets.items():
hook_asset_dir = os.path.join(hook_loc, "assets", value["path"])
repo_asset_dir = os.path.join(git_root, value["path"])

# If key is .reuse and the custom template is being used
if key == ".reuse" and args.custom_template == DEFAULT_TEMPLATE:
mkdirs_and_link(value["path"], hook_asset_dir, repo_asset_dir, value["default_file"])

# If key is LICENSES, the default license is being used, and ignore_license_check is False
if (
key == "LICENSES"
and args.custom_license == DEFAULT_LICENSE
and not args.ignore_license_check
):
mkdirs_and_link(value["path"], hook_asset_dir, repo_asset_dir, value["default_file"])


def mkdirs_and_link(
asset_dir: str, hook_asset_dir: str, repo_asset_dir: str, filename: str
) -> None:
"""
Make .reuse or LICENSES directory and create symbolic link to file.
Parameters
----------
asset_dir: str
Path of the asset directory required for REUSE (.reuse/templates or LICENSES).
hook_asset_dir: str
Full path of the hook's asset directory.
repo_asset_dir: str
Full path of the git repository's asset directory.
filename: str
Name of the file to be linked from the hook_asset_dir to the repo_asset_dir.
"""
src = os.path.join(hook_asset_dir, filename)
dest = os.path.join(repo_asset_dir, filename)
# If .reuse/templates or LICENSES directories do not exist, create them
if not os.path.isdir(asset_dir):
os.makedirs(asset_dir)
# Make symbolic links to files within the assets folder
os.symlink(src, dest)


def list_noncompliant_files(args: argparse.Namespace, proj: project.Project) -> list:
Expand Down Expand Up @@ -373,6 +390,27 @@ def get_full_paths(file_list: list) -> list:
return full_path_files


def cleanup(assets: dict, os_git_root: str) -> None:
"""
Unlink the default asset files, and remove directories if empty.
Parameters
----------
assets: dict
Dictionary containing assets information
os_git_root: str
Full path of the repository's root directory.
"""
# Remove default assets (.reuse/templates/ansys.jinja2 and LICENSES/MIT.txt)
for key, value in assets.items():
dest = os.path.join(os_git_root, value["path"], value["default_file"])
# If the default asset files exist, unlink and remove directory
if os.path.exists(dest):
os.remove(dest)
if not os.listdir(value["path"]):
shutil.rmtree(key)


def find_files_missing_header() -> int:
"""
Find files that are missing license headers and run `REUSE <https://reuse.software/>`_ on them.
Expand Down Expand Up @@ -407,26 +445,49 @@ def find_files_missing_header() -> int:
"git_repo": git_repo,
}

# Run REUSE on root of the repository
git_root = values["git_repo"].git.rev_parse("--show-toplevel")

# git_root with correct line separators for operating system
os_git_root = git_root.replace("/", os.sep)

# Dictionary containing the asset folder information
assets = {
".reuse": {
"path": os.path.join(".reuse", "templates"),
"default_file": f"{DEFAULT_TEMPLATE}.jinja2",
},
"LICENSES": {
"path": "LICENSES",
"default_file": f"{DEFAULT_LICENSE}.txt",
},
}

# Add header arguments to parser. Arguments are: copyright, license, contributor,
# year, style, copyright-style, template, exclude-year, merge-copyrights, single-line,
# multi-line, explicit-license, force-dot-license, recursive, no-replace,
# skip-unrecognized, and skip-existing
header.add_arguments(parser)

# Run REUSE on root of the repository
git_root = values["git_repo"].git.rev_parse("--show-toplevel")

# Copy .reuse folder and LICENSES folder (if licenses are being checked)
copy_assets(git_root, args)
# Link the default template and/or license from the assets folder to your git repo.
link_assets(assets, os_git_root, args)

# Project to run `REUSE <https://reuse.software/>`_ on
proj = project.Project(git_root)

# Get files missing headers (copyright and/or license information)
missing_headers = list(list_noncompliant_files(args, proj))

# Add or update headers of required files.
# Return 1 if files were added or updated, and return 0 if no files were altered.
return_code = check_exists(changed_headers, parser, values, proj, missing_headers, 0)

# Unlink default files & remove .reuse and LICENSES folders if empty
cleanup(assets, os_git_root)

# Returns 1 if REUSE changes noncompliant files
# Returns 0 if all files are compliant
return check_exists(changed_headers, parser, values, proj, missing_headers, 0)
return return_code


def main():
Expand Down
30 changes: 0 additions & 30 deletions tests/test_dependencies.py

This file was deleted.

Loading

0 comments on commit 0167d2c

Please sign in to comment.