Skip to content

Commit 009566a

Browse files
committed
lint: Start using ruff ruleset "flake8-bandit"
* Remove bandit * Add ruff ruleset "flake8-bandit" * verify_release is now checked by bandit * Avoid some asserts as suggested * ignore a subprocess.run lint: it seems dumb * ignore all bandit rules for tests and examples (just like before) Signed-off-by: Jussi Kukkonen <jkukkonen@google.com>
1 parent 52fa73a commit 009566a

5 files changed

Lines changed: 22 additions & 15 deletions

File tree

.github/dependabot.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ updates:
1515
test-and-lint-dependencies:
1616
# Python dependencies that are only pinned to ensure test reproducibility
1717
patterns:
18-
- "bandit"
1918
- "coverage"
2019
- "mypy"
2120
- "ruff"

pyproject.toml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,22 @@ select = [
8888
"I", # isort
8989
"N", # pep8-naming
9090
"PL", # pylint
91+
"S", # flake8-bandit
9192
]
9293
ignore = ["D400","D415","D213","D205","D202","D107","D407","D413","D212","D104","D406","D105","D411","D401","D200","D203", "PLR0913", "PLR2004"]
9394

9495
[tool.ruff.lint.per-file-ignores]
9596
"tests/*" = [
96-
"D", # pydocstyle: no docstrings required for tests
97-
"E501" # line-too-long: embedded test data in "fmt: off" blocks is ok
97+
"D", # pydocstyle: no docstrings required for tests
98+
"E501", # line-too-long: embedded test data in "fmt: off" blocks is ok
99+
"S", # bandit: Not running bandit on tests
98100
]
99101
"examples/*/*" = [
100-
"D", # pydocstyle: no docstrings required for examples
102+
"D", # pydocstyle: no docstrings required for examples
103+
"S" # bandit: Not running bandit on examples
104+
]
105+
"verify_release" = [
106+
"S603", # bandit: this flags all uses of subprocess.run as vulnerable
101107
]
102108

103109
# mypy section

requirements/lint.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,3 @@
88
# are pinned to prevent unexpected linting failures when tools update)
99
ruff==0.2.2
1010
mypy==1.8.0
11-
bandit==1.7.7

tox.ini

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ commands =
5252

5353
mypy {[testenv:lint]lint_dirs}
5454

55-
bandit -r tuf
56-
5755
[testenv:docs]
5856
deps =
5957
-r{toxinidir}/requirements/docs.txt

verify_release

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,21 @@ def build(build_dir: str) -> str:
5454
build_cmd, stdout=subprocess.DEVNULL, check=True, env=env
5555
)
5656

57-
build_version = None
5857
for filename in os.listdir(build_dir):
5958
prefix, postfix = f"{PYPI_PROJECT}-", ".tar.gz"
6059
if filename.startswith(prefix) and filename.endswith(postfix):
61-
build_version = filename[len(prefix) : -len(postfix)]
60+
return filename[len(prefix) : -len(postfix)]
6261

63-
assert build_version
64-
return build_version
62+
raise RuntimeError("Build version not found")
6563

6664

6765
def get_git_version() -> str:
6866
"""Return version string from git describe"""
6967
cmd = ["git", "describe"]
7068
process = subprocess.run(cmd, text=True, capture_output=True, check=True)
71-
assert process.stdout.startswith("v") and process.stdout.endswith("\n")
69+
if not process.stdout.startswith("v") or not process.stdout.endswith("\n"):
70+
raise RuntimeError(f"Unexpected git version {process.stdout}")
71+
7272
return process.stdout[1:-1]
7373

7474

@@ -93,7 +93,7 @@ def get_pypi_pip_version() -> str:
9393
prefix, postfix = f"{PYPI_PROJECT}-", ".tar.gz"
9494
if filename.startswith(prefix) and filename.endswith(postfix):
9595
return filename[len(prefix) : -len(postfix)]
96-
assert False
96+
raise RuntimeError("PyPI version not found")
9797

9898

9999
def verify_github_release(version: str, compare_dir: str) -> bool:
@@ -164,7 +164,9 @@ def sign_release_artifacts(
164164
subprocess.run(
165165
cmd + ["--output", signature_path, artifact_path], check=True
166166
)
167-
assert os.path.exists(signature_path)
167+
168+
if not os.path.exists(signature_path):
169+
raise RuntimeError("Signing failed, signature not found")
168170

169171

170172
def finished(s: str) -> None:
@@ -209,7 +211,10 @@ def main() -> int: # noqa: D103
209211
finished(f"Built release {build_version}")
210212

211213
git_version = get_git_version()
212-
assert git_version.startswith(build_version)
214+
if not git_version.startswith(build_version):
215+
raise RuntimeError(
216+
f"Git version is {git_version}, expected {build_version}"
217+
)
213218
if git_version != build_version:
214219
finished(f"WARNING: Git describes version as {git_version}")
215220

0 commit comments

Comments
 (0)