mirror of
https://github.com/daylinmorgan/viv.git
synced 2024-12-22 10:40:44 -06:00
refactor: slight change to subprocess output style/typing
This commit is contained in:
parent
d1ea8c3868
commit
9229337f29
1 changed files with 47 additions and 53 deletions
|
@ -43,6 +43,7 @@ from typing import (
|
||||||
List,
|
List,
|
||||||
NoReturn,
|
NoReturn,
|
||||||
Optional,
|
Optional,
|
||||||
|
Sequence,
|
||||||
TextIO,
|
TextIO,
|
||||||
Tuple,
|
Tuple,
|
||||||
Type,
|
Type,
|
||||||
|
@ -50,7 +51,7 @@ from typing import (
|
||||||
from urllib.error import HTTPError
|
from urllib.error import HTTPError
|
||||||
from urllib.request import urlopen
|
from urllib.request import urlopen
|
||||||
|
|
||||||
__version__ = "23.5a4-33-g0996628-dev"
|
__version__ = "23.5a4-34-gd1ea8c3-dev"
|
||||||
|
|
||||||
|
|
||||||
class Spinner:
|
class Spinner:
|
||||||
|
@ -171,7 +172,7 @@ class Ansi:
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
def subprocess(self, output: str) -> None:
|
def subprocess(self, command: List[str], output: str) -> None:
|
||||||
"""generate output for subprocess error
|
"""generate output for subprocess error
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
|
@ -179,9 +180,12 @@ class Ansi:
|
||||||
"""
|
"""
|
||||||
if not output:
|
if not output:
|
||||||
return
|
return
|
||||||
echo("subprocess output:")
|
|
||||||
|
error("subprocess failed")
|
||||||
|
echo("see below for command output", style="red")
|
||||||
|
echo(f"cmd:\n {' '.join(command)}", style="red")
|
||||||
new_output = [f"{self.red}->{self.end} {line}" for line in output.splitlines()]
|
new_output = [f"{self.red}->{self.end} {line}" for line in output.splitlines()]
|
||||||
sys.stdout.write("\n".join(new_output) + "\n")
|
echo("subprocess output:" + "\n".join(("", *new_output, "")), style="red")
|
||||||
|
|
||||||
def viv_preamble(self, style: str = "magenta", sep: str = "::") -> str:
|
def viv_preamble(self, style: str = "magenta", sep: str = "::") -> str:
|
||||||
return f"{self.cyan}viv{self.end}{self.__dict__[style]}{sep}{self.end}"
|
return f"{self.cyan}viv{self.end}{self.__dict__[style]}{sep}{self.end}"
|
||||||
|
@ -561,9 +565,7 @@ def run(
|
||||||
)
|
)
|
||||||
|
|
||||||
if p.returncode != 0 and not ignore_error:
|
if p.returncode != 0 and not ignore_error:
|
||||||
error("subprocess failed")
|
a.subprocess(command, p.stdout)
|
||||||
echo("see below for command output", style="red")
|
|
||||||
a.subprocess(p.stdout)
|
|
||||||
|
|
||||||
if clean_up_path and clean_up_path.is_dir():
|
if clean_up_path and clean_up_path.is_dir():
|
||||||
shutil.rmtree(str(clean_up_path))
|
shutil.rmtree(str(clean_up_path))
|
||||||
|
@ -808,7 +810,6 @@ def combined_spec(reqs: List[str], requirements: Path) -> List[str]:
|
||||||
def resolve_deps(args: Namespace) -> List[str]:
|
def resolve_deps(args: Namespace) -> List[str]:
|
||||||
spec = combined_spec(args.reqs, args.requirements)
|
spec = combined_spec(args.reqs, args.requirements)
|
||||||
|
|
||||||
with Spinner("resolving depedencies"):
|
|
||||||
cmd = [
|
cmd = [
|
||||||
"pip",
|
"pip",
|
||||||
"install",
|
"install",
|
||||||
|
@ -818,7 +819,7 @@ def resolve_deps(args: Namespace) -> List[str]:
|
||||||
"-",
|
"-",
|
||||||
] + spec
|
] + spec
|
||||||
|
|
||||||
report = json.loads(run(cmd, check_output=True))
|
report = json.loads(run(cmd, check_output=True, spinmsg="resolving depedencies"))
|
||||||
resolved_spec = [
|
resolved_spec = [
|
||||||
f"{pkg['metadata']['name']}=={pkg['metadata']['version']}"
|
f"{pkg['metadata']['name']}=={pkg['metadata']['version']}"
|
||||||
for pkg in report["install"]
|
for pkg in report["install"]
|
||||||
|
@ -1232,7 +1233,7 @@ class Arg:
|
||||||
|
|
||||||
class Cli:
|
class Cli:
|
||||||
args = {
|
args = {
|
||||||
"list": [
|
("list",): [
|
||||||
Arg(
|
Arg(
|
||||||
"-f",
|
"-f",
|
||||||
"--full",
|
"--full",
|
||||||
|
@ -1246,7 +1247,7 @@ class Cli:
|
||||||
action="store_true",
|
action="store_true",
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
"shim": [
|
("shim",): [
|
||||||
Arg(
|
Arg(
|
||||||
"-f",
|
"-f",
|
||||||
"--freeze",
|
"--freeze",
|
||||||
|
@ -1261,7 +1262,7 @@ class Cli:
|
||||||
metavar="<path>",
|
metavar="<path>",
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
"remove": [Arg("vivenv", help="name/hash of vivenv", nargs="*")],
|
("remove",): [Arg("vivenv", help="name/hash of vivenv", nargs="*")],
|
||||||
("exe|pip", "exe|python"): [Arg("vivenv", help="name/hash of vivenv")],
|
("exe|pip", "exe|python"): [Arg("vivenv", help="name/hash of vivenv")],
|
||||||
("list", "info"): [
|
("list", "info"): [
|
||||||
Arg(
|
Arg(
|
||||||
|
@ -1342,30 +1343,24 @@ class Cli:
|
||||||
)
|
)
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
cmds = (
|
cmds = {
|
||||||
"list",
|
"list": None,
|
||||||
(
|
"exe": dict(
|
||||||
"exe",
|
|
||||||
dict(
|
|
||||||
pip=dict(help="run cmd with pip"),
|
pip=dict(help="run cmd with pip"),
|
||||||
python=dict(help="run cmd with python"),
|
python=dict(help="run cmd with python"),
|
||||||
),
|
),
|
||||||
),
|
"remove": None,
|
||||||
"remove",
|
"freeze": None,
|
||||||
"freeze",
|
"info": None,
|
||||||
"info",
|
"manage": dict(
|
||||||
(
|
|
||||||
"manage",
|
|
||||||
dict(
|
|
||||||
show=dict(help="show current installation", aliases=["s"]),
|
show=dict(help="show current installation", aliases=["s"]),
|
||||||
install=dict(help="install fresh viv", aliases=["i"]),
|
install=dict(help="install fresh viv", aliases=["i"]),
|
||||||
update=dict(help="update viv version", aliases=["u"]),
|
update=dict(help="update viv version", aliases=["u"]),
|
||||||
purge=dict(help="remove traces of viv", aliases=["p"]),
|
purge=dict(help="remove traces of viv", aliases=["p"]),
|
||||||
),
|
),
|
||||||
),
|
"shim": None,
|
||||||
"shim",
|
"run": None,
|
||||||
"run",
|
}
|
||||||
)
|
|
||||||
|
|
||||||
def __init__(self, viv: Viv) -> None:
|
def __init__(self, viv: Viv) -> None:
|
||||||
self.viv = viv
|
self.viv = viv
|
||||||
|
@ -1375,7 +1370,7 @@ class Cli:
|
||||||
self._add_args()
|
self._add_args()
|
||||||
|
|
||||||
def _cmd_arg_group_map(self) -> None:
|
def _cmd_arg_group_map(self) -> None:
|
||||||
self.cmd_arg_group_map = {}
|
self.cmd_arg_group_map: Dict[str, List[Sequence[str] | str]] = {}
|
||||||
for grp in self.args:
|
for grp in self.args:
|
||||||
if isinstance(grp, str):
|
if isinstance(grp, str):
|
||||||
self.cmd_arg_group_map.setdefault(grp, []).append(grp)
|
self.cmd_arg_group_map.setdefault(grp, []).append(grp)
|
||||||
|
@ -1389,7 +1384,7 @@ class Cli:
|
||||||
def _add_args(self) -> None:
|
def _add_args(self) -> None:
|
||||||
for grp, args in self.args.items():
|
for grp, args in self.args.items():
|
||||||
for arg in args:
|
for arg in args:
|
||||||
self.parsers.get(grp).add_argument(*arg.args, **arg.kwargs)
|
self.parsers[grp].add_argument(*arg.args, **arg.kwargs)
|
||||||
|
|
||||||
def _validate_args(self, args: Namespace) -> None:
|
def _validate_args(self, args: Namespace) -> None:
|
||||||
if args.func.__name__ in ("freeze", "shim", "run"):
|
if args.func.__name__ in ("freeze", "shim", "run"):
|
||||||
|
@ -1459,7 +1454,7 @@ class Cli:
|
||||||
|
|
||||||
return parser
|
return parser
|
||||||
|
|
||||||
def run(self):
|
def run(self) -> None:
|
||||||
self.parser.add_argument(
|
self.parser.add_argument(
|
||||||
"-V",
|
"-V",
|
||||||
"--version",
|
"--version",
|
||||||
|
@ -1471,9 +1466,8 @@ class Cli:
|
||||||
metavar="<sub-cmd>", title="subcommands", required=True
|
metavar="<sub-cmd>", title="subcommands", required=True
|
||||||
)
|
)
|
||||||
|
|
||||||
for cmd in self.cmds:
|
for cmd, subcmds in self.cmds.items():
|
||||||
if isinstance(cmd, tuple):
|
if subcmds:
|
||||||
cmd, subcmds = cmd
|
|
||||||
subcmd_p = self._get_subcmd_parser(cmd_p, cmd)
|
subcmd_p = self._get_subcmd_parser(cmd_p, cmd)
|
||||||
subcmd_cmd_p = subcmd_p.add_subparsers(
|
subcmd_cmd_p = subcmd_p.add_subparsers(
|
||||||
title="subcommand", metavar="<sub-cmd>", required=True
|
title="subcommand", metavar="<sub-cmd>", required=True
|
||||||
|
@ -1482,7 +1476,7 @@ class Cli:
|
||||||
subcmd_cmd_p.add_parser(
|
subcmd_cmd_p.add_parser(
|
||||||
subcmd,
|
subcmd,
|
||||||
parents=[
|
parents=[
|
||||||
self.parsers.get(k)
|
self.parsers[k]
|
||||||
for k in self.cmd_arg_group_map[f"{cmd}|{subcmd}"]
|
for k in self.cmd_arg_group_map[f"{cmd}|{subcmd}"]
|
||||||
],
|
],
|
||||||
**kwargs,
|
**kwargs,
|
||||||
|
|
Loading…
Reference in a new issue