fix: use raw string literal

This commit is contained in:
Daylin Morgan 2023-05-30 16:24:17 -05:00
parent a6bd81dfc3
commit e257b2a49b
Signed by: daylin
GPG key ID: C1E52E7DD81DF79F
2 changed files with 36 additions and 30 deletions

View file

@ -1,32 +1,36 @@
#!/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]
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")
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)
((sha256:=i("hashlib").sha256()).update((s(spec)+
(((exe:=("N/A",s(P(i("sys").executable).resolve()))[e(track_exe)])))).encode()))
if {env:=cache/(((_id:=sha256.hexdigest()),name)[e(name)])}-{*cache.glob("*/")} or ge("VIV_FORCE"):
v=e(ge("VIV_VERBOSE"));ew(f"generating new vivenv -> {env.name}
")
i("venv").EnvBuilder(with_pip=T,clear=T).create(env)
w(env/"pip.conf","[global]
disable-pip-version-check=true")
if (rc:=(p:=i("subprocess").run([env/"bin"/"pip","install","--force-reinstall",*spec],text=T,
stdout=(-1,N)[v],stderr=(-2,N)[v])).returncode)!=0:
if env.is_dir():i("shutil").rmtree(env)
ew(f"pip had non zero exit ({rc})
{p.stdout}
");sys.exit(rc)
w(env/"viv-info.json",i("json").dumps(
{"created":s(i("datetime").datetime.today()),"id":_id,"spec":spec,"exe":exe}))
sys.path=[p for p in (*sys.path,s(*(env/"lib").glob("py*/si*")))if p!=i("site").USER_SITE]
return env
# 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
import subprocess
import sys
if __name__ == "__main__":
vivenv = _viv_use("black==23.3.0", "click==8.1.3", "mypy-extensions==1.0.0", "packaging==23.1",
"pathspec==0.11.1", "platformdirs==3.5.1")
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
sys.exit(subprocess.run([vivenv / "bin" / "black", *sys.argv[1:]]).returncode)

View file

@ -51,7 +51,7 @@ from typing import (
from urllib.error import HTTPError
from urllib.request import urlopen
__version__ = "23.5a4-12-g138809e-dev"
__version__ = "23.5a4-13-ga6bd81d-dev"
class Config:
@ -339,7 +339,7 @@ to create/activate a vivenv:
- from command line: `{a.style("viv -h","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]
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")
@ -426,7 +426,9 @@ to create/activate a vivenv:
bin: str,
) -> str:
if standalone:
imports = self._standalone_func
imports = "\n".join(
("# fmt: off", self.noqa(self._standalone_func), "# fmt: on")
)
elif path == "abs":
imports = self._absolute_import(local_source)
elif path == "rel":
@ -440,7 +442,7 @@ import subprocess
import sys
if __name__ == "__main__":
vivenv = {self._use_str(spec, standalone)}
vivenv = {self.noqa(self._use_str(spec, standalone))}
sys.exit(subprocess.run([vivenv / "bin" / "{bin}", *sys.argv[1:]]).returncode)
"""