Skip to content

Commit a84ddf1

Browse files
committed
freshen cwltool/sandboxjs.py
1 parent 36aab33 commit a84ddf1

File tree

1 file changed

+22
-27
lines changed

1 file changed

+22
-27
lines changed

cwltool/sandboxjs.py

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,19 @@
1010
import sys
1111
import threading
1212
from io import BytesIO
13-
from typing import Any, Dict, List, Optional, Tuple, Union, cast
13+
from typing import List, Optional, Tuple, cast
1414

1515
from pkg_resources import resource_stream
1616
from schema_salad.utils import json_dumps
1717

1818
from .loghandler import _logger
19-
from .utils import onWindows, processes_to_kill
19+
from .utils import CWLOutputType, onWindows, processes_to_kill
2020

2121

2222
class JavascriptException(Exception):
2323
pass
2424

2525

26-
JSON = Union[Dict[str, Any], List[Any], str, int, float, bool, None]
27-
2826
localdata = threading.local()
2927

3028
default_timeout = 20
@@ -33,8 +31,7 @@ class JavascriptException(Exception):
3331
minimum_node_version_str = "0.10.26"
3432

3533

36-
def check_js_threshold_version(working_alias):
37-
# type: (str) -> bool
34+
def check_js_threshold_version(working_alias: str) -> bool:
3835
"""
3936
Check if the nodeJS engine version on the system with the allowed minimum version.
4037
@@ -153,13 +150,12 @@ def new_js_proc(js_text: str, force_docker_pull: bool = False):
153150

154151

155152
def exec_js_process(
156-
js_text, # type: str
157-
timeout=default_timeout, # type: float
158-
js_console=False, # type: bool
159-
context=None, # type: Optional[str]
160-
force_docker_pull=False, # type: bool
161-
):
162-
# type: (...) -> Tuple[int, str, str]
153+
js_text: str,
154+
timeout: float = default_timeout,
155+
js_console: bool = False,
156+
context: Optional[str] = None,
157+
force_docker_pull: bool = False,
158+
) -> Tuple[int, str, str]:
163159

164160
if not hasattr(localdata, "procs"):
165161
localdata.procs = {}
@@ -201,7 +197,7 @@ def exec_js_process(
201197

202198
killed = []
203199

204-
def terminate(): # type: () -> None
200+
def terminate() -> None:
205201
"""Kill the node process if it exceeds timeout limit."""
206202
try:
207203
killed.append(True)
@@ -225,7 +221,7 @@ def terminate(): # type: () -> None
225221
rselect = [nodejs.stdout, nodejs.stderr] # type: List[BytesIO]
226222
wselect = [nodejs.stdin] # type: List[BytesIO]
227223

228-
def process_finished(): # type: () -> bool
224+
def process_finished() -> bool:
229225
return stdout_buf.getvalue().decode("utf-8").endswith(
230226
PROCESS_FINISHED_STR
231227
) and stderr_buf.getvalue().decode("utf-8").endswith(PROCESS_FINISHED_STR)
@@ -342,8 +338,7 @@ def get_error(error_queue):
342338
return returncode, stdoutdata.decode("utf-8"), stderrdata.decode("utf-8")
343339

344340

345-
def code_fragment_to_js(jscript, jslib=""):
346-
# type: (str, str) -> str
341+
def code_fragment_to_js(jscript: str, jslib: str = "") -> str:
347342
if isinstance(jscript, str) and len(jscript) > 1 and jscript[0] == "{":
348343
inner_js = jscript
349344
else:
@@ -353,13 +348,13 @@ def code_fragment_to_js(jscript, jslib=""):
353348

354349

355350
def execjs(
356-
js, # type: str
357-
jslib, # type: str
358-
timeout, # type: float
359-
force_docker_pull=False, # type: bool
360-
debug=False, # type: bool
361-
js_console=False, # type: bool
362-
): # type: (...) -> JSON
351+
js: str,
352+
jslib: str,
353+
timeout: float,
354+
force_docker_pull: bool = False,
355+
debug: bool = False,
356+
js_console: bool = False,
357+
) -> CWLOutputType:
363358

364359
fn = code_fragment_to_js(js, jslib)
365360

@@ -378,12 +373,12 @@ def execjs(
378373
)
379374
_logger.info("----------------------------------------")
380375

381-
def stdfmt(data): # type: (str) -> str
376+
def stdfmt(data: str) -> str:
382377
if "\n" in data:
383378
return "\n" + data.strip()
384379
return data
385380

386-
def fn_linenum(): # type: () -> str
381+
def fn_linenum() -> str:
387382
lines = fn.splitlines()
388383
ofs = 0
389384
maxlines = 99
@@ -413,7 +408,7 @@ def fn_linenum(): # type: () -> str
413408
raise JavascriptException(info)
414409

415410
try:
416-
return cast(JSON, json.loads(stdout))
411+
return cast(CWLOutputType, json.loads(stdout))
417412
except ValueError as err:
418413
raise JavascriptException(
419414
"{}\nscript was:\n{}\nstdout was: '{}'\nstderr was: '{}'\n".format(

0 commit comments

Comments
 (0)