diff --git a/README.md b/README.md index c3464da..ccca263 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,8 @@ viv remove $(viv list -q) # Standalone Viv +*Requires* `python>=3.8` + Supposing you want to increase the portability of your script while still employing `viv`. The below function can be freely pasted at the top of your scripts and requires @@ -91,27 +93,28 @@ The only part necessary to modify if copied verbatim from below is the call to ` output of `viv freeze rich --standalone`: ```python + # <<<<< auto-generated by daylinmorgan/viv (v.22.12a3) # fmt: off -def _viv_activate(*pkgs: str, track_exe: bool = False, name: str = "") -> None: # noqa - i,s,m,e,spec=__import__,str,map,lambda x: True if x else False, [*pkgs] # 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=True, exist_ok=True) # noqa - ((hash:=i("hashlib").sha256()).update((s(spec)+ # noqa - (((exe:=s(P(i("sys").executable).resolve()) if track_exe else "N/A")))).encode())) # noqa - if (env:=cache/(name if name else (_id:=hash.hexdigest()))) not in 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=True,clear=True).create(env) # noqa - with (env/"pip.conf").open("w") as f:f.write("[global]\ndisable-pip-version-check=true") # noqa - if (p:=i("subprocess").run([env/"bin"/"pip","install","--force-reinstall",*spec],text=True, # noqa - stdout=(-1,None)[v],stderr=(-2,None)[v])).returncode!=0: # noqa - if env.is_dir():i("shutil").rmtree(env) # noqa - ew(f"pip had non zero exit ({p.returncode})\n{p.stdout}\n");sys.exit(p.returncode) # noqa - with (env/"viv-info.json").open("w") as f: # noqa - i("json").dump({"created":s(i("datetime").datetime.today()),"id":_id,"spec":spec,"exe":exe},f) # noqa - sys.path = [p for p in (*sys.path,s(*(env/"lib").glob("py*/si*"))) if p!=i("site").USER_SITE] # noqa -_viv_activate("markdown-it-py==2.2.0", "mdurl==0.1.2", "Pygments==2.14.0", "rich==13.3.2") # noqa +def _viv_activate(*pkgs: str, track_exe: bool = False, name: str = "") -> None: # noqa + i,s,m,e,spec=__import__,str,map,lambda x: True if x else False,[*pkgs] # 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=True,exist_ok=True) # noqa + ((sha256:=i("hashlib").sha256()).update((s(spec)+ # noqa + (((exe:=s(P(i("sys").executable).resolve()) if track_exe else "N/A")))).encode())) # noqa + if (env:=cache/(name if name else (_id:=sha256.hexdigest()))) not in 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=True,clear=True).create(env) # noqa + with (env/"pip.conf").open("w") as f:f.write("[global]\ndisable-pip-version-check=true") # noqa + if (p:=i("subprocess").run([env/"bin"/"pip","install","--force-reinstall",*spec],text=True, # noqa + stdout=(-1,None)[v],stderr=(-2,None)[v])).returncode!=0: # noqa + if env.is_dir():i("shutil").rmtree(env) # noqa + ew(f"pip had non zero exit ({p.returncode})\n{p.stdout}\n");sys.exit(p.returncode) # noqa + with (env/"viv-info.json").open("w") as f: # noqa + i("json").dump({"created":s(i("datetime").datetime.today()),"id":_id,"spec":spec,"exe":exe},f) # noqa + sys.path = [p for p in (*sys.path,s(*(env/"lib").glob("py*/si*"))) if p!=i("site").USER_SITE] # noqa +_viv_activate("markdown-it-py==2.2.0", "mdurl==0.1.2", "Pygments==2.14.0", "rich==13.3.2") # noqa # fmt: on # >>>>> code golfed with <3 ``` diff --git a/examples/standalone.py b/examples/standalone.py index 2f6d1c0..2c672be 100644 --- a/examples/standalone.py +++ b/examples/standalone.py @@ -2,28 +2,29 @@ # <<<<< auto-generated by daylinmorgan/viv (v.22.12a3) # fmt: off -def _viv_activate(*pkgs: str, track_exe: bool = False, name: str = "") -> None: # noqa - i,s,m,e,spec=__import__,str,map,lambda x: True if x else False, [*pkgs] # 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=True, exist_ok=True) # noqa - ((hash:=i("hashlib").sha256()).update((s(spec)+ # noqa - (((exe:=s(P(i("sys").executable).resolve()) if track_exe else "N/A")))).encode())) # noqa - if (env:=cache/(name if name else (_id:=hash.hexdigest()))) not in 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=True,clear=True).create(env) # noqa - with (env/"pip.conf").open("w") as f:f.write("[global]\ndisable-pip-version-check=true") # noqa - if (p:=i("subprocess").run([env/"bin"/"pip","install","--force-reinstall",*spec],text=True, # noqa - stdout=(-1,None)[v],stderr=(-2,None)[v])).returncode!=0: # noqa - if env.is_dir():i("shutil").rmtree(env) # noqa - ew(f"pip had non zero exit ({p.returncode})\n{p.stdout}\n");sys.exit(p.returncode) # noqa - with (env/"viv-info.json").open("w") as f: # noqa - i("json").dump({"created":s(i("datetime").datetime.today()),"id":_id,"spec":spec,"exe":exe},f) # noqa - sys.path = [p for p in (*sys.path,s(*(env/"lib").glob("py*/si*"))) if p!=i("site").USER_SITE] # noqa -_viv_activate("pyfiglet==0.8.post1") # noqa +def _viv_activate(*pkgs: str, track_exe: bool = False, name: str = "") -> None: # noqa + i,s,m,e,spec=__import__,str,map,lambda x: True if x else False,[*pkgs] # 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=True,exist_ok=True) # noqa + ((sha256:=i("hashlib").sha256()).update((s(spec)+ # noqa + (((exe:=s(P(i("sys").executable).resolve()) if track_exe else "N/A")))).encode())) # noqa + if (env:=cache/(name if name else (_id:=sha256.hexdigest()))) not in 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=True,clear=True).create(env) # noqa + with (env/"pip.conf").open("w") as f:f.write("[global]\ndisable-pip-version-check=true") # noqa + if (p:=i("subprocess").run([env/"bin"/"pip","install","--force-reinstall",*spec],text=True, # noqa + stdout=(-1,None)[v],stderr=(-2,None)[v])).returncode!=0: # noqa + if env.is_dir():i("shutil").rmtree(env) # noqa + ew(f"pip had non zero exit ({p.returncode})\n{p.stdout}\n");sys.exit(p.returncode) # noqa + with (env/"viv-info.json").open("w") as f: # noqa + i("json").dump({"created":s(i("datetime").datetime.today()),"id":_id,"spec":spec,"exe":exe},f) # noqa + sys.path = [p for p in (*sys.path,s(*(env/"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__": diff --git a/src/viv/viv.py b/src/viv/viv.py index 92b00ff..874ba63 100755 --- a/src/viv/viv.py +++ b/src/viv/viv.py @@ -487,13 +487,13 @@ STANDALONE_TEMPLATE = r""" STANDALONE_TEMPLATE_ACTIVATE = r""" def _viv_activate(*pkgs: str, track_exe: bool = False, name: str = "") -> None: - i,s,m,e,spec=__import__,str,map,lambda x: True if x else False, [*pkgs] + i,s,m,e,spec=__import__,str,map,lambda x: True if x else False,[*pkgs] 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 - (cache:=(P(ge("XDG_CACHE_HOME",P.home()/".cache"))/"viv"/"venvs")).mkdir(parents=True, exist_ok=True) - ((hash:=i("hashlib").sha256()).update((s(spec)+ + (cache:=(P(ge("XDG_CACHE_HOME",P.home()/".cache"))/"viv"/"venvs")).mkdir(parents=True,exist_ok=True) + ((sha256:=i("hashlib").sha256()).update((s(spec)+ (((exe:=s(P(i("sys").executable).resolve()) if track_exe else "N/A")))).encode())) - if (env:=cache/(name if name else (_id:=hash.hexdigest()))) not in cache.glob("*/") or ge("VIV_FORCE"): + if (env:=cache/(name if name else (_id:=sha256.hexdigest()))) not in cache.glob("*/") or ge("VIV_FORCE"): v=e(ge("VIV_VERBOSE"));ew(f"generating new vivenv -> {{env.name}}\n") i("venv").EnvBuilder(with_pip=True,clear=True).create(env) with (env/"pip.conf").open("w") as f:f.write("[global]\ndisable-pip-version-check=true")