From 94bc35147a7c1e4ecf438ca27db09e720f2cbcd7 Mon Sep 17 00:00:00 2001 From: Daylin Morgan Date: Sat, 27 May 2023 15:00:21 -0500 Subject: [PATCH] refactor: update standalone for remote release --- src/viv/viv.py | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/src/viv/viv.py b/src/viv/viv.py index 5f082f8..c333696 100755 --- a/src/viv/viv.py +++ b/src/viv/viv.py @@ -35,7 +35,7 @@ from dataclasses import dataclass from datetime import datetime from itertools import zip_longest from pathlib import Path -from textwrap import dedent, wrap +from textwrap import dedent, fill, wrap from types import TracebackType from typing import ( Any, @@ -52,7 +52,7 @@ from typing import ( from urllib.error import HTTPError from urllib.request import urlopen -__version__ = "23.5a1-3-g3a85fe4-dev" +__version__ = "23.5a1-dev" @dataclass @@ -544,14 +544,15 @@ REL_SYS_PATH_TEMPLATE = ( IMPORT_TEMPLATE = """__import__("viv").use({spec}) # noqa""" STANDALONE_TEMPLATE = r""" -# <<<<< auto-generated by daylinmorgan/viv v{version} +# <<<<< auto-generated by viv (v{version}) +# see `python3 <(curl -fsSL gh.dayl.in/viv/viv.py) --help` # fmt: off -{use} +{func} # fmt: on # >>>>> code golfed with <3 """ # noqa -STANDALONE_TEMPLATE_USE = r"""def _viv_use(*pkgs: str, track_exe: bool = False, name: str = "") -> None: +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") ge,sys,P,ew=i("os").getenv,i("sys"),i("pathlib").Path,i("sys").stderr.write @@ -569,7 +570,7 @@ STANDALONE_TEMPLATE_USE = r"""def _viv_use(*pkgs: str, track_exe: bool = False, 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) sys.path = [p for p in (*sys.path,s(*(env/"lib").glob("py*/si*"))) if p!=i("site").USER_SITE] -_viv_use({spec}) +{use} """ # noqa SHOW_TEMPLATE = f""" @@ -661,9 +662,15 @@ def generate_import( sys.stdout.write( STANDALONE_TEMPLATE.format( version=__version__, - use=noqa( - STANDALONE_TEMPLATE_USE.format( - spec=", ".join(f'"{pkg}"' for pkg in resolved_spec.splitlines()) + func=noqa( + STANDALONE_TEMPLATE_FUNC.format( + use="_viv_use(" + + fill( + ", ".join(f'"{pkg}"' for pkg in resolved_spec.splitlines()), + width=100, + subsequent_indent=" ", + ) + + ")" ) ), ) @@ -1113,6 +1120,9 @@ class Viv: ), ): self._install_local_src(sha256, args.src, args.cli) + def shim(self, args): + """generate viv-powered cli apps""" + echo("not implemented.") def _get_subcmd_parser( self, @@ -1274,6 +1284,11 @@ class Viv: "-p", "--pythonpath", help="show the path/to/install", action="store_true" ) + self._get_subcmd_parser( + subparsers, + "shim", + ).set_defaults(func=self.shim, cmd="shim") + args = parser.parse_args() args.func(args)