tomte 0.7.2__tar.gz → 0.7.4__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (30) hide show
  1. {tomte-0.7.2 → tomte-0.7.4}/PKG-INFO +20 -2
  2. {tomte-0.7.2 → tomte-0.7.4}/README.md +18 -1
  3. {tomte-0.7.2 → tomte-0.7.4}/pyproject.toml +1 -1
  4. tomte-0.7.4/tomte/__init__.py +1 -0
  5. {tomte-0.7.2 → tomte-0.7.4}/tomte/cli.py +10 -0
  6. tomte-0.7.4/tomte/configs/safety-policy.yml +25 -0
  7. {tomte-0.7.2 → tomte-0.7.4}/tomte/configs/tox.ini +8 -2
  8. tomte-0.7.4/tomte/tools/export_safety_requirements.py +60 -0
  9. {tomte-0.7.2 → tomte-0.7.4}/tomte/tools/tox_runtime.py +76 -0
  10. tomte-0.7.2/tomte/__init__.py +0 -1
  11. tomte-0.7.2/tomte/configs/safety-policy.yml +0 -12
  12. {tomte-0.7.2 → tomte-0.7.4}/LICENSE +0 -0
  13. {tomte-0.7.2 → tomte-0.7.4}/tomte/configs/__init__.py +0 -0
  14. {tomte-0.7.2 → tomte-0.7.4}/tomte/configs/bandit.yaml +0 -0
  15. {tomte-0.7.2 → tomte-0.7.4}/tomte/configs/darglint.cfg +0 -0
  16. {tomte-0.7.2 → tomte-0.7.4}/tomte/configs/flake8.cfg +0 -0
  17. {tomte-0.7.2 → tomte-0.7.4}/tomte/configs/gitleaks.toml +0 -0
  18. {tomte-0.7.2 → tomte-0.7.4}/tomte/configs/isort.cfg +0 -0
  19. {tomte-0.7.2 → tomte-0.7.4}/tomte/configs/mypy.ini +0 -0
  20. {tomte-0.7.2 → tomte-0.7.4}/tomte/configs/pylintrc +0 -0
  21. {tomte-0.7.2 → tomte-0.7.4}/tomte/pytest_plugin.py +0 -0
  22. {tomte-0.7.2 → tomte-0.7.4}/tomte/tools/__init__.py +0 -0
  23. {tomte-0.7.2 → tomte-0.7.4}/tomte/tools/check_copyright.py +0 -0
  24. {tomte-0.7.2 → tomte-0.7.4}/tomte/tools/check_doc_links.py +0 -0
  25. {tomte-0.7.2 → tomte-0.7.4}/tomte/tools/check_readme.py +0 -0
  26. {tomte-0.7.2 → tomte-0.7.4}/tomte/tools/freeze_dependencies.py +0 -0
  27. {tomte-0.7.2 → tomte-0.7.4}/tomte/tools/freeze_gitleaks.py +0 -0
  28. {tomte-0.7.2 → tomte-0.7.4}/tomte/tools/packages_json.py +0 -0
  29. {tomte-0.7.2 → tomte-0.7.4}/tomte/tools/render_isort_config.py +0 -0
  30. {tomte-0.7.2 → tomte-0.7.4}/tomte/tools/render_mypy_config.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: tomte
3
- Version: 0.7.2
3
+ Version: 0.7.4
4
4
  Summary: A library that wraps many useful tools (linters, analysers, etc) to keep Python code clean, secure, well-documented and optimised.
5
5
  License: MIT
6
6
  License-File: LICENSE
@@ -14,6 +14,7 @@ Classifier: Programming Language :: Python :: 3.11
14
14
  Classifier: Programming Language :: Python :: 3.12
15
15
  Classifier: Programming Language :: Python :: 3.13
16
16
  Classifier: Programming Language :: Python :: 3.14
17
+ Classifier: Programming Language :: Python :: 3.15
17
18
  Provides-Extra: bandit
18
19
  Provides-Extra: black
19
20
  Provides-Extra: cli
@@ -123,7 +124,24 @@ drop-in replacement: `tomte tox -e <env>`.
123
124
  `known_first_party`, `open_autonomy_version`, `open_aea_version`,
124
125
  `check_handlers_ignores`, `check_dependencies_extra_excludes`,
125
126
  `upstream_pins`, `gitleaks_extra_paths`, `gitleaks_extra_regexes`,
126
- `tomte_dep_pin`. Most defaults auto-derive from `packages.json`.
127
+ `safety_ignores`, `safety_scan_lock`, `tomte_dep_pin`. Most defaults
128
+ auto-derive from `packages.json`.
129
+
130
+ By default the `safety` env scans only safety's own virtualenv, which
131
+ says nothing about the repo. Set `safety_scan_lock = true` to scan the
132
+ repo's locked dependencies instead: the env runs
133
+ `tomte export-safety-requirements`, which pins every released package in
134
+ `uv.lock` (or, failing that, `poetry.lock`), and the scan reads only that
135
+ export. The fleet baseline is not applied then, since it covers safety's
136
+ own dependencies. A missing or unreadable lock fails the env. A package
137
+ locked from git or a URL is checked as the PyPI release of its version,
138
+ not as its source, and the export names each one.
139
+
140
+ `safety_ignores` takes an advisory id or a list of them and renders them
141
+ as `--ignore` flags on the `safety` scan, after the fleet baseline in
142
+ `_FLEET_SAFETY_IGNORES`. Add an entry only when the vulnerable code is
143
+ unreachable from the repo, and record why alongside it. An unavailable
144
+ fix is a reason an entry cannot be retired yet, not a reason to add one.
127
145
 
128
146
  `service_public_id` takes a single public id or a list of them, and
129
147
  `[testenv:analyse-service]` runs `autonomy analyse service` once per
@@ -55,7 +55,24 @@ drop-in replacement: `tomte tox -e <env>`.
55
55
  `known_first_party`, `open_autonomy_version`, `open_aea_version`,
56
56
  `check_handlers_ignores`, `check_dependencies_extra_excludes`,
57
57
  `upstream_pins`, `gitleaks_extra_paths`, `gitleaks_extra_regexes`,
58
- `tomte_dep_pin`. Most defaults auto-derive from `packages.json`.
58
+ `safety_ignores`, `safety_scan_lock`, `tomte_dep_pin`. Most defaults
59
+ auto-derive from `packages.json`.
60
+
61
+ By default the `safety` env scans only safety's own virtualenv, which
62
+ says nothing about the repo. Set `safety_scan_lock = true` to scan the
63
+ repo's locked dependencies instead: the env runs
64
+ `tomte export-safety-requirements`, which pins every released package in
65
+ `uv.lock` (or, failing that, `poetry.lock`), and the scan reads only that
66
+ export. The fleet baseline is not applied then, since it covers safety's
67
+ own dependencies. A missing or unreadable lock fails the env. A package
68
+ locked from git or a URL is checked as the PyPI release of its version,
69
+ not as its source, and the export names each one.
70
+
71
+ `safety_ignores` takes an advisory id or a list of them and renders them
72
+ as `--ignore` flags on the `safety` scan, after the fleet baseline in
73
+ `_FLEET_SAFETY_IGNORES`. Add an entry only when the vulnerable code is
74
+ unreachable from the repo, and record why alongside it. An unavailable
75
+ fix is a reason an entry cannot be retired yet, not a reason to add one.
59
76
 
60
77
  `service_public_id` takes a single public id or a list of them, and
61
78
  `[testenv:analyse-service]` runs `autonomy analyse service` once per
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "tomte"
3
- version = "0.7.2"
3
+ version = "0.7.4"
4
4
  description = "A library that wraps many useful tools (linters, analysers, etc) to keep Python code clean, secure, well-documented and optimised."
5
5
  authors = ["David Minarsch <david.minarsch@googlemail.com>"]
6
6
  license = "MIT"
@@ -0,0 +1 @@
1
+ __version__ = "0.7.4"
@@ -7,6 +7,7 @@ from tomte import __version__
7
7
  from tomte.tools.check_copyright import main as check_copyright_main
8
8
  from tomte.tools.check_doc_links import main as check_doc_links_main
9
9
  from tomte.tools.check_readme import main as check_readme_main
10
+ from tomte.tools.export_safety_requirements import main as export_safety_requirements_main
10
11
  from tomte.tools.freeze_dependencies import main as freeze_dependencies_main
11
12
  from tomte.tools.freeze_gitleaks import main as freeze_gitleaks_main
12
13
  from tomte.tools.render_isort_config import main as render_isort_config_main
@@ -127,6 +128,14 @@ def freeze_dependencies(output_path: Optional[str], exclude_package: Tuple[str,
127
128
  freeze_dependencies_main(output_path=output_path, exclude_packages=exclude_package)
128
129
 
129
130
 
131
+ @click.command(name="export-safety-requirements")
132
+ @click.option("--output-path", required=True, type=click.Path(dir_okay=False, path_type=Path))
133
+ @click.option("--repo-root", default=".", type=click.Path(file_okay=False, path_type=Path))
134
+ def export_safety_requirements(output_path: Path, repo_root: Path) -> None:
135
+ """Pin the released packages in the repo's uv.lock or poetry.lock for `safety check -r`."""
136
+ export_safety_requirements_main(output_path=output_path, repo_root=repo_root)
137
+
138
+
130
139
  @click.command(name="render-isort-config")
131
140
  @click.option("--output", required=True, type=click.Path(), help="Where to write the merged isort.cfg.")
132
141
  @click.option("--known-first-party", default="", help="Per-repo `known_first_party` value.")
@@ -169,6 +178,7 @@ cli.add_command(check_security)
169
178
  cli.add_command(render_isort_config)
170
179
  cli.add_command(render_mypy_config)
171
180
  cli.add_command(freeze_gitleaks)
181
+ cli.add_command(export_safety_requirements)
172
182
  cli.add_command(tomte_tox)
173
183
 
174
184
 
@@ -0,0 +1,25 @@
1
+ # Canonical safety policy shipped by tomte.
2
+ #
3
+ # Repos consume this via:
4
+ # safety check --policy-file $(python -c "from tomte.configs import SAFETY_POLICY; print(SAFETY_POLICY)")
5
+ #
6
+ # `ignore-vulnerabilities` below is inert, and adding entries to it does
7
+ # nothing, for two reasons:
8
+ #
9
+ # * safety's `check` validates its keys as positive integers and rejects the
10
+ # `SFTY-<date>-<n>` form advisories are published under
11
+ # * safety reads it only when no `--ignore` flag is given, and `tomte tox`
12
+ # always passes the fleet baseline as flags
13
+ #
14
+ # Ignores are therefore `--ignore` flags rendered by `tomte tox`: the fleet
15
+ # baseline in `_FLEET_SAFETY_IGNORES` (tomte/tools/tox_runtime.py) plus a
16
+ # repo's own `safety_ignores` in `[tool.tomte]`.
17
+ #
18
+ # Coverage: by default the scan inspects safety's own virtualenv, not the
19
+ # consuming repo. With `safety_scan_lock = true` in `[tool.tomte]`, the env
20
+ # exports the repo's uv.lock (or poetry.lock) with
21
+ # `tomte export-safety-requirements` and scans only that export.
22
+
23
+ security:
24
+ ignore-vulnerabilities: {}
25
+ continue-on-vulnerability-error: false
@@ -25,6 +25,11 @@
25
25
  ; (e.g. "--ignored-modules=web3 --disable=C0111"),
26
26
  ; or empty when no extras declared
27
27
  ; $$CHECK_HANDLERS_IGNORES check-handlers `-i pkg` ignore flags
28
+ ; $$SAFETY_IGNORES safety `--ignore <id>` flags: the fleet
29
+ ; baseline plus [tool.tomte].safety_ignores
30
+ ; $$SAFETY_EXTRAS with [tool.tomte].safety_scan_lock: `,cli`,
31
+ ; $$SAFETY_EXPORT the command exporting the repo's lock,
32
+ ; $$SAFETY_REQUIREMENTS and `-r <export>`; each empty otherwise
28
33
  ; $$TOMTE_DEP_PIN PEP 508 spec right of `tomte[<extras>]`,
29
34
  ; typically `==0.7.0` or ` @ git+...`
30
35
  ;
@@ -200,9 +205,10 @@ skip_install = True
200
205
  ; httpx 0.28 dropped `NetworkError`; safety 3.7.0's event-handler imports it.
201
206
  deps =
202
207
  httpx<0.28
203
- tomte[safety]$TOMTE_DEP_PIN
208
+ tomte[safety$SAFETY_EXTRAS]$TOMTE_DEP_PIN
209
+ commands_pre = $SAFETY_EXPORT
204
210
  commands =
205
- safety check --policy-file {envsitepackagesdir}/tomte/configs/safety-policy.yml
211
+ safety check $SAFETY_REQUIREMENTS --policy-file {envsitepackagesdir}/tomte/configs/safety-policy.yml $SAFETY_IGNORES
206
212
 
207
213
  ; -- autonomy / aea checks -----------------------------------------------
208
214
 
@@ -0,0 +1,60 @@
1
+ """Pin the released packages in a repo's lock for `safety check -r`."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from pathlib import Path
6
+ from typing import List, Set, Tuple
7
+
8
+ import click
9
+
10
+ try:
11
+ import tomllib
12
+ except ImportError: # pragma: no cover
13
+ import tomli as tomllib # type: ignore[no-redef]
14
+
15
+
16
+ LOCKFILES = ("uv.lock", "poetry.lock")
17
+ # Lock sources (uv keys, poetry `type`s) that are local code, not a release.
18
+ _LOCAL_SOURCES = {"editable", "virtual", "directory", "path", "file"}
19
+ # Pinned like any release, so safety checks that version's PyPI release, not this source.
20
+ _UNPUBLISHED_SOURCES = {"git", "url"}
21
+
22
+
23
+ def export(repo_root: Path) -> Tuple[str, List[str]]:
24
+ """Return the pins, and the names of packages locked from git or a URL."""
25
+ lock_path = next(
26
+ (repo_root / name for name in LOCKFILES if (repo_root / name).is_file()),
27
+ None,
28
+ )
29
+ if lock_path is None:
30
+ raise click.ClickException(
31
+ f"No {' or '.join(LOCKFILES)} in {repo_root}; nothing to scan."
32
+ )
33
+ try:
34
+ with lock_path.open("rb") as fh:
35
+ lock = tomllib.load(fh)
36
+ except (OSError, tomllib.TOMLDecodeError) as exc:
37
+ raise click.ClickException(f"Cannot read {lock_path}: {exc}") from exc
38
+ pins: Set[str] = set()
39
+ unpublished: Set[str] = set()
40
+ for package in lock.get("package", []):
41
+ source = package.get("source", {})
42
+ kinds = {*source, source.get("type")}
43
+ if _LOCAL_SOURCES & kinds or "name" not in package or "version" not in package:
44
+ continue
45
+ pins.add(f"{package['name']}=={package['version']}")
46
+ if _UNPUBLISHED_SOURCES & kinds:
47
+ unpublished.add(package["name"])
48
+ return "".join(f"{pin}\n" for pin in sorted(pins)), sorted(unpublished)
49
+
50
+
51
+ def main(output_path: Path, repo_root: Path) -> None:
52
+ """Write the pins to `output_path`, and name what they cannot cover."""
53
+ pins, unpublished = export(repo_root)
54
+ output_path.write_text(pins, encoding="utf-8")
55
+ if unpublished:
56
+ click.echo(
57
+ "warning: locked from git or a URL, so safety checks the PyPI "
58
+ f"release of the same version instead: {', '.join(unpublished)}",
59
+ err=True,
60
+ )
@@ -19,6 +19,7 @@ import re
19
19
  import shutil
20
20
  import subprocess
21
21
  import sys
22
+ from datetime import date
22
23
  from pathlib import Path
23
24
  from string import Template
24
25
  from typing import Any, Dict, List, Optional, Sequence, Set, Tuple, Union
@@ -45,6 +46,28 @@ from tomte.tools.packages_json import (
45
46
  # moved here because they never vary across consumers downstream of OA.
46
47
  _KNOWN_FIRST_PARTY = "autonomy"
47
48
 
49
+ # Advisories in the safety scanner's own dependency tree, passed as `--ignore`
50
+ # flags to the default scan of its venv, each mapped to the date its entry
51
+ # must be re-justified by. Not applied with `safety_scan_lock`, which does not
52
+ # scan that venv. The rule for adding an entry is in the README.
53
+ #
54
+ # safety looks ids up verbatim and says nothing when one matches no advisory,
55
+ # so a mistyped id is a silent no-op and the scan fails as if it were absent.
56
+ _FLEET_SAFETY_IGNORES: Dict[str, date] = {
57
+ # nltk, required by safety.
58
+ "SFTY-20260902-58666": date(2027, 1, 5),
59
+ # filelock, required by safety (`<4.0`). safety locks with `FileLock`, which
60
+ # is `UnixFileLock` on Linux; these cover `WindowsFileLock` (72390) and
61
+ # `StrictSoftFileLock` (54654, 57659). Fixed releases need Python 3.11+.
62
+ "SFTY-20261017-72390": date(2027, 2, 17),
63
+ "SFTY-20261017-54654": date(2027, 2, 17),
64
+ "SFTY-20261017-57659": date(2027, 2, 17),
65
+ }
66
+
67
+ # What an advisory id may contain. Ids are rendered into a tox command line,
68
+ # where whitespace starts a new argument and braces are substitution syntax.
69
+ _SAFETY_ID_PATTERN = re.compile(r"^[A-Za-z0-9_-]+$")
70
+
48
71
  # Multi-line continuation values need re-indenting after configparser strip.
49
72
  _DEPS_INDENT = 4
50
73
 
@@ -250,6 +273,57 @@ def _render_pylint_flags(extensions: Dict[str, str], identity: Dict[str, Any]) -
250
273
  return " ".join(flags)
251
274
 
252
275
 
276
+ def _resolve_safety_scan_lock(identity: Dict[str, Any]) -> bool:
277
+ explicit = identity.get("safety_scan_lock", False)
278
+ if not isinstance(explicit, bool):
279
+ raise click.UsageError(
280
+ f"[tool.tomte] safety_scan_lock must be true or false, "
281
+ f"got {type(explicit).__name__}."
282
+ )
283
+ return explicit
284
+
285
+
286
+ def _resolve_safety_lock_scan(identity: Dict[str, Any]) -> Dict[str, str]:
287
+ if not _resolve_safety_scan_lock(identity):
288
+ return {"SAFETY_EXTRAS": "", "SAFETY_EXPORT": "", "SAFETY_REQUIREMENTS": ""}
289
+ requirements = "{envtmpdir}/safety-requirements.txt"
290
+ return {
291
+ # Kept out of the default scan, which would report the CLI's deps.
292
+ "SAFETY_EXTRAS": ",cli",
293
+ "SAFETY_EXPORT": f"tomte export-safety-requirements --output-path {requirements}",
294
+ "SAFETY_REQUIREMENTS": f"-r {requirements}",
295
+ }
296
+
297
+
298
+ def _resolve_safety_ignores(identity: Dict[str, Any]) -> str:
299
+ """`--ignore <id>` flags: the fleet baseline, then `safety_ignores`."""
300
+ explicit = identity.get("safety_ignores")
301
+ if explicit is None:
302
+ declared: List[Any] = []
303
+ elif isinstance(explicit, str):
304
+ declared = [explicit]
305
+ elif isinstance(explicit, list):
306
+ declared = explicit
307
+ else:
308
+ raise click.UsageError(
309
+ f"[tool.tomte] safety_ignores must be a string or a list of "
310
+ f"strings, got {type(explicit).__name__}."
311
+ )
312
+ ids: List[str] = (
313
+ [] if _resolve_safety_scan_lock(identity) else list(_FLEET_SAFETY_IGNORES)
314
+ )
315
+ for index, entry in enumerate(declared):
316
+ advisory = entry.strip() if isinstance(entry, str) else ""
317
+ if not _SAFETY_ID_PATTERN.match(advisory):
318
+ raise click.UsageError(
319
+ f"[tool.tomte] safety_ignores entry {index} is not an advisory "
320
+ f"id ({entry!r}); expected letters, digits, `-` and `_` only."
321
+ )
322
+ if advisory not in ids:
323
+ ids.append(advisory)
324
+ return " ".join(f"--ignore {advisory}" for advisory in ids)
325
+
326
+
253
327
  def _join_listish(value: Union[str, Sequence[str]]) -> str:
254
328
  if isinstance(value, str):
255
329
  return value
@@ -693,6 +767,8 @@ def _build_substitutions(
693
767
  "CHECK_DEPENDENCIES_EXTRA_EXCLUDES": _resolve_check_dependencies_extra_excludes(
694
768
  identity
695
769
  ),
770
+ "SAFETY_IGNORES": _resolve_safety_ignores(identity),
771
+ **_resolve_safety_lock_scan(identity),
696
772
  "EXTRA_DEPS_PACKAGES": _reindent(
697
773
  _resolve_extra_deps(pyproject, extensions), _DEPS_INDENT
698
774
  ),
@@ -1 +0,0 @@
1
- __version__ = "0.7.2"
@@ -1,12 +0,0 @@
1
- # Canonical safety policy shipped by tomte.
2
- #
3
- # Repos consume this via:
4
- # safety check --policy-file $(python -c "from tomte.configs import SAFETY_POLICY; print(SAFETY_POLICY)")
5
- #
6
- # Vulnerability allowlist — repos extend with their own ignores by
7
- # composing a per-repo policy file when needed. The fleet's baseline is
8
- # "fail on any unfixed CVE", so this list stays empty by default.
9
-
10
- security:
11
- ignore-vulnerabilities: {}
12
- continue-on-vulnerability-error: false
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes