Skip to content

Commit 218f8b5

Browse files
committed
Using cache-control to cache downloaded input files
1 parent 0ae3209 commit 218f8b5

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

cwltool/pathmapper.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
from tempfile import NamedTemporaryFile
99

1010
import requests
11+
from cachecontrol import CacheControl
12+
from cachecontrol.caches import FileCache
1113
from typing import Any, Callable, Dict, Iterable, List, Set, Text, Tuple, Union
1214

1315
import schema_salad.validate as validate
@@ -144,13 +146,28 @@ def trim_listing(obj):
144146

145147
# Download http Files
146148
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=16384):
150-
if chunk: # filter out keep-alive new chunks
151-
f.write(chunk)
152-
r.close()
153-
return f.name
149+
cache_session = None
150+
if "HOME" in os.environ:
151+
cache_session = CacheControl(
152+
requests.Session(),
153+
cache=FileCache(
154+
os.path.join(os.environ["HOME"], ".cache", "cwltool")))
155+
elif "TMP" in os.environ:
156+
cache_session = CacheControl(
157+
requests.Session(),
158+
cache=FileCache(os.path.join(os.environ["TMP"], ".cache", "cwltool")))
159+
else:
160+
cache_session = CacheControl(
161+
requests.Session(),
162+
cache=FileCache("/tmp", ".cache", "cwltool"))
163+
164+
r = cache_session.get(httpurl, stream=True)
165+
with NamedTemporaryFile(mode='wb', delete=False) as f:
166+
for chunk in r.iter_content(chunk_size=16384):
167+
if chunk: # filter out keep-alive new chunks
168+
f.write(chunk)
169+
r.close()
170+
return f.name
154171

155172
class PathMapper(object):
156173
"""Mapping of files from relative path provided in the file to a tuple of

0 commit comments

Comments
 (0)