From f522352a3329d5435ba725cc20cbd99583a1f7b3 Mon Sep 17 00:00:00 2001 From: Daylin Morgan Date: Tue, 22 Aug 2023 21:08:23 -0500 Subject: [PATCH] fix: standalone also remove pkgs --- examples/black | 42 +++++++++++++++++++++++++++++++++++++++++- src/viv/viv.py | 43 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 83 insertions(+), 2 deletions(-) diff --git a/examples/black b/examples/black index 83ec278..2403d7e 100755 --- a/examples/black +++ b/examples/black @@ -1,5 +1,5 @@ #!/usr/bin/env python3 -# AUTOGENERATED by viv (v23.8b2-6-gdafa099-dev) +# AUTOGENERATED by viv (v23.8b2-9-g044a229-dev) # see `python3 <(curl -fsSL viv.dayl.in/viv.py) --help` @@ -9,6 +9,46 @@ def _viv_use(*pkgs, track_exe=False, name=""): from datetime import datetime # noqa from subprocess import run # noqa + if not {*map(type, pkgs)} == {str}: + raise ValueError(f"spec: {pkgs} is invalid") + + meta = dict.fromkeys(("created", "accessed"), (t := str(datetime.today()))) + runner = str(Path(__file__).absolute().resolve()) + envvar = lambda x: os.getenv(f"VIV_{x}") # noqa + nopkgs = lambda p: not p.endswith(("dist-packages", "site-packages")) # noqa + F, V, no_st = map(envvar, ("FORCE", "VERBOSE", "NO_SETUPTOOLS")) + base = Path(xdg) if (xdg := os.getenv("XDG_CACHE_HOME")) else Path.home() / ".cache" + (cache := (base) / "viv/venvs").mkdir(parents=True, exist_ok=True) + exe = str(Path(sys.executable).resolve()) if track_exe else "N/A" + _id = hashlib.sha256((str(spec := [*pkgs]) + exe).encode()).hexdigest() + if (env := cache / (name if name else _id[:8])) not in cache.glob("*/") or F: + sys.stderr.write(f"generating new vivenv -> {env.name}\n") + venv.create(env, prompt=f"viv-{name}", symlinks=True, clear=True) + kw = dict(zip(("stdout", "stderr"), ((None,) * 2 if V else (-1, 2)))) + cmd = ["pip", "--python", str(env / "bin" / "python"), "install", *spec] + if (not no_st) and (not [x for x in spec if x.startswith("setuptools")]): + cmd.append("setuptools") + p = run(cmd, **kw) + if (rc := p.returncode) != 0: + if env.is_dir(): + shutil.rmtree(env) + sys.stderr.write(f"pip had non zero exit ({rc})\n{p.stdout.decode()}\n") + sys.exit(rc) + meta.update(dict(id=_id, spec=spec, exe=exe, name=name, files=[runner])) + else: + meta = json.loads((env / "vivmeta.json").read_text()) + meta.update(dict(accessed=t, files=sorted({*meta["files"], runner}))) + (env / "vivmeta.json").write_text(json.dumps(meta)) + site.addsitedir(sitepkgs := str(*(env / "lib").glob("py*/si*"))) + sys.path = [sitepkgs, *filter(nopkgs, sys.path)] + + return env + + import hashlib, json, os, site, shutil, sys, venv # noqa + from pathlib import Path # noqa + from datetime import datetime # noqa + from subprocess import run # noqa + if not {*map(type, pkgs)} == {str}: raise ValueError(f"spec: {pkgs} is invalid") diff --git a/src/viv/viv.py b/src/viv/viv.py index f10f0b8..5cbeae0 100755 --- a/src/viv/viv.py +++ b/src/viv/viv.py @@ -52,7 +52,7 @@ from typing import ( Union, ) -__version__ = "23.8b2-9-g044a229-dev" +__version__ = "23.8b2-10-g0cc9faa-dev" class Spinner: @@ -346,6 +346,47 @@ class Template: from datetime import datetime # noqa from subprocess import run # noqa + if not {*map(type, pkgs)} == {str}: + raise ValueError(f"spec: {pkgs} is invalid") + + meta = dict.fromkeys(("created", "accessed"), (t := str(datetime.today()))) + runner = str(Path(__file__).absolute().resolve()) + envvar = lambda x: os.getenv(f"VIV_{x}") # noqa + nopkgs = lambda p: not p.endswith(("dist-packages", "site-packages")) # noqa + F, V, no_st = map(envvar, ("FORCE", "VERBOSE", "NO_SETUPTOOLS")) + base = Path(xdg) if (xdg := os.getenv("XDG_CACHE_HOME")) else Path.home() / ".cache" + (cache := (base) / "viv/venvs").mkdir(parents=True, exist_ok=True) + exe = str(Path(sys.executable).resolve()) if track_exe else "N/A" + _id = hashlib.sha256((str(spec := [*pkgs]) + exe).encode()).hexdigest() + if (env := cache / (name if name else _id[:8])) not in cache.glob("*/") or F: + sys.stderr.write(f"generating new vivenv -> {env.name}\n") + venv.create(env, prompt=f"viv-{name}", symlinks=True, clear=True) + kw = dict(zip(("stdout", "stderr"), ((None,) * 2 if V else (-1, 2)))) + cmd = ["pip", "--python", str(env / "bin" / "python"), "install", *spec] + if (not no_st) and (not [x for x in spec if x.startswith("setuptools")]): + cmd.append("setuptools") + p = run(cmd, **kw) + if (rc := p.returncode) != 0: + if env.is_dir(): + shutil.rmtree(env) + sys.stderr.write(f"pip had non zero exit ({rc})\n{p.stdout.decode()}\n") + sys.exit(rc) + meta.update(dict(id=_id, spec=spec, exe=exe, name=name, files=[runner])) + else: + meta = json.loads((env / "vivmeta.json").read_text()) + meta.update(dict(accessed=t, files=sorted({*meta["files"], runner}))) + (env / "vivmeta.json").write_text(json.dumps(meta)) + site.addsitedir(sitepkgs := str(*(env / "lib").glob("py*/si*"))) + sys.path = [sitepkgs, *filter(nopkgs, sys.path)] + + return env + + + import hashlib, json, os, site, shutil, sys, venv # noqa + from pathlib import Path # noqa + from datetime import datetime # noqa + from subprocess import run # noqa + if not {*map(type, pkgs)} == {str}: raise ValueError(f"spec: {pkgs} is invalid")