Skip to content

Commit 798cd06

Browse files
committed
use safer avro imports
1 parent 184ad96 commit 798cd06

File tree

3 files changed

+29
-36
lines changed

3 files changed

+29
-36
lines changed

cwltool/builder.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
import six
99
from six import iteritems, string_types
1010

11-
import avro
1211
import schema_salad.validate as validate
12+
import schema_salad.schema as schema
1313
from schema_salad.sourceline import SourceLine
1414

1515
from rdflib import Graph, URIRef
@@ -25,8 +25,6 @@
2525

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

28-
AvroSchemaFromJSONData = avro.schema.make_avsc_object
29-
3028
CONTENT_LIMIT = 64 * 1024
3129

3230

@@ -85,7 +83,7 @@ def checkFormat(actualFile, inputFormats, ontology):
8583

8684
class Builder(object):
8785
def __init__(self): # type: () -> None
88-
self.names = None # type: avro.schema.Names
86+
self.names = None # type: schema.Names
8987
self.schemaDefs = None # type: Dict[Text, Dict[Text, Any]]
9088
self.files = None # type: List[Dict[Text, Text]]
9189
self.fs_access = None # type: StdFsAccess
@@ -151,7 +149,7 @@ def bind_input(self, schema, datum, lead_pos=None, tail_pos=None, discover_secon
151149
elif isinstance(t, dict) and "name" in t and self.names.has_name(t["name"], ""):
152150
avsc = self.names.get_name(t["name"], "")
153151
else:
154-
avsc = AvroSchemaFromJSONData(t, self.names)
152+
avsc = schema.AvroSchemaFromJSONData(t, self.names)
155153
if validate.validate(avsc, datum):
156154
schema = copy.deepcopy(schema)
157155
schema["type"] = t

cwltool/process.py

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
from typing import (Any, Callable, Dict, Generator, List, Set, Text,
1919
Tuple, Union, cast)
2020

21-
import avro.schema
22-
import schema_salad.schema
21+
import schema_salad.schema as schema
2322
import schema_salad.validate as validate
2423
import six
2524
from pkg_resources import resource_stream
@@ -42,11 +41,6 @@
4241
from .utils import aslist, get_feature, copytree_with_merge, onWindows
4342

4443

45-
# if six.PY3:
46-
# AvroSchemaFromJSONData = avro.schema.SchemaFromJSONData
47-
# else:
48-
AvroSchemaFromJSONData = avro.schema.make_avsc_object
49-
5044
class LogAsDebugFilter(logging.Filter):
5145
def __init__(self, name, parent): # type: (Text, logging.Logger) -> None
5246
name = str(name)
@@ -108,7 +102,7 @@ def filter(self, record):
108102
'vocab_res_src.yml',
109103
'vocab_res_proc.yml')
110104

111-
SCHEMA_CACHE = {} # type: Dict[Text, Tuple[Loader, Union[avro.schema.Names, avro.schema.SchemaParseException], Dict[Text, Any], Loader]]
105+
SCHEMA_CACHE = {} # type: Dict[Text, Tuple[Loader, Union[schema.Names, schema.SchemaParseException], Dict[Text, Any], Loader]]
112106
SCHEMA_FILE = None # type: Dict[Text, Any]
113107
SCHEMA_DIR = None # type: Dict[Text, Any]
114108
SCHEMA_ANY = None # type: Dict[Text, Any]
@@ -129,7 +123,7 @@ def use_custom_schema(version, name, text):
129123
del SCHEMA_CACHE[version]
130124

131125
def get_schema(version):
132-
# type: (Text) -> Tuple[Loader, Union[avro.schema.Names, avro.schema.SchemaParseException], Dict[Text,Any], Loader]
126+
# type: (Text) -> Tuple[Loader, Union[schema.Names, schema.SchemaParseException], Dict[Text,Any], Loader]
133127

134128
if version in SCHEMA_CACHE:
135129
return SCHEMA_CACHE[version]
@@ -159,10 +153,10 @@ def get_schema(version):
159153

160154
if version in custom_schemas:
161155
cache[custom_schemas[version][0]] = custom_schemas[version][1]
162-
SCHEMA_CACHE[version] = schema_salad.schema.load_schema(
156+
SCHEMA_CACHE[version] = schema.load_schema(
163157
custom_schemas[version][0], cache=cache)
164158
else:
165-
SCHEMA_CACHE[version] = schema_salad.schema.load_schema(
159+
SCHEMA_CACHE[version] = schema.load_schema(
166160
"https://w3id.org/cwl/CommonWorkflowLanguage.yml", cache=cache)
167161

168162
return SCHEMA_CACHE[version]
@@ -409,7 +403,7 @@ def __init__(self, toolpath_object, **kwargs):
409403
"""
410404

411405
self.metadata = kwargs.get("metadata", {}) # type: Dict[Text,Any]
412-
self.names = None # type: avro.schema.Names
406+
self.names = None # type: schema.Names
413407

414408
global SCHEMA_FILE, SCHEMA_DIR, SCHEMA_ANY # pylint: disable=global-statement
415409
if SCHEMA_FILE is None:
@@ -421,9 +415,9 @@ def __init__(self, toolpath_object, **kwargs):
421415
SCHEMA_DIR = cast(Dict[Text, Any],
422416
SCHEMA_CACHE["v1.0"][3].idx["https://w3id.org/cwl/cwl#Directory"])
423417

424-
names = schema_salad.schema.make_avro_schema([SCHEMA_FILE, SCHEMA_DIR, SCHEMA_ANY],
425-
schema_salad.ref_resolver.Loader({}))[0]
426-
if isinstance(names, avro.schema.SchemaParseException):
418+
names = schema.make_avro_schema([SCHEMA_FILE, SCHEMA_DIR, SCHEMA_ANY],
419+
Loader({}))[0]
420+
if isinstance(names, schema.SchemaParseException):
427421
raise names
428422
else:
429423
self.names = names
@@ -449,10 +443,10 @@ def __init__(self, toolpath_object, **kwargs):
449443

450444
if sd:
451445
sdtypes = sd["types"]
452-
av = schema_salad.schema.make_valid_avro(sdtypes, {t["name"]: t for t in avroize_type(sdtypes)}, set())
446+
av = schema.make_valid_avro(sdtypes, {t["name"]: t for t in avroize_type(sdtypes)}, set())
453447
for i in av:
454448
self.schemaDefs[i["name"]] = i # type: ignore
455-
AvroSchemaFromJSONData(av, self.names) # type: ignore
449+
schema.AvroSchemaFromJSONData(av, self.names) # type: ignore
456450

457451
# Build record schema from inputs
458452
self.inputs_record_schema = {
@@ -484,14 +478,13 @@ def __init__(self, toolpath_object, **kwargs):
484478

485479
with SourceLine(toolpath_object, "inputs", validate.ValidationException):
486480
self.inputs_record_schema = cast(
487-
Dict[six.text_type, Any], schema_salad.schema.make_valid_avro(
481+
Dict[six.text_type, Any], schema.make_valid_avro(
488482
self.inputs_record_schema, {}, set()))
489-
AvroSchemaFromJSONData(self.inputs_record_schema, self.names)
483+
schema.AvroSchemaFromJSONData(self.inputs_record_schema, self.names)
490484
with SourceLine(toolpath_object, "outputs", validate.ValidationException):
491485
self.outputs_record_schema = cast(Dict[six.text_type, Any],
492-
schema_salad.schema.make_valid_avro(
493-
self.outputs_record_schema, {}, set()))
494-
AvroSchemaFromJSONData(self.outputs_record_schema, self.names)
486+
schema.make_valid_avro(self.outputs_record_schema, {}, set()))
487+
schema.AvroSchemaFromJSONData(self.outputs_record_schema, self.names)
495488

496489
if toolpath_object.get("class") is not None and not kwargs.get("disable_js_validation", False):
497490
if kwargs.get("js_hint_options_file") is not None:

cwltool/validate_js.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
from typing import Any, Dict, List, Text, Tuple, Union
99

1010
import copy
11-
import avro.schema
1211
import schema_salad
13-
from ruamel.yaml.comments import CommentedMap, CommentedSeq
1412
from schema_salad.sourceline import SourceLine
15-
from schema_salad.validate import ValidationException, validate_ex
13+
from schema_salad.validate import Schema, ValidationException, validate_ex
14+
import avro.schema # always import after schema_salad, never before
15+
from ruamel.yaml.comments import CommentedMap, CommentedSeq
1616

1717
from .expression import scanner as scan_expression
1818
from .sandboxjs import (JavascriptException, code_fragment_to_js,
@@ -22,7 +22,7 @@
2222
_logger = logging.getLogger("cwltool")
2323

2424
def is_expression(tool, schema):
25-
# type: (Union[CommentedMap, Any], avro.schema.Schema) -> bool
25+
# type: (Union[CommentedMap, Any], Schema) -> bool
2626
return isinstance(schema, avro.schema.EnumSchema) and schema.name == "Expression" and isinstance(tool, (str, Text))
2727

2828
class SuppressLog(logging.Filter):
@@ -33,6 +33,7 @@ def __init__(self, name): # type: (Text) -> None
3333
def filter(self, record):
3434
return False
3535

36+
3637
_logger_validation_warnings = logging.getLogger("cwltool.validation_warnings")
3738
_logger_validation_warnings.addFilter(SuppressLog("cwltool.validation_warnings"))
3839

@@ -55,7 +56,7 @@ def get_expressions(tool, schema, source_line=None):
5556
return []
5657

5758
return list(itertools.chain(*
58-
map(lambda x: get_expressions(x[1], schema.items, SourceLine(tool, x[0])), enumerate(tool)) # type: ignore # https://github.com/python/mypy/issues/4679
59+
map(lambda x: get_expressions(x[1], schema.items, SourceLine(tool, x[0])), enumerate(tool)) # type: ignore # https://github.com/python/mypy/issues/4679
5960
))
6061

6162
elif isinstance(schema, avro.schema.RecordSchema):
@@ -76,6 +77,7 @@ def get_expressions(tool, schema, source_line=None):
7677
else:
7778
return []
7879

80+
7981
JSHintJSReturn = namedtuple("jshint_return", ["errors", "globals"])
8082

8183
def jshint_js(js_text, globals=None, options=None):
@@ -85,7 +87,7 @@ def jshint_js(js_text, globals=None, options=None):
8587
if options is None:
8688
options = {
8789
"includewarnings": [
88-
"W117", # <VARIABLE> not defined
90+
"W117", # <VARIABLE> not defined
8991
"W104", "W119" # using ES6 features
9092
],
9193
"strict": "implied",
@@ -130,12 +132,12 @@ def dump_jshint_error():
130132
except ValueError:
131133
dump_jshint_error()
132134

133-
jshint_errors = [] # type: List[Text]
135+
jshint_errors = [] # type: List[Text]
134136

135137
js_text_lines = js_text.split("\n")
136138

137139
for jshint_error_obj in jshint_json.get("errors", []):
138-
text = u"JSHINT: " + js_text_lines[jshint_error_obj["line"] - 1] + "\n"
140+
text = u"JSHINT: " + js_text_lines[jshint_error_obj["line"] - 1] + "\n"
139141
text += u"JSHINT: " + " " * (jshint_error_obj["character"] - 1) + "^\n"
140142
text += u"JSHINT: %s: %s" % (jshint_error_obj["code"], jshint_error_obj["reason"])
141143
jshint_errors.append(text)
@@ -149,7 +151,7 @@ def print_js_hint_messages(js_hint_messages, source_line):
149151
_logger.warn(source_line.makeError(js_hint_message))
150152

151153
def validate_js_expressions(tool, schema, jshint_options=None):
152-
# type: (CommentedMap, avro.schema.Schema, Dict) -> None
154+
# type: (CommentedMap, Schema, Dict) -> None
153155

154156
if tool.get("requirements") is None:
155157
return

0 commit comments

Comments
 (0)