From 0939736304611200c702743d73d39bfcc7f8c254 Mon Sep 17 00:00:00 2001 From: Daylin Morgan Date: Fri, 26 May 2023 14:35:38 -0500 Subject: [PATCH] more dev --- src/viv/viv.py | 123 +++++++++++++++++++++++++------------------------ 1 file changed, 62 insertions(+), 61 deletions(-) diff --git a/src/viv/viv.py b/src/viv/viv.py index 2a0f084..d5bc001 100755 --- a/src/viv/viv.py +++ b/src/viv/viv.py @@ -51,7 +51,7 @@ from typing import ( Generator, ) -__version__ = "22.12a3-49-g6e940fc-dev" +__version__ = "22.12a3-50-g4932d2b-dev" @dataclass @@ -559,7 +559,8 @@ _viv_use({spec}) SHOW_TEMPLATE = f""" {a.style('Version', 'bold')}: {{version}} {a.style('CLI', 'bold')}: {{cli}} - {a.style('Current Source', 'bold')}: {{src}} + {a.style('Running Source', 'bold')}: {{running_src}} + {a.style('Local Source', 'bold')}: {{local_src}} """ INSTALL_TEMPLATE = f""" @@ -806,12 +807,9 @@ class ArgumentParser(StdArgParser): description = f""" {a.tagline()} - -{a.style('create/activate a vivenv','underline')} -from command line: - `{a.style("viv -h","bold")}` -within python script: - {a.style('__import__("viv").use("typer", "rich-click")','bold')} +{a.style('to create/activate a vivenv','underline')}: +- from command line: `{a.style("viv -h","bold")}` +- within python script: {a.style('__import__("viv").use("typer", "rich-click")','bold')} """ @@ -847,12 +845,24 @@ def fetch_source(reference: str) -> str: class Viv: def __init__(self) -> None: self.vivenvs = get_venvs() - self.current_source = Path(__file__).resolve() - self.local = not str(self.current_source).startswith("/proc/") + self._get_sources() self.name = ( - "python3 <(curl -fsSL gh.dayl.in/viv/viv.py)" if self.local else "viv" + "viv" if self.local else "python3 <(curl -fsSL gh.dayl.in/viv/viv.py)" ) + def _get_sources(self): + # ?: should we check for a `.git` install? + + self.running_source = Path(__file__).resolve() + self.local = not str(self.running_source).startswith("/proc/") + if self.local: + self.local_source = self.running_source + else: + try: + self.local_source = __import__("viv").__file__ + except ImportError: + self.local_source = "NotFound" + def _match_vivenv(self, name_id: str) -> ViVenv: # type: ignore[return] # TODO: improve matching algorithm to favor names over id's matches: List[ViVenv] = [] @@ -961,48 +971,32 @@ class Viv: def manage(self, args: Namespace) -> None: """manage viv installation""" - if args.cmd == "show": echo("Current:") sys.stdout.write( SHOW_TEMPLATE.format( version=__version__, cli=shutil.which("viv"), - src=Path(__file__).resolve(), + running_src=self.running_source, + local_src=self.local_source, ) ) elif args.cmd == "update": - if not self.local: + if not self.local_source: error( a.style("viv manage update", "bold") - + " should only be used with a locally installed viv", + + " should be used with an exisiting installation", 1, ) - # - # viv_src = fetch_source(args.reference) - # - # (hash := hashlib.sha256()).update(viv_src) - # sha256 = hash.hexdigest() - # - # ( - # src_cache := Path(os.getenv("XDG_CACHE_HOME", Path.home() / ".cache")) - # / "viv" - # / "src" - # ).mkdir(exist_ok=True, parents=True) - # cached_version = src_cache / f"{sha256}.py" - # - # if not cached_version.is_file(): - # with cached_version.open("w") as f: - # f.write(viv_src.decode()) - # + sha256 = fetch_source(args.reference) sys.path.append(str(c.srccache)) next_version = __import__(sha256).__version__ q = ( "Update source at: " - + a.style(self.current_source, "bold") + + a.style(self.running_source, "bold") + f" \n from version {__version__} to {next_version}?" ) @@ -1010,11 +1004,8 @@ class Viv: print("Holding off on the additions of this piece") elif args.cmd == "install": - if self.local: - error( - a.style("viv manage install", "bold") - + " should only be used with a remote viv", - ) + if self.local_source: + error(f"found existing viv installation at {self.local_source}") echo( "use " + a.style("viv manage update", "bold") @@ -1027,6 +1018,9 @@ class Viv: sys.path.append(str(c.srccache)) downloaded_version = __import__(sha256).__version__ echo(f"Downloaded version: {downloaded_version}") + + # TODO: src path and cli path should be shard args between update/install + q = INSTALL_TEMPLATE.format( src_location="~/.local/share/viv/viv.py", cli_location="~/bin/viv" ) + ("Would you like to perform the above " "installation steps?") @@ -1147,37 +1141,44 @@ class Viv: "info", parents=[p_vivenv_arg], ) + p_manage_shared = ArgumentParser(add_help=False) + p_manage_shared.add_argument( + "-r", + "--reference", + help="git reference (branch/tag/commit)", + default="main", + ) + + p_manage_shared.add_argument( + "-s", + "--src", + help="path/to/source file", + # default = + default=c.srccache / "viv.py", + ) + p_manage_shared.add_argument( + "-c", + "--cli", + help="path/to/cli (symlink to src)", + default=Path.home() / "bin" / "viv.py", + ) p_manage_sub = self._get_subcmd_parser( - subparsers, name="manage" + subparsers, + name="manage", ).add_subparsers(title="subcommand", metavar="", required=True) - # TODO: shared parser? - ( - p_manage_install := p_manage_sub.add_parser( - "install", help="install viv", aliases="i" - ) + p_manage_sub.add_parser( + "install", help="install viv", aliases="i", parents=[p_manage_shared] ).set_defaults(func=self.manage, cmd="install") - p_manage_install.add_argument( - "-r", - "--reference", - help="git reference (branch/tag/commit)", - default="main", - ) - ( - p_manage_update := p_manage_sub.add_parser( - "update", help="update viv version", aliases="u" - ) + p_manage_sub.add_parser( + "update", + help="update viv version", + aliases="u", + parents=[p_manage_shared], ).set_defaults(func=self.manage, cmd="update") - p_manage_update.add_argument( - "-r", - "--reference", - help="git reference (branch/tag/commit)", - default="main", - ) - p_manage_sub.add_parser( "show", help="show current installation info", aliases="s" ).set_defaults(func=self.manage, cmd="show")