Skip to content

Commit

Permalink
fix: metadata to msgpack, allow local install
Browse files Browse the repository at this point in the history
  • Loading branch information
robinvandernoord committed Mar 5, 2024
1 parent e874d3f commit 1880062
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 19 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ dependencies = [
"plumbum",
"threadful>=0.2",
"rich",
"quickle",
"msgspec",
"packaging",
]

Expand Down
10 changes: 3 additions & 7 deletions src/uvx/_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,8 @@ def get_python_executable(venv: Path):
def get_package_version(package: str, venv: Path) -> str:
"""Get the currently installed version of a specific package."""
# assumes `with virtualenv(venv)` block executing this function
# uv pip freeze | grep ^su6==
python = venv / "bin" / "python"

print(
python,
plumbum.local[python]["-m", "uv"]["pip", "freeze"]()
)

return (plumbum.local[python]["-m", "uv"]["pip", "freeze"] | grep[f"^{package}=="])().strip().split("==")[-1]
regex = f"^({package}==|{package} @)"
line: str = (plumbum.local[python]["-m", "uv"]["pip", "freeze"] | grep["-E", regex])().strip()
return (line.split("@")[-1] if "@" in line else line.split("==")[-1]).strip()
2 changes: 1 addition & 1 deletion src/uvx/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def reinstall_package(package_name: str, python: Optional[str] = None, force: bo
workdir = ensure_local_folder()
venv = workdir / "venvs" / new_metadata.name

if not venv.exists():
if not venv.exists() and not force:
rich.print(
f"'{new_metadata.name}' was not previously installed. " f"Please run 'uvx install {package_name}' instead."
)
Expand Down
19 changes: 9 additions & 10 deletions src/uvx/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from typing import Optional

import plumbum
import quickle # type: ignore
import msgspec
import threadful
from packaging.requirements import Requirement, InvalidRequirement

Expand All @@ -18,7 +18,7 @@
from typing_extensions import Self


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

name: str
Expand Down Expand Up @@ -46,8 +46,8 @@ def check_script_symlinks(self, name: str) -> "Self":
return self


quickle_enc = quickle.Encoder(registry=[Metadata])
quickle_dec = quickle.Decoder(registry=[Metadata])
encoder = msgspec.msgpack.Encoder()
decoder = msgspec.msgpack.Decoder(type=Metadata)


def fake_install(spec: str) -> dict:
Expand Down Expand Up @@ -77,8 +77,7 @@ def resolve_local(spec: str) -> tuple[str | None, str | None]:
return f"{name}[{_extras}]", file_url
else:
return name, file_url
except Exception as e:
print('eeeee', e)
except Exception:
return None, None


Expand All @@ -90,8 +89,8 @@ def collect_metadata(spec: str) -> Metadata:
except InvalidRequirement as e:
local, path = threadful.animate(resolve_local(spec), text=f"Trying to install local package '{spec}'")

if not local:
raise None
if not local or not path:
raise e

parsed_spec = Requirement(local)

Expand All @@ -111,7 +110,7 @@ def collect_metadata(spec: str) -> Metadata:
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(quickle_enc.dumps(meta))
f.write(encoder.encode(meta))


def read_metadata(venv: Path) -> Metadata | None:
Expand All @@ -121,4 +120,4 @@ def read_metadata(venv: Path) -> Metadata | None:
return None

with metafile.open("rb") as f:
return typing.cast(Metadata, quickle_dec.loads(f.read()))
return typing.cast(Metadata, decoder.decode(f.read()))

0 comments on commit 1880062

Please sign in to comment.