rules_zapp/zapp/support/unpack.py

55 lines
1.2 KiB
Python
Raw Normal View History

2021-08-09 15:26:06 +00:00
"""Conditionally unpack a zapp (and its deps)."""
import os
import sys
2021-08-09 15:26:06 +00:00
from pathlib import Path
from zipfile import ZipFile
from zapp.support.manifest import manifest
2021-08-09 15:26:06 +00:00
def cache_root() -> Path:
return Path(os.path.join(os.path.expanduser("~"))) / ".cache" / "zapp"
def cache_wheel_root():
return cache_root() / "wheels"
def cache_wheel_path(wheel: str) -> Path:
return cache_wheel_root() / wheel
def cache_zapp_root():
return cache_root() / "zapps"
def cache_zapp_path(fingerprint):
return cache_zapp_root() / fingerprint
def unpack_deps():
"""Unpack deps, populating and updating the host's cache."""
# Create the cache dir as needed
cache_wheel_root().mkdir(parents=True, exist_ok=True)
# For each wheel, touch the existing cached wheel or unpack this one.
with ZipFile(sys.argv[0], "r") as zf:
for whl, config in manifest()["wheels"].items():
2021-08-09 15:26:06 +00:00
cached_whl = cache_wheel_path(whl)
if cached_whl.exists():
cached_whl.touch()
else:
with open(cached_whl, "wb") as of:
of.write(zf.read(".deps/" + whl))
sys.path.insert(0, str(cached_whl))
def main():
"""Inspect the manifest."""
unpack_deps()