Skip to content

Commit b243a10

Browse files
committed
Use resource_stream for validate_js
1 parent a8b1266 commit b243a10

File tree

3 files changed

+6
-26
lines changed

3 files changed

+6
-26
lines changed

cwltool/sandboxjs.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import sys
1111
from io import BytesIO
1212
from typing import Any, Dict, List, Mapping, Text, Tuple, Union
13-
from .utils import onWindows, get_data
13+
from .utils import onWindows
1414
from pkg_resources import resource_stream
1515

1616
import six

cwltool/utils.py

-18
Original file line numberDiff line numberDiff line change
@@ -175,21 +175,3 @@ def bytes2str_in_dicts(a):
175175

176176
# simply return elements itself
177177
return a
178-
179-
def get_data(filename):
180-
# type: (Text) -> Text
181-
filename = os.path.normpath(
182-
filename) # normalizing path depending on OS or else it will cause problem when joining path
183-
filepath = None
184-
try:
185-
filepath = resource_filename(
186-
Requirement.parse("cwltool"), filename)
187-
except ResolutionError:
188-
pass
189-
if not filepath or not (os.path.isfile(filepath) or os.path.isdir(filepath)):
190-
filepath = os.path.join(os.path.dirname(__file__), os.pardir, filename)
191-
192-
if not(os.path.isfile(filepath) or os.path.isdir(filepath)):
193-
raise Exception("Resource %s not found." % filepath)
194-
195-
return filepath

cwltool/validate_js.py

+5-7
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from .process import Process
2121
from .sandboxjs import (JavascriptException, code_fragment_to_js,
2222
exec_js_process, execjs)
23-
from .utils import get_data
23+
from pkg_resources import resource_stream
2424

2525
_logger = logging.getLogger("cwltool")
2626

@@ -84,16 +84,14 @@ def jshint_js(js_text, globals=None, options=None):
8484
"esversion": 5
8585
}
8686

87-
linter_folder = get_data("cwltool/jshint")
88-
89-
with open(path.join(linter_folder, "jshint.js"), 'r', encoding='utf-8') as file:
87+
with resource_stream(__name__, "jshint/jshint.js") as file:
9088
# NOTE: we need a global variable for lodash (which jshint depends on)
91-
jshint_functions_text = "var global = this;" + file.read()
89+
jshint_functions_text = "var global = this;" + file.read().decode('utf-8')
9290

93-
with open(path.join(linter_folder, "jshint_wrapper.js"), "r", encoding='utf-8') as file:
91+
with resource_stream(__name__, "jshint/jshint_wrapper.js") as file:
9492
# NOTE: we need to assign to ob, as the expression {validateJS: validateJS} as an expression
9593
# is interpreted as a block with a label `validateJS`
96-
jshint_functions_text += "\n" + file.read() + "\nvar ob = {validateJS: validateJS}; ob"
94+
jshint_functions_text += "\n" + file.read().decode('utf-8') + "\nvar ob = {validateJS: validateJS}; ob"
9795

9896
returncode, stdout, stderr = exec_js_process(
9997
"validateJS(%s)" % json.dumps({

0 commit comments

Comments
 (0)