Compare commits

...

3 commits

4 changed files with 74 additions and 75 deletions

View file

@ -129,12 +129,12 @@ python3 <(curl -fsSL viv.dayl.in/viv.py) run \
-s https://gist.githubusercontent.com/cs01/fa721a17a326e551ede048c5088f9e0f/raw/6bdfbb6e9c1132b1c38fdd2f195d4a24c540c324/pipx-demo.py -s https://gist.githubusercontent.com/cs01/fa721a17a326e551ede048c5088f9e0f/raw/6bdfbb6e9c1132b1c38fdd2f195d4a24c540c324/pipx-demo.py
``` ```
## Bonus: use `viv` with just standalone snippet (37LOC) ## Bonus: use `viv` with just standalone snippet (30LOC)
`--standalone` will auto-generate a mini-function version of `viv` `--standalone` will auto-generate a mini-function version of `viv`
to accomplish the same basic task as using a local copy of `viv`. to accomplish the same basic task as using a local copy of `viv`.
After generating this standalone shim you can freely use this script After generating this standalone shim you can freely use this script
across Unix machines which have Python > 3.8. across Unix machines which have Python > 3.8 and pip.
See [examples/black](https://github.com/daylinmorgan/viv/blob/dev/examples/black) See [examples/black](https://github.com/daylinmorgan/viv/blob/dev/examples/black)
for output of the below command. for output of the below command.

View file

@ -1,47 +1,37 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# AUTOGENERATED by viv (v2023.1001-dev) # AUTOGENERATED by viv (v2024.1005-dev)
# see `python3 <(curl -fsSL viv.dayl.in/viv.py) --help` # see `python3 <(curl -fsSL viv.dayl.in/viv.py) --help`
def _viv_use(*pkgs, track_exe=False, name=""): def _viv_use(*pkgs, track_exe=False, name=""):
import hashlib, json, os, site, shutil, sys, venv # noqa i, meta, add_meta = __import__, {}, lambda **kw: meta.update(**kw)
from pathlib import Path # noqa P, ge, q, noop = i("pathlib").Path, i("os").getenv, i("sys").exit, lambda: None
from datetime import datetime # noqa _if, p_str = lambda x, f: (noop, f)[x](), lambda x: f"{P(x).absolute().resolve()}"
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 nopkgs = lambda p: not p.endswith(("dist-packages", "site-packages")) # noqa
F, V, no_st = map(envvar, ("FORCE", "VERBOSE", "NO_SETUPTOOLS")) _if({*map(type, spec := [*pkgs])} != {str}, lambda: q(f"spec: {pkgs} not string"))
base = Path(xdg) if (xdg := os.getenv("XDG_CACHE_HOME")) else Path.home() / ".cache" F, V, NS = map(lambda x: ge(f"VIV_{x}"), ("FORCE", "VERBOSE", "NO_SETUPTOOLS"))
ST = not (NS and [x for x in spec if x.startswith("setuptools")])
add_meta(created=(now := str(i("datetime").datetime.today())))
base = P(xdg) if (xdg := ge("XDG_CACHE_HOME")) else P.home() / ".cache"
(cache := (base) / "viv/venvs").mkdir(parents=True, exist_ok=True) (cache := (base) / "viv/venvs").mkdir(parents=True, exist_ok=True)
exe = str(Path(sys.executable).resolve()) if track_exe else "N/A" exe = p_str(i("sys").executable) if track_exe else "N/A"
_id = hashlib.sha256((str(spec := [*pkgs]) + exe).encode()).hexdigest() _id = i("hashlib").sha256((str(spec) + exe).encode()).hexdigest()
if (env := cache / (name if name else _id[:8])) not in cache.glob("*/") or F: 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") i("sys").stderr.write(f"generating new vivenv -> {env.name}\n")
venv.create(env, prompt=f"viv-{name}", symlinks=True, clear=True) i("venv").create(env, prompt=f"viv-{name}", symlinks=True, clear=True)
kw = dict(zip(("stdout", "stderr"), ((None,) * 2 if V else (-1, 2)))) run_kw = dict(zip(("stdout", "stderr"), ((None,) * 2 if V else (-1, 2))))
cmd = ["pip", "--python", str(env / "bin" / "python"), "install", *spec] cmd = ["pip", "--python", str(env / "bin" / "python"), "install", *spec]
if (not no_st) and (not [x for x in spec if x.startswith("setuptools")]): _if(ST, lambda: cmd.append("setuptools"))
cmd.append("setuptools") if (rc := (p := i("subprocess").run(cmd, **run_kw)).returncode) != 0:
p = run(cmd, **kw) _if(env.is_dir(), lambda: i("shutil").rmtree(env))
if (rc := p.returncode) != 0: q(f"pip had non zero exit ({rc})\n{p.stdout.decode()}\n")
if env.is_dir(): add_meta(id=_id, spec=spec, exe=exe, name=name)
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: else:
meta = json.loads((env / "vivmeta.json").read_text()) meta = i("json").loads((env / "vivmeta.json").read_text())
meta.update(dict(accessed=t, files=sorted({*meta["files"], runner}))) add_meta(accessed=now, files=sorted({*meta.get("files", []), p_str(__file__)}))
(env / "vivmeta.json").write_text(json.dumps(meta)) (env / "vivmeta.json").write_text(i("json").dumps(meta))
site.addsitedir(sitepkgs := str(*(env / "lib").glob("py*/si*"))) i("site").addsitedir(sitepkgs := str(*(env / "lib").glob("py*/si*")))
sys.path = [sitepkgs, *filter(nopkgs, sys.path)] sys.path = [sitepkgs, *filter(nopkgs, sys.path)]
return env return env
@ -50,11 +40,11 @@ import sys
if __name__ == "__main__": if __name__ == "__main__":
vivenv = _viv_use( vivenv = _viv_use(
"black==23.7.0", "black==24.4.2",
"click==8.1.7", "click==8.1.7",
"mypy-extensions==1.0.0", "mypy-extensions==1.0.0",
"packaging==23.1", # noqa "packaging==24.1", # noqa
"pathspec==0.11.2", "pathspec==0.12.1",
"platformdirs==3.10.0", "platformdirs==4.2.2",
) # noqa ) # noqa
sys.exit(subprocess.run([vivenv / "bin" / "black", *sys.argv[1:]]).returncode) sys.exit(subprocess.run([vivenv / "bin" / "black", *sys.argv[1:]]).returncode)

View file

@ -2008,44 +2008,34 @@ def err_quit(*msg: str, code: int = 1) -> NoReturn:
class Template: class Template:
_standalone_func = r"""def _viv_use(*pkgs, track_exe=False, name=""): _standalone_func = r"""def _viv_use(*pkgs, track_exe=False, name=""):
import hashlib, json, os, site, shutil, sys, venv # noqa i, meta, add_meta = __import__, {}, lambda **kw: meta.update(**kw)
from pathlib import Path # noqa P, ge, q, noop = i("pathlib").Path, i("os").getenv, i("sys").exit, lambda: None
from datetime import datetime # noqa _if, p_str = lambda x, f: (noop, f)[x](), lambda x: f"{P(x).absolute().resolve()}"
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 nopkgs = lambda p: not p.endswith(("dist-packages", "site-packages")) # noqa
F, V, no_st = map(envvar, ("FORCE", "VERBOSE", "NO_SETUPTOOLS")) _if({*map(type, spec := [*pkgs])} != {str}, lambda: q(f"spec: {pkgs} not string"))
base = Path(xdg) if (xdg := os.getenv("XDG_CACHE_HOME")) else Path.home() / ".cache" F, V, NS = map(lambda x: ge(f"VIV_{x}"), ("FORCE", "VERBOSE", "NO_SETUPTOOLS"))
ST = not (NS and [x for x in spec if x.startswith("setuptools")])
add_meta(created=(now := str(i("datetime").datetime.today())))
base = P(xdg) if (xdg := ge("XDG_CACHE_HOME")) else P.home() / ".cache"
(cache := (base) / "viv/venvs").mkdir(parents=True, exist_ok=True) (cache := (base) / "viv/venvs").mkdir(parents=True, exist_ok=True)
exe = str(Path(sys.executable).resolve()) if track_exe else "N/A" exe = p_str(i("sys").executable) if track_exe else "N/A"
_id = hashlib.sha256((str(spec := [*pkgs]) + exe).encode()).hexdigest() _id = i("hashlib").sha256((str(spec) + exe).encode()).hexdigest()
if (env := cache / (name if name else _id[:8])) not in cache.glob("*/") or F: 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") i("sys").stderr.write(f"generating new vivenv -> {env.name}\n")
venv.create(env, prompt=f"viv-{name}", symlinks=True, clear=True) i("venv").create(env, prompt=f"viv-{name}", symlinks=True, clear=True)
kw = dict(zip(("stdout", "stderr"), ((None,) * 2 if V else (-1, 2)))) run_kw = dict(zip(("stdout", "stderr"), ((None,) * 2 if V else (-1, 2))))
cmd = ["pip", "--python", str(env / "bin" / "python"), "install", *spec] cmd = ["pip", "--python", str(env / "bin" / "python"), "install", *spec]
if (not no_st) and (not [x for x in spec if x.startswith("setuptools")]): _if(ST, lambda: cmd.append("setuptools"))
cmd.append("setuptools") if (rc := (p := i("subprocess").run(cmd, **run_kw)).returncode) != 0:
p = run(cmd, **kw) _if(env.is_dir(), lambda: i("shutil").rmtree(env))
if (rc := p.returncode) != 0: q(f"pip had non zero exit ({rc})\n{p.stdout.decode()}\n")
if env.is_dir(): add_meta(id=_id, spec=spec, exe=exe, name=name)
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: else:
meta = json.loads((env / "vivmeta.json").read_text()) meta = i("json").loads((env / "vivmeta.json").read_text())
meta.update(dict(accessed=t, files=sorted({*meta["files"], runner}))) add_meta(accessed=now, files=sorted({*meta.get("files", []), p_str(__file__)}))
(env / "vivmeta.json").write_text(json.dumps(meta)) (env / "vivmeta.json").write_text(i("json").dumps(meta))
site.addsitedir(sitepkgs := str(*(env / "lib").glob("py*/si*"))) i("site").addsitedir(sitepkgs := str(*(env / "lib").glob("py*/si*")))
sys.path = [sitepkgs, *filter(nopkgs, sys.path)] sys.path = [sitepkgs, *filter(nopkgs, sys.path)]
return env return env
""" """
@ -3872,6 +3862,7 @@ class Cli:
self._validate_args(args) self._validate_args(args)
func = args.__dict__.pop("func") func = args.__dict__.pop("func")
_pip_check()
func( func(
**vars(args), **vars(args),
) )
@ -3883,9 +3874,27 @@ def _pip_check():
err_quit("viv requires pip to be installed") err_quit("viv requires pip to be installed")
# importing viv may have side effects I'm not aware of... # importing viv may have side effects I'm not aware of...
if Version((pip_version := __import__("pip").__version__)) not in SpecifierSet( try:
pip_version_requirement pip_version = __import__("pip").__version__
): except ModuleNotFoundError:
cmd = ["pip", "--version"]
p = subprocess.run(
cmd,
text=True,
stderr=subprocess.STDOUT,
stdout=subprocess.PIPE,
)
if p.returncode != 0:
a.subprocess(cmd, p.stdout)
err_quit("viv failed to get version from pip, see above")
if not p.stdout.startswith("pip"):
a.subprocess(cmd, p.stdout)
err_quit("unexpected output from pip, see above")
pip_version = p.stdout.split()[1]
if Version(pip_version) not in SpecifierSet(pip_version_requirement):
err_quit( err_quit(
f"viv requires pip version {pip_version_requirement} but got {pip_version}" f"viv requires pip version {pip_version_requirement} but got {pip_version}"
) )
@ -3902,7 +3911,6 @@ def _no_traceback_excepthook(
def main() -> None: def main() -> None:
try: try:
_pip_check()
viv = Viv() viv = Viv()
Cli(viv).run() Cli(viv).run()
except KeyboardInterrupt: except KeyboardInterrupt:

View file

@ -11,6 +11,7 @@ def test_use():
use("pyjokes") use("pyjokes")
import pyjokes # noqa import pyjokes # noqa
# sample is installed in the test venv which should be removed from sys.path
with pytest.raises(ImportError): with pytest.raises(ImportError):
from sample.simple import add_one # noqa from sample.simple import add_one # noqa