-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathduties.py
More file actions
168 lines (122 loc) · 4.92 KB
/
duties.py
File metadata and controls
168 lines (122 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
"""Inspired from griffe-pydantic."""
import os
import shutil
from contextlib import contextmanager
from pathlib import Path
from typing import Iterator
from duty import context, duty
PYTHON_VERSIONS = os.getenv('PYTHON_VERSIONS', '3.9 3.10 3.11 3.12 3.13').split()
SRC = ('.',)
SEP = os.sep
DOCS_LANGS = ('az',)
if os.name == 'nt':
import sys
sys.stdin.reconfigure(encoding='utf-8') # type: ignore[union-attr]
sys.stdout.reconfigure(encoding='utf-8') # type: ignore[union-attr]
@contextmanager
def environ(**kwargs: str) -> Iterator[None]:
"""Temporarily set environment variables."""
original = dict(os.environ)
os.environ.update(kwargs)
try:
yield
finally:
os.environ.clear()
os.environ.update(original)
##################################################################################################
@duty
def setup(ctx: context.Context) -> None:
"""Setup the project."""
if not shutil.which('uv'):
raise ValueError('make: setup: uv must be installed, see https://github.com/astral-sh/uv')
if not shutil.which('pre-commit'):
raise ValueError('make: setup: pre-commit must be installed, see https://pre-commit.com/')
ctx.run('pre-commit install')
print('Installing dependencies (default environment)') # noqa: T201
default_venv = Path('.venv')
if not default_venv.exists():
ctx.run('uv venv')
with environ(UV_PROJECT_ENVIRONMENT=str(default_venv.resolve())):
ctx.run('uv sync')
if PYTHON_VERSIONS:
for ver in PYTHON_VERSIONS:
print(f'\nInstalling dependencies (python{ver})') # noqa: T201
venv_path = Path(f'.venvs{SEP}{ver}')
if not venv_path.exists():
ctx.run(f'uv venv --python {ver} {venv_path}')
with environ(UV_PROJECT_ENVIRONMENT=str(venv_path.resolve())):
ctx.run('uv sync')
@duty
def format(ctx: context.Context, *files): # pylint: disable=redefined-builtin
"""Format the files."""
arg = ' '.join(files if files else SRC)
ctx.run(f'ruff check {arg} --fix-only --exit-zero', title='Auto-fixing code')
ctx.run(f'ruff format {arg}', title='Formatting code')
@duty
def lint(ctx: context.Context, *files):
"""Lint the files."""
arg = ' '.join(files if files else SRC)
ctx.run(f'ruff check {arg}', title='Linting with ruff check')
ctx.run(f'ruff format {arg} --check', title='Linting with ruff format')
ctx.run(f'pylint {arg}', title='Linting with pylint')
@duty
def type_check(ctx: context.Context, *files):
"""Type check the files."""
arg = ' '.join(files if files else SRC)
ctx.run(f'mypy {arg}', title='Type checking with mypy')
@duty
def test(ctx: context.Context, live: bool = False):
"""Run tests with local environment"""
for ver in PYTHON_VERSIONS:
venv_path = Path(f'.venvs{SEP}{ver}')
with environ(VIRTUAL_ENV=str(venv_path)):
ctx.run(
'uv run --active --no-sync coverage run --data-file=coverage/.coverage.py'
+ ver
+ ' -m pytest -sv --durations=10 '
+ ('--live' if live else ''),
title=f'Running tests (python {ver})',
)
@duty
def coverage(ctx: context.Context, title: str = ''):
"""Generate coverage report"""
ctx.run('coverage combine coverage', title='Combining coverage')
ctx.run('coverage report', title='Generating coverage report', capture=False)
ctx.run(
f'coverage html --title="Coverage report for {title}"',
title='Generating coverage HTML report',
)
@duty
def docs(ctx: context.Context):
"""Build the documentation."""
for lang in DOCS_LANGS:
ctx.run(
f'mkdocs build -f docs/{lang}/mkdocs.yml --strict',
title=f'Building documentation ({lang})',
)
@duty
def docs_serve(ctx: context.Context, lang='az'):
"""Serve the documentation."""
ctx.run(
f'mkdocs serve -f docs/{lang}/mkdocs.yml',
title=f'Serving documentation ({lang})',
)
@duty
def secure(ctx: context.Context):
"""Run security checks with bandit."""
ctx.run('bandit -r src/integrify --config pyproject.toml', title='Running bandit')
@duty(pre=['format', 'lint', 'test', 'docs'])
def all(): # pylint: disable=redefined-builtin
"""Run all main tasks: format, lint, test, docs."""
@duty
def clean(ctx: context.Context): # pylint: disable=unused-argument
"""Delete build artifacts and cache files."""
print('Cleaning...') # noqa: T201
paths_to_clean = ['htmlcov', 'coverage']
for path in paths_to_clean:
shutil.rmtree(path, ignore_errors=True)
cache_dirs = {'site', '.cache', '.pytest_cache', '.mypy_cache', '.ruff_cache', '__pycache__'}
for dirpath in Path('.').rglob('*/'):
if dirpath.parts[0] not in ('.venv', '.venvs') and dirpath.name in cache_dirs:
shutil.rmtree(dirpath, ignore_errors=True)
print('Done.') # noqa: T201