Skip to content

Commit

Permalink
Modernize project infrastructure and switching to hatch (#108)
Browse files Browse the repository at this point in the history
* Switch to src project structure

* Prevent CI being run twice on PR

Having CI jobs run on PR and push without any further restrictions runs
the CI jobs twice on a new PR, with this setting the CI is only run on
pushes to main.

* Ignore QbFields warning in sphinx

Bug that appeears with aiida 2.6 (independent of this PR), it also goes
away when building the docs twice. For now I ignore it.

* Remove outdated install_aiida_github.sh

It is nowhere used and I am not sure in which context it should be used.
Installing aiida-core seems trivial. What would be useful is something
for creating new profiles in the tests.

* Split pre-commit hook fmt into lint and format

* Change ruff settings single quotes -> double quotes

double quotes are default so nothing has to be changed

* Apply the formatting to double quotes on template repo

* Move docs to an optional dependency for readthedocs

Because readthedocs can only use extra dependencies from the
pyproject.toml, we move the deps of docs into one, so we can reference
it in the readthedocs.yaml

* Updates in the update-aiida-diff.sh script

- Remove `git init` in update aiida-diff script to base on current version
- Remove `hatch fmt` at the end since it is done by hook properly
* Fix formatting in the cookiecutter hook

Now `hatch fmt` is executed twice, running it one time did not fix all
isues. Needs to be investigated, if a reviewer has some ideas.

* Add script update-template-formatting.sh that can be used to fix formatting

To apply the formatting on the cookiecutter template the script initiates
a cookiecutter repo instance and applies the formatting on this instance
the changes are diffed and copied back to the template.

* Update update-aiida-diff.sh

Since the cookiecutter hooks is now also performing the formatting,
the pre-commit is not necessary. The git init has been removed since
the repository already exists.

---------

Co-authored-by: Rico Haeuselmann <r.haeuselmann@gmx.ch>
  • Loading branch information
agoscinski and DropD authored Sep 10, 2024
1 parent 59c009e commit 5fa3ff4
Show file tree
Hide file tree
Showing 24 changed files with 283 additions and 226 deletions.
40 changes: 18 additions & 22 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
timeout-minutes: 30
strategy:
matrix:
python-version: ['3.10']
python-version: ['3.12']
aiida-version: ['stable']
services:
postgres:
Expand All @@ -31,10 +31,10 @@ jobs:
- 5672:5672

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

Expand All @@ -44,7 +44,6 @@ jobs:
pip install -r requirements.txt
# Change plugin_name to test we're not specific to "aiida-diff"
cookiecutter --no-input . plugin_name=${PLUGIN_NAME}
pip install -e ${PLUGIN_NAME}[testing]
env:
PLUGIN_NAME: aiida-ck

Expand All @@ -55,17 +54,17 @@ jobs:
PLUGIN_NAME: aiida-ck
run: |
cd ${PLUGIN_NAME}
pytest -v
hatch test -v
docs:
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- uses: actions/checkout@v2
- name: Set up Python 3.10
uses: actions/setup-python@v2
- uses: actions/checkout@v4
- name: Set up Python 3.12
uses: actions/setup-python@v5
with:
python-version: "3.10"
python-version: "3.12"
- name: Install python dependencies
env:
PLUGIN_NAME: aiida-ck
Expand All @@ -74,21 +73,22 @@ jobs:
pip install -r requirements.txt
# Change plugin_name to test we're not specific to "aiida-diff"
cookiecutter --no-input . plugin_name=${PLUGIN_NAME}
pip install -e ${PLUGIN_NAME}[docs]
- name: Build docs
env:
PLUGIN_NAME: aiida-ck
run: cd ${PLUGIN_NAME}/docs && make
run: |
cd ${PLUGIN_NAME}
hatch run docs:build
pre-commit:
static-analysis:
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- uses: actions/checkout@v2
- name: Set up Python 3.10
uses: actions/setup-python@v2
- uses: actions/checkout@v4
- name: Set up Python 3.12
uses: actions/setup-python@v5
with:
python-version: "3.10"
python-version: "3.12"
- name: Install python dependencies
env:
PLUGIN_NAME: aiida-ck
Expand All @@ -97,13 +97,9 @@ jobs:
pip install -r requirements.txt
# Change plugin_name to test we're not specific to "aiida-diff"
cookiecutter --no-input . plugin_name=${PLUGIN_NAME}
pip install -e ${PLUGIN_NAME}[pre-commit,docs,testing]
- name: Run pre-commit
- name: Run formatter and linter
env:
PLUGIN_NAME: aiida-ck
run: |
cd ${PLUGIN_NAME}
git init
git add -A
pre-commit install
pre-commit run --all-files || ( git status --short ; git diff ; exit 1 )
hatch fmt
10 changes: 6 additions & 4 deletions hooks/post_gen_project.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#!/bin/bash
if [ -x "$(command -v black)" ]; then
echo "Running black on {{ cookiecutter.plugin_name }}"
black '../{{ cookiecutter.plugin_name }}/'
if [ -x "$(command -v hatch)" ]; then
echo "Running hatch on {{ cookiecutter.plugin_name }}"
# For some reason we need to invoke the formatting twice to be effective
hatch fmt || true
hatch fmt || true
else
echo "black not found. 'pip install black' to automatically \
echo "hatch not found. 'pip install hatch' to automatically \
run formatter on {{ cookiecutter.plugin_name }}"
fi

Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
cookiecutter
black==22.12.0
hatch~=1.12.0
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ commands =
cookiecutter --no-input . plugin_name={env:PLUGIN_NAME}
git init -b main {env:PLUGIN_NAME}
cd {env:PLUGIN_NAME} && git add -A
pip install -e {env:PLUGIN_NAME}[testing,docs,pre-commit]
pip install -e {env:PLUGIN_NAME}
5 changes: 0 additions & 5 deletions update-aiida-diff.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,3 @@ else
fi

cookiecutter --no-input -f . version=1.2.0
pip install -e aiida-diff[docs,pre-commit,testing]
cd aiida-diff
git init && git add -A
pre-commit install
pre-commit run
29 changes: 29 additions & 0 deletions update-template-formatting.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash
# Creates plugin cutter with unique labels so we can format and then reverses
# the ids to the cookiecutter identifiers and git apply the diff
set -e

# clean directory contents if exist (but don't delete .git directory)
if [ -d aiida-diff ]; then
rm -rf cookiecutter_plugin_name
fi
cookiecutter --accept-hooks no --no-input -f . plugin_name=cookiecutter_plugin_name module_name=cookiecutter_module_name short_description=cookiecutter_short_description entry_point_prefix=cookiecutter_entry_point_prefix version=0.0.0-dev author=cookiecutter_author year=cookiecutter_year

cd cookiecutter_plugin_name
git init && git add -A && git commit -am 'init'
hatch fmt || true
# takes the diff and applies it on the cookie cutter template
git diff > patch
cp patch ..
cd ..
# git diff replacements of specified labels with cookiecutter identifiers
sed -i 's/ a\// a\/\{\{cookiecutter.plugin_name\}\}\//g' patch
sed -i 's/ b\// b\/\{\{cookiecutter.plugin_name\}\}\//g' patch
sed -i 's/cookiecutter_plugin_name/\{\{cookiecutter.plugin_name\}\}/g' patch
sed -i 's/cookiecutter_module_name/\{\{cookiecutter.module_name\}\}/g' patch
sed -i 's/cookiecutter_entry_point_prefix/\{\{cookiecutter.entry_point_prefix\}\}/g' patch
sed -i 's/cookiecutter_short_description/\{\{cookiecutter.short_description\}\}/g' patch
sed -i 's/0\.0\.0-dev/\{\{cookiecutter.version\}\}/g' patch
sed -i 's/cookiecutter_author/\{\{cookiecutter.author\}\}/g' patch
sed -i 's/cookiecutter_year/\{\{cookiecutter.year\}\}/g' patch
git apply --reject --whitespace=fix patch
6 changes: 0 additions & 6 deletions {{cookiecutter.plugin_name}}/.github/install_aiida_github.sh

This file was deleted.

60 changes: 30 additions & 30 deletions {{cookiecutter.plugin_name}}/.github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
name: ci

on: [push, pull_request]
on:
push:
# only pushes to main trigger
branches: [main]
pull_request:
# always triggered

jobs:

Expand All @@ -9,7 +14,7 @@ jobs:
timeout-minutes: 30
strategy:
matrix:
python-version: ['3.10']
python-version: ['3.12']
aiida-version: ['stable']

services:
Expand All @@ -32,54 +37,49 @@ jobs:
- 5672:5672

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- name: Set up Python ${{ '{{ matrix.python-version }}' }}
uses: actions/setup-python@v2
uses: actions/setup-python@v5
with:
python-version: ${{ '{{ matrix.python-version }}' }}

- name: Install python dependencies
- name: Install project manager
run: |
pip install --upgrade pip
pip install -e .[testing]
pip install hatch
- name: Run test suite
env:
# show timings of tests
PYTEST_ADDOPTS: "--durations=0"
run: pytest --cov {{cookiecutter.module_name}} --cov-append .
run: |
hatch test --cover
docs:
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- uses: actions/checkout@v2
- name: Set up Python 3.10
uses: actions/setup-python@v2
- uses: actions/checkout@v4
- name: Set up Python ${{ '{{ matrix.python-version }}' }}
uses: actions/setup-python@v5
with:
python-version: "3.10"
- name: Install python dependencies
python-version: ${{ '{{ matrix.python-version }}' }}
- name: Install project manager
run: |
pip install --upgrade pip
pip install -e .[docs]
pip install hatch
- name: Build docs
run: cd docs && make
run: |
hatch run docs:build
pre-commit:
static-analysis:
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- uses: actions/checkout@v2
- name: Set up Python 3.10
uses: actions/setup-python@v2
- uses: actions/checkout@v4
- name: Set up Python ${{ '{{ matrix.python-version }}' }}
uses: actions/setup-python@v5
with:
python-version: "3.10"
- name: Install python dependencies
python-version: ${{ '{{ matrix.python-version }}' }}
- name: Install project manager
run: |
pip install --upgrade pip
pip install -e .[pre-commit,docs,testing]
- name: Run pre-commit
pip install hatch
- name: Run formatter and linter
run: |
pre-commit install
pre-commit run --all-files || ( git status --short ; git diff ; exit 1 )
hatch fmt --check
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v2

- name: Set up Python 3.10
uses: actions/setup-python@v1
- name: Set up Python 3.12
uses: actions/setup-python@v5
with:
python-version: '3.10'
python-version: '3.12'

- name: Install flit
- name: Install hatch
run: |
python -m pip install --upgrade pip
python -m pip install flit~=3.4
python -m pip install hatch~=1.12.0
- name: Build and publish
run: |
flit publish
hatch publish
env:
FLIT_USERNAME: __token__
FLIT_PASSWORD: ${{ '{{ secrets.pypi_token }}' }}
HATCH_INDEX_USER: __token__
HATCH_INDEX_AUTH: ${{ '{{ secrets.pypi_token }}' }}
44 changes: 9 additions & 35 deletions {{cookiecutter.plugin_name}}/.pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,39 +1,13 @@
# Install pre-commit hooks via:
# pre-commit install
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: end-of-file-fixer
- id: mixed-line-ending
- id: trailing-whitespace
- id: check-json

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

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

- repo: https://github.com/psf/black
rev: 22.12.0
hooks:
- id: black

- repo: local
hooks:
- id: pylint
- id: format
name: format
entry: hatch fmt -f
language: system
types: [python]
- id: lint
name: lint
entry: hatch fmt -l
language: system
types: [file, python]
name: pylint
description: "This hook runs the pylint static code analyzer"
exclude: &exclude_files >
(?x)^(
docs/.*|
)$
entry: pylint
types: [python]
Loading

0 comments on commit 5fa3ff4

Please sign in to comment.