mirror of
https://github.com/daylinmorgan/viv.git
synced 2024-11-14 04:57:53 -06:00
refactor: modify standalone output
This commit is contained in:
parent
ea0342dfa3
commit
138809ebeb
2 changed files with 39 additions and 52 deletions
|
@ -1,43 +1,32 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
def _viv_use(*pkgs, track_exe=False, name=""):
|
||||||
|
T,F,N=True,False,None;i,s,m,spec=__import__,str,map,[*pkgs]
|
||||||
# <<<<< auto-generated by viv (v23.5a2-2-gebb657c-dev)
|
e,w=lambda x: T if x else F,lambda p,t: p.write_text(t)
|
||||||
# see `python3 <(curl -fsSL gh.dayl.in/viv/viv.py) --help`
|
if not {*m(type,pkgs)}=={s}: raise ValueError(f"spec: {pkgs} is invalid")
|
||||||
# fmt: off
|
ge,sys,P,ew=i("os").getenv,i("sys"),i("pathlib").Path,i("sys").stderr.write
|
||||||
def _viv_use(*pkgs, track_exe=False, name=""): # noqa
|
(cache:=(P(ge("XDG_CACHE_HOME",P.home()/".cache"))/"viv"/"venvs")).mkdir(parents=T,exist_ok=T)
|
||||||
T,F=True,False;i,s,m,e,spec=__import__,str,map,lambda x: T if x else F,[*pkgs] # noqa
|
((sha256:=i("hashlib").sha256()).update((s(spec)+
|
||||||
if not {*m(type,pkgs)}=={s}: raise ValueError(f"spec: {pkgs} is invalid") # noqa
|
(((exe:=("N/A",s(P(i("sys").executable).resolve()))[e(track_exe)])))).encode()))
|
||||||
ge,sys,P,ew=i("os").getenv,i("sys"),i("pathlib").Path,i("sys").stderr.write # noqa
|
if {env:=cache/(((_id:=sha256.hexdigest()),name)[e(name)])}-{*cache.glob("*/")} or ge("VIV_FORCE"):
|
||||||
(cache:=(P(ge("XDG_CACHE_HOME",P.home()/".cache"))/"viv"/"venvs")).mkdir(parents=T,exist_ok=T) # noqa
|
v=e(ge("VIV_VERBOSE"));ew(f"generating new vivenv -> {env.name}
|
||||||
((sha256:=i("hashlib").sha256()).update((s(spec)+ # noqa
|
")
|
||||||
(((exe:=("N/A",s(P(i("sys").executable).resolve()))[e(track_exe)])))).encode())) # noqa
|
i("venv").EnvBuilder(with_pip=T,clear=T).create(env)
|
||||||
if ((env:=cache/(name if name else (_id:=sha256.hexdigest()))) # noqa
|
w(env/"pip.conf","[global]
|
||||||
not in cache.glob("*/")) or ge("VIV_FORCE"): # noqa
|
disable-pip-version-check=true")
|
||||||
v=e(ge("VIV_VERBOSE"));ew(f"generating new vivenv -> {env.name}\n") # noqa
|
if (rc:=(p:=i("subprocess").run([env/"bin"/"pip","install","--force-reinstall",*spec],text=T,
|
||||||
i("venv").EnvBuilder(with_pip=T,clear=T).create(env) # noqa
|
stdout=(-1,N)[v],stderr=(-2,N)[v])).returncode)!=0:
|
||||||
with (env/"pip.conf").open("w") as f:f.write("[global]\ndisable-pip-version-check=true") # noqa
|
if env.is_dir():i("shutil").rmtree(env)
|
||||||
if (p:=i("subprocess").run([env/"bin"/"pip","install","--force-reinstall",*spec],text=True, # noqa
|
ew(f"pip had non zero exit ({rc})
|
||||||
stdout=(-1,None)[v],stderr=(-2,None)[v])).returncode!=0: # noqa
|
{p.stdout}
|
||||||
if env.is_dir():i("shutil").rmtree(env) # noqa
|
");sys.exit(rc)
|
||||||
ew(f"pip had non zero exit ({p.returncode})\n{p.stdout}\n");sys.exit(p.returncode) # noqa
|
w(env/"viv-info.json",i("json").dumps(
|
||||||
with (env/"viv-info.json").open("w") as f: # noqa
|
{"created":s(i("datetime").datetime.today()),"id":_id,"spec":spec,"exe":exe}))
|
||||||
i("json").dump({"created":s(i("datetime").datetime.today()), # noqa
|
sys.path=[p for p in (*sys.path,s(*(env/"lib").glob("py*/si*")))if p!=i("site").USER_SITE]
|
||||||
"id":_id,"spec":spec,"exe":exe},f) # noqa
|
return env
|
||||||
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
|
|
||||||
# >>>>> code golfed with <3
|
|
||||||
|
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
vivenv = _viv_use(
|
vivenv = _viv_use("black==23.3.0", "click==8.1.3", "mypy-extensions==1.0.0", "packaging==23.1",
|
||||||
"black==23.3.0",
|
"pathspec==0.11.1", "platformdirs==3.5.1")
|
||||||
"click==8.1.3",
|
|
||||||
"mypy-extensions==1.0.0",
|
|
||||||
"packaging==23.1",
|
|
||||||
"pathspec==0.11.1",
|
|
||||||
"platformdirs==3.5.1",
|
|
||||||
)
|
|
||||||
sys.exit(subprocess.run([vivenv / "bin" / "black", *sys.argv[1:]]).returncode)
|
sys.exit(subprocess.run([vivenv / "bin" / "black", *sys.argv[1:]]).returncode)
|
||||||
|
|
|
@ -339,28 +339,26 @@ to create/activate a vivenv:
|
||||||
- from command line: `{a.style("viv -h","bold")}`
|
- from command line: `{a.style("viv -h","bold")}`
|
||||||
- within python script: {a.style('__import__("viv").use("typer", "rich-click")','bold')}
|
- within python script: {a.style('__import__("viv").use("typer", "rich-click")','bold')}
|
||||||
"""
|
"""
|
||||||
|
_standalone_func = """def _viv_use(*pkgs, track_exe=False, name=""):
|
||||||
_standalone_func = r"""def _viv_use(*pkgs, track_exe=False, name=""):
|
T,F,N=True,False,None;i,s,m,spec=__import__,str,map,[*pkgs]
|
||||||
T,F=True,False;i,s,m,e,spec=__import__,str,map,lambda x: T if x else F,[*pkgs]
|
e,w=lambda x: T if x else F,lambda p,t: p.write_text(t)
|
||||||
if not {*m(type,pkgs)}=={s}: raise ValueError(f"spec: {pkgs} is invalid")
|
if not {*m(type,pkgs)}=={s}: raise ValueError(f"spec: {pkgs} is invalid")
|
||||||
ge,sys,P,ew=i("os").getenv,i("sys"),i("pathlib").Path,i("sys").stderr.write
|
ge,sys,P,ew=i("os").getenv,i("sys"),i("pathlib").Path,i("sys").stderr.write
|
||||||
(cache:=(P(ge("XDG_CACHE_HOME",P.home()/".cache"))/"viv"/"venvs")).mkdir(parents=T,exist_ok=T)
|
(cache:=(P(ge("XDG_CACHE_HOME",P.home()/".cache"))/"viv"/"venvs")).mkdir(parents=T,exist_ok=T)
|
||||||
((sha256:=i("hashlib").sha256()).update((s(spec)+
|
((sha256:=i("hashlib").sha256()).update((s(spec)+
|
||||||
(((exe:=("N/A",s(P(i("sys").executable).resolve()))[e(track_exe)])))).encode()))
|
(((exe:=("N/A",s(P(i("sys").executable).resolve()))[e(track_exe)])))).encode()))
|
||||||
if ((env:=cache/(name if name else (_id:=sha256.hexdigest())))
|
if {env:=cache/(((_id:=sha256.hexdigest()),name)[e(name)])}-{*cache.glob("*/")} or ge("VIV_FORCE"):
|
||||||
not in cache.glob("*/")) or ge("VIV_FORCE"):
|
|
||||||
v=e(ge("VIV_VERBOSE"));ew(f"generating new vivenv -> {env.name}\n")
|
v=e(ge("VIV_VERBOSE"));ew(f"generating new vivenv -> {env.name}\n")
|
||||||
i("venv").EnvBuilder(with_pip=T,clear=T).create(env)
|
i("venv").EnvBuilder(with_pip=T,clear=T).create(env)
|
||||||
with (env/"pip.conf").open("w") as f:f.write("[global]\ndisable-pip-version-check=true")
|
w(env/"pip.conf","[global]\ndisable-pip-version-check=true")
|
||||||
if (p:=i("subprocess").run([env/"bin"/"pip","install","--force-reinstall",*spec],text=True,
|
if (rc:=(p:=i("subprocess").run([env/"bin"/"pip","install","--force-reinstall",*spec],text=T,
|
||||||
stdout=(-1,None)[v],stderr=(-2,None)[v])).returncode!=0:
|
stdout=(-1,N)[v],stderr=(-2,N)[v])).returncode)!=0:
|
||||||
if env.is_dir():i("shutil").rmtree(env)
|
if env.is_dir():i("shutil").rmtree(env)
|
||||||
ew(f"pip had non zero exit ({p.returncode})\n{p.stdout}\n");sys.exit(p.returncode)
|
ew(f"pip had non zero exit ({rc})\n{p.stdout}\n");sys.exit(rc)
|
||||||
with (env/"viv-info.json").open("w") as f:
|
w(env/"viv-info.json",i("json").dumps(
|
||||||
i("json").dump({"created":s(i("datetime").datetime.today()),
|
{"created":s(i("datetime").datetime.today()),"id":_id,"spec":spec,"exe":exe}))
|
||||||
"id":_id,"spec":spec,"exe":exe},f)
|
sys.path=[p for p in (*sys.path,s(*(env/"lib").glob("py*/si*")))if p!=i("site").USER_SITE]
|
||||||
sys.path = [p for p in (*sys.path,s(*(env/"lib").glob("py*/si*"))) if p!=i("site").USER_SITE]
|
return env""" # noqa
|
||||||
return env""" # noqa
|
|
||||||
|
|
||||||
def noqa(self, txt: str) -> str:
|
def noqa(self, txt: str) -> str:
|
||||||
max_length = max(map(len, txt.splitlines()))
|
max_length = max(map(len, txt.splitlines()))
|
||||||
|
@ -371,7 +369,7 @@ to create/activate a vivenv:
|
||||||
if standalone:
|
if standalone:
|
||||||
return f"""_viv_use({fill(spec_str,width=90,subsequent_indent=" ",)})"""
|
return f"""_viv_use({fill(spec_str,width=90,subsequent_indent=" ",)})"""
|
||||||
else:
|
else:
|
||||||
return f"""__import("viv").use({spec_str})"""
|
return f"""__import__("viv").use({spec_str})"""
|
||||||
|
|
||||||
def standalone(self, spec: List[str]) -> str:
|
def standalone(self, spec: List[str]) -> str:
|
||||||
func_use = self.noqa(
|
func_use = self.noqa(
|
||||||
|
|
Loading…
Reference in a new issue