bcbd1bdb22
Integ / changes (push) Has been skipped
Pre-commit / pre-commit (push) Failing after 1s
CLI exit codes / changes (push) Has been skipped
Test (Install) / changes (push) Has been skipped
Test (Python) / changes (push) Has been skipped
Test (TypeScript) / changes (push) Has been skipped
CLI exit codes / cli-gate (push) Has been cancelled
Test (Install) / test-install-gate (push) Has been cancelled
Integ / integ-gate (push) Has been cancelled
Test (Python) / test-python-gate (push) Has been cancelled
Test (TypeScript) / test-typescript-gate (push) Has been cancelled
Test (Install) / python-minimal (3.12) (push) Has been cancelled
Test (Install) / python-minimal (3.11) (push) Has been cancelled
Test (Install) / python-extra (agno, mirage.agents.agno) (push) Has been cancelled
Test (Install) / python-extra (chroma, mirage.resource.chroma) (push) Has been cancelled
Test (Install) / python-extra (pdf, mirage.core.filetype.pdf) (push) Has been cancelled
Integ / integ (push) Has been cancelled
Integ / integ-database (push) Has been cancelled
Integ / integ-database-ts (push) Has been cancelled
Integ / integ-data (push) Has been cancelled
Integ / integ-ssh (push) Has been cancelled
Integ / integ-ssh-ts (push) Has been cancelled
Test (Python) / audit (push) Has been cancelled
Test (TypeScript) / test (push) Has been cancelled
Test (TypeScript) / python-fs-shim (push) Has been cancelled
CLI exit codes / Python CLI (push) Has been cancelled
CLI exit codes / TypeScript CLI (push) Has been cancelled
CLI exit codes / Cross-language snapshot interop (push) Has been cancelled
Test (Python) / test (push) Has been cancelled
Test (Python) / import-isolation (deepagents, openai, mirage.agents.openai_agents) (push) Has been cancelled
Test (Python) / import-isolation (deepagents, pydantic-ai, mirage.agents.pydantic_ai) (push) Has been cancelled
Integ / integ-ts (push) Has been cancelled
Integ / integ-fuse (push) Has been cancelled
Test (Install) / python-extra (databricks, mirage.resource.databricks_volume) (push) Has been cancelled
Test (Install) / python-extra (deepagents, mirage.agents.langchain) (push) Has been cancelled
Test (Install) / python-extra (email, mirage.resource.email) (push) Has been cancelled
Test (Install) / python-extra (fuse, mirage.fuse.mount) (push) Has been cancelled
Test (Install) / python-extra (hdf5, mirage.core.filetype.hdf5) (push) Has been cancelled
Test (Install) / python-extra (hf, mirage.resource.hf_buckets) (push) Has been cancelled
Test (Install) / python-extra (lancedb, mirage.resource.lancedb) (push) Has been cancelled
Test (Install) / python-extra (langfuse, mirage.resource.langfuse) (push) Has been cancelled
Test (Install) / python-extra (mongodb, mirage.resource.mongodb) (push) Has been cancelled
Test (Install) / python-extra (nextcloud, mirage.resource.nextcloud) (push) Has been cancelled
Test (Install) / python-extra (openai, mirage.agents.openai_agents) (push) Has been cancelled
Test (Install) / python-extra (openhands, mirage.agents.openhands, 3.12) (push) Has been cancelled
Test (Install) / python-extra (parquet, mirage.core.filetype.parquet) (push) Has been cancelled
Test (Install) / python-extra (postgres, mirage.resource.postgres) (push) Has been cancelled
Test (Install) / python-extra (pydantic-ai, mirage.agents.pydantic_ai) (push) Has been cancelled
Test (Install) / python-extra (qdrant, mirage.resource.qdrant) (push) Has been cancelled
Test (Install) / python-extra (redis, mirage.resource.redis) (push) Has been cancelled
Test (Install) / python-extra (s3, mirage.resource.s3) (push) Has been cancelled
Test (Install) / python-extra (ssh, mirage.resource.ssh) (push) Has been cancelled
Test (Install) / ts-minimal (push) Has been cancelled
85 lines
3.0 KiB
Python
85 lines
3.0 KiB
Python
import io
|
|
import zipfile
|
|
from collections.abc import Awaitable, Callable
|
|
|
|
from mirage.accessor.base import Accessor
|
|
from mirage.io.types import ByteSource, IOResult
|
|
from mirage.types import PathSpec
|
|
from mirage.utils.key_prefix import mount_prefix_of
|
|
|
|
|
|
def _resolve_dest(d: str | PathSpec | None, mount_prefix: str) -> str:
|
|
d_str = d.virtual if isinstance(d, PathSpec) else d
|
|
dest_raw = d_str if d_str else "/"
|
|
if mount_prefix and dest_raw.startswith(mount_prefix + "/"):
|
|
return dest_raw[len(mount_prefix):]
|
|
if dest_raw == mount_prefix:
|
|
return "/"
|
|
return dest_raw
|
|
|
|
|
|
async def unzip(
|
|
paths: list[PathSpec],
|
|
*,
|
|
read_bytes: Callable[..., Awaitable[bytes]],
|
|
write_bytes: Callable[..., Awaitable[None]],
|
|
mkdir_fn: Callable[..., Awaitable[None]],
|
|
accessor: Accessor | None = None,
|
|
o: bool = False,
|
|
args_l: bool = False,
|
|
d: str | PathSpec | None = None,
|
|
q: bool = False,
|
|
p: bool = False,
|
|
t: bool = False,
|
|
) -> tuple[ByteSource | None, IOResult]:
|
|
if not paths:
|
|
raise ValueError("unzip: missing operand")
|
|
archive_path = paths[0]
|
|
data = await read_bytes(accessor, archive_path)
|
|
with zipfile.ZipFile(io.BytesIO(data), "r") as zf:
|
|
if args_l:
|
|
lines = [" Length Name", "--------- ----"]
|
|
for info in zf.infolist():
|
|
lines.append(f"{info.file_size:>9} {info.filename}")
|
|
return ("\n".join(lines) + "\n").encode(), IOResult()
|
|
if t:
|
|
bad = zf.testzip()
|
|
if bad is None:
|
|
msg = f"No errors detected in {archive_path.virtual}\n"
|
|
else:
|
|
msg = f"first bad file: {bad}\n"
|
|
return msg.encode(), IOResult()
|
|
if p:
|
|
chunks: list[bytes] = []
|
|
for info in zf.infolist():
|
|
if not info.is_dir():
|
|
chunks.append(zf.read(info.filename))
|
|
return b"".join(chunks), IOResult()
|
|
mount_prefix = mount_prefix_of(
|
|
archive_path.virtual, archive_path.resource_path) if isinstance(
|
|
archive_path, PathSpec) else ""
|
|
dest = _resolve_dest(d, mount_prefix)
|
|
writes: dict[str, bytes] = {}
|
|
output_lines: list[str] = []
|
|
for info in zf.infolist():
|
|
if info.is_dir():
|
|
continue
|
|
content = zf.read(info.filename)
|
|
entry_name = info.filename.lstrip("/")
|
|
out_path = dest.rstrip("/") + "/" + entry_name
|
|
parent = out_path.rsplit("/", 1)[0] or "/"
|
|
if parent != "/":
|
|
await mkdir_fn(accessor, parent, parents=True)
|
|
await write_bytes(accessor, out_path, content)
|
|
report_path = (mount_prefix +
|
|
out_path) if mount_prefix else out_path
|
|
writes[out_path] = content
|
|
if not q:
|
|
output_lines.append(f" inflating: {report_path}")
|
|
output = ("\n".join(output_lines) +
|
|
"\n").encode() if output_lines else None
|
|
return output, IOResult(writes=writes)
|
|
|
|
|
|
__all__ = ["unzip"]
|