Skip to content

Commit 0e30a52

Browse files
committed
appease bandit
1 parent ca3fcfc commit 0e30a52

File tree

7 files changed

+38
-27
lines changed

7 files changed

+38
-27
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ test: $(PYSOURCES) FORCE
152152

153153
## testcov : run the wes-service test suite and collect coverage
154154
testcov: $(PYSOURCES)
155-
pytest --cov ${PYTEST_EXTRA}
155+
python -m pytest -rsx --cov ${PYTEST_EXTRA}
156156

157157
sloccount.sc: $(PYSOURCES) Makefile
158158
sloccount --duplicates --wide --details $^ > $@

test-requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
pytest
2+
pytest-cov

wes_client/util.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
import json
55
import logging
66
import os
7-
from subprocess import DEVNULL, CalledProcessError, check_call
7+
import sys
8+
from subprocess import DEVNULL, CalledProcessError, check_call # nosec B404
89
from typing import Any, Dict, List, Optional, Set, Tuple, Union, cast
910
from urllib.request import pathname2url, urlopen
1011

@@ -18,7 +19,10 @@
1819
def py3_compatible(filePath: str) -> bool:
1920
"""Determines if a python file is 3.x compatible by seeing if it compiles in a subprocess"""
2021
try:
21-
check_call(["python3", "-m", "py_compile", filePath], stderr=DEVNULL)
22+
check_call(
23+
[sys.executable, "-m", "py_compile", os.path.normpath(filePath)],
24+
stderr=DEVNULL,
25+
) # nosec B603
2226
except CalledProcessError as e:
2327
raise RuntimeError("Python files must be 3.x compatible") from e
2428
return True
@@ -29,9 +33,7 @@ def get_version(extension: str, workflow_file: str) -> str:
2933
if extension == "py" and py3_compatible(workflow_file):
3034
return "3"
3135
elif extension == "cwl":
32-
return cast(
33-
str, yaml.load(open(workflow_file), Loader=yaml.FullLoader)["cwlVersion"]
34-
)
36+
return cast(str, yaml.safe_load(open(workflow_file))["cwlVersion"])
3537
else: # Must be a wdl file.
3638
# Borrowed from https://github.com/Sage-Bionetworks/synapse-orchestrator/
3739
# blob/develop/synorchestrator/util.py#L142
@@ -66,7 +68,7 @@ def wf_info(workflow_path: str) -> Tuple[str, str]:
6668
"http://"
6769
):
6870
# If file not local go fetch it.
69-
html = urlopen(workflow_path).read()
71+
html = urlopen(workflow_path).read() # nosec B310
7072
local_loc = os.path.join(os.getcwd(), "fetchedFromRemote." + file_type)
7173
with open(local_loc, "w") as f:
7274
f.write(html.decode())
@@ -174,7 +176,7 @@ def build_wes_request(
174176
attach_f: Any = open(attachment, "rb")
175177
relpath = os.path.relpath(attachment, wfbase)
176178
elif attachment.startswith("http"):
177-
attach_f = urlopen(attachment)
179+
attach_f = urlopen(attachment) # nosec B310
178180
relpath = os.path.basename(attach_f)
179181

180182
parts.append(("workflow_attachment", (relpath, attach_f)))
@@ -226,7 +228,7 @@ def get_service_info(self) -> Dict[str, Any]:
226228
:param host: Port where the post request will be sent and the wes server listens at (default 8080)
227229
:return: The body of the get result as a dictionary.
228230
"""
229-
postresult = requests.get(
231+
postresult = requests.get( # nosec B113
230232
f"{self.proto}://{self.host}/ga4gh/wes/v1/service-info",
231233
headers=self.auth,
232234
)
@@ -244,7 +246,7 @@ def list_runs(self) -> Dict[str, Any]:
244246
:param host: Port where the post request will be sent and the wes server listens at (default 8080)
245247
:return: The body of the get result as a dictionary.
246248
"""
247-
postresult = requests.get(
249+
postresult = requests.get( # nosec B113
248250
f"{self.proto}://{self.host}/ga4gh/wes/v1/runs", headers=self.auth
249251
)
250252
return wes_reponse(postresult)
@@ -266,7 +268,7 @@ def run(
266268
"""
267269
attachments = list(expand_globs(attachments))
268270
parts = build_wes_request(wf, jsonyaml, attachments)
269-
postresult = requests.post(
271+
postresult = requests.post( # nosec B113
270272
f"{self.proto}://{self.host}/ga4gh/wes/v1/runs",
271273
files=parts,
272274
headers=self.auth,
@@ -283,7 +285,7 @@ def cancel(self, run_id: str) -> Dict[str, Any]:
283285
:param host: Port where the post request will be sent and the wes server listens at (default 8080)
284286
:return: The body of the delete result as a dictionary.
285287
"""
286-
postresult = requests.post(
288+
postresult = requests.post( # nosec B113
287289
f"{self.proto}://{self.host}/ga4gh/wes/v1/runs/{run_id}/cancel",
288290
headers=self.auth,
289291
)
@@ -299,7 +301,7 @@ def get_run_log(self, run_id: str) -> Dict[str, Any]:
299301
:param host: Port where the post request will be sent and the wes server listens at (default 8080)
300302
:return: The body of the get result as a dictionary.
301303
"""
302-
postresult = requests.get(
304+
postresult = requests.get( # nosec B113
303305
f"{self.proto}://{self.host}/ga4gh/wes/v1/runs/{run_id}",
304306
headers=self.auth,
305307
)
@@ -315,7 +317,7 @@ def get_run_status(self, run_id: str) -> Dict[str, Any]:
315317
:param host: Port where the post request will be sent and the wes server listens at (default 8080)
316318
:return: The body of the get result as a dictionary.
317319
"""
318-
postresult = requests.get(
320+
postresult = requests.get( # nosec B113
319321
f"{self.proto}://{self.host}/ga4gh/wes/v1/runs/{run_id}/status",
320322
headers=self.auth,
321323
)

wes_client/wes_client_main.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,9 @@ def main(argv: List[str] = sys.argv[1:]) -> int:
9797

9898
if args.log:
9999
response = client.get_run_log(run_id=args.log)
100-
sys.stdout.write(requests.get(response["run_log"]["stderr"], headers=auth).text)
100+
sys.stdout.write(
101+
requests.get(response["run_log"]["stderr"], headers=auth).text # nosec B113
102+
)
101103
return 0
102104

103105
if args.get:
@@ -146,7 +148,7 @@ def main(argv: List[str] = sys.argv[1:]) -> int:
146148
try:
147149
# TODO: Only works with Arvados atm
148150
logging.info(str(s["run_log"]["stderr"]))
149-
logs = requests.get(s["run_log"]["stderr"], headers=auth).text
151+
logs = requests.get(s["run_log"]["stderr"], headers=auth).text # nosec B113
150152
logging.info("Run log:\n" + logs)
151153
except InvalidSchema:
152154
logging.info("Run log:\n" + str(s["run_log"]["stderr"]))

wes_service/arvados_wes.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import logging
44
import os
55
import shutil
6-
import subprocess
6+
import subprocess # nosec B404
77
import tempfile
88
import threading
99
from typing import Any, Callable, Dict, List, Optional, Tuple, Union, cast
@@ -83,8 +83,9 @@ def catch_exceptions_wrapper(self: Any, *args: str, **kwargs: str) -> Any:
8383

8484
class ArvadosBackend(WESBackend):
8585
def GetServiceInfo(self) -> Dict[str, Any]:
86-
stdout, stderr = subprocess.Popen(
87-
["arvados-cwl-runner", "--version"], stderr=subprocess.PIPE
86+
stdout, stderr = subprocess.Popen( # nosec B603
87+
[shutil.which("arvados-cwl-runner") or "arvados-cwl-runner", "--version"],
88+
stderr=subprocess.PIPE,
8889
).communicate()
8990
return {
9091
"workflow_type_versions": {
@@ -218,7 +219,7 @@ def invoke_cwl_runner(
218219
cr_uuid, "Executing %s" % cmd, env["ARVADOS_API_TOKEN"]
219220
)
220221

221-
proc = subprocess.Popen(
222+
proc = subprocess.Popen( # nosec B603
222223
cmd,
223224
env=env,
224225
cwd=tempdir,

wes_service/cwl_runner.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import json
22
import os
3-
import subprocess
3+
import subprocess # nosec B404
44
import uuid
55
from typing import Any, Dict, List, Tuple, cast
66

@@ -72,7 +72,7 @@ def run(
7272

7373
# build args and run
7474
command_args: List[str] = [runner] + extra2 + [workflow_url, jsonpath]
75-
proc = subprocess.Popen(
75+
proc = subprocess.Popen( # nosec B603
7676
command_args, stdout=output, stderr=stderr, close_fds=True, cwd=tempdir
7777
)
7878
output.close()
@@ -162,7 +162,7 @@ def cancel(self) -> None:
162162
class CWLRunnerBackend(WESBackend):
163163
def GetServiceInfo(self) -> Dict[str, Any]:
164164
runner = cast(str, self.getopt("runner", default="cwl-runner"))
165-
stdout, stderr = subprocess.Popen(
165+
stdout, stderr = subprocess.Popen( # nosec B603
166166
[runner, "--version"], stderr=subprocess.PIPE
167167
).communicate()
168168
r = {

wes_service/toil_wes.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import logging
33
import os
44
import shutil
5-
import subprocess
5+
import subprocess # nosec B404
66
import time
77
import uuid
88
from multiprocessing import Process
@@ -131,7 +131,7 @@ def call_cmd(self, cmd: Union[List[str], str], cwd: str) -> int:
131131
self.outfile,
132132
self.errfile,
133133
)
134-
process = subprocess.Popen(
134+
process = subprocess.Popen( # nosec B603
135135
cmd, stdout=stdout, stderr=stderr, close_fds=True, cwd=cwd
136136
)
137137
stdout.close()
@@ -287,8 +287,13 @@ def getstate(self) -> Tuple[str, int]:
287287
open(self.staterrorfile, "a").close()
288288
return "EXECUTOR_ERROR", 255
289289
if (
290-
subprocess.run(
291-
["toil", "status", "--failIfNotComplete", self.jobstorefile]
290+
subprocess.run( # nosec B603
291+
[
292+
shutil.which("toil") or "toil",
293+
"status",
294+
"--failIfNotComplete",
295+
self.jobstorefile,
296+
]
292297
).returncode
293298
== 0
294299
):

0 commit comments

Comments
 (0)