Skip to content

Commit

Permalink
fix: backport .metadata header from v2 to v1
Browse files Browse the repository at this point in the history
  • Loading branch information
robinvandernoord committed Apr 27, 2024
1 parent e2fd52f commit 9b9b82b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/uvx/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ def uninject(


def system_supports_uvx2() -> bool:
"""Check whether current system supports uvx 2.x."""
return sys.platform in UVX2_PLATFORMS and platform.machine() in UVX2_ARCH


Expand Down
13 changes: 12 additions & 1 deletion src/uvx/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
V = typing.TypeVar("V", bound=typing.Any)


# tells 'file' that a .metadata file is 'data' (instead of making it guess)
# U V X SOH version(2) STX (padding):
MAGIC_HEADER = bytes((0x55, 0x56, 0x58, 0x01, 0x32, 0x04, 0x00)) # hex, 7 bytes


class Metadata(msgspec.Struct, array_like=True):
"""Structure of the .metadata file."""

Expand Down Expand Up @@ -126,6 +131,7 @@ def collect_metadata(spec: str) -> Result[Metadata, InvalidRequirement]:
def store_metadata(meta: Metadata, venv: Path):
"""Save the metadata struct to .metadata in a venv."""
with (venv / ".metadata").open("wb") as f:
f.write(MAGIC_HEADER)
f.write(encoder.encode(meta))


Expand All @@ -136,5 +142,10 @@ def read_metadata(venv: Path) -> Maybe[Metadata]:
return Empty()

with metafile.open("rb") as f:
data = decoder.decode(f.read()) # type: Metadata
raw_b = f.read()

if raw_b.startswith(MAGIC_HEADER):
raw_b = raw_b[len(MAGIC_HEADER) :]

data = decoder.decode(raw_b) # type: Metadata
return Ok(data)

0 comments on commit 9b9b82b

Please sign in to comment.