2023-08-04 12:32:06 -05:00
|
|
|
#!/usr/bin/env python3
|
2023-08-06 13:46:28 -05:00
|
|
|
# AUTOGENERATED by viv (v23.8a1-5-g4c782db-dev)
|
2023-06-02 13:14:04 -05:00
|
|
|
# see `python3 <(curl -fsSL viv.dayl.in/viv.py) --help`
|
|
|
|
|
|
|
|
|
2023-06-01 09:51:15 -05:00
|
|
|
def _viv_use(*pkgs, track_exe=False, name=""):
|
|
|
|
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")
|
|
|
|
|
|
|
|
meta = dict.fromkeys(("created", "accessed"), (t := str(datetime.today())))
|
|
|
|
runner = str(Path(__file__).absolute().resolve())
|
2023-08-06 13:46:28 -05:00
|
|
|
envvar = lambda x: os.getenv(f"VIV_{x}") # 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)
|
2023-06-01 09:51:15 -05:00
|
|
|
exe = str(Path(sys.executable).resolve()) if track_exe else "N/A"
|
2023-08-06 13:46:28 -05:00
|
|
|
_id = hashlib.sha256((str(spec := [*pkgs]) + exe).encode()).hexdigest()
|
2023-08-04 12:32:06 -05:00
|
|
|
if (env := cache / (name if name else _id)) not in cache.glob("*/") or F:
|
2023-06-01 09:51:15 -05:00
|
|
|
sys.stderr.write(f"generating new vivenv -> {env.name}\n")
|
2023-08-04 12:32:06 -05:00
|
|
|
venv.create(env, prompt=f"viv-{name}", symlinks=True, clear=True)
|
|
|
|
kw = dict(zip(("stdout", "stderr"), ((None,) * 2 if V else (-1, 2))))
|
2023-06-05 14:02:33 -05:00
|
|
|
cmd = ["pip", "--python", str(env / "bin" / "python"), "install", *spec]
|
2023-08-06 13:46:28 -05:00
|
|
|
if (not no_st) and (not [x for x in spec if x.startswith("setuptools")]):
|
|
|
|
cmd.append("setuptools")
|
2023-06-05 14:02:33 -05:00
|
|
|
p = run(cmd, **kw)
|
2023-06-01 09:51:15 -05:00
|
|
|
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))
|
2023-06-05 16:28:50 -05:00
|
|
|
site.addsitedir(sitepkgs := str(*(env / "lib").glob("py*/si*")))
|
|
|
|
sys.path = [p for p in (sitepkgs, *sys.path) if p != site.USER_SITE]
|
2023-06-01 09:51:15 -05:00
|
|
|
return env
|
|
|
|
|
|
|
|
|
2023-05-25 17:37:22 -05:00
|
|
|
import subprocess
|
2023-05-26 22:20:37 -05:00
|
|
|
import sys
|
|
|
|
|
2023-05-25 17:37:22 -05:00
|
|
|
if __name__ == "__main__":
|
2023-05-30 16:24:17 -05:00
|
|
|
vivenv = _viv_use(
|
2023-08-04 12:32:06 -05:00
|
|
|
"black==23.7.0",
|
|
|
|
"click==8.1.6",
|
2023-05-30 16:24:17 -05:00
|
|
|
"mypy-extensions==1.0.0",
|
|
|
|
"packaging==23.1", # noqa
|
2023-08-04 12:32:06 -05:00
|
|
|
"pathspec==0.11.2",
|
|
|
|
"platformdirs==3.10.0",
|
2023-05-30 16:24:17 -05:00
|
|
|
) # noqa
|
2023-05-28 18:36:41 -05:00
|
|
|
sys.exit(subprocess.run([vivenv / "bin" / "black", *sys.argv[1:]]).returncode)
|