Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pre-commit to cookitcutter #179

Open
wants to merge 5 commits into
base: review23
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions cookiecutter.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"Prefer default anaconda channel with pip fallback",
"Dependencies from pip only (no conda)"
],

"include_ReadTheDocs": ["y", "n"],
"_cms_cc_version": 1.10
"install_precommit_hooks": ["y", "n"],
"_cms_cc_version": 1.10
}
43 changes: 32 additions & 11 deletions hooks/post_gen_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,27 +51,31 @@ def git_init_and_tag():
# because it works with older versions of git
invoke_shell("git branch -M main")

# Add files created by cookiecutter
# Add files created by cookiecutter
invoke_shell("git add .")
invoke_shell(
"git commit -m \"Initial commit after CMS Cookiecutter creation, version {}\"".format(
'{{ cookiecutter._cms_cc_version }}'))

'git commit -m "Initial commit after CMS Cookiecutter creation, version {}"'.format(
"{{ cookiecutter._cms_cc_version }}"
)
)

# Check for a tag
version = invoke_shell("git tag", error_ok=True)
# Tag if no tag exists
if not version:
invoke_shell("git tag 0.0.0")
else:
print("\ngit repository detected. "
"CookieCutter files have been created in {{ cookiecutter.repo_name }} directory.")
print(
"\ngit repository detected. "
"CookieCutter files have been created in {{ cookiecutter.repo_name }} directory."
)


def remove_rtd():
include_rtd = '{{ cookiecutter.include_ReadTheDocs }}'
include_rtd = "{{ cookiecutter.include_ReadTheDocs }}"
if include_rtd == "n":
rtd_env = os.path.join("docs", "requirements.yaml")
os.remove('readthedocs.yml')
os.remove("readthedocs.yml")
os.remove(rtd_env)


Expand All @@ -87,6 +91,23 @@ def random_file_cleanup_removal():
pass


remove_rtd()
random_file_cleanup_removal()
git_init_and_tag()
def install_pre_commit_hooks():
"""Install pre-commit hooks"""
invoke_shell("python -m pip install pre-commit")
invoke_shell("python -m pre-commit install")


if __name__ == "__main__":
remove_rtd()
random_file_cleanup_removal()
git_init_and_tag()

if "{{ cookiecutter.install_precommit_hooks }}" == "y":
try:
install_pre_commit_hooks()
except Exception as e:
print(str(e))
print(
"Failed to install pre-commit hooks. "
"For more on pre-commit, please refer to https://pre-commit.com"
)
105 changes: 105 additions & 0 deletions {{cookiecutter.repo_name}}/.pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# pre-commit is a tool to perform a predefined set of tasks manually and/or
# automatically before git commits are made.

ci:
# https://pre-commit.ci/
# pre-commit.ci will open PRs updating our hooks once a month
# 'weekly', 'monthly', 'quarterly'
autoupdate_schedule: quarterly
autofix_prs: false
autofix_commit_msg: "[pre-commit.ci] Auto fixes from pre-commit.com hooks."
submodules: false

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
# https://pre-commit.com/hooks.html
# - id: fix-encoding-pragma
- id: sort-simple-yaml
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-toml
# replaces or checks mixed line ending
- id: mixed-line-ending
- id: debug-statements
# - id: name-tests-test
- id: trailing-whitespace
# sorts entries in requirements.txt
- id: requirements-txt-fixer

# - repo: https://github.com/psf/black-pre-commit-mirror
- repo: https://github.com/psf/black
rev: 23.10.1
hooks:
- id: black
args: [
--line-length=100,
]

- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort

- repo: https://github.com/pycqa/flake8
rev: 6.1.0
hooks:
- id: flake8
args: ["--max-line-length=100"]
additional_dependencies:
- flake8-bugbear
- flake8-comprehensions
- flake8-simplify
- flake8-docstrings
- flake8-import-order>=0.9
- flake8-colors
exclude: ^src/blib2to3/

- repo: https://github.com/pycqa/bandit
rev: 1.7.5
hooks:
- id: bandit
# exclude some directories
exclude: |
(?x)(
^test/|
^book/
^devtools/
^docs/
^doc/
)
args: [
"-c", "pyproject.toml"
]
additional_dependencies: [ "bandit[toml]" ]

- repo: https://github.com/asottile/pyupgrade
rev: v3.15.0
hooks:
- id: pyupgrade
args: [--py37-plus]

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.5.1
hooks:
- id: mypy
exclude: ^docs/conf.py
args: ["--config-file", "pyproject.toml"]
additional_dependencies:
- types-PyYAML
- tomli >= 0.2.6, < 2.0.0
- click >= 8.1.0, != 8.1.4, != 8.1.5
- packaging >= 22.0
- platformdirs >= 2.1.0
- pytest
- hypothesis
- aiohttp >= 3.7.4
- types-commonmark
- urllib3
- hypothesmith

{% if cookiecutter.install_precommit_hooks == "n" %}
# Additional hooks and configuration when pre-commit hooks are not selected
{% endif %}
4 changes: 4 additions & 0 deletions {{cookiecutter.repo_name}}/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ test = [
"pytest>=6.1.2",
]

{%- if cookiecutter.install_precommit_hooks == 'y' %}
pre-commit = {version = "^3.4.0", optional = true}
{%- endif %}

[tool.setuptools]
# This subkey is a beta stage development and keys may change in the future, see https://setuptools.pypa.io/en/latest/userguide/pyproject_config.html for more details
#
Expand Down