Skip to content

Commit

Permalink
fix: improvements in uv binary detection (right venv)
Browse files Browse the repository at this point in the history
  • Loading branch information
robinvandernoord committed Mar 4, 2024
1 parent 2f2a780 commit e874d3f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 24 deletions.
11 changes: 9 additions & 2 deletions src/uvx/_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,15 @@ def get_python_executable(venv: Path):
return str(executable.resolve()) # /usr/bin/python3.xx


def get_package_version(package: str) -> str:
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==
return (_uv["pip", "freeze"] | grep[f"^{package}=="])().strip().split("==")[-1]
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]
4 changes: 2 additions & 2 deletions src/uvx/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,10 @@ def install_package(

with virtualenv(venv), exit_on_pb_error():
try:
animate(uv("pip", "install", package_name), text=f"installing {meta.name}")
animate(uv("pip", "install", meta.install_spec), text=f"installing {meta.name}")

# must still be in the venv for these:
meta.installed_version = get_package_version(meta.name)
meta.installed_version = get_package_version(meta.name, venv)
meta.python = get_python_version(venv)
meta.python_raw = get_python_executable(venv)

Expand Down
66 changes: 46 additions & 20 deletions src/uvx/metadata.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""This file contains Logic related to the .metadata file."""
import json
import sys
import tempfile
import typing
from pathlib import Path
from typing import Optional
Expand Down Expand Up @@ -48,38 +50,62 @@ def check_script_symlinks(self, name: str) -> "Self":
quickle_dec = quickle.Decoder(registry=[Metadata])


@threadful.thread
def resolve_local(spec: str):
# todo: finish
def fake_install(spec: str) -> dict:
_python = plumbum.local[sys.executable]

_python("-m", "uv", "pip", "install", "pip") # ensure we have pip

result = _python("-m", "pip", "install", "--no-deps", "--dry-run", "--ignore-installed", "--report",
"/tmp/out.json", spec)
with tempfile.NamedTemporaryFile() as f:
_python("-m", "pip", "install", "--no-deps", "--dry-run", "--ignore-installed", "--report",
f.name, spec)

return json.load(f)


@threadful.thread
def resolve_local(spec: str) -> tuple[str | None, str | None]:
try:
full_data = fake_install(spec)
install_data = full_data["install"][0]

print(result)
return result
name = install_data['metadata']["name"]
extras = install_data.get('requested_extras')
file_url = install_data["download_info"]["url"]

if extras:
_extras = ",".join(extras)
return f"{name}[{_extras}]", file_url
else:
return name, file_url
except Exception as e:
print('eeeee', e)
return None, None


def collect_metadata(spec: str) -> Metadata:
"""Parse an install spec into a (incomplete) Metadata object."""
try:
parsed_spec = Requirement(spec)

return Metadata(
install_spec=spec,
name=parsed_spec.name,
scripts={}, # postponed
extras=parsed_spec.extras,
requested_version=str(parsed_spec.specifier),
installed_version="", # postponed
python="", # postponed
)
except InvalidRequirement:
threadful.animate(resolve_local(spec), text=f"Trying to install local package '{spec}'")
print("reeeeeeeeeeeeeeee")
exit(1)
except InvalidRequirement as e:
local, path = threadful.animate(resolve_local(spec), text=f"Trying to install local package '{spec}'")

if not local:
raise None

parsed_spec = Requirement(local)

spec = f"{parsed_spec.name} @ {path.removeprefix('file://')}"

return Metadata(
install_spec=spec,
name=parsed_spec.name,
scripts={}, # postponed
extras=parsed_spec.extras,
requested_version=str(parsed_spec.specifier),
installed_version="", # postponed
python="", # postponed
)


def store_metadata(meta: Metadata, venv: Path):
Expand Down

0 comments on commit e874d3f

Please sign in to comment.