one big wip 🙈

This commit is contained in:
Daylin Morgan 2023-05-27 17:00:54 -05:00
parent 298e8b274c
commit e68f5a3ba1
Signed by: daylin
GPG key ID: C1E52E7DD81DF79F

View file

@ -52,31 +52,39 @@ from typing import (
from urllib.error import HTTPError from urllib.error import HTTPError
from urllib.request import urlopen from urllib.request import urlopen
__version__ = "23.5a1-dev" __version__ = "23.5a1-13-g1717203"
@dataclass
class Config: class Config:
"""viv config manager""" """viv config manager"""
venvcache: Path = ( def __init__(self):
Path(os.getenv("XDG_CACHE_HOME", Path.home() / ".cache")) / "viv" / "venvs" self._cache = Path(os.getenv("XDG_CACHE_HOME", Path.home() / ".cache")) / "viv"
)
srccache: Path = ( def _ensure(self, p: Path) -> Path:
Path(os.getenv("XDG_CACHE_HOME", Path.home() / ".cache")) / "viv" / "src" p.mkdir(parents=True, exist_ok=True)
) return p
share: Path = (
Path(os.getenv("XDG_DATA_HOME", Path.home() / ".local" / "share")) / "viv" @property
def venvcache(self):
return self._ensure(self._cache / "venvs")
@property
def srccache(self):
return self._ensure(self._cache / "src")
@property
def binparent(self):
return self._ensure(
Path(os.getenv("VIV_BIN_DIR", Path.home() / ".local" / "bin"))
) )
def __post_init__(self) -> None: @property
self.venvcache.mkdir(parents=True, exist_ok=True) def srcdefault(self) -> None:
self.srccache.mkdir( parent = (
parents=True, Path(os.getenv("XDG_DATA_HOME", Path.home() / ".local" / "share")) / "viv"
exist_ok=True,
) )
self.share.mkdir(parents=True, exist_ok=True) return self._ensure(parent) / "viv.py"
self.srcdefault = self.share / "viv.py"
c = Config() c = Config()
@ -483,7 +491,7 @@ class ViVenv:
a.table((("key", "value"), *((k, v) for k, v in info.items()))) a.table((("key", "value"), *((k, v) for k, v in info.items())))
def use(*packages: str, track_exe: bool = False, name: str = "") -> ViVenv: def use(*packages: str, track_exe: bool = False, name: str = "") -> Path:
"""create a vivenv and append to sys.path """create a vivenv and append to sys.path
Args: Args:
@ -502,7 +510,7 @@ def use(*packages: str, track_exe: bool = False, name: str = "") -> ViVenv:
vivenv.dump_info(write=True) vivenv.dump_info(write=True)
modify_sys_path(vivenv.path) modify_sys_path(vivenv.path)
return vivenv return vivenv.path
def validate_spec(spec: Tuple[str, ...]) -> None: def validate_spec(spec: Tuple[str, ...]) -> None:
@ -552,25 +560,27 @@ STANDALONE_TEMPLATE = r"""
# >>>>> code golfed with <3 # >>>>> code golfed with <3
""" # noqa """ # noqa
STANDALONE_TEMPLATE_FUNC = r"""def _viv_use(*pkgs: str, track_exe: bool = False, name: str = "") -> None: STANDALONE_TEMPLATE_FUNC = r"""def _viv_use(*pkgs, track_exe=False, name=""):
i,s,m,e,spec=__import__,str,map,lambda x: True if x else False,[*pkgs] T,F=True,False;i,s,m,e,spec=__import__,str,map,lambda x: T if x else F,[*pkgs]
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=True,exist_ok=True) (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()))) not in cache.glob("*/") or ge("VIV_FORCE"): if ((env:=cache/(name if name else (_id:=sha256.hexdigest())))
v=e(ge("VIV_VERBOSE"));ew(f"generating new vivenv -> {{env.name}}\n") not in cache.glob("*/")) or ge("VIV_FORCE"):
i("venv").EnvBuilder(with_pip=True,clear=True).create(env) v=e(ge("VIV_VERBOSE"));ew(f"generating new vivenv -> {env.name}\n")
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") with (env/"pip.conf").open("w") as f:f.write("[global]\ndisable-pip-version-check=true")
if (p:=i("subprocess").run([env/"bin"/"pip","install","--force-reinstall",*spec],text=True, if (p:=i("subprocess").run([env/"bin"/"pip","install","--force-reinstall",*spec],text=True,
stdout=(-1,None)[v],stderr=(-2,None)[v])).returncode!=0: stdout=(-1,None)[v],stderr=(-2,None)[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 ({p.returncode})\n{p.stdout}\n");sys.exit(p.returncode)
with (env/"viv-info.json").open("w") as f: with (env/"viv-info.json").open("w") as f:
i("json").dump({{"created":s(i("datetime").datetime.today()),"id":_id,"spec":spec,"exe":exe}},f) i("json").dump({"created":s(i("datetime").datetime.today()),
"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]
{use} return env
""" # noqa """ # noqa
SHOW_TEMPLATE = f""" SHOW_TEMPLATE = f"""
@ -593,6 +603,18 @@ UPDATE_TEMPLATE = f"""
""" """
SHIM_TEMPLATE = """\
#!/usr/bin/env python3
{imports}
import subprocess
import sys
if __name__ == "__main__":
vivenv = {use}
sys.exit(subprocess.run([vivenv / "bin" / "{bin}", *sys.argv[1:]]).returncode)
"""
def noqa(txt: str) -> str: def noqa(txt: str) -> str:
max_length = max(map(len, txt.splitlines())) max_length = max(map(len, txt.splitlines()))
@ -663,15 +685,14 @@ def generate_import(
STANDALONE_TEMPLATE.format( STANDALONE_TEMPLATE.format(
version=__version__, version=__version__,
func=noqa( func=noqa(
STANDALONE_TEMPLATE_FUNC.format( STANDALONE_TEMPLATE_FUNC
use="_viv_use(" + "_viv_use("
+ fill( + fill(
", ".join(f'"{pkg}"' for pkg in resolved_spec.splitlines()), ", ".join(f'"{pkg}"' for pkg in resolved_spec.splitlines()),
width=100, width=100,
subsequent_indent=" ", subsequent_indent=" ",
) )
+ ")" + ")"
)
), ),
) )
+ "\n" + "\n"
@ -904,10 +925,13 @@ class Viv:
except ImportError: except ImportError:
self.local_source = self.local_version = "Not Found" self.local_source = self.local_version = "Not Found"
self.git = self.local_source != "Not Found" and str(self.local_source).endswith( self.git = (self.local_source.parent.parent.parent / ".git").is_dir()
"src/viv/__init__.py" def _check_local_source(self, args:Namespace ) -> None:
if self.local_source == "Not Found" and not (args.standalone or args.path):
warn(
"failed to find local copy of `viv` "
"make sure to add it to your PYTHONPATH"
) )
def _match_vivenv(self, name_id: str) -> ViVenv: # type: ignore[return] def _match_vivenv(self, name_id: str) -> ViVenv: # type: ignore[return]
# TODO: improve matching algorithm to favor names over id's # TODO: improve matching algorithm to favor names over id's
matches: List[ViVenv] = [] matches: List[ViVenv] = []
@ -948,8 +972,8 @@ class Viv:
def freeze(self, args: Namespace) -> None: def freeze(self, args: Namespace) -> None:
"""create import statement from package spec""" """create import statement from package spec"""
# TODO: warn user about using anything but standalone when
# self.local_source is 'Not Found' self._check_local_source(args)
if not args.reqs: if not args.reqs:
error("must specify a requirement", code=1) error("must specify a requirement", code=1)
@ -1124,9 +1148,53 @@ class Viv:
): ):
self._install_local_src(sha256, args.src, args.cli) self._install_local_src(sha256, args.src, args.cli)
def shim(self, args): def shim(self, args):
"""generate viv-powered cli apps""" """generate viv-powered cli apps"""
echo("not implemented.") self._check_local_source(args)
if not args.reqs:
error("please specify at lease one dependency")
spec = ", ".join(f'"{req}"' for req in args.reqs)
if len(args.reqs) == 1:
bin = args.reqs[0]
output = c.binparent / args.reqs[0]
else:
error("please specify an explicit -b/--bin and -o/--output", code=1)
if output.is_file():
error(f"{output} already exists...exiting", code=1)
if args.standalone:
imports = STANDALONE_TEMPLATE.format(
version=__version__, func=noqa(STANDALONE_TEMPLATE_FUNC)
)
use = f"_viv_use({spec})"
elif args.path:
if self.local_source == "Not Found":
error("No local viv found to import from", code=1)
imports = (
REL_SYS_PATH_TEMPLATE.format(
path_to_viv=str(
self.local_source.resolve().absolute().parent.parent
).replace(str(Path.home()), "~")
)
if args.path == "abs"
else SYS_PATH_TEMPLATE.format(
path_to_viv=self.local_source.resolve().absolute().parent.parent
)
)
use = IMPORT_TEMPLATE.format(spec=spec)
else:
imports = ""
use = IMPORT_TEMPLATE.format(spec=spec)
# TODO: confirm next steps?
with output.open("w") as f:
f.write(SHIM_TEMPLATE.format(imports=imports, use=use, bin=bin))
make_executable(output)
def _get_subcmd_parser( def _get_subcmd_parser(
self, self,
@ -1295,15 +1363,23 @@ class Viv:
subparsers, "shim", parents=[p_freeze_shim_shared] subparsers, "shim", parents=[p_freeze_shim_shared]
) )
).set_defaults(func=self.shim, cmd="shim") ).set_defaults(func=self.shim, cmd="shim")
p_manage_shim.add_argument(
"-f",
"--freeze",
help="freeze/resolve all dependencies",
action="store_true",
)
p_manage_shim.add_argument( p_manage_shim.add_argument(
"-o", "-o",
"--output", "--output",
help="path/to/output file", help="path/to/output file",
type=Path,
metavar="<path>", metavar="<path>",
) )
p_manage_shim.add_argument( p_manage_shim.add_argument(
"-b", "--bin", help="console_script/script to invoke" "-b", "--bin", help="console_script/script to invoke"
) )
args = parser.parse_args() args = parser.parse_args()
args.func(args) args.func(args)