Compare commits

...

7 commits

Author SHA1 Message Date
a350fa30b7
simplify 2023-05-28 10:39:15 -05:00
187f070679
context 2023-05-28 10:35:36 -05:00
b9415949c8
whoooops 2023-05-28 10:26:57 -05:00
2dddbf31af
try again 2023-05-28 10:26:23 -05:00
e7476a6863
respect output 2023-05-28 10:21:16 -05:00
49164f8b1b
fix: don't use local-source that doesn't exist 2023-05-28 10:14:19 -05:00
e68f5a3ba1
one big wip 🙈 2023-05-28 10:06:59 -05:00

View file

@ -52,31 +52,39 @@ from typing import (
from urllib.error import HTTPError
from urllib.request import urlopen
__version__ = "23.5a1-dev"
__version__ = "23.5a1-18-g187f070"
@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 _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"))
)
def __post_init__(self) -> None:
self.venvcache.mkdir(parents=True, exist_ok=True)
self.srccache.mkdir(
parents=True,
exist_ok=True,
@property
def srcdefault(self) -> None:
parent = (
Path(os.getenv("XDG_DATA_HOME", Path.home() / ".local" / "share")) / "viv"
)
self.share.mkdir(parents=True, exist_ok=True)
self.srcdefault = self.share / "viv.py"
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("
STANDALONE_TEMPLATE_FUNC
+ "_viv_use("
+ fill(
", ".join(f'"{pkg}"' for pkg in resolved_spec.splitlines()),
width=100,
subsequent_indent=" ",
)
+ ")"
)
),
)
+ "\n"
@ -898,14 +919,23 @@ class Viv:
try:
_local_viv = __import__("viv")
self.local_source = (
_local_viv.__file__ if _local_viv.__file__ else "Not Found"
Path(_local_viv.__file__) if _local_viv.__file__ else "Not Found"
)
self.local_version = _local_viv.__version__
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"
if self.local_source != "Not Found":
self.git = (self.local_source.parent.parent.parent / ".git").is_dir()
else:
self.git = False
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 "
"or consider using --path/--standalone"
)
def _match_vivenv(self, name_id: str) -> ViVenv: # type: ignore[return]
@ -948,8 +978,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)
@ -1126,7 +1156,55 @@ class Viv:
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", code=1)
# TODO: generalize freeze and this spec generator
spec = ", ".join(f'"{req}"' for req in args.reqs)
# TODO: split first req on version specifier characters... use re?
bin = args.reqs[0] if not args.bin else args.bin
output = (
c.binparent / args.reqs[0] if not args.output else args.output.absolute()
)
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)
if confirm(
f"Write shim for {a.style(bin,'bold')} to {a.style(output,'green')}?"
):
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 +1373,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)