viv/examples/black

51 lines
2.5 KiB
Text
Raw Normal View History

#!/usr/bin/env python3
2024-07-12 14:37:07 -05:00
# AUTOGENERATED by viv (v2024.1005-dev)
2023-06-02 13:14:04 -05:00
# see `python3 <(curl -fsSL viv.dayl.in/viv.py) --help`
def _viv_use(*pkgs, track_exe=False, name=""):
2024-07-12 14:37:07 -05:00
i, meta, add_meta = __import__, {}, lambda **kw: meta.update(**kw)
P, ge, q, noop = i("pathlib").Path, i("os").getenv, i("sys").exit, lambda: None
_if, p_str = lambda x, f: (noop, f)[x](), lambda x: f"{P(x).absolute().resolve()}"
2023-08-22 21:08:23 -05:00
nopkgs = lambda p: not p.endswith(("dist-packages", "site-packages")) # noqa
2024-07-12 14:37:07 -05:00
_if({*map(type, spec := [*pkgs])} != {str}, lambda: q(f"spec: {pkgs} not string"))
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"
2023-08-22 21:08:23 -05:00
(cache := (base) / "viv/venvs").mkdir(parents=True, exist_ok=True)
2024-07-12 14:37:07 -05:00
exe = p_str(i("sys").executable) if track_exe else "N/A"
_id = i("hashlib").sha256((str(spec) + exe).encode()).hexdigest()
2023-08-22 21:08:23 -05:00
if (env := cache / (name if name else _id[:8])) not in cache.glob("*/") or F:
2024-07-12 14:37:07 -05:00
i("sys").stderr.write(f"generating new vivenv -> {env.name}\n")
i("venv").create(env, prompt=f"viv-{name}", symlinks=True, clear=True)
run_kw = dict(zip(("stdout", "stderr"), ((None,) * 2 if V else (-1, 2))))
2023-08-22 21:08:23 -05:00
cmd = ["pip", "--python", str(env / "bin" / "python"), "install", *spec]
2024-07-12 14:37:07 -05:00
_if(ST, lambda: cmd.append("setuptools"))
if (rc := (p := i("subprocess").run(cmd, **run_kw)).returncode) != 0:
_if(env.is_dir(), lambda: i("shutil").rmtree(env))
q(f"pip had non zero exit ({rc})\n{p.stdout.decode()}\n")
add_meta(id=_id, spec=spec, exe=exe, name=name)
2023-08-22 21:08:23 -05:00
else:
2024-07-12 14:37:07 -05:00
meta = i("json").loads((env / "vivmeta.json").read_text())
add_meta(accessed=now, files=sorted({*meta.get("files", []), p_str(__file__)}))
(env / "vivmeta.json").write_text(i("json").dumps(meta))
i("site").addsitedir(sitepkgs := str(*(env / "lib").glob("py*/si*")))
2023-08-22 21:08:23 -05:00
sys.path = [sitepkgs, *filter(nopkgs, sys.path)]
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(
2024-07-12 14:37:07 -05:00
"black==24.4.2",
"click==8.1.7",
2023-05-30 16:24:17 -05:00
"mypy-extensions==1.0.0",
2024-07-12 14:37:07 -05:00
"packaging==24.1", # noqa
"pathspec==0.12.1",
"platformdirs==4.2.2",
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)