feat: generalize viv exe

now can be used to any binary within the environment
This commit is contained in:
Daylin Morgan 2023-06-02 13:49:56 -05:00
parent 6c2bb1b84f
commit 993e0e699b
Signed by: daylin
GPG key ID: C1E52E7DD81DF79F

View file

@ -14,7 +14,6 @@ import itertools
import json import json
import os import os
import re import re
import shlex
import shutil import shutil
import site import site
import subprocess import subprocess
@ -51,7 +50,7 @@ from typing import (
from urllib.error import HTTPError from urllib.error import HTTPError
from urllib.request import urlopen from urllib.request import urlopen
__version__ = "23.5a4-41-gf069eaf-dev" __version__ = "23.5a4-43-gcdeed9f-dev"
class Spinner: class Spinner:
@ -1018,22 +1017,23 @@ class Viv:
vivenv.show() vivenv.show()
def exe(self, args: Namespace) -> None: def exe(self, args: Namespace) -> None:
"""run python/pip in existing vivenv""" """\
run binary/script in existing vivenv
examples:
viv exe <vivenv> pip -- list
viv exe <vivenv> python -- script.py
"""
vivenv = self._match_vivenv(args.vivenv) vivenv = self._match_vivenv(args.vivenv)
bin = vivenv.path / "bin" / args.cmd
pip_path, python_path = (vivenv.path / "bin" / cmd for cmd in ("pip", "python")) if not bin.exists():
# todo check for vivenv error(f"{args.cmd} does not exist in {vivenv.name}", code=1)
echo(f"executing command within {vivenv.name}")
cmd = ( cmd = [bin, *args.rest]
f"{pip_path} {' '.join(args.cmd)}"
if args.subcmd == "pip"
else f"{python_path} {' '.join(args.cmd)}"
) + " ".join(args.rest)
echo(f"executing {cmd}") run(cmd, verbose=True)
run(shlex.split(cmd), verbose=True)
def info(self, args: Namespace) -> None: def info(self, args: Namespace) -> None:
"""get metadata about a vivenv""" """get metadata about a vivenv"""
@ -1267,7 +1267,7 @@ class Cli:
), ),
], ],
("remove",): [Arg("vivenv", help="name/hash of vivenv", nargs="*")], ("remove",): [Arg("vivenv", help="name/hash of vivenv", nargs="*")],
("exe|pip", "exe|python", "info"): [Arg("vivenv", help="name/hash of vivenv")], ("exe", "info"): [Arg("vivenv", help="name/hash of vivenv")],
("list", "info"): [ ("list", "info"): [
Arg( Arg(
"--json", "--json",
@ -1339,11 +1339,10 @@ class Cli:
action="store_true", action="store_true",
) )
], ],
("exe|python", "exe|pip"): [ ("exe"): [
Arg( Arg(
"cmd", "cmd",
help="command to to execute", help="command to to execute",
nargs="*",
) )
], ],
} }
@ -1362,10 +1361,10 @@ class Cli:
) )
).update( ).update(
{ {
"exe": dict( # "exe": dict(
pip=dict(help="run cmd with pip"), # pip=dict(help="run cmd with pip"),
python=dict(help="run cmd with python"), # python=dict(help="run cmd with python"),
), # ),
"manage": dict( "manage": dict(
show=dict(help="show current installation", aliases=["s"]), show=dict(help="show current installation", aliases=["s"]),
install=dict(help="install fresh viv", aliases=["i"]), install=dict(help="install fresh viv", aliases=["i"]),