refactor: abandon python 3.7 to maximize code-golf in standalone mode

This commit is contained in:
Daylin Morgan 2023-03-09 12:42:02 -06:00
parent 834cd449bd
commit e548487918
Signed by: daylin
GPG key ID: C1E52E7DD81DF79F
3 changed files with 68 additions and 70 deletions

View file

@ -66,26 +66,24 @@ The only part necessary to modify if copied verbatim from here is the call to `_
# <<<<< auto-generated by daylinmorgan/viv (v.22.12a3) # <<<<< auto-generated by daylinmorgan/viv (v.22.12a3)
# fmt: off # fmt: off
def _viv_activate(*pkgs: str, track_exe: bool = False, name: str = "") -> None: # noqa 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 i,s,m,e,spec=__import__,str,map,lambda x: True if x else False, [*pkgs] # noqa
if not {*m(type, pkgs)} == {str}: raise ValueError(f"spec: {pkgs} is invalid") # noqa if not {*m(type,pkgs)}=={s}: raise ValueError(f"spec: {pkgs} is invalid") # noqa
ge,sys,P,exe = i("os").getenv,i("sys"),i("pathlib").Path,i("sys").executable # noqa ge,sys,P,ew=i("os").getenv,i("sys"),i("pathlib").Path,i("sys").stderr.write # noqa
vivcache = (P(ge("XDG_CACHE_HOME",P.home()/".cache"))/"viv"/"venvs");vivcache.mkdir(parents=True, exist_ok=True) # noqa (cache:=(P(ge("XDG_CACHE_HOME",P.home()/".cache"))/"viv"/"venvs")).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()).update((s(spec)+ # noqa
hash = i("hashlib").sha256(); hash.update(str(spec).encode()) # noqa (((exe:=s(P(i("sys").executable).resolve()) if track_exe else "N/A")))).encode())) # noqa
if track_exe: hash.update(exe_path.encode()) # noqa if (env:=cache/(name if name else (_id:=hash.hexdigest()))) not in cache.glob("*/") or ge("VIV_FORCE"): # noqa
_id = hash.hexdigest() # noqa v=e(ge("VIV_VERBOSE"));ew(f"generating new vivenv -> {env.name}\n") # noqa
name, envpath = (lambda n: (n,vivcache/n))(name if name else _id) # noqa i("venv").EnvBuilder(with_pip=True,clear=True).create(env) # noqa
if name not in (d.name for d in vivcache.iterdir()) or ge("VIV_FORCE"): # noqa with (env/"pip.conf").open("w") as f:f.write("[global]\ndisable-pip-version-check=true") # noqa
run = i("subprocess").run; i("venv").EnvBuilder(with_pip=True, clear=True).create(envpath) # noqa if (p:=i("subprocess").run([env/"bin"/"pip","install","--force-reinstall",*spec],text=True, # noqa
with (envpath/"pip.conf").open("w") as f:f.write("[global]\ndisable-pip-version-check = true") # noqa stdout=(-1,None)[v],stderr=(-2,None)[v])).returncode!=0: # noqa
p = run([envpath/"bin"/"pip","install","--force-reinstall", *spec], universal_newlines=True, # noqa if env.is_dir():i("shutil").rmtree(env) # noqa
**dict(zip(("stdout","stderr"),[(-1,-2),(None,)*2,][e(ge("VIV_VERBOSE"))]))) # noqa ew(f"pip had non zero exit ({p.returncode})\n{p.stdout}\n");sys.exit(p.returncode) # noqa
if (p.returncode!=0)*envpath.is_dir():i("shutil").rmtree(str(envpath)) # noqa with (env/"viv-info.json").open("w") as f: # noqa
if p.returncode!=0:sys.stderr.write(f"pip had non zero exit ({p.returncode})\n{p.stdout}");sys.exit(p.returncode) # noqa i("json").dump({"created":s(i("datetime").datetime.today()),"id":_id,"spec":spec,"exe":exe},f) # noqa
with (envpath/"viv-info.json").open("w") as f: # noqa sys.path = [p for p in (*sys.path,s(*(env/"lib").glob("py*/si*"))) if p!=i("site").USER_SITE] # noqa
i("json").dump({"created":str(i("datetime").datetime.today()),"id":_id,"spec":spec,"exe":exe},f) # noqa _viv_activate("markdown-it-py==2.2.0", "mdurl==0.1.2", "Pygments==2.14.0", "rich==13.3.2") # noqa
sys.path = [p for p in (*sys.path, str(*(envpath/"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")
# fmt: on # fmt: on
# >>>>> code golfed with <3 # >>>>> code golfed with <3
``` ```

View file

@ -3,27 +3,24 @@
# <<<<< auto-generated by daylinmorgan/viv (v.22.12a3) # <<<<< auto-generated by daylinmorgan/viv (v.22.12a3)
# fmt: off # fmt: off
def _viv_activate(*pkgs: str, track_exe: bool = False, name: str = "") -> None: # noqa 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 i,s,m,e,spec=__import__,str,map,lambda x: True if x else False, [*pkgs] # noqa
if not {*m(type, pkgs)} == {str}: raise ValueError(f"spec: {pkgs} is invalid") # noqa if not {*m(type,pkgs)}=={s}: raise ValueError(f"spec: {pkgs} is invalid") # noqa
ge,sys,P,exe = i("os").getenv,i("sys"),i("pathlib").Path,i("sys").executable # noqa ge,sys,P,ew=i("os").getenv,i("sys"),i("pathlib").Path,i("sys").stderr.write # noqa
vivcache = (P(ge("XDG_CACHE_HOME",P.home()/".cache"))/"viv"/"venvs");vivcache.mkdir(parents=True, exist_ok=True) # noqa (cache:=(P(ge("XDG_CACHE_HOME",P.home()/".cache"))/"viv"/"venvs")).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()).update((s(spec)+ # noqa
hash = i("hashlib").sha256(); hash.update(str(spec).encode()) # noqa (((exe:=s(P(i("sys").executable).resolve()) if track_exe else "N/A")))).encode())) # noqa
if track_exe: hash.update(exe_path.encode()) # noqa if (env:=cache/(name if name else (_id:=hash.hexdigest()))) not in cache.glob("*/") or ge("VIV_FORCE"): # noqa
_id = hash.hexdigest() # noqa v=e(ge("VIV_VERBOSE"));ew(f"generating new vivenv -> {env.name}\n") # noqa
name, envpath = (lambda n: (n,vivcache/n))(name if name else _id) # noqa i("venv").EnvBuilder(with_pip=True,clear=True).create(env) # noqa
if name not in (d.name for d in vivcache.iterdir()) or ge("VIV_FORCE"): # noqa with (env/"pip.conf").open("w") as f:f.write("[global]\ndisable-pip-version-check=true") # noqa
run = i("subprocess").run; i("venv").EnvBuilder(with_pip=True, clear=True).create(envpath) # noqa if (p:=i("subprocess").run([env/"bin"/"pip","install","--force-reinstall",*spec],text=True, # noqa
with (envpath/"pip.conf").open("w") as f:f.write("[global]\ndisable-pip-version-check = true") # noqa stdout=(-1,None)[v],stderr=(-2,None)[v])).returncode!=0: # noqa
p = run([envpath/"bin"/"pip","install","--force-reinstall", *spec], universal_newlines=True, # noqa if env.is_dir():i("shutil").rmtree(env) # noqa
**dict(zip(("stdout","stderr"),[(-1,-2),(None,)*2,][e(ge("VIV_VERBOSE"))]))) # noqa ew(f"pip had non zero exit ({p.returncode})\n{p.stdout}\n");sys.exit(p.returncode) # noqa
if (p.returncode!=0)*envpath.is_dir():i("shutil").rmtree(str(envpath)) # noqa with (env/"viv-info.json").open("w") as f: # noqa
if p.returncode!=0:sys.stderr.write(f"pip had non zero exit ({p.returncode})\\n{p.stdout}");sys.exit(p.returncode) # noqa i("json").dump({"created":s(i("datetime").datetime.today()),"id":_id,"spec":spec,"exe":exe},f) # noqa
with (envpath/"viv-info.json").open("w") as f: # noqa sys.path = [p for p in (*sys.path,s(*(env/"lib").glob("py*/si*"))) if p!=i("site").USER_SITE] # 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 _viv_activate("pyfiglet==0.8.post1") # noqa
# fmt: on # fmt: on
# >>>>> code golfed with <3 # >>>>> code golfed with <3

View file

@ -485,33 +485,34 @@ STANDALONE_TEMPLATE = r"""
# >>>>> code golfed with <3 # >>>>> code golfed with <3
""" # noqa """ # noqa
STANDALONE_TEMPLATE_ACTIVATE = " # noqa\n".join( STANDALONE_TEMPLATE_ACTIVATE = r"""
r"""
def _viv_activate(*pkgs: str, track_exe: bool = False, name: str = "") -> None: def _viv_activate(*pkgs: str, track_exe: bool = False, name: str = "") -> None:
i,m,e = __import__,map,lambda x: True if x else False i,s,m,e,spec=__import__,str,map,lambda x: True if x else False, [*pkgs]
if not {{*m(type, pkgs)}} == {{str}}: raise ValueError(f"spec: {{pkgs}} is invalid") if not {{*m(type,pkgs)}}=={{s}}: raise ValueError(f"spec: {{pkgs}} is invalid")
ge,sys,P,exe = i("os").getenv,i("sys"),i("pathlib").Path,i("sys").executable ge,sys,P,ew=i("os").getenv,i("sys"),i("pathlib").Path,i("sys").stderr.write
vivcache = (P(ge("XDG_CACHE_HOME",P.home()/".cache"))/"viv"/"venvs");vivcache.mkdir(parents=True, exist_ok=True) (cache:=(P(ge("XDG_CACHE_HOME",P.home()/".cache"))/"viv"/"venvs")).mkdir(parents=True, exist_ok=True)
spec,exe_path = [*pkgs], (str(P(exe).resolve()) if track_exe else "N/A") ((hash:=i("hashlib").sha256()).update((s(spec)+
hash = i("hashlib").sha256(); hash.update(str(spec).encode()) (((exe:=s(P(i("sys").executable).resolve()) if track_exe else "N/A")))).encode()))
if track_exe: hash.update(exe_path.encode()) if (env:=cache/(name if name else (_id:=hash.hexdigest()))) not in cache.glob("*/") or ge("VIV_FORCE"):
_id = hash.hexdigest() v=e(ge("VIV_VERBOSE"));ew(f"generating new vivenv -> {{env.name}}\n")
name, envpath = (lambda n: (n,vivcache/n))(name if name else _id) i("venv").EnvBuilder(with_pip=True,clear=True).create(env)
if name not in (d.name for d in vivcache.iterdir()) or ge("VIV_FORCE"): with (env/"pip.conf").open("w") as f:f.write("[global]\ndisable-pip-version-check=true")
run = i("subprocess").run; i("venv").EnvBuilder(with_pip=True, clear=True).create(envpath) if (p:=i("subprocess").run([env/"bin"/"pip","install","--force-reinstall",*spec],text=True,
with (envpath/"pip.conf").open("w") as f:f.write("[global]\ndisable-pip-version-check = true") stdout=(-1,None)[v],stderr=(-2,None)[v])).returncode!=0:
p = run([envpath/"bin"/"pip","install","--force-reinstall", *spec], universal_newlines=True, if env.is_dir():i("shutil").rmtree(env)
**dict(zip(("stdout","stderr"),[(-1,-2),(None,)*2,][e(ge("VIV_VERBOSE"))]))) ew(f"pip had non zero exit ({{p.returncode}})\n{{p.stdout}}\n");sys.exit(p.returncode)
if (p.returncode!=0)*envpath.is_dir():i("shutil").rmtree(str(envpath)) with (env/"viv-info.json").open("w") as f:
if p.returncode!=0:sys.stderr.write(f"pip had non zero exit ({{p.returncode}})\n{{p.stdout}}");sys.exit(p.returncode) i("json").dump({{"created":s(i("datetime").datetime.today()),"id":_id,"spec":spec,"exe":exe}},f)
with (envpath/"viv-info.json").open("w") as f: sys.path = [p for p in (*sys.path,s(*(env/"lib").glob("py*/si*"))) if p!=i("site").USER_SITE]
i("json").dump({{"created":str(i("datetime").datetime.today()),"id":_id,"spec":spec,"exe":exe}},f)
sys.path = [p for p in (*sys.path, str(*(envpath/"lib").glob("py*/si*"))) if p!=i("site").USER_SITE]
_viv_activate({spec}) _viv_activate({spec})
""".splitlines()[ # noqa """[ # noqa
1: 1:
] ]
)
def noqa(txt: str) -> str:
max_length = max(map(len, txt.splitlines()))
return "\n".join((f"{line:{max_length}} # noqa" for line in txt.splitlines()))
def spec_to_import(spec: List[str]) -> None: def spec_to_import(spec: List[str]) -> None:
@ -584,8 +585,10 @@ def generate_import(
sys.stdout.write( sys.stdout.write(
STANDALONE_TEMPLATE.format( STANDALONE_TEMPLATE.format(
version=__version__, version=__version__,
activate=STANDALONE_TEMPLATE_ACTIVATE.format( activate=noqa(
STANDALONE_TEMPLATE_ACTIVATE.format(
spec=", ".join(f'"{pkg}"' for pkg in resolved_spec.splitlines()) spec=", ".join(f'"{pkg}"' for pkg in resolved_spec.splitlines())
)
), ),
) )
+ "\n" + "\n"