diff --git a/.github/workflows/style.yml b/.github/workflows/style.yml index e2fd07637..09f1d82c4 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 isort-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 + run: make check-style diff --git a/Makefile b/Makefile index f01b97de1..5ac6ef545 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ PROJECT=verde TESTDIR=tmp-test-dir-with-unique-name PYTEST_ARGS=--cov-config=../.coveragerc --cov-report=term-missing --cov=$(PROJECT) --doctest-modules -v --pyargs -CHECK_STYLE=$(PROJECT) doc tools +CHECK_STYLE=$(PROJECT) doc help: @echo "Commands:" @@ -28,29 +28,19 @@ test: cp $(TESTDIR)/.coverage* . rm -rvf $(TESTDIR) -format: license isort black - -check: black-check isort-check license-check flake8 - -black: +format: + isort $(CHECK_STYLE) black $(CHECK_STYLE) + burocrata --extension=py $(PROJECT) -black-check: - black --check $(CHECK_STYLE) - -license: - python tools/license_notice.py +check: check-format check-style -license-check: - python tools/license_notice.py --check - -isort: - isort $(CHECK_STYLE) - -isort-check: +check-format: + black --check $(CHECK_STYLE) isort --check $(CHECK_STYLE) + burocrata --check --extension=py $(PROJECT) -flake8: +check-style: flake8 $(CHECK_STYLE) clean: diff --git a/env/requirements-style.txt b/env/requirements-style.txt index 28e39b9ed..9dcec7cc7 100644 --- a/env/requirements-style.txt +++ b/env/requirements-style.txt @@ -10,3 +10,4 @@ flake8-rst-docstrings flake8-simplify flake8-unused-arguments pep8-naming +burocrata diff --git a/environment.yml b/environment.yml index 772379906..4d3a7cb48 100644 --- a/environment.yml +++ b/environment.yml @@ -49,8 +49,7 @@ dependencies: - flake8-mutable - flake8-rst-docstrings - flake8-simplify + - flake8-unused-arguments - pep8-naming - pip: - # Install flake8-unused-arguments through pip - # (not available through conda yet) - - flake8-unused-arguments + - burocrata diff --git a/pyproject.toml b/pyproject.toml index c7589447b..ffd9f449b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -13,3 +13,12 @@ write_to = "verde/_version_generated.py" [tool.isort] profile = "black" multi_line_output = 3 + +[tool.burocrata] +notice = ''' +# Copyright (c) 2017 The Verde 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 1a2fc15b7..000000000 --- a/tools/license_notice.py +++ /dev/null @@ -1,94 +0,0 @@ -# Copyright (c) 2017 The Verde 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 argparse import ArgumentParser -from pathlib import Path - -from pathspec import PathSpec - -PROJECT = "verde" -YEAR = "2017" -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()