refactor: more consolidating

This commit is contained in:
Daylin Morgan 2023-05-29 11:57:25 -05:00
parent c97bc81f08
commit a015592fb0
Signed by: daylin
GPG key ID: C1E52E7DD81DF79F

View file

@ -52,7 +52,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.5a2-7-g0aff002-dev" __version__ = "23.5a2-8-gf5dd514-dev"
class Config: class Config:
@ -565,6 +565,7 @@ class ViVenv:
self.id = id if id else get_hash(spec, track_exe) self.id = id if id else get_hash(spec, track_exe)
self.name = name if name else self.id self.name = name if name else self.id
self.path = path if path else c.venvcache / self.name self.path = path if path else c.venvcache / self.name
self.exists = self.name in [d.name for d in c.venvcache.iterdir()]
@classmethod @classmethod
def load(cls, name: str) -> "ViVenv": def load(cls, name: str) -> "ViVenv":
@ -584,7 +585,7 @@ class ViVenv:
return vivenv return vivenv
def _validate_spec(self, spec: Tuple[str, ...]) -> List[str]: def _validate_spec(self, spec: List[str]) -> List[str]:
"""ensure spec is at least of sequence of strings """ensure spec is at least of sequence of strings
Args: Args:
@ -593,7 +594,7 @@ class ViVenv:
if not set(map(type, spec)) == {str}: if not set(map(type, spec)) == {str}:
error("unexepected input in package spec") error("unexepected input in package spec")
error(f"check your packages definitions: {spec}", code=1) error(f"check your packages definitions: {spec}", code=1)
else:
return sorted(spec) return sorted(spec)
def create(self, quiet: bool = False) -> None: def create(self, quiet: bool = False) -> None:
@ -651,9 +652,7 @@ def use(*packages: str, track_exe: bool = False, name: str = "") -> Path:
""" """
vivenv = ViVenv(list(packages), track_exe=track_exe, name=name) vivenv = ViVenv(list(packages), track_exe=track_exe, name=name)
if vivenv.name not in [d.name for d in c.venvcache.iterdir()] or os.getenv( if not vivenv.exists or os.getenv("VIV_FORCE"):
"VIV_FORCE"
):
vivenv.create() vivenv.create()
vivenv.install_pkgs() vivenv.install_pkgs()
vivenv.dump_info(write=True) vivenv.dump_info(write=True)
@ -1049,9 +1048,10 @@ class Viv:
echo("symlinking cli") echo("symlinking cli")
if cli.is_file() and confirm( if cli.is_file() and confirm(
f"Existing file at {a.style(cli,'bold')}, would you like to overwrite it?" f"Existing file at {a.style(str(cli),'bold')}, "
"would you like to overwrite it?"
): ):
cli.unlink(src) cli.unlink()
cli.symlink_to(src) cli.symlink_to(src)
else: else:
cli.symlink_to(src) cli.symlink_to(src)
@ -1181,6 +1181,10 @@ class Viv:
"`python3 <(curl -fsSL gh.dayl.in/viv/viv.py) manage install`" "`python3 <(curl -fsSL gh.dayl.in/viv/viv.py) manage install`"
) )
def _pick_bin(self, args: Namespace) -> Tuple[str, str]:
default = re.split(r"[=><~!*]+", args.reqs[0])[0]
return default, (default if not args.bin else args.bin)
def shim(self, args: Namespace) -> None: def shim(self, args: Namespace) -> None:
"""\ """\
generate viv-powered cli apps generate viv-powered cli apps
@ -1194,8 +1198,7 @@ class Viv:
if not args.reqs: if not args.reqs:
error("please specify at lease one dependency", code=1) error("please specify at lease one dependency", code=1)
default_bin = re.split(r"[=><~!*]+", args.reqs[0])[0] default_bin, bin = self._pick_bin(args)
bin = default_bin if not args.bin else args.bin
output = ( output = (
c.binparent / default_bin if not args.output else args.output.absolute() c.binparent / default_bin if not args.output else args.output.absolute()
) )
@ -1253,14 +1256,15 @@ class Viv:
if not args.reqs: if not args.reqs:
error("please specify at lease one dependency", code=1) error("please specify at lease one dependency", code=1)
default_bin = re.split(r"[=><~!*]+", args.reqs[0])[0] _, bin = self._pick_bin(args)
bin = default_bin if not args.bin else args.bin
spec = combined_spec(args.reqs, args.requirements) spec = combined_spec(args.reqs, args.requirements)
vivenv = ViVenv(spec) vivenv = ViVenv(spec)
if vivenv.name not in [d.name for d in c.venvcache.iterdir()] or os.getenv( # TODO: respect a VIV_RUN_MODE env variable as the same as keep i.e.
"VIV_FORCE" # ephemeral (default), semi-ephemeral (persist inside /tmp), or
): # persist (use c.cache)
if not vivenv.exists or os.getenv("VIV_FORCE"):
if not args.keep: if not args.keep:
with tempfile.TemporaryDirectory(prefix="viv-") as tmpdir: with tempfile.TemporaryDirectory(prefix="viv-") as tmpdir:
vivenv.path = Path(tmpdir) vivenv.path = Path(tmpdir)