Skip to content

Commit

Permalink
feat: work in progress on uvx upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
robinvandernoord committed Apr 2, 2024
1 parent fc4cc6a commit d48524a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/uvx/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def inject(into: str, package_specs: list[str]):
output(
inject_packages(
into,
package_specs,
set(package_specs),
)
)

Expand Down
38 changes: 30 additions & 8 deletions src/uvx/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ def reinstall_package(
return install_package(install_spec, python=python, force=force, extras=extras, no_cache=no_cache)


def inject_packages(into: str, package_specs: list[str]) -> Result[str, Exception]:
def inject_packages(into: str, package_specs: set[str]) -> Result[str, Exception]:
match collect_metadata(into):
case Err(e):
return Err(e)
Expand All @@ -257,7 +257,7 @@ def inject_packages(into: str, package_specs: list[str]) -> Result[str, Exceptio
except plumbum.ProcessExecutionError as e:
return Err(e)

meta.injected = (meta.injected or []) + package_specs
meta.injected = (meta.injected or set()) | package_specs
store_metadata(meta, venv)

return Ok(f"💉 Injected {package_specs} into {meta.name}.") # :needle:
Expand Down Expand Up @@ -292,13 +292,35 @@ def upgrade_package(
venv = workdir / "venvs" / spec_metadata.name

if not venv.exists():
return Err(
NotADirectoryError(
f"No virtualenv for '{package_name}', stopping. Use '--force' to remove an executable with that name anyway."
)
)
return Err(NotADirectoryError(f"No virtualenv for '{package_name}', stopping. Use 'uvx install' instead."))

meta = read_metadata(venv).unwrap_or(spec_metadata)

with virtualenv(venv), exit_on_pb_error():
# pip upgrade package[extras]==version *injected
# if version spec in spec_metadata use that instead
# if --force, drop version spec
base_pkg = meta.name
extras = meta.extras
injected = [] if skip_injected else meta.injected
version = spec_metadata.requested_version or ("" if force else meta.requested_version)
options = []
if force:
options.append("--no-cache")

upgrade_spec = base_pkg + version
if extras:
upgrade_spec += "[" + ",".join(extras) + "]"

# todo: get version before and after

try:
print("pip", "install", "--upgrade", upgrade_spec, *injected, *options)
# animate(uv("pip", "install", "--upgrade", upgrade_spec, *injected, *options), text=f"upgrading {base_pkg}")
except plumbum.ProcessExecutionError as e:
return Err(e)

return Ok("todo")
return Ok("TODO: upgraded or the same msg")


def uninstall_package(package_name: str, force: bool = False) -> Result[str, Exception]:
Expand Down
2 changes: 1 addition & 1 deletion src/uvx/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Metadata(msgspec.Struct, array_like=True):
installed_version: str
python: str = ""
python_raw: str = ""
injected: Optional[list[str]] = None
injected: Optional[set[str]] = None

@typing.overload
def _convert_type(self, value: set[V]) -> list[V]: # type: ignore
Expand Down

0 comments on commit d48524a

Please sign in to comment.