Skip to content

Commit b5a85f6

Browse files
Fix bandit high-severity warnings and use pre-commit (#7913)
Remove `shell=True` argument of `subprocess.call`. Refactor: Use `subprocess.run` instead of `subprocess.call`. Add `bandit` to .pre-commit-config.yaml. Use `gitpython` for clone command. Co-authored-by: Pierre Sassoulas <[email protected]>
1 parent 4827cfa commit b5a85f6

File tree

5 files changed

+15
-12
lines changed

5 files changed

+15
-12
lines changed

.pre-commit-config.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,9 @@ repos:
146146
pylint/__pkginfo__.py|
147147
setup.cfg
148148
)$
149+
- repo: https://github.com/PyCQA/bandit
150+
rev: 1.7.4
151+
hooks:
152+
- id: bandit
153+
args: ["-r", "-lll"]
154+
exclude: *fixtures

pylint/graph.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import os
1414
import shutil
1515
import subprocess
16-
import sys
1716
import tempfile
1817
from collections.abc import Sequence
1918
from typing import Any
@@ -113,9 +112,8 @@ def generate(
113112
"executable not found. Install graphviz, or specify a `.gv` "
114113
"outputfile to produce the DOT source code."
115114
)
116-
use_shell = sys.platform == "win32"
117115
if mapfile:
118-
subprocess.call(
116+
subprocess.run(
119117
[
120118
self.renderer,
121119
"-Tcmapx",
@@ -127,12 +125,12 @@ def generate(
127125
"-o",
128126
outputfile,
129127
],
130-
shell=use_shell,
128+
check=True,
131129
)
132130
else:
133-
subprocess.call(
131+
subprocess.run(
134132
[self.renderer, "-T", target, dot_sourcepath, "-o", outputfile],
135-
shell=use_shell,
133+
check=True,
136134
)
137135
os.unlink(dot_sourcepath)
138136
return outputfile

pylint/pyreverse/dot_printer.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
import os
1010
import subprocess
11-
import sys
1211
import tempfile
1312
from enum import Enum
1413
from pathlib import Path
@@ -164,10 +163,8 @@ def generate(self, outputfile: str) -> None:
164163
with open(dot_sourcepath, "w", encoding="utf8") as outfile:
165164
outfile.writelines(self.lines)
166165
if target not in graphviz_extensions:
167-
use_shell = sys.platform == "win32"
168-
subprocess.call(
169-
["dot", "-T", target, dot_sourcepath, "-o", outputfile],
170-
shell=use_shell,
166+
subprocess.run(
167+
["dot", "-T", target, dot_sourcepath, "-o", outputfile], check=True
171168
)
172169
os.unlink(dot_sourcepath)
173170

requirements_test_pre_commit.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Everything in this file should reflect the pre-commit configuration
22
# in .pre-commit-config.yaml
3+
bandit==1.7.4
34
black==22.10.0
45
flake8==6.0.0
56
flake8-bugbear==22.10.27

tests/profile/test_profile_against_externals.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from pathlib import Path
1414

1515
import pytest
16+
from git.repo import Repo
1617

1718
from pylint.testutils import GenericTestReporter as Reporter
1819
from pylint.testutils._run import _Run as Run
@@ -45,7 +46,7 @@ def test_run(tmp_path: Path, name: str, git_repo: str) -> None:
4546
"""Runs pylint against external sources."""
4647
checkoutdir = tmp_path / name
4748
checkoutdir.mkdir()
48-
os.system(f"git clone --depth=1 {git_repo} {checkoutdir}")
49+
Repo.clone_from(url=git_repo, to_path=checkoutdir, depth=1)
4950
filepaths = _get_py_files(scanpath=str(checkoutdir))
5051
print(f"Have {len(filepaths)} files")
5152

0 commit comments

Comments
 (0)