From 993e0e699b81825eaf826ec06f6e87c42aa3acc1 Mon Sep 17 00:00:00 2001 From: Daylin Morgan Date: Fri, 2 Jun 2023 13:49:56 -0500 Subject: [PATCH] feat: generalize viv exe now can be used to any binary within the environment --- src/viv/viv.py | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/src/viv/viv.py b/src/viv/viv.py index e62df71..137e8b1 100755 --- a/src/viv/viv.py +++ b/src/viv/viv.py @@ -14,7 +14,6 @@ import itertools import json import os import re -import shlex import shutil import site import subprocess @@ -51,7 +50,7 @@ from typing import ( from urllib.error import HTTPError from urllib.request import urlopen -__version__ = "23.5a4-41-gf069eaf-dev" +__version__ = "23.5a4-43-gcdeed9f-dev" class Spinner: @@ -1018,22 +1017,23 @@ class Viv: vivenv.show() def exe(self, args: Namespace) -> None: - """run python/pip in existing vivenv""" + """\ + run binary/script in existing vivenv + + examples: + viv exe pip -- list + viv exe python -- script.py + """ 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")) - # todo check for vivenv - echo(f"executing command within {vivenv.name}") + if not bin.exists(): + error(f"{args.cmd} does not exist in {vivenv.name}", code=1) - cmd = ( - f"{pip_path} {' '.join(args.cmd)}" - if args.subcmd == "pip" - else f"{python_path} {' '.join(args.cmd)}" - ) + " ".join(args.rest) + cmd = [bin, *args.rest] - echo(f"executing {cmd}") - run(shlex.split(cmd), verbose=True) + run(cmd, verbose=True) def info(self, args: Namespace) -> None: """get metadata about a vivenv""" @@ -1267,7 +1267,7 @@ class Cli: ), ], ("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"): [ Arg( "--json", @@ -1339,11 +1339,10 @@ class Cli: action="store_true", ) ], - ("exe|python", "exe|pip"): [ + ("exe"): [ Arg( "cmd", help="command to to execute", - nargs="*", ) ], } @@ -1362,10 +1361,10 @@ class Cli: ) ).update( { - "exe": dict( - pip=dict(help="run cmd with pip"), - python=dict(help="run cmd with python"), - ), + # "exe": dict( + # pip=dict(help="run cmd with pip"), + # python=dict(help="run cmd with python"), + # ), "manage": dict( show=dict(help="show current installation", aliases=["s"]), install=dict(help="install fresh viv", aliases=["i"]),