viv/examples/black

37 lines
2.6 KiB
Text
Raw Normal View History

2023-05-25 17:37:22 -05:00
#!/usr/bin/env python3
2023-05-30 16:24:17 -05:00
# fmt: off
def _viv_use(*pkgs, track_exe=False, name=""): # noqa
T,F,N=True,False,None;i,s,m,spec=__import__,str,map,[*pkgs] # noqa
e,w=lambda x: T if x else F,lambda p,t: p.write_text(t) # noqa
if not {*m(type,pkgs)}=={s}: raise ValueError(f"spec: {pkgs} is invalid") # noqa
ge,sys,P,ew=i("os").getenv,i("sys"),i("pathlib").Path,i("sys").stderr.write # noqa
(cache:=(P(ge("XDG_CACHE_HOME",P.home()/".cache"))/"viv"/"venvs")).mkdir(parents=T,exist_ok=T) # noqa
((sha256:=i("hashlib").sha256()).update((s(spec)+ # noqa
(((exe:=("N/A",s(P(i("sys").executable).resolve()))[e(track_exe)])))).encode())) # noqa
if {env:=cache/(((_id:=sha256.hexdigest()),name)[e(name)])}-{*cache.glob("*/")} or ge("VIV_FORCE"): # noqa
v=e(ge("VIV_VERBOSE"));ew(f"generating new vivenv -> {env.name}\n") # noqa
i("venv").EnvBuilder(with_pip=T,clear=T).create(env) # noqa
w(env/"pip.conf","[global]\ndisable-pip-version-check=true") # noqa
if (rc:=(p:=i("subprocess").run([env/"bin"/"pip","install","--force-reinstall",*spec],text=T, # noqa
stdout=(-1,N)[v],stderr=(-2,N)[v])).returncode)!=0: # noqa
if env.is_dir():i("shutil").rmtree(env) # noqa
ew(f"pip had non zero exit ({rc})\n{p.stdout}\n");sys.exit(rc) # noqa
w(env/"viv-info.json",i("json").dumps( # noqa
{"created":s(i("datetime").datetime.today()),"id":_id,"spec":spec,"exe":exe})) # noqa
sys.path=[p for p in (*sys.path,s(*(env/"lib").glob("py*/si*")))if p!=i("site").USER_SITE] # noqa
return env # noqa
# fmt: on
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(
"black==23.3.0",
"click==8.1.3",
"mypy-extensions==1.0.0",
"packaging==23.1", # noqa
"pathspec==0.11.1",
"platformdirs==3.5.1",
) # noqa
2023-05-28 18:36:41 -05:00
sys.exit(subprocess.run([vivenv / "bin" / "black", *sys.argv[1:]]).returncode)