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

136 lines
5.7 KiB
Python

# ========= Copyright 2026 @ Strukto.AI All Rights Reserved. =========
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ========= Copyright 2026 @ Strukto.AI All Rights Reserved. =========
from mirage.accessor.s3 import S3Accessor
from mirage.cache.index import IndexCacheStore, ResourceType
from mirage.core.s3._client import _client_kwargs, _key, async_session
from mirage.core.timeutil import to_iso_z
from mirage.types import FileStat, FileType, PathSpec
from mirage.utils.errors import enoent
from mirage.utils.filetype import guess_type
from mirage.utils.key_prefix import mount_prefix_of
def _is_not_found(exc: Exception) -> bool:
if hasattr(exc, "response"):
code = exc.response.get("Error", {}).get("Code")
return code in ("404", "NoSuchKey")
return False
async def stat(accessor: S3Accessor,
path: PathSpec,
index: IndexCacheStore = None) -> FileStat:
if isinstance(path, str):
path = PathSpec(virtual=path,
directory=path,
resource_path=path.strip("/"))
virtual = path.virtual if isinstance(path, PathSpec) else path
original_prefix = ""
if isinstance(path, PathSpec):
original_prefix = mount_prefix_of(path.virtual, path.resource_path)
path = path.virtual
if original_prefix and path.startswith(original_prefix):
path = path[len(original_prefix):] or "/"
# A trailing slash ("/s3/csv/") signals the caller treats it as a
# directory. S3 allows both an object at key "csv" AND a prefix "csv/"
# to coexist; without this hint head_object would return the file and
# `ls /s3/csv/` would list the file itself instead of the prefix.
hints_directory = path.endswith("/")
stripped = path.strip("/")
if not stripped:
return FileStat(name="/", type=FileType.DIRECTORY)
# Fast path: check the index cache populated by readdir().
# readdir() stores entries with resource_type="folder" or "file"
# and file sizes, so stat can return instantly for known paths.
if index is not None:
virtual_key = (original_prefix + "/" +
stripped if original_prefix else "/" + stripped)
lookup = await index.get(virtual_key)
if lookup.entry is not None:
entry = lookup.entry
# S3 "folders" are synthetic common-prefixes with no object,
# so readdir() records no time or size for them.
if entry.resource_type == ResourceType.FOLDER:
return FileStat(name=entry.name, type=FileType.DIRECTORY)
# TODO: propagate ETag into IndexCacheEntry so this fast
# path can also carry fingerprint.
return FileStat(
name=entry.name,
size=entry.size,
modified=entry.remote_time or None,
type=guess_type(entry.name),
)
# If the parent directory was already listed by readdir() but
# this path is not among its children, it does not exist.
# This avoids expensive network calls for paths that shells
# probe speculatively (e.g. .git, HEAD, .hg during cd).
parent = virtual_key.rsplit("/", 1)[0] or "/"
parent_listing = await index.list_dir(parent)
if parent_listing.entries is not None:
raise enoent(virtual)
# Slow path: no index cache available, or parent directory not yet
# listed. Hit the network.
config = accessor.config
key = _key(path, config)
session = async_session(config)
async with session.client(**_client_kwargs(config)) as client:
# Try head_object first — works for files. Skipped when the path
# hints a directory (trailing slash), so a coexisting object of the
# same name does not shadow the prefix.
if not hints_directory:
try:
resp = await client.head_object(Bucket=config.bucket, Key=key)
modified = to_iso_z(resp["LastModified"])
etag_raw = resp.get("ETag", "").strip('"')
vid_raw = resp.get("VersionId")
if vid_raw == "null":
vid_raw = None
return FileStat(
name=path.rstrip("/").rsplit("/", 1)[-1],
size=resp["ContentLength"],
modified=modified,
type=guess_type(path),
fingerprint=etag_raw or None,
revision=vid_raw or None,
extra={"etag": etag_raw},
)
except Exception as exc:
if not _is_not_found(exc):
raise
# head_object returned 404 (or was skipped) — check if the path is a
# valid
# prefix (directory). S3/GCS don't have real directory objects,
# so we probe with list_objects_v2 using MaxKeys=1.
pfx = key.rstrip("/") + "/" if key else ""
resp = await client.list_objects_v2(
Bucket=config.bucket,
Prefix=pfx,
Delimiter="/",
MaxKeys=1,
)
if resp.get("CommonPrefixes") or resp.get("Contents"):
return FileStat(
name=path.rstrip("/").rsplit("/", 1)[-1] or "/",
type=FileType.DIRECTORY,
)
raise enoent(virtual)