Skip to content

Commit 92d59e6

Browse files
authored
Use schema_salad functions file_uri and file_uri_path (#269)
* Use schema_salad functions file_uri and file_uri_path for more correct file URI handling. * Remove dependency on pathlib2.
1 parent b4585d2 commit 92d59e6

File tree

8 files changed

+26
-24
lines changed

8 files changed

+26
-24
lines changed

cwltool/draft2tool.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import avro.schema
1515
import schema_salad.validate as validate
16+
from schema_salad.ref_resolver import file_uri, uri_file_path
1617
import shellescape
1718
from typing import Any, Callable, cast, Generator, Text, Union
1819

@@ -94,11 +95,11 @@ def revmap_file(builder, outdir, f):
9495

9596
split = urlparse.urlsplit(outdir)
9697
if not split.scheme:
97-
outdir = "file://" + outdir
98+
outdir = file_uri(str(outdir))
9899

99100
if "location" in f:
100101
if f["location"].startswith("file://"):
101-
path = f["location"][7:]
102+
path = uri_file_path(f["location"])
102103
revmap_f = builder.pathmapper.reversemap(path)
103104
if revmap_f:
104105
f["location"] = revmap_f[1]

cwltool/load_tool.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@
66
import logging
77
import re
88
import urlparse
9-
import pathlib2
109

1110
from typing import Any, AnyStr, Callable, cast, Dict, Text, Tuple, Union
1211
from ruamel.yaml.comments import CommentedSeq, CommentedMap
1312
from avro.schema import Names
1413
import requests.sessions
1514

16-
17-
from schema_salad.ref_resolver import Loader, Fetcher
15+
from schema_salad.ref_resolver import Loader, Fetcher, file_uri
1816
import schema_salad.validate as validate
1917
from schema_salad.validate import ValidationException
2018
import schema_salad.schema as schema
@@ -44,7 +42,7 @@ def fetch_document(argsworkflow, # type: Union[Text, dict[Text, Any]]
4442
if split.scheme:
4543
uri = argsworkflow
4644
elif os.path.exists(os.path.abspath(argsworkflow)):
47-
uri = pathlib2.Path(os.path.abspath(argsworkflow)).as_uri()
45+
uri = file_uri(str(os.path.abspath(argsworkflow)))
4846
elif resolver:
4947
uri = resolver(document_loader, argsworkflow)
5048

cwltool/main.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from typing import (Union, Any, AnyStr, cast, Callable, Dict, Sequence, Text,
1919
Tuple, Type, IO)
2020

21-
from schema_salad.ref_resolver import Loader, Fetcher
21+
from schema_salad.ref_resolver import Loader, Fetcher, file_uri, uri_file_path
2222
import schema_salad.validate as validate
2323
import schema_salad.jsonld_context
2424
import schema_salad.makedoc
@@ -258,7 +258,7 @@ def __call__(self, parser, namespace, values, option_string=None):
258258
setattr(namespace,
259259
self.dest, # type: ignore
260260
{"class": self.objclass,
261-
"location": "file://%s" % os.path.abspath(cast(AnyStr, values))})
261+
"location": file_uri(str(os.path.abspath(cast(AnyStr, values))))})
262262

263263
class FSAppendAction(argparse.Action):
264264
objclass = None # type: Text
@@ -281,7 +281,7 @@ def __call__(self, parser, namespace, values, option_string=None):
281281
g)
282282
g.append(
283283
{"class": self.objclass,
284-
"location": "file://%s" % os.path.abspath(cast(AnyStr, values))})
284+
"location": file_uri(str(os.path.abspath(cast(AnyStr, values))))})
285285

286286
class FileAction(FSAction):
287287
objclass = "File"
@@ -471,7 +471,7 @@ def load_job_order(args, t, stdin, print_input_deps=False, relative_deps=False,
471471

472472
if print_input_deps:
473473
printdeps(job_order_object, loader, stdout, relative_deps, "",
474-
basedir=u"file://%s/" % input_basedir)
474+
basedir=file_uri(input_basedir+"/"))
475475
return 0
476476

477477
def pathToLoc(p):
@@ -498,7 +498,7 @@ def makeRelative(base, ob):
498498
pass
499499
else:
500500
if u.startswith("file://"):
501-
u = u[7:]
501+
u = uri_file_path(u)
502502
ob["location"] = os.path.relpath(u, base)
503503

504504
def printdeps(obj, document_loader, stdout, relative_deps, uri, basedir=None):
@@ -519,7 +519,7 @@ def loadref(b, u):
519519
if relative_deps == "primary":
520520
base = basedir if basedir else os.path.dirname(uri)
521521
elif relative_deps == "cwd":
522-
base = "file://" + os.getcwd()
522+
base = file_uri(os.getcwd())
523523
else:
524524
raise Exception(u"Unknown relative_deps %s" % relative_deps)
525525

@@ -720,7 +720,7 @@ def main(argsl=None, # type: List[str]
720720
if out is not None:
721721
def locToPath(p):
722722
if p["location"].startswith("file://"):
723-
p["path"] = p["location"][7:]
723+
p["path"] = uri_file_path(p["location"])
724724

725725
adjustDirObjs(out, locToPath)
726726
adjustFileObjs(out, locToPath)

cwltool/pathmapper.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from typing import Any, Callable, Set, Text, Tuple, Union
1010
import schema_salad.validate as validate
1111
from schema_salad.sourceline import SourceLine
12+
from schema_salad.ref_resolver import uri_file_path
1213

1314
_logger = logging.getLogger("cwltool")
1415

@@ -65,15 +66,15 @@ def addLocation(d):
6566

6667
if "basename" not in d:
6768
parse = urlparse.urlparse(d["location"])
68-
d["basename"] = os.path.basename(parse.path)
69+
d["basename"] = os.path.basename(urllib.url2pathname(parse.path))
6970

7071
adjustFileObjs(job, addLocation)
7172
adjustDirObjs(job, addLocation)
7273

7374

7475
def abspath(src, basedir): # type: (Text, Text) -> Text
7576
if src.startswith(u"file://"):
76-
ab = urllib.url2pathname(urlparse.urlparse(src).path)
77+
ab = unicode(uri_file_path(str(src)))
7778
else:
7879
ab = src if os.path.isabs(src) else os.path.join(basedir, src)
7980
return ab

cwltool/process.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import abc
1717
import schema_salad.validate as validate
1818
import schema_salad.schema
19-
from schema_salad.ref_resolver import Loader
19+
from schema_salad.ref_resolver import Loader, file_uri
2020
from schema_salad.sourceline import SourceLine
2121
import avro.schema
2222
from typing import (Any, AnyStr, Callable, cast, Dict, List, Generator, IO, Text,
@@ -220,7 +220,7 @@ def moveIt(src, dst):
220220
stageFiles(pm, moveIt)
221221

222222
def _check_adjust(f):
223-
f["location"] = "file://" + pm.mapper(f["location"])[1]
223+
f["location"] = file_uri(pm.mapper(f["location"])[1])
224224
if "contents" in f:
225225
del f["contents"]
226226
if f["class"] == "File":

cwltool/resolver.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import urllib
44
import urlparse
55

6+
from schema_salad.ref_resolver import file_uri
7+
68
_logger = logging.getLogger("cwltool")
79

810
def resolve_local(document_loader, uri):
@@ -17,14 +19,14 @@ def resolve_local(document_loader, uri):
1719

1820
for s in shares:
1921
if os.path.exists(s):
20-
return ("file://%s" % s)
22+
return file_uri(s)
2123
if os.path.exists("%s.cwl" % s):
22-
return ("file://%s.cwl" % s)
24+
return file_uri(s)
2325
return None
2426

2527
def tool_resolver(document_loader, uri):
2628
for r in [resolve_local]:
2729
ret = r(document_loader, uri)
2830
if ret is not None:
2931
return ret
30-
return "file://" + os.path.abspath(uri)
32+
return file_uri(os.path.abspath(uri))

cwltool/stdfsaccess.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from .pathmapper import abspath
33
import glob
44
import os
5+
from schema_salad.ref_resolver import file_uri
56

67
class StdFsAccess(object):
78

@@ -12,7 +13,7 @@ def _abs(self, p): # type: (Text) -> Text
1213
return abspath(p, self.basedir)
1314

1415
def glob(self, pattern): # type: (Text) -> List[Text]
15-
return ["file://%s" % self._abs(l) for l in glob.glob(self._abs(pattern))]
16+
return [file_uri(str(self._abs(l))) for l in glob.glob(self._abs(pattern))]
1617

1718
def open(self, fn, mode): # type: (Text, Text) -> BinaryIO
1819
return open(self._abs(fn), mode)

setup.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,9 @@
4747
'ruamel.yaml >= 0.12.4',
4848
'rdflib >= 4.2.0, < 4.3.0',
4949
'shellescape >= 3.4.1, < 3.5',
50-
'schema-salad >= 2.1.20161227191302, < 3',
50+
'schema-salad >= 2.2.20170111180227, < 3',
5151
'typing >= 3.5.2, < 3.6',
52-
'cwltest >= 1.0.20160907111242',
53-
'pathlib2 >= 2.1.0'
52+
'cwltest >= 1.0.20161227194859'
5453
],
5554
test_suite='tests',
5655
tests_require=[],

0 commit comments

Comments
 (0)