1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: Apache-2.0
11 import arvados.collection
16 logger = logging.getLogger('arvados.cwl-runner')
18 def my_formatdate(dt):
19 return email.utils.formatdate(timeval=calendar.timegm(dt.timetuple()),
20 localtime=False, usegmt=True)
22 def my_parsedate(text):
23 parsed = email.utils.parsedate_tz(text)
27 return datetime.datetime(*parsed[:6]) + datetime.timedelta(seconds=parsed[9])
29 # TZ is zero or missing, assume UTC.
30 return datetime.datetime(*parsed[:6])
32 return datetime.datetime(1970, 1, 1)
34 def fresh_cache(url, properties, now):
38 logger.debug("Checking cache freshness for %s using %s", url, pr)
40 if "Cache-Control" in pr:
41 if re.match(r"immutable", pr["Cache-Control"]):
44 g = re.match(r"(s-maxage|max-age)=(\d+)", pr["Cache-Control"])
46 expires = my_parsedate(pr["Date"]) + datetime.timedelta(seconds=int(g.group(2)))
48 if expires is None and "Expires" in pr:
49 expires = my_parsedate(pr["Expires"])
52 # Use a default cache time of 24 hours if upstream didn't set
53 # any cache headers, to reduce redundant downloads.
54 expires = my_parsedate(pr["Date"]) + datetime.timedelta(hours=24)
59 return (now < expires)
61 def remember_headers(url, properties, headers, now):
62 properties.setdefault(url, {})
63 for h in ("Cache-Control", "ETag", "Expires", "Date", "Content-Length"):
65 properties[url][h] = headers[h]
66 if "Date" not in headers:
67 properties[url]["Date"] = my_formatdate(now)
70 def changed(url, properties, now):
71 req = requests.head(url, allow_redirects=True)
72 remember_headers(url, properties, req.headers, now)
74 if req.status_code != 200:
75 raise Exception("Got status %s" % req.status_code)
78 if "ETag" in pr and "ETag" in req.headers:
79 if pr["ETag"] == req.headers["ETag"]:
84 def http_to_keep(api, project_uuid, url, utcnow=datetime.datetime.utcnow):
85 r = api.collections().list(filters=[["properties", "exists", url]]).execute()
89 for item in r["items"]:
90 properties = item["properties"]
91 if fresh_cache(url, properties, now):
93 cr = arvados.collection.CollectionReader(item["portable_data_hash"], api_client=api)
94 return "keep:%s/%s" % (item["portable_data_hash"], cr.keys()[0])
96 if not changed(url, properties, now):
97 # ETag didn't change, same content, just update headers
98 api.collections().update(uuid=item["uuid"], body={"collection":{"properties": properties}}).execute()
99 cr = arvados.collection.CollectionReader(item["portable_data_hash"], api_client=api)
100 return "keep:%s/%s" % (item["portable_data_hash"], cr.keys()[0])
103 req = requests.get(url, stream=True, allow_redirects=True)
105 if req.status_code != 200:
106 raise Exception("Failed to download '%s' got status %s " % (url, req.status_code))
108 remember_headers(url, properties, req.headers, now)
110 if "Content-Length" in properties[url]:
111 cl = int(properties[url]["Content-Length"])
112 logger.info("Downloading %s (%s bytes)", url, cl)
115 logger.info("Downloading %s (unknown size)", url)
117 c = arvados.collection.Collection()
119 if req.headers.get("Content-Disposition"):
120 grp = re.search(r'filename=("((\"|[^"])+)"|([^][()<>@,;:\"/?={} ]+))', req.headers["Content-Disposition"])
126 name = urlparse.urlparse(url).path.split("/")[-1]
131 with c.open(name, "w") as f:
132 for chunk in req.iter_content(chunk_size=1024):
135 loopnow = time.time()
136 if (loopnow - checkpoint) > 20:
137 bps = (float(count)/float(loopnow - start))
139 logger.info("%2.1f%% complete, %3.2f MiB/s, %1.0f seconds left",
140 float(count * 100) / float(cl),
144 logger.info("%d downloaded, %3.2f MiB/s", count, bps/(1024*1024))
147 c.save_new(name="Downloaded from %s" % url, owner_uuid=project_uuid, ensure_unique_name=True)
149 api.collections().update(uuid=c.manifest_locator(), body={"collection":{"properties": properties}}).execute()
151 return "keep:%s/%s" % (c.portable_data_hash(), name)