enable launch from local meca file

pull/1335/head
stevejpurves 2024-07-07 17:44:58 -04:00
rodzic 85ce3002de
commit 850751e02c
2 zmienionych plików z 28 dodań i 12 usunięć

Wyświetl plik

@ -153,9 +153,9 @@ class Repo2Docker(Application):
contentproviders.Hydroshare, contentproviders.Hydroshare,
contentproviders.Swhid, contentproviders.Swhid,
contentproviders.CKAN, contentproviders.CKAN,
contentproviders.Meca,
contentproviders.Mercurial, contentproviders.Mercurial,
contentproviders.Git, contentproviders.Git,
contentproviders.Meca,
], ],
config=True, config=True,
help=""" help="""

Wyświetl plik

@ -1,3 +1,4 @@
import hashlib
import os import os
import shutil import shutil
import tempfile import tempfile
@ -78,35 +79,50 @@ class Meca(ContentProvider):
) )
def detect(self, spec, ref=None, extra_args=None): def detect(self, spec, ref=None, extra_args=None):
"""`spec` contains a faux protocol of meca+http[s] for detection purposes """`spec` contains a faux protocol of http[s]+meca for detection purposes
and we assume `spec` trusted as a reachable MECA bundle from an allowed origin and we assume `spec` trusted as a reachable MECA bundle from an allowed origin
(binderhub RepoProvider class is already checking for this). (binderhub RepoProvider class is already checking for this).
An other HEAD check in made here in order to get the content-length header An other HEAD check in made here in order to get the content-length header
""" """
parsed = urlparse(spec) is_local_file = False
if not parsed.scheme.endswith("+meca"): if spec.endswith(".meca.zip") and os.path.isfile(spec):
return None url = os.path.abspath(spec)
parsed = parsed._replace(scheme=parsed.scheme[:-5]) is_local_file = True
url = urlunparse(parsed) with open(url, "rb") as f:
file_hash = hashlib.blake2b()
while chunk := f.read(8192):
file_hash.update(chunk)
changes_with_content = file_hash.hexdigest()
else:
parsed = urlparse(spec)
if not parsed.scheme.endswith("+meca"):
return None
parsed = parsed._replace(scheme=parsed.scheme[:-5])
url = urlunparse(parsed)
headers = self.session.head(url).headers headers = self.session.head(url).headers
changes_with_content = headers.get("ETag") or headers.get("Content-Length") changes_with_content = headers.get("ETag") or headers.get("Content-Length")
self.hashed_slug = get_hashed_slug(url, changes_with_content) self.hashed_slug = get_hashed_slug(url, changes_with_content)
return {"url": url, "slug": self.hashed_slug} return {"url": url, "slug": self.hashed_slug, "is_local_file": is_local_file}
def fetch(self, spec, output_dir, yield_output=False): def fetch(self, spec, output_dir, yield_output=False):
hashed_slug = spec["slug"] hashed_slug = spec["slug"]
url = spec["url"] url = spec["url"]
is_local_file = spec["is_local_file"]
yield f"Creating temporary directory.\n" yield f"Creating temporary directory.\n"
with tempfile.TemporaryDirectory() as tmpdir: with tempfile.TemporaryDirectory() as tmpdir:
yield f"Temporary directory created at {tmpdir}.\n" yield f"Temporary directory created at {tmpdir}.\n"
yield f"Fetching MECA Bundle {url}.\n" if is_local_file:
zip_filename = fetch_zipfile(self.session, url, tmpdir) yield f"Found MECA Bundle {url}.\n"
zip_filename = url
else:
yield f"Fetching MECA Bundle {url}.\n"
zip_filename = fetch_zipfile(self.session, url, tmpdir)
yield f"Extracting MECA Bundle {zip_filename}.\n" yield f"Extracting MECA Bundle {zip_filename}.\n"
is_meca, bundle_dir = extract_validate_and_identify_bundle( is_meca, bundle_dir = extract_validate_and_identify_bundle(