Skip to content

Commit

Permalink
enable launch from local meca file
Browse files Browse the repository at this point in the history
  • Loading branch information
stevejpurves committed Jul 7, 2024
1 parent 85ce300 commit 850751e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
2 changes: 1 addition & 1 deletion repo2docker/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,9 @@ def _default_log_level(self):
contentproviders.Hydroshare,
contentproviders.Swhid,
contentproviders.CKAN,
contentproviders.Meca,
contentproviders.Mercurial,
contentproviders.Git,
contentproviders.Meca,
],
config=True,
help="""
Expand Down
40 changes: 28 additions & 12 deletions repo2docker/contentproviders/meca.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import hashlib
import os
import shutil
import tempfile
Expand Down Expand Up @@ -78,35 +79,50 @@ def __init__(self):
)

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
(binderhub RepoProvider class is already checking for this).
An other HEAD check in made here in order to get the content-length header
"""
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
changes_with_content = headers.get("ETag") or headers.get("Content-Length")
is_local_file = False
if spec.endswith(".meca.zip") and os.path.isfile(spec):
url = os.path.abspath(spec)
is_local_file = True
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
changes_with_content = headers.get("ETag") or headers.get("Content-Length")

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):
hashed_slug = spec["slug"]
url = spec["url"]
is_local_file = spec["is_local_file"]

yield f"Creating temporary directory.\n"
with tempfile.TemporaryDirectory() as tmpdir:
yield f"Temporary directory created at {tmpdir}.\n"

yield f"Fetching MECA Bundle {url}.\n"
zip_filename = fetch_zipfile(self.session, url, tmpdir)
if is_local_file:
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"
is_meca, bundle_dir = extract_validate_and_identify_bundle(
Expand Down

0 comments on commit 850751e

Please sign in to comment.