Compare commits

...

3 commits

Author SHA1 Message Date
954a7b556e
WIP 2023-05-27 17:37:08 -05:00
9a76c5e672
WIP 2023-05-27 17:33:35 -05:00
baad1783a2
wip 2023-05-27 17:00:54 -05:00

View file

@ -52,32 +52,46 @@ from typing import (
from urllib.error import HTTPError
from urllib.request import urlopen
__version__ = "23.5a1-dev"
__version__ = "23.5a1-14-g9a76c5e"
@dataclass
class Config:
"""viv config manager"""
venvcache: Path = (
_venvcache: Path = (
Path(os.getenv("XDG_CACHE_HOME", Path.home() / ".cache")) / "viv" / "venvs"
)
srccache: Path = (
_srccache: Path = (
Path(os.getenv("XDG_CACHE_HOME", Path.home() / ".cache")) / "viv" / "src"
)
share: Path = (
_share: Path = (
Path(os.getenv("XDG_DATA_HOME", Path.home() / ".local" / "share")) / "viv"
)
_binparent: Path = 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,
)
self.share.mkdir(parents=True, exist_ok=True)
self.srcdefault = self.share / "viv.py"
@property
def venvcache(self):
self._venvcache.mkdir(parents=True, exist_ok=True)
return self._venvcache
@property
def srccache(self):
self._srccache.mkdir(parents=True, exist_ok=True)
return self._srccache
@property
def share(self):
self._share.mkdir(parents=True, exist_ok=True)
return self._share
@property
def binparent(self):
self._binparent.mkdir(parents=True, exist_ok=True)
return self._binparent
@property
def srcdefault(self) -> None:
return self.share / "viv.py"
c = Config()
@ -593,6 +607,16 @@ UPDATE_TEMPLATE = f"""
"""
SHIM_TEMPLATE = """\
#!/usr/bin/env python3
import subprocess
import sys
if __name__ == "__main__":
vivenv = {use}
sys.exit(subprocess.run([vivenv.path / "bin" / "{bin}", *sys.argv[1:]]).returncode)
"""
def noqa(txt: str) -> str:
max_length = max(map(len, txt.splitlines()))
@ -950,6 +974,8 @@ class Viv:
"""create import statement from package spec"""
# TODO: warn user about using anything but standalone when
# self.local_source is 'Not Found'
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")
if not args.reqs:
error("must specify a requirement", code=1)
@ -1126,7 +1152,23 @@ class Viv:
def shim(self, args):
"""generate viv-powered cli apps"""
echo("not implemented.")
# TODO: robustness....
spec = ", ".join(f'"{req}"' for req in args.reqs)
if len(args.reqs) == 1:
bin = args.reqs[0]
output = Path.cwd() / args.reqs[0]
else:
error("please specify an explicit -b/--bin and -o/--output")
if output.is_file():
error(f"{output} already exists...exiting", code=1)
with output.open("w") as f:
f.write(SHIM_TEMPLATE.format(use=f'__import__("viv").use({spec})', bin=bin))
make_executable(output)
def _get_subcmd_parser(
self,
@ -1295,15 +1337,20 @@ 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)