mirror of
https://github.com/daylinmorgan/viv.git
synced 2024-11-10 03:13:14 -06:00
one big wip 🙈
This commit is contained in:
parent
298e8b274c
commit
e68f5a3ba1
1 changed files with 121 additions and 45 deletions
166
src/viv/viv.py
166
src/viv/viv.py
|
@ -52,31 +52,39 @@ from typing import (
|
|||
from urllib.error import HTTPError
|
||||
from urllib.request import urlopen
|
||||
|
||||
__version__ = "23.5a1-dev"
|
||||
__version__ = "23.5a1-13-g1717203"
|
||||
|
||||
|
||||
@dataclass
|
||||
class Config:
|
||||
"""viv config manager"""
|
||||
|
||||
venvcache: Path = (
|
||||
Path(os.getenv("XDG_CACHE_HOME", Path.home() / ".cache")) / "viv" / "venvs"
|
||||
)
|
||||
srccache: Path = (
|
||||
Path(os.getenv("XDG_CACHE_HOME", Path.home() / ".cache")) / "viv" / "src"
|
||||
)
|
||||
share: Path = (
|
||||
Path(os.getenv("XDG_DATA_HOME", Path.home() / ".local" / "share")) / "viv"
|
||||
)
|
||||
def __init__(self):
|
||||
self._cache = Path(os.getenv("XDG_CACHE_HOME", Path.home() / ".cache")) / "viv"
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
self.venvcache.mkdir(parents=True, exist_ok=True)
|
||||
self.srccache.mkdir(
|
||||
parents=True,
|
||||
exist_ok=True,
|
||||
def _ensure(self, p: Path) -> Path:
|
||||
p.mkdir(parents=True, exist_ok=True)
|
||||
return p
|
||||
|
||||
@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"))
|
||||
)
|
||||
self.share.mkdir(parents=True, exist_ok=True)
|
||||
self.srcdefault = self.share / "viv.py"
|
||||
|
||||
@property
|
||||
def srcdefault(self) -> None:
|
||||
parent = (
|
||||
Path(os.getenv("XDG_DATA_HOME", Path.home() / ".local" / "share")) / "viv"
|
||||
)
|
||||
return self._ensure(parent) / "viv.py"
|
||||
|
||||
|
||||
c = Config()
|
||||
|
@ -483,7 +491,7 @@ class ViVenv:
|
|||
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
|
||||
|
||||
Args:
|
||||
|
@ -502,7 +510,7 @@ def use(*packages: str, track_exe: bool = False, name: str = "") -> ViVenv:
|
|||
vivenv.dump_info(write=True)
|
||||
|
||||
modify_sys_path(vivenv.path)
|
||||
return vivenv
|
||||
return vivenv.path
|
||||
|
||||
|
||||
def validate_spec(spec: Tuple[str, ...]) -> None:
|
||||
|
@ -552,25 +560,27 @@ STANDALONE_TEMPLATE = r"""
|
|||
# >>>>> code golfed with <3
|
||||
""" # noqa
|
||||
|
||||
STANDALONE_TEMPLATE_FUNC = r"""def _viv_use(*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]
|
||||
if not {{*m(type,pkgs)}}=={{s}}: raise ValueError(f"spec: {{pkgs}} is invalid")
|
||||
STANDALONE_TEMPLATE_FUNC = r"""def _viv_use(*pkgs, track_exe=False, name=""):
|
||||
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")
|
||||
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)+
|
||||
(((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"):
|
||||
v=e(ge("VIV_VERBOSE"));ew(f"generating new vivenv -> {{env.name}}\n")
|
||||
i("venv").EnvBuilder(with_pip=True,clear=True).create(env)
|
||||
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=T,clear=T).create(env)
|
||||
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,
|
||||
stdout=(-1,None)[v],stderr=(-2,None)[v])).returncode!=0:
|
||||
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:
|
||||
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]
|
||||
{use}
|
||||
return env
|
||||
""" # noqa
|
||||
|
||||
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:
|
||||
max_length = max(map(len, txt.splitlines()))
|
||||
|
@ -663,15 +685,14 @@ def generate_import(
|
|||
STANDALONE_TEMPLATE.format(
|
||||
version=__version__,
|
||||
func=noqa(
|
||||
STANDALONE_TEMPLATE_FUNC.format(
|
||||
use="_viv_use("
|
||||
+ fill(
|
||||
", ".join(f'"{pkg}"' for pkg in resolved_spec.splitlines()),
|
||||
width=100,
|
||||
subsequent_indent=" ",
|
||||
)
|
||||
+ ")"
|
||||
STANDALONE_TEMPLATE_FUNC
|
||||
+ "_viv_use("
|
||||
+ fill(
|
||||
", ".join(f'"{pkg}"' for pkg in resolved_spec.splitlines()),
|
||||
width=100,
|
||||
subsequent_indent=" ",
|
||||
)
|
||||
+ ")"
|
||||
),
|
||||
)
|
||||
+ "\n"
|
||||
|
@ -904,10 +925,13 @@ class Viv:
|
|||
except ImportError:
|
||||
self.local_source = self.local_version = "Not Found"
|
||||
|
||||
self.git = self.local_source != "Not Found" and str(self.local_source).endswith(
|
||||
"src/viv/__init__.py"
|
||||
)
|
||||
|
||||
self.git = (self.local_source.parent.parent.parent / ".git").is_dir()
|
||||
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]
|
||||
# TODO: improve matching algorithm to favor names over id's
|
||||
matches: List[ViVenv] = []
|
||||
|
@ -948,8 +972,8 @@ class Viv:
|
|||
|
||||
def freeze(self, args: Namespace) -> None:
|
||||
"""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:
|
||||
error("must specify a requirement", code=1)
|
||||
|
@ -1123,10 +1147,54 @@ class Viv:
|
|||
),
|
||||
):
|
||||
self._install_local_src(sha256, args.src, args.cli)
|
||||
|
||||
|
||||
def shim(self, args):
|
||||
"""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(
|
||||
self,
|
||||
|
@ -1295,15 +1363,23 @@ class Viv:
|
|||
subparsers, "shim", parents=[p_freeze_shim_shared]
|
||||
)
|
||||
).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(
|
||||
"-o",
|
||||
"--output",
|
||||
help="path/to/output file",
|
||||
type=Path,
|
||||
metavar="<path>",
|
||||
)
|
||||
p_manage_shim.add_argument(
|
||||
"-b", "--bin", help="console_script/script to invoke"
|
||||
)
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
args.func(args)
|
||||
|
|
Loading…
Reference in a new issue