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 hooks and CI linter #1

Merged
merged 1 commit into from
Oct 14, 2022
Merged
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
33 changes: 33 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: lint

on: [push, pull_request]

jobs:
format:
runs-on: ${{ matrix.os }}-latest
strategy:
matrix:
os: [ubuntu]
python-version: ["3.10"]

steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

- uses: actions/cache@v3
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('**/pyproject.toml') }}
restore-keys: |
${{ runner.os }}-pip-

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install .[lint]

- name: Lint
run: pre-commit run --all-files --show-diff-on-failure --color always
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
*~
\#*
dev.py.egg-info
__pycache__
__pycache__
37 changes: 37 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Install pre-commit hooks via
# pre-commit install

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
hooks:
- id: check-added-large-files
- id: check-ast
- id: check-builtin-literals
- id: check-case-conflict
- id: check-json
- id: check-toml
- id: check-yaml
args: [--allow-multiple-documents]
- id: debug-statements
- id: end-of-file-fixer
- id: mixed-line-ending
- id: trailing-whitespace

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

- repo: https://github.com/pre-commit/mirrors-prettier
rev: v2.7.1
hooks:
- id: prettier
files: \.(css|md|yml|yaml)
args: [--prose-wrap=preserve]

- repo: https://github.com/asottile/pyupgrade
rev: v3.1.0
hooks:
- id: pyupgrade
args: [--py37-plus]
7 changes: 4 additions & 3 deletions dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
import os
import sys
import runpy

sys.path.remove(os.path.abspath(os.path.dirname(sys.argv[0])))
try:
runpy.run_module('dev.py', run_name="__main__")
runpy.run_module("dev.py", run_name="__main__")
except ImportError:
print('Cannot import dev.py; please install it using')
print("Cannot import dev.py; please install it using")
print()
print(' pip install dev.py')
print(" pip install dev.py")
print()
sys.exit(1)
14 changes: 8 additions & 6 deletions dev/cmds/_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@


@click.command()
@click.option('--build-dir', default='build', help='Build directory; default is `$PWD/build`')
@click.option('-j', '--jobs', help='Nr of parallel tasks to launch', type=int)
@click.option(
"--build-dir", default="build", help="Build directory; default is `$PWD/build`"
)
@click.option("-j", "--jobs", help="Nr of parallel tasks to launch", type=int)
def build(build_dir, jobs=None):
"""🔧 Build package with Meson/ninja"""
build_dir = os.path.abspath(build_dir)
build_cmd = ['meson', f'--prefix={build_dir}', 'build']
build_cmd = ["meson", f"--prefix={build_dir}", "build"]
if os.path.exists(build_dir):
build_cmd += ['--reconfigure']
build_cmd += ["--reconfigure"]
run(build_cmd)
run(['ninja', '-C', build_dir])
run(['meson', f'--prefix={build_dir}', 'install'])
run(["ninja", "-C", build_dir])
run(["meson", f"--prefix={build_dir}", "install"])
7 changes: 3 additions & 4 deletions dev/cmds/_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@


@click.command()
@click.argument('pytest_args', nargs=-1)
@click.argument("pytest_args", nargs=-1)
def test(pytest_args):
"""🔧 Run tests

PYTEST_ARGS are passed through directly to pytest.
"""
if not pytest_args:
pytest_args = ('TODO__PROJECT_NAME_FROM_CONFIG',)
run(['pytest'] + list(pytest_args))

pytest_args = ("TODO__PROJECT_NAME_FROM_CONFIG",)
run(["pytest"] + list(pytest_args))
20 changes: 10 additions & 10 deletions dev/py.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,35 @@


if __name__ == "__main__":
if not os.path.exists('pyproject.toml'):
print('Error: cannot find [pyproject.toml]')
if not os.path.exists("pyproject.toml"):
print("Error: cannot find [pyproject.toml]")
sys.exit(1)

with open('pyproject.toml', 'r') as f:
with open("pyproject.toml") as f:
try:
toml_config = toml.load(f)
except:
print('Cannot parse [pyproject.toml]')
print("Cannot parse [pyproject.toml]")
sys.exit(1)

try:
project_config = toml_config['project']
config = toml_config['tool']['dev']['py']
project_config = toml_config["project"]
config = toml_config["tool"]["dev"]["py"]
except KeyError:
print('No configuration found in [pyproject.toml] for [tool.dev.py]')
print("No configuration found in [pyproject.toml] for [tool.dev.py]")
sys.exit(1)

commands = {
f'dev.py.{name}': getattr(cmds, name)
f"dev.py.{name}": getattr(cmds, name)
for name in dir(cmds)
if not name.startswith('_')
if not name.startswith("_")
}

@click.group(help=f"Developer tool for {project_config['name']}")
def group():
pass

for cmd in config['commands']:
for cmd in config["commands"]:
group.add_command(commands[cmd])

group()
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.1a1.dev0"
description = "Developer tool for scientific Python libraries"
license = {file = "LICENSE"}
maintainers = [
{name = "Scientific Python", email = "[email protected]"}
{name = "Scientific Python", email = "dev.py@discuss.scientific-python.org"}
]
classifiers = [
"Development Status :: 4 - Beta",
Expand All @@ -15,6 +15,9 @@ dependencies = [
"toml"
]

[project.optional-dependencies]
lint = ["pre-commit >= 2.20"]

[project.urls]
homepage = "https://github.com/scientific-python/dev.py"

Expand Down