Skip to content

Commit

Permalink
fix: include --no-cache for (re)install
Browse files Browse the repository at this point in the history
  • Loading branch information
robinvandernoord committed Mar 19, 2024
1 parent 7b04284 commit 8ab4de9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
17 changes: 12 additions & 5 deletions src/uvx/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,16 @@ def output(result: Result[str, Exception]) -> None:


@app.command()
def install(package_name: str, force: bool = False, python: str = ""):
def install(package_name: str, force: bool = False, python: str = "", no_cache: bool = False):
"""Install a package (by pip name)."""
# todo: support 'install .'
output(install_package(package_name, python=python, force=force))
output(install_package(package_name, python=python, force=force, no_cache=no_cache))


@app.command(name="upgrade")
@app.command(name="update")
def upgrade(package_name: str, force: bool = False):
output(upgrade_package(package_name, force=force))
def upgrade(package_name: str, force: bool = False, skip_injected: bool = False, no_cache: bool = False):
output(upgrade_package(package_name, force=force, skip_injected=skip_injected, no_cache=no_cache))


@app.command(name="remove")
Expand All @@ -62,14 +62,21 @@ def uninstall(package_name: str, force: bool = False):


@app.command()
def reinstall(package: str, python: Optional[str] = None, force: bool = False, without_injected: bool = False):
def reinstall(
package: str,
python: Optional[str] = None,
force: bool = False,
without_injected: bool = False,
no_cache: bool = False,
):
"""Uninstall a package (by pip name) and re-install from the original spec (unless a new spec is supplied)."""
output(
reinstall_package(
package,
python=python,
force=force,
with_injected=not without_injected,
no_cache=no_cache,
).map(lambda _: _.replace(" installed", " reinstalled"))
)

Expand Down
22 changes: 18 additions & 4 deletions src/uvx/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ def install_package(
python: Optional[str] = None,
force: bool = False,
extras: Optional[list[str]] = None,
no_cache: bool = False,
) -> Result[str, Exception]:
"""
Install a package in a virtual environment.
Expand All @@ -138,10 +139,16 @@ def install_package(

with virtualenv(venv), exit_on_pb_error():
try:
args = []
text = f"installing {meta.name}"
if extras:
args.extend(extras)
text += f" with {extras}"
animate(uv("pip", "install", meta.install_spec, *extras), text=text)

if no_cache:
args += ["--no-cache"]

animate(uv("pip", "install", meta.install_spec, *args), text=text)

# must still be in the venv for these:
meta.installed_version = get_package_version(meta.name, venv)
Expand All @@ -162,7 +169,11 @@ def install_package(


def reinstall_package(
package_name: str, python: Optional[str] = None, force: bool = False, with_injected: bool = True
package_name: str,
python: Optional[str] = None,
force: bool = False,
with_injected: bool = True,
no_cache: bool = False,
) -> Result[str, Exception]:
"""
Reinstalls a package in a virtual environment.
Expand Down Expand Up @@ -207,6 +218,7 @@ def reinstall_package(
new_install_spec = bool(new_metadata.requested_version or new_metadata.extras)

# install_spec = package_name if new_install_spec or not existing_metadata else existing_metadata.install_spec
metadata: Optional[Metadata] = None
match (new_install_spec, existing_metadata):
case (False, Ok(metadata)):
install_spec = metadata.install_spec
Expand All @@ -219,7 +231,7 @@ def reinstall_package(

uninstall_package(new_metadata.name, force=force)
extras = metadata.injected if (with_injected and metadata and metadata.injected) else []
return install_package(install_spec, python=python, force=force, extras=extras)
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]:
Expand Down Expand Up @@ -262,7 +274,9 @@ def remove_dir(path: Path):
shutil.rmtree(path)


def upgrade_package(package_name: str, force: bool = False) -> Result[str, Exception]:
def upgrade_package(
package_name: str, force: bool = False, skip_injected: bool = False, no_cache: bool = False
) -> 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)
Expand Down

0 comments on commit 8ab4de9

Please sign in to comment.