Files
wehub-resource-sync 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
chore: import upstream snapshot with attribution
2026-07-13 12:30:44 +08:00

104 lines
3.6 KiB
Python

from collections.abc import AsyncIterator, Awaitable, Callable
from mirage.accessor.base import Accessor
from mirage.commands.builtin.utils.stream import _resolve_source
from mirage.commands.spec.types import CommandName
from mirage.commands.spec.usage import extra_operand_error
from mirage.io.async_line_iterator import AsyncLineIterator
from mirage.io.types import ByteSource, IOResult
from mirage.types import PathSpec
def _alpha_suffix(index: int, length: int) -> str:
chars: list[str] = []
for _ in range(length):
chars.append(chr(ord("a") + index % 26))
index //= 26
return "".join(reversed(chars))
def _numeric_suffix(index: int, length: int) -> str:
return str(index).zfill(length)
async def split(
paths: list[PathSpec],
*,
read_stream: Callable[..., AsyncIterator[bytes]],
write_bytes: Callable[..., Awaitable[None]],
accessor: Accessor | None = None,
stdin: AsyncIterator[bytes] | bytes | None = None,
lines_per_file: int = 0,
byte_limit: int = 0,
n_chunks: int = 0,
suffix_len: int = 2,
numeric_suffix: bool = False,
) -> tuple[ByteSource | None, IOResult]:
if len(paths) > 2:
raise extra_operand_error(CommandName.SPLIT, paths[2].raw_path)
prefix_name = paths[1].mount_path if len(paths) >= 2 else "x"
if lines_per_file == 0 and byte_limit == 0 and n_chunks == 0:
lines_per_file = 1000
suffix_fn = _numeric_suffix if numeric_suffix else _alpha_suffix
if paths:
source: AsyncIterator[bytes] = read_stream(accessor, paths[0])
else:
source = _resolve_source(stdin)
writes: dict[str, bytes] = {}
file_idx = 0
if n_chunks > 0:
all_data = bytearray()
async for chunk in source:
all_data.extend(chunk)
total = len(all_data)
chunk_size = max(1, (total + n_chunks - 1) // n_chunks)
offset = 0
for i in range(n_chunks):
part = bytes(all_data[offset:offset + chunk_size])
if not part:
break
out_path = prefix_name + suffix_fn(i, suffix_len)
await write_bytes(accessor, out_path, part)
writes[out_path] = part
offset += chunk_size
elif byte_limit > 0:
buf = bytearray()
async for chunk in source:
buf.extend(chunk)
while len(buf) >= byte_limit:
out_path = prefix_name + suffix_fn(file_idx, suffix_len)
data = bytes(buf[:byte_limit])
await write_bytes(accessor, out_path, data)
writes[out_path] = data
buf = buf[byte_limit:]
file_idx += 1
if buf:
out_path = prefix_name + suffix_fn(file_idx, suffix_len)
data = bytes(buf)
await write_bytes(accessor, out_path, data)
writes[out_path] = data
else:
line_buf: list[bytes] = []
async for line in AsyncLineIterator(source):
line_buf.append(line)
if len(line_buf) >= lines_per_file:
out_path = prefix_name + suffix_fn(file_idx, suffix_len)
data = b"\n".join(line_buf) + b"\n"
await write_bytes(accessor, out_path, data)
writes[out_path] = data
line_buf = []
file_idx += 1
if line_buf:
out_path = prefix_name + suffix_fn(file_idx, suffix_len)
data = b"\n".join(line_buf) + b"\n"
await write_bytes(accessor, out_path, data)
writes[out_path] = data
return None, IOResult(writes=writes)
__all__ = ["split"]