Skip to content

Commit

Permalink
feat(uninject): implemented eject
Browse files Browse the repository at this point in the history
  • Loading branch information
robinvandernoord committed Apr 17, 2024
1 parent e4b2099 commit fbd2f6e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 9 deletions.
17 changes: 11 additions & 6 deletions src/uvx/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
import subprocess # nosec
import sys
import typing
from datetime import datetime
from pathlib import Path
from typing import Annotated
Expand Down Expand Up @@ -112,7 +113,6 @@ def uninstall(
output(uninstall_package(package_name, force=force).map(lambda version: f"🗑️ {package_name}{version} removed!"))



@app.command()
def uninstall_all(
force: Annotated[
Expand All @@ -128,6 +128,7 @@ def uninstall_all(
for venv_name, _ in list_packages():
uninstall(venv_name, force=force)


@app.command()
def reinstall(
package: str,
Expand Down Expand Up @@ -163,6 +164,8 @@ def reinstall_all(
for venv_name, _ in list_packages():
reinstall(venv_name, python=python, force=force, without_injected=without_injected, no_cache=no_cache)


@app.command()
def inject(into: str, package_specs: list[str]):
"""Install additional packages to a virtual environment managed by uvx."""
output(
Expand All @@ -172,13 +175,14 @@ def inject(into: str, package_specs: list[str]):
)
)

@app.command(name='eject')
@app.command(name='uninject')
def uninject(outof: str, package_specs: list[str]):
output(

@app.command(name="eject")
@app.command(name="uninject")
def uninject(outof: str, package_specs: typing.Annotated[list[str], typer.Argument()] = None):
output(
eject_packages(
outof,
set(package_specs),
set(package_specs or []),
)
)

Expand Down Expand Up @@ -312,6 +316,7 @@ def list_venvs(short: bool = False, verbose: bool = False, json: bool = False):
else:
_list_normal(name, metadata, verbose=verbose)


# todo: run


Expand Down
38 changes: 35 additions & 3 deletions src/uvx/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,41 @@ def inject_packages(into: str, package_specs: set[str]) -> Result[str, Exception

return Ok(f"💉 Injected {package_specs} into {meta.name}.") # :needle:

def eject_packages(outouf: str, package_specs: set[str]) -> Result[str, Exception]:
print(f'remove {package_specs} from {outouf}')
return Ok('-')

def eject_packages(outof: str, package_specs: set[str]) -> Result[str, Exception]:
"""Uninstall extra libraries into a package-specific venv."""
match collect_metadata(outof):
case Err(e):
return Err(e)
case Ok(meta):
# just bind meta
...

workdir = ensure_local_folder()
venv = workdir / "venvs" / meta.name

if not venv.exists():
return Err(ValueError(f"'{meta.name}' was not previously installed. Please run 'uvx install {outof}' first."))

# after getting the right venv, load actual metadata (with fallback to previous emptier metadata):
meta = read_metadata(venv).unwrap_or(meta)

if not package_specs:
package_specs = meta.injected
if not package_specs:
return Err(ValueError(f"⚠️ No previous packages to uninject."))

with virtualenv(venv), exit_on_pb_error():
try:
animate(uv("pip", "uninstall", *package_specs), text=f"ejecting {package_specs}")
except plumbum.ProcessExecutionError as e:
return Err(e)

meta.injected = (meta.injected or set()) - package_specs
store_metadata(meta, venv)

return Ok(f"⏏️ Uninjected {package_specs} into {meta.name}.") # :eject:


def remove_dir(path: Path):
"""
Expand Down

0 comments on commit fbd2f6e

Please sign in to comment.