Skip to content

Commit 95098e3

Browse files
committed
adding function to Download http/s files as input
1 parent 4600bbd commit 95098e3

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

cwltool/pathmapper.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
import stat
66
import uuid
77
from functools import partial
8+
from tempfile import NamedTemporaryFile
9+
10+
import requests
811
from typing import Any, Callable, Dict, Iterable, List, Set, Text, Tuple, Union
912

1013
import schema_salad.validate as validate
@@ -139,6 +142,15 @@ def trim_listing(obj):
139142
if obj.get("location", "").startswith("file://") and "listing" in obj:
140143
del obj["listing"]
141144

145+
# Download http Files
146+
def downloadHttpFile(httpurl):
147+
r = requests.get(httpurl, stream=True)
148+
with NamedTemporaryFile(mode='wb', delete=False) as f:
149+
for chunk in r.iter_content(chunk_size=1024):
150+
if chunk: # filter out keep-alive new chunks
151+
f.write(chunk)
152+
r.close()
153+
return f.name
142154

143155
class PathMapper(object):
144156
"""Mapping of files from relative path provided in the file to a tuple of
@@ -208,15 +220,18 @@ def visit(self, obj, stagedir, basedir, copy=False, staged=False):
208220
self._pathmap[obj["location"]] = MapperEnt(obj["contents"], tgt, "CreateFile", staged)
209221
else:
210222
with SourceLine(obj, "location", validate.ValidationException):
211-
# Dereference symbolic links
212223
deref = ab
213-
if urllib.parse.urlsplit(deref).scheme not in ['http','https']:
224+
if urllib.parse.urlsplit(deref).scheme in ['http','https']:
225+
deref = downloadHttpFile(path)
226+
else:
227+
# Dereference symbolic links
214228
st = os.lstat(deref)
215229
while stat.S_ISLNK(st.st_mode):
216230
rl = os.readlink(deref)
217231
deref = rl if os.path.isabs(rl) else os.path.join(
218232
os.path.dirname(deref), rl)
219233
st = os.lstat(deref)
234+
220235
self._pathmap[path] = MapperEnt(deref, tgt, "WritableFile" if copy else "File", staged)
221236
self.visitlisting(obj.get("secondaryFiles", []), stagedir, basedir, copy=copy, staged=staged)
222237

0 commit comments

Comments
 (0)