Skip to content

Commit cf33726

Browse files
GlassOfWhiskeymr-c
andauthored
Removed cwltool deps (#135)
* Removed javascript cwltool deps Before this commit, JS evaluation was taken from the cwltool codebase. Now, all the logic has been migrated here. Plus, the code has been made more modular, so that JSEngines can be substituted at runtime without having to rewrite the entire logic. A basic NodeJSEngine has been added and loaded by default, which contains the original logic. Co-authored-by: Michael R. Crusoe <[email protected]>
1 parent e1f3531 commit cf33726

24 files changed

+1876
-83
lines changed

MANIFEST.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ include Makefile
33
include MANIFEST.in
44
include LICENSE.txt
55
include *requirements.txt
6+
include cwl_utils/cwlNodeEngine.js
7+
include cwl_utils/cwlNodeEngineJSConsole.js
8+
include cwl_utils/cwlNodeEngineWithContext.js
69
include testdata/*.cwl
710
include testdata/*.yaml
811
include testdata/*.input

cwl_utils/cwlNodeEngine.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"use strict";
2+
process.stdin.setEncoding("utf8");
3+
var incoming = "";
4+
process.stdin.on("data", function(chunk) {
5+
incoming += chunk;
6+
var i = incoming.indexOf("\n");
7+
if (i > -1) {
8+
try{
9+
var fn = JSON.parse(incoming.substr(0, i));
10+
incoming = incoming.substr(i+1);
11+
process.stdout.write(JSON.stringify(require("vm").runInNewContext(fn, {})) + "\n");
12+
}
13+
catch(e){
14+
console.error(e)
15+
}
16+
/*strings to indicate the process has finished*/
17+
console.log("r1cepzbhUTxtykz5XTC4");
18+
console.error("r1cepzbhUTxtykz5XTC4");
19+
}
20+
});
21+
process.stdin.on("end", process.exit);

cwl_utils/cwlNodeEngineJSConsole.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"use strict";
2+
function js_console_log(){
3+
console.error("[log] "+require("util").format.apply(this, arguments).split("\n").join("\n[log] "));
4+
}
5+
function js_console_err(){
6+
console.error("[err] "+require("util").format.apply(this, arguments).split("\n").join("\n[err] "));
7+
}
8+
process.stdin.setEncoding("utf8");
9+
var incoming = "";
10+
process.stdin.on("data", function(chunk) {
11+
incoming += chunk;
12+
var i = incoming.indexOf("\n");
13+
if (i > -1) {
14+
try{
15+
var fn = JSON.parse(incoming.substr(0, i));
16+
incoming = incoming.substr(i+1);
17+
process.stdout.write(JSON.stringify(require("vm").runInNewContext(fn, {
18+
console: {
19+
log: js_console_log,
20+
error: js_console_err
21+
}
22+
})) + "\n");
23+
}
24+
catch(e){
25+
console.error(e)
26+
}
27+
/*strings to indicate the process has finished*/
28+
console.log("r1cepzbhUTxtykz5XTC4");
29+
console.error("r1cepzbhUTxtykz5XTC4");
30+
}
31+
});
32+
process.stdin.on("end", process.exit);

cwl_utils/cwlNodeEngineWithContext.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"use strict";
2+
process.stdin.setEncoding("utf8");
3+
var incoming = "";
4+
var firstInput = true;
5+
var context = {};
6+
7+
process.stdin.on("data", function(chunk) {
8+
incoming += chunk;
9+
var i = incoming.indexOf("\n");
10+
while (i > -1) {
11+
try{
12+
var input = incoming.substr(0, i);
13+
incoming = incoming.substr(i+1);
14+
var fn = JSON.parse(input);
15+
if(firstInput){
16+
context = require("vm").runInNewContext(fn, {});
17+
}
18+
else{
19+
process.stdout.write(JSON.stringify(require("vm").runInNewContext(fn, context)) + "\n");
20+
}
21+
}
22+
catch(e){
23+
console.error(e);
24+
}
25+
if(firstInput){
26+
firstInput = false;
27+
}
28+
else{
29+
/*strings to indicate the process has finished*/
30+
console.log("r1cepzbhUTxtykz5XTC4");
31+
console.error("r1cepzbhUTxtykz5XTC4");
32+
}
33+
34+
i = incoming.indexOf("\n");
35+
}
36+
});
37+
process.stdin.on("end", process.exit);

cwl_utils/cwl_expression_refactor.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@
1919
Union,
2020
)
2121

22-
from cwltool.loghandler import _logger as _cwltoollogger
2322
from ruamel import yaml
2423

24+
from cwl_utils.loghandler import _logger as _cwlutilslogger
25+
2526
if TYPE_CHECKING:
2627
from typing_extensions import Protocol
2728
else:
@@ -31,7 +32,7 @@
3132
defaultStreamHandler = logging.StreamHandler() # pylint: disable=invalid-name
3233
_logger.addHandler(defaultStreamHandler)
3334
_logger.setLevel(logging.INFO)
34-
_cwltoollogger.setLevel(100)
35+
_cwlutilslogger.setLevel(100)
3536

3637
from cwl_utils import (
3738
cwl_v1_0_expression_refactor,

cwl_utils/cwl_normalizer.py

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,18 @@
99
from pathlib import Path
1010
from typing import List, MutableSequence, Optional, Set
1111

12-
from cwltool.context import LoadingContext, RuntimeContext
13-
from cwltool.load_tool import (
14-
fetch_document,
15-
resolve_and_validate_document,
16-
resolve_tool_uri,
17-
)
18-
from cwltool.loghandler import _logger as _cwltoollogger
19-
from cwltool.main import print_pack
20-
from cwltool.process import use_standard_schema
2112
from cwlupgrader import main as cwlupgrader
2213
from ruamel import yaml
2314
from schema_salad.sourceline import add_lc_filename
2415

16+
from cwl_utils.loghandler import _logger as _cwlutilslogger
17+
from cwl_utils.pack import pack
18+
2519
_logger = logging.getLogger("cwl-normalizer") # pylint: disable=invalid-name
2620
defaultStreamHandler = logging.StreamHandler() # pylint: disable=invalid-name
2721
_logger.addHandler(defaultStreamHandler)
2822
_logger.setLevel(logging.INFO)
29-
_cwltoollogger.setLevel(100)
23+
_cwlutilslogger.setLevel(100)
3024

3125
from cwl_utils import cwl_v1_2_expression_refactor
3226
from cwl_utils.parser.cwl_v1_2 import load_document_by_yaml, save
@@ -127,29 +121,7 @@ def run(args: argparse.Namespace) -> int:
127121
else:
128122
with tempfile.TemporaryDirectory() as tmpdirname:
129123
path = Path(tmpdirname) / Path(document).name
130-
with open(path, "w") as handle:
131-
yaml.main.round_trip_dump(result, handle)
132-
# TODO replace the cwltool based packing with a parser_v1_2 based packer
133-
runtimeContext = RuntimeContext()
134-
loadingContext = LoadingContext()
135-
use_standard_schema("v1.2")
136-
# loadingContext.construct_tool_object = workflow.default_make_tool
137-
# loadingContext.resolver = tool_resolver
138-
loadingContext.do_update = False
139-
uri, tool_file_uri = resolve_tool_uri(
140-
str(path),
141-
resolver=loadingContext.resolver,
142-
fetcher_constructor=loadingContext.fetcher_constructor,
143-
)
144-
loadingContext, workflowobj, uri = fetch_document(uri, loadingContext)
145-
loadingContext, uri = resolve_and_validate_document(
146-
loadingContext,
147-
workflowobj,
148-
uri,
149-
preprocess_only=True,
150-
skip_schemas=True,
151-
)
152-
packed = print_pack(loadingContext, uri)
124+
packed = pack(str(path))
153125
output = Path(args.dir) / Path(document).name
154126
with open(output, "w", encoding="utf-8") as output_filehandle:
155127
output_filehandle.write(packed)

cwl_utils/cwl_v1_0_expression_refactor.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,20 @@
1212
MutableSequence,
1313
Optional,
1414
Sequence,
15-
Text,
1615
Tuple,
1716
Type,
1817
Union,
1918
cast,
2019
)
2120

22-
from cwltool.errors import WorkflowException
23-
from cwltool.expression import do_eval, interpolate
24-
from cwltool.sandboxjs import JavascriptException
25-
from cwltool.utils import CWLObjectType, CWLOutputType
2621
from ruamel import yaml
2722
from schema_salad.sourceline import SourceLine
2823
from schema_salad.utils import json_dumps
2924

3025
import cwl_utils.parser.cwl_v1_0 as cwl
26+
from cwl_utils.errors import JavascriptException, WorkflowException
27+
from cwl_utils.expression import do_eval, interpolate
28+
from cwl_utils.types import CWLObjectType, CWLOutputType
3129

3230

3331
def expand_stream_shortcuts(process: cwl.CommandLineTool) -> cwl.CommandLineTool:

cwl_utils/cwl_v1_1_expression_refactor.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,20 @@
1212
MutableSequence,
1313
Optional,
1414
Sequence,
15-
Text,
1615
Tuple,
1716
Type,
1817
Union,
1918
cast,
2019
)
2120

22-
from cwltool.errors import WorkflowException
23-
from cwltool.expression import do_eval, interpolate
24-
from cwltool.sandboxjs import JavascriptException
25-
from cwltool.utils import CWLObjectType, CWLOutputType
2621
from ruamel import yaml
2722
from schema_salad.sourceline import SourceLine
2823
from schema_salad.utils import json_dumps
2924

3025
import cwl_utils.parser.cwl_v1_1 as cwl
26+
from cwl_utils.errors import JavascriptException, WorkflowException
27+
from cwl_utils.expression import do_eval, interpolate
28+
from cwl_utils.types import CWLObjectType, CWLOutputType
3129

3230

3331
def expand_stream_shortcuts(process: cwl.CommandLineTool) -> cwl.CommandLineTool:
@@ -184,7 +182,7 @@ def etool_to_cltool(
184182
contents += "\n" + "\n".join(expressionLib)
185183
contents += (
186184
"""
187-
var ret = function(){"""
185+
var ret = function(){"""
188186
+ escape_expression_field(etool.expression.strip()[2:-1])
189187
+ """}();
190188
process.stdout.write(JSON.stringify(ret));"""
@@ -390,7 +388,7 @@ def generate_etool_from_expr(
390388
expression += "\n var self=inputs.self;"
391389
expression += (
392390
"""
393-
return {"result": function(){"""
391+
return {"result": function(){"""
394392
+ expr[2:-2]
395393
+ """}()};
396394
}"""
@@ -1782,7 +1780,7 @@ def generate_etool_from_expr2(
17821780
expression += f"\n var self=inputs.{self_name};"
17831781
expression += (
17841782
"""
1785-
return {"result": function(){"""
1783+
return {"result": function(){"""
17861784
+ expr[2:-2]
17871785
+ """}()};
17881786
}"""

cwl_utils/cwl_v1_2_expression_refactor.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,20 @@
1212
MutableSequence,
1313
Optional,
1414
Sequence,
15-
Text,
1615
Tuple,
1716
Type,
1817
Union,
1918
cast,
2019
)
2120

22-
from cwltool.errors import WorkflowException
23-
from cwltool.expression import do_eval, interpolate
24-
from cwltool.sandboxjs import JavascriptException
25-
from cwltool.utils import CWLObjectType, CWLOutputType
2621
from ruamel import yaml
2722
from schema_salad.sourceline import SourceLine
2823
from schema_salad.utils import json_dumps
2924

3025
import cwl_utils.parser.cwl_v1_2 as cwl
26+
from cwl_utils.errors import JavascriptException, WorkflowException
27+
from cwl_utils.expression import do_eval, interpolate
28+
from cwl_utils.types import CWLObjectType, CWLOutputType
3129

3230

3331
def expand_stream_shortcuts(process: cwl.CommandLineTool) -> cwl.CommandLineTool:
@@ -184,7 +182,7 @@ def etool_to_cltool(
184182
contents += "\n" + "\n".join(expressionLib)
185183
contents += (
186184
"""
187-
var ret = function(){"""
185+
var ret = function(){"""
188186
+ escape_expression_field(etool.expression.strip()[2:-1])
189187
+ """}();
190188
process.stdout.write(JSON.stringify(ret));"""
@@ -390,7 +388,7 @@ def generate_etool_from_expr(
390388
expression += "\n var self=inputs.self;"
391389
expression += (
392390
"""
393-
return {"result": function(){"""
391+
return {"result": function(){"""
394392
+ expr[2:-2]
395393
+ """}()};
396394
}"""
@@ -1878,7 +1876,7 @@ def generate_etool_from_expr2(
18781876
expression += f"\n var self=inputs.{self_name};"
18791877
expression += (
18801878
"""
1881-
return {"result": function(){"""
1879+
return {"result": function(){"""
18821880
+ expr[2:-2]
18831881
+ """}()};
18841882
}"""

cwl_utils/errors.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class ArrayMissingItems(BaseException):
2+
"""From https://github.com/rabix/sbpack/blob/b8404a0859ffcbe1edae6d8f934e51847b003320/sbpack/lib.py"""
3+
4+
5+
class JavascriptException(Exception):
6+
pass
7+
8+
9+
class MissingKeyField(BaseException):
10+
"""From https://github.com/rabix/sbpack/blob/b8404a0859ffcbe1edae6d8f934e51847b003320/sbpack/lib.py"""
11+
12+
13+
class MissingTypeName(BaseException):
14+
"""From https://github.com/rabix/sbpack/blob/b8404a0859ffcbe1edae6d8f934e51847b003320/sbpack/lib.py"""
15+
16+
17+
class RecordMissingFields(BaseException):
18+
"""From https://github.com/rabix/sbpack/blob/b8404a0859ffcbe1edae6d8f934e51847b003320/sbpack/lib.py"""
19+
20+
21+
class SubstitutionError(Exception):
22+
pass
23+
24+
25+
class WorkflowException(Exception):
26+
pass

0 commit comments

Comments
 (0)