Debian Codegraph @

justfile

raw
1 set windows-shell := ["powershell.exe", "-NoLogo", "-Command"]
2 set unstable
3 set script-interpreter := ['uv', 'run', '--project', '.', '--script']
4
5 export PYTHONPATH := source_directory()
6
7 [private]
8 default:
9 @just --list --list-submodules
10
11 # run the django admin
12 [script]
13 manage *COMMAND:
14 import os
15 import sys
16 from django.core import management
17 sys.path.append(os.getcwd())
18 os.environ["DJANGO_SETTINGS_MODULE"] = "polymorphic.tests.debug"
19 os.environ["SQLITE_DATABASES"] = "test1.db,test2.db"
20 management.execute_from_command_line(sys.argv + "{{ COMMAND }}".split(" "))
21
22 # install the uv package manager
23 [linux]
24 [macos]
25 install_uv:
26 curl -LsSf https://astral.sh/uv/install.sh | sh
27
28 # install the uv package manager
29 [windows]
30 install_uv:
31 powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
32
33 # setup the venv, pre-commit hooks
34 setup python="python":
35 uv venv -p {{ python }}
36 @just install-precommit
37
38 # install git pre-commit hooks
39 install-precommit:
40 @just run --no-default-groups --group precommit --exact --isolated pre-commit install
41
42 # update and install development dependencies
43 install *OPTS:
44 @just install-precommit
45 uv sync {{ OPTS }}
46
47 # install playwright dependencies
48 install-playwright:
49 @just run --no-default-groups --group test playwright install chromium
50
51 # run static type checking with mypy
52 check-types-mypy *ENV:
53 @just run {{ ENV }} --no-default-groups --group integrations --group typing mypy src/polymorphic
54
55 # run static type checking with pyright
56 check-types-pyright *ENV:
57 @just run {{ ENV }} --no-default-groups --group integrations --group typing pyright src/polymorphic
58 @just run {{ ENV }} --no-default-groups --group integrations --group typing pyright --project src/polymorphic/tests/examples/type_hints/pyright.json
59
60 # run all static type checking
61 check-types *ENV:
62 @just check-types-mypy {{ ENV }}
63 @just check-types-pyright {{ ENV }}
64
65 # run all static type checking in an isolated environment
66 check-types-isolated *ENV:
67 @just check-types-mypy {{ ENV }} --exact --isolated
68 @just check-types-pyright {{ ENV }} --exact --isolated
69
70 # run package checks
71 check-package:
72 uv pip check
73
74 # remove doc build artifacts-
75 [script]
76 clean-docs:
77 import shutil
78 shutil.rmtree('./docs/_build', ignore_errors=True)
79
80 # remove the virtual environment
81 [script]
82 clean-env:
83 import shutil
84 import sys
85 shutil.rmtree(".venv", ignore_errors=True)
86
87 # remove all git ignored files
88 clean-git-ignored:
89 git clean -fdX
90
91 # remove all non repository artifacts
92 clean: clean-docs clean-env clean-git-ignored
93
94 # build html documentation
95 build-docs-html:
96 @just run --group docs --group integrations sphinx-build --fresh-env --builder html --doctree-dir ./docs/_build/doctrees ./docs/ ./docs/_build/html
97
98 [script]
99 _open-pdf-docs:
100 import webbrowser
101 from pathlib import Path
102 webbrowser.open(f"file://{Path('./docs/_build/pdf/django-polymorphic.pdf').absolute()}")
103
104 # build pdf documentation
105 build-docs-pdf:
106 @just run --group docs --group integrations sphinx-build --fresh-env --builder latex --doctree-dir ./docs/_build/doctrees ./docs/ ./docs/_build/pdf
107 make -C ./docs/_build/pdf
108 @just _open-pdf-docs
109
110 # build the docs
111 build-docs: build-docs-html
112
113 # build docs and package
114 build: build-docs-html
115 @just manage compilemessages --ignore ".venv/*"
116 uv build
117
118 # regenerate test migrations using the lowest version of Django
119 remake-test-migrations:
120 - rm src/polymorphic/tests/migrations/00*.py
121 - rm src/polymorphic/tests/deletion/migrations/00*.py
122 - rm src/polymorphic/tests/other/migrations/00*.py
123 - rm src/polymorphic/tests/examples/**/migrations/00*.py
124 - rm src/polymorphic/tests/examples/integrations/**/migrations/00*.py
125 - rm src/polymorphic/tests/examples/type_hints/**/migrations/00*.py
126 uv run --no-default-groups --exact --isolated --resolution lowest-direct --group integrations --script ./manage.py makemigrations
127
128 # open the html documentation
129 [script]
130 open-docs:
131 import os
132 import webbrowser
133 webbrowser.open(f'file://{os.getcwd()}/docs/_build/html/index.html')
134
135 # build and open the documentation
136 docs: build-docs-html open-docs
137
138 # serve the documentation, with auto-reload
139 docs-live:
140 @just run --no-default-groups --group docs --all-extras --group integrations --exact --isolated sphinx-autobuild docs docs/_build --open-browser --watch src --port 0 --delay 1
141
142 _link_check:
143 -uv run --no-default-groups --group docs sphinx-build -b linkcheck -Q -D linkcheck_timeout=10 ./docs/ ./docs/_build
144
145 # check the documentation links for broken links
146 [script]
147 check-docs-links: _link_check
148 import os
149 import sys
150 import json
151 from pathlib import Path
152 # The json output isn't valid, so we have to fix it before we can process.
153 data = json.loads(f"[{','.join((Path(os.getcwd()) / 'docs/_build/output.json').read_text().splitlines())}]")
154 broken_links = [link for link in data if link["status"] not in {"working", "redirected", "unchecked", "ignored"}]
155 if broken_links:
156 for link in broken_links:
157 print(f"[{link['status']}] {link['filename']}:{link['lineno']} -> {link['uri']}", file=sys.stderr)
158 sys.exit(1)
159
160 # lint the documentation
161 check-docs *ENV:
162 @just run {{ ENV }} --no-default-groups --group lint doc8 --ignore-path ./docs/_build --max-line-length 100 -q ./docs
163
164 # lint the code
165 check-lint *ENV:
166 @just run {{ ENV }} --no-default-groups --group lint ruff check --select I
167 @just run {{ ENV }} --no-default-groups --group lint ruff check
168
169 # check if the code needs formatting
170 check-format *ENV:
171 @just run {{ ENV }} --no-default-groups --group lint ruff format --check
172 @just run {{ ENV }} --no-default-groups --group lint ruff format --line-length 80 --check src/polymorphic/tests/examples
173
174 # check that the readme renders
175 check-readme *ENV:
176 @just run {{ ENV }} --no-default-groups --group lint -m readme_renderer ./README.md -o /tmp/README.html
177
178 _check-readme-quiet *ENV:
179 @just --quiet check-readme {{ ENV }}
180
181 # sort the python imports
182 sort-imports:
183 @just run --no-default-groups --group lint ruff check --fix --select I
184
185 # format the code and sort imports
186 format: sort-imports
187 just --fmt --unstable
188 @just run --no-default-groups --group lint ruff format
189 @just run --no-default-groups --group lint ruff format --line-length 80 src/polymorphic/tests/examples
190
191 # format the github workflow files
192 format-workflows:
193 npx prettier --write ".github/workflows/*.{yml,yaml}"
194
195 # sort the imports and fix linting issues
196 lint: sort-imports
197 @just run --no-default-groups --group lint ruff check --fix
198
199 # fix formatting, linting issues and import sorting
200 fix: lint format
201
202 # run bandit static security analysis
203 bandit:
204 @just run --no-default-groups --group lint bandit -c pyproject.toml -r ./src -f sarif -o bandit.sarif
205
206 # run all static checks
207 check *ENV:
208 @just check-lint {{ ENV }}
209 @just check-format {{ ENV }}
210 @just check-types {{ ENV }}
211 @just check-docs {{ ENV }}
212 @just check-readme {{ ENV }}
213 @just check-package
214
215 # run all checks including documentation link checking (slow)
216 check-all *ENV:
217 @just check {{ ENV }}
218 @just check-docs-links
219
220 # run zizmor security analysis of CI
221 zizmor:
222 cargo install --locked zizmor
223 zizmor --persona auditor --format sarif .github/workflows/ > zizmor.sarif
224
225 # run tests
226 test *TESTS:
227 @just run --group test pytest {{ TESTS }} --cov
228
229 # run all tests including integrations (accepts uv run flags e.g. -p /path/to/python --group dj52)
230 test-all *ENV:
231 @just run {{ ENV }} --no-default-groups --exact --group test --isolated pytest --cov
232 @just run {{ ENV }} --no-default-groups --group integrations --group guardian --group reversion --group test --exact --isolated pytest -m integration --cov --cov-append
233
234 # test django-revision integration
235 test-reversion *TESTS:
236 @just run --no-default-groups --group reversion --group test --exact --isolated pytest -m integration src/polymorphic/tests/examples/integrations/reversion {{ TESTS }}
237
238 # test django extra views integration
239 test-extra-views *TESTS:
240 @just run --no-default-groups --group extra-views --group test --exact --isolated pytest -m integration src/polymorphic/tests/examples/integrations/extra_views {{ TESTS }}
241
242 # test django rest framework integration
243 test-drf *TESTS:
244 @just run --no-default-groups --group drf --group test --exact --isolated pytest -m integration src/polymorphic/tests/examples/integrations/drf {{ TESTS }}
245
246 # test guardian integration
247 test-guardian *TESTS:
248 @just run --no-default-groups --group guardian --group test --exact --isolated pytest -m integration src/polymorphic/tests/examples/integrations/guardian {{ TESTS }}
249
250 # run all third party integration tests
251 test-integrations:
252 @just run --no-default-groups --group integrations --group guardian --group reversion --group test --exact --isolated pytest -m integration --cov --cov-append
253
254 # debug an test
255 debug-test *TESTS:
256 @just run pytest \
257 -o addopts='-ra -q' \
258 -s --trace --pdbcls=IPython.terminal.debugger:Pdb \
259 --headed {{ TESTS }}
260
261 # run the pre-commit checks
262 precommit:
263 @just run pre-commit
264
265 # generate the test coverage report
266 coverage:
267 @just run --no-default-groups --group coverage coverage combine --keep *.coverage
268 @just run --no-default-groups --group coverage coverage report
269 @just run --no-default-groups --group coverage coverage xml
270
271 _install-docs:
272 uv sync --no-default-groups --group docs --all-extras
273
274 # get the intersphinx references for the given library
275 [script]
276 fetch-refs LIB: _install-docs
277 import os
278 from pathlib import Path
279 import logging as _logging
280 import sys
281 import runpy
282 from sphinx.ext.intersphinx import inspect_main
283 _logging.basicConfig()
284
285 libs = runpy.run_path(Path(os.getcwd()) / "docs/conf.py").get("intersphinx_mapping")
286 url = libs.get("{{ LIB }}", None)
287 if not url:
288 sys.exit(f"Unrecognized {{ LIB }}, must be one of: {', '.join(libs.keys())}")
289 if url[1] is None:
290 url = f"{url[0].rstrip('/')}/objects.inv"
291 else:
292 url = url[1]
293
294 raise SystemExit(inspect_main([url]))
295
296 # run the command in the virtual environment
297 run +ARGS:
298 uv run {{ ARGS }}
299
300 # validate the given version string against the lib version
301 [script]
302 validate_version VERSION:
303 import re
304 import tomllib
305 import polymorphic
306 from packaging.version import Version
307 raw_version = "{{ VERSION }}".lstrip("v")
308 version_obj = Version(raw_version)
309 # the version should be normalized
310 assert str(version_obj) == raw_version
311 # make sure all places the version appears agree
312 assert raw_version == tomllib.load(open('pyproject.toml', 'rb'))['project']['version']
313 assert raw_version == polymorphic.__version__
314 print(raw_version)
315
316 # issue a release for the given semver string (e.g. 2.1.0)
317 release VERSION: install check-all
318 @just validate_version v{{ VERSION }}
319 git tag -s v{{ VERSION }} -m "{{ VERSION }} Release"
320 git push upstream v{{ VERSION }}