From b32d38947c93e204bda96e1e0b3adcfa07b16a1a Mon Sep 17 00:00:00 2001 From: Leonardo Uieda Date: Fri, 8 Mar 2024 10:40:01 -0300 Subject: [PATCH] Use Burocrata to check and add license notices (#402) Replaces our custom script in `tools`. Checks that all files have a license notice and adds it if they are missing one. Simplify the `Makefile` to distinguish only between checking style and format instead of each individual tool. --- .github/workflows/style.yml | 4 +- Makefile | 22 ++++----- env/requirements-style.txt | 1 + environment.yml | 2 + pyproject.toml | 9 ++++ tools/license_notice.py | 93 ------------------------------------- 6 files changed, 22 insertions(+), 109 deletions(-) delete mode 100644 tools/license_notice.py diff --git a/.github/workflows/style.yml b/.github/workflows/style.yml index 77646aab..87e9d4db 100644 --- a/.github/workflows/style.yml +++ b/.github/workflows/style.yml @@ -36,7 +36,7 @@ jobs: run: python -m pip freeze - name: Check code format - run: make black-check license-check + run: make check-format style: runs-on: ubuntu-latest @@ -58,4 +58,4 @@ jobs: run: python -m pip freeze - name: Check code style - run: make flake8 lint + run: make check-style lint diff --git a/Makefile b/Makefile index ddc90a47..9d7ec156 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ PROJECT=pooch TESTDIR=tmp-test-dir-with-unique-name PYTEST_ARGS=--cov-config=../.coveragerc --cov-report=term-missing --cov=$(PROJECT) --doctest-modules -v --pyargs LINT_FILES=$(PROJECT) -CHECK_STYLE=doc/conf.py $(PROJECT) tools +CHECK_STYLE=$(PROJECT) doc help: @echo "Commands:" @@ -30,23 +30,17 @@ test: cp $(TESTDIR)/.coverage* . rm -r $(TESTDIR) -format: license black - -check: black-check license-check flake8 - -black: +format: black $(CHECK_STYLE) + burocrata --extension=py $(CHECK_STYLE) -black-check: - black --check $(CHECK_STYLE) +check: check-format check-style -license: - python tools/license_notice.py - -license-check: - python tools/license_notice.py --check +check-format: + black --check $(CHECK_STYLE) + burocrata --check --extension=py $(CHECK_STYLE) -flake8: +check-style: flake8 $(CHECK_STYLE) lint: diff --git a/env/requirements-style.txt b/env/requirements-style.txt index cb76f5ed..1a7e06ca 100644 --- a/env/requirements-style.txt +++ b/env/requirements-style.txt @@ -3,3 +3,4 @@ black flake8 pylint>=2.4 pathspec +burocrata diff --git a/environment.yml b/environment.yml index 24f983d3..8c36ebbb 100644 --- a/environment.yml +++ b/environment.yml @@ -26,3 +26,5 @@ dependencies: - black>=20.8b1 - flake8 - pylint>=2.4 + - pip: + - burocrata diff --git a/pyproject.toml b/pyproject.toml index 44b23a0a..fefd5e99 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -61,3 +61,12 @@ write_to = "pooch/_version.py" markers = [ "network: test requires network access", ] + +[tool.burocrata] +notice = ''' +# Copyright (c) 2018 The Pooch Developers. +# Distributed under the terms of the BSD 3-Clause License. +# SPDX-License-Identifier: BSD-3-Clause +# +# This code is part of the Fatiando a Terra project (https://www.fatiando.org) +#''' diff --git a/tools/license_notice.py b/tools/license_notice.py deleted file mode 100644 index af995903..00000000 --- a/tools/license_notice.py +++ /dev/null @@ -1,93 +0,0 @@ -# Copyright (c) 2018 The Pooch Developers. -# Distributed under the terms of the BSD 3-Clause License. -# SPDX-License-Identifier: BSD-3-Clause -# -# This code is part of the Fatiando a Terra project (https://www.fatiando.org) -# -""" -Add license notice to every source file if not present -""" -import sys -from pathlib import Path -from argparse import ArgumentParser -from pathspec import PathSpec - - -PROJECT = "pooch" -YEAR = "2018" -NOTICE = f""" -# Copyright (c) {YEAR} The {PROJECT.title()} Developers. -# Distributed under the terms of the BSD 3-Clause License. -# SPDX-License-Identifier: BSD-3-Clause -# -# This code is part of the Fatiando a Terra project (https://www.fatiando.org) -# -""".strip() -CHECK_HELP = """ -Don't write the files, just return the status. Return code 0 means -nothing would change. Return code 1 means some files lacks the license notice. -""" - - -def get_gitignore(root): - """ - Return a PathSpec matching gitignore content if present. - This function is a modified version of the one present in Black - (https://github.com/psf/black) available under MIT License. - """ - gitignore = root / ".gitignore" - lines = [] - if gitignore.is_file(): - with gitignore.open() as gi_file: - lines = gi_file.readlines() - return PathSpec.from_lines("gitwildmatch", lines) - - -def main(): - """ - Add license notice to every source file if not present or just check - """ - # Create option parser - parser = ArgumentParser( - description=" Add license notice to every source file if not present." - ) - parser.add_argument( - "--check", action="store_true", dest="check", default=False, help=CHECK_HELP - ) - args = parser.parse_args() - - gitignore = get_gitignore(Path(".")) - - python_files = [ - path - for path in Path(".").glob("**/*.py") - if not str(path).startswith(".") - if not gitignore.match_file(path) - ] - - missing_notice_files = [] - for pyfile in python_files: - code = pyfile.read_text() - if not code.startswith(NOTICE): - missing_notice_files.append(pyfile) - - if args.check: - if missing_notice_files: - print("License notice is missing in some source files! 💔") - for pyfile in missing_notice_files: - print(f" {pyfile}") - sys.exit(1) - else: - print("All source files have the license notice! 🎉") - sys.exit(0) - else: - print("Successfully added license notice to:") - for pyfile in missing_notice_files: - code = pyfile.read_text() - pyfile.write_text("\n".join([NOTICE, code])) - print(f" {pyfile}") - sys.exit(0) - - -if __name__ == "__main__": - main()