From baad1783a2605048998244ad556d3432f8be923c Mon Sep 17 00:00:00 2001 From: Daylin Morgan Date: Sat, 27 May 2023 17:00:54 -0500 Subject: [PATCH] wip --- src/viv/viv.py | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/src/viv/viv.py b/src/viv/viv.py index 7234533..730b980 100755 --- a/src/viv/viv.py +++ b/src/viv/viv.py @@ -52,7 +52,7 @@ from typing import ( from urllib.error import HTTPError from urllib.request import urlopen -__version__ = "23.5a1-dev" +__version__ = "23.5a1-12-g298e8b2" @dataclass @@ -593,6 +593,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())) @@ -1126,7 +1136,24 @@ class Viv: def shim(self, args): """generate viv-powered cli apps""" - echo("not implemented.") + if args.output.is_file(): + error(f"{args.output} already exists...exiting", code=1) + + # TODO: robustness.... + + spec = ", ".join(f'"{req}"' for req in args.reqs) + + if args.bin: + bin = args.bin + elif len(args.reqs) == 1: + bin = args.reqs[0] + else: + error("please specify an explicit -b/--bin") + + with args.output.open("w") as f: + f.write(SHIM_TEMPLATE.format(use=f'__import__("viv").use({spec})', bin=bin)) + + make_executable(args.output) def _get_subcmd_parser( self, @@ -1299,6 +1326,8 @@ class Viv: "-o", "--output", help="path/to/output file", + required=True, + type=Path, metavar="", ) p_manage_shim.add_argument(