Skip to content

Commit

Permalink
feat: implemented first version of uvx upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
robinvandernoord committed Apr 5, 2024
1 parent d48524a commit ffcb73a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/uvx/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,9 @@ def runpython(venv: str, ctx: Context):
subprocess.run([python, *ctx.args]) # nosec


# todo:
# self-upgrade (uv and uvx)

# ...
# upgrade-all


def add_to_bashrc(text: str, with_comment: bool = True):
Expand Down
26 changes: 18 additions & 8 deletions src/uvx/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,6 @@ def upgrade_package(
) -> Result[str, Exception]:
# run `uv pip install --upgrade package` with requested install spec (version, extras, injected)
# if --force is used, the previous version is ignored.
print("upgrade", package_name)

match collect_metadata(package_name):
case Err(e):
return Err(e)
Expand All @@ -296,13 +294,15 @@ def upgrade_package(

meta = read_metadata(venv).unwrap_or(spec_metadata)

old_version = meta.installed_version

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
injected = [] if skip_injected else (meta.injected or [])
version = spec_metadata.requested_version or ("" if force else meta.requested_version)
options = []
if force:
Expand All @@ -312,15 +312,25 @@ def upgrade_package(
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}")
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: upgraded or the same msg")
meta.requested_version = version
new_version = meta.installed_version = get_package_version(meta.name, venv)
store_metadata(meta, venv)

if old_version == new_version:
# todo: if meta.requested_version - warn
msg = f"🌟 '{package_name}' is already up to date at version {new_version}!"
if meta.requested_version:
msg += f"\n💡 This package was installed with a version constraint ({meta.requested_version}). If you want to ignore this constraint, use `uvx upgrade --force {package_name}`."

else:
msg = f"🚀 Successfully updated '{package_name}' from version {old_version} to version {new_version}!"

return Ok(msg)


def uninstall_package(package_name: str, force: bool = False) -> Result[str, Exception]:
Expand Down

0 comments on commit ffcb73a

Please sign in to comment.