mirror of
https://github.com/daylinmorgan/viv.git
synced 2024-11-13 20:47:53 -06:00
36 lines
2.1 KiB
Python
36 lines
2.1 KiB
Python
#!/usr/bin/env python3
|
|
|
|
# <<<<< auto-generated by daylinmorgan/viv (v.22.12a3)
|
|
# fmt: off
|
|
def _viv_activate(*pkgs: str, track_exe: bool = False, name: str = "") -> None: # noqa
|
|
i,m,e = __import__,map,lambda x: True if x else False # noqa
|
|
if not {*m(type, pkgs)} == {str}: raise ValueError(f"spec: {pkgs} is invalid") # noqa
|
|
ge,sys,P,exe = i("os").getenv,i("sys"),i("pathlib").Path,i("sys").executable # noqa
|
|
vivcache = (P(ge("XDG_CACHE_HOME",P.home()/".cache"))/"viv"/"venvs");vivcache.mkdir(parents=True, exist_ok=True) # noqa
|
|
spec,exe_path = [*pkgs], (str(P(exe).resolve()) if track_exe else "N/A") # noqa
|
|
hash = i("hashlib").sha256(); hash.update(str(spec).encode()) # noqa
|
|
if track_exe: hash.update(exe_path.encode()) # noqa
|
|
_id = hash.hexdigest() # noqa
|
|
name, envpath = (lambda n: (n,vivcache/n))(name if name else _id) # noqa
|
|
if name not in (d.name for d in vivcache.iterdir()) or ge("VIV_FORCE"): # noqa
|
|
run = i("subprocess").run; i("venv").EnvBuilder(with_pip=True, clear=True).create(envpath) # noqa
|
|
with (envpath/"pip.conf").open("w") as f:f.write("[global]\ndisable-pip-version-check = true") # noqa
|
|
p = run([envpath/"bin"/"pip","install","--force-reinstall", *spec], universal_newlines=True, # noqa
|
|
**dict(zip(("stdout","stderr"),[(-1,-2),(None,)*2,][e(ge("VIV_VERBOSE"))]))) # noqa
|
|
if (p.returncode!=0)*envpath.is_dir():i("shutil").rmtree(str(envpath)) # noqa
|
|
if p.returncode!=0:sys.stderr.write(f"pip had non zero exit ({p.returncode})\\n{p.stdout}");sys.exit(p.returncode) # noqa
|
|
with (envpath/"viv-info.json").open("w") as f: # noqa
|
|
i("json").dump({"created":str(i("datetime").datetime.today()),"id":_id,"spec":spec,"exe":exe},f) # noqa
|
|
sys.path = [p for p in (*sys.path, str(*(envpath/"lib").glob("py*/si*"))) if p!=i("site").USER_SITE] # noqa
|
|
_viv_activate("pyfiglet==0.8.post1") # noqa
|
|
|
|
# fmt: on
|
|
# >>>>> code golfed with <3
|
|
|
|
from pyfiglet import Figlet
|
|
|
|
if __name__ == "__main__":
|
|
f = Figlet(font="slant")
|
|
figtxt = f.renderText("viv").splitlines()
|
|
figtxt[-2] += " isn't venv!"
|
|
print("\n".join(figtxt))
|