feat: add --standalone generator to viv freeze

This commit is contained in:
Daylin Morgan 2023-03-07 14:36:51 -06:00
parent 10599dcd01
commit c797347aa6
Signed by: daylin
GPG key ID: C1E52E7DD81DF79F
2 changed files with 108 additions and 3 deletions

36
examples/standalone.py Normal file
View file

@ -0,0 +1,36 @@
#!/usr/bin/env python3
# <<<<< auto-generated by daylinmorgan/viv (v.22.12a3)
# fmt: off
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
if not {*m(type, pkgs)} == {str}: raise ValueError(f"spec: {pkgs} is invalid") # noqa
ge,sys,P,exe = i("os").getenv,i("sys"),i("pathlib").Path,i("sys").executable # noqa
vivcache = (P(ge("XDG_CACHE_HOME",P.home()/".cache"))/"viv"/"venvs");vivcache.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(); hash.update(str(spec).encode()) # noqa
if track_exe: hash.update(exe_path.encode()) # noqa
_id = hash.hexdigest() # noqa
name, envpath = (lambda n: (n,vivcache/n))(name if name else _id) # noqa
if name not in (d.name for d in vivcache.iterdir()) or ge("VIV_FORCE"): # noqa
run = i("subprocess").run; i("venv").EnvBuilder(with_pip=True, clear=True).create(envpath) # noqa
with (envpath/"pip.conf").open("w") as f:f.write("[global]\ndisable-pip-version-check = true") # noqa
p = run([envpath/"bin"/"pip","install","--force-reinstall", *spec], universal_newlines=True, # noqa
**dict(zip(("stdout","stderr"),[(-1,-2),(None,)*2,][e(ge("VIV_VERBOSE"))]))) # noqa
if (p.returncode!=0)*envpath.is_dir():i("shutil").rmtree(str(envpath)) # noqa
if p.returncode!=0:sys.stderr.write(f"pip had non zero exit ({p.returncode})\\n{p.stdout}");sys.exit(p.returncode) # noqa
with (envpath/"viv-info.json").open("w") as f: # 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
# fmt: on
# >>>>> code golfed with <3
from pyfiglet import Figlet
if __name__ == "__main__":
f = Figlet(font="slant")
figtxt = f.renderText("viv").splitlines()
figtxt[-2] += " isn't venv!"
print("\n".join(figtxt))

View file

@ -471,9 +471,49 @@ def get_venvs():
SYS_PATH_TEMPLATE = """__import__("sys").path.append("{path_to_viv}") # noqa""" SYS_PATH_TEMPLATE = """__import__("sys").path.append("{path_to_viv}") # noqa"""
REL_SYS_PATH_TEMPLATE = """__import__("sys").path.append(__import__("os").path.expanduser("{path_to_viv}")) # noqa""" REL_SYS_PATH_TEMPLATE = (
"""__import__("sys").path.append(__import__("os")"""
""".path.expanduser("{path_to_viv}")) # noqa"""
)
IMPORT_TEMPLATE = """__import__("viv").activate({spec}) # noqa""" IMPORT_TEMPLATE = """__import__("viv").activate({spec}) # noqa"""
STANDALONE_TEMPLATE = r"""
# <<<<< auto-generated by daylinmorgan/viv (v.22.12a3)
# fmt: off
{activate}
# fmt: on
# >>>>> code golfed with <3
""" # noqa
STANDALONE_TEMPLATE_ACTIVATE = " # noqa\n".join(
r"""
def _viv_activate(*pkgs: str, track_exe: bool = False, name: str = "") -> None:
i,m,e = __import__,map,lambda x: True if x else False
if not {{*m(type, pkgs)}} == {{str}}: raise ValueError(f"spec: {{pkgs}} is invalid")
ge,sys,P,exe = i("os").getenv,i("sys"),i("pathlib").Path,i("sys").executable
vivcache = (P(ge("XDG_CACHE_HOME",P.home()/".cache"))/"viv"/"venvs");vivcache.mkdir(parents=True, exist_ok=True)
spec,exe_path = [*pkgs], (str(P(exe).resolve()) if track_exe else "N/A")
hash = i("hashlib").sha256(); hash.update(str(spec).encode())
if track_exe: hash.update(exe_path.encode())
_id = hash.hexdigest()
name, envpath = (lambda n: (n,vivcache/n))(name if name else _id)
if name not in (d.name for d in vivcache.iterdir()) or ge("VIV_FORCE"):
run = i("subprocess").run; i("venv").EnvBuilder(with_pip=True, clear=True).create(envpath)
with (envpath/"pip.conf").open("w") as f:f.write("[global]\ndisable-pip-version-check = true")
p = run([envpath/"bin"/"pip","install","--force-reinstall", *spec], universal_newlines=True,
**dict(zip(("stdout","stderr"),[(-1,-2),(None,)*2,][e(ge("VIV_VERBOSE"))])))
if (p.returncode!=0)*envpath.is_dir():i("shutil").rmtree(str(envpath))
if p.returncode!=0:sys.stderr.write(f"pip had non zero exit ({{p.returncode}})\\n{{p.stdout}}");sys.exit(p.returncode)
with (envpath/"viv-info.json").open("w") as f:
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})
""".splitlines()[ # noqa
1:
]
)
def spec_to_import(spec: List[str]) -> None: def spec_to_import(spec: List[str]) -> None:
spec_str = ", ".join(f'"{pkg}"' for pkg in spec) spec_str = ", ".join(f'"{pkg}"' for pkg in spec)
@ -501,7 +541,12 @@ def freeze_venv(spec: List[str], path: Path | None = None):
def generate_import( def generate_import(
requirements: Path, reqs: List[str], vivenvs, include_path: bool, keep: bool requirements: Path,
reqs: List[str],
vivenvs,
include_path: bool,
keep: bool,
standalone: bool,
) -> None: ) -> None:
# TODO: make compatible with Venv class for now just use the name /tmp/ # TODO: make compatible with Venv class for now just use the name /tmp/
reqs_from_file = [] reqs_from_file = []
@ -535,6 +580,19 @@ def generate_import(
) )
echo("see below for import statements\n") echo("see below for import statements\n")
if standalone:
sys.stdout.write(
STANDALONE_TEMPLATE.format(
version=__version__,
activate=STANDALONE_TEMPLATE_ACTIVATE.format(
spec=", ".join(f'"{pkg}"' for pkg in resolved_spec.splitlines())
),
)
+ "\n"
)
return
if include_path == "absolute": if include_path == "absolute":
sys.stdout.write( sys.stdout.write(
SYS_PATH_TEMPLATE.format( SYS_PATH_TEMPLATE.format(
@ -750,7 +808,12 @@ class Viv:
sys.exit(1) sys.exit(1)
generate_import( generate_import(
args.requirements, args.reqs, self.vivenvs, args.path, args.keep args.requirements,
args.reqs,
self.vivenvs,
args.path,
args.keep,
args.standalone,
) )
def list(self, args): def list(self, args):
@ -903,6 +966,12 @@ class Viv:
help="preserve environment", help="preserve environment",
action="store_true", action="store_true",
) )
p_freeze.add_argument(
"-s",
"--standalone",
help="generate standalone activation function",
action="store_true",
)
p_freeze.add_argument("reqs", help="requirements specifiers", nargs="*") p_freeze.add_argument("reqs", help="requirements specifiers", nargs="*")
self._get_subcmd_parser( self._get_subcmd_parser(