mirror of
https://github.com/daylinmorgan/viv.git
synced 2024-11-12 20:23:15 -06:00
manythings
This commit is contained in:
parent
4b61cb9381
commit
5e89d63b66
1 changed files with 105 additions and 43 deletions
148
src/viv/viv.py
148
src/viv/viv.py
|
@ -51,7 +51,7 @@ from typing import (
|
|||
Generator,
|
||||
)
|
||||
|
||||
__version__ = "22.12a3-43-g0e9779a-dev"
|
||||
__version__ = "22.12a3-43-g4b61cb9-dev"
|
||||
|
||||
|
||||
@dataclass
|
||||
|
@ -61,9 +61,16 @@ class Config:
|
|||
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"
|
||||
)
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
self.venvcache.mkdir(parents=True, exist_ok=True)
|
||||
self.srccache.mkdir(
|
||||
parents=True,
|
||||
exist_ok=True,
|
||||
)
|
||||
|
||||
|
||||
c = Config()
|
||||
|
@ -555,6 +562,10 @@ SHOW_TEMPLATE = f"""
|
|||
{a.style('Current Source', 'bold')}: {{src}}
|
||||
"""
|
||||
|
||||
INSTALL_TEMPLATE = f"""\
|
||||
Install viv.py to {a.bold}{{src_location}}{a.end}
|
||||
Symlink {a.bold}{{src_location}}{a.end} to {a.bold}{{cli_location}}{a.end}"""
|
||||
|
||||
|
||||
def noqa(txt: str) -> str:
|
||||
max_length = max(map(len, txt.splitlines()))
|
||||
|
@ -802,14 +813,42 @@ within python script:
|
|||
"""
|
||||
|
||||
|
||||
def fetch_source(reference: str) -> str:
|
||||
try:
|
||||
r = urlopen(
|
||||
"https://raw.githubusercontent.com/daylinmorgan/viv/"
|
||||
+ reference
|
||||
+ "/src/viv/viv.py"
|
||||
)
|
||||
except HTTPError as e:
|
||||
error(
|
||||
"Issue updating viv see below:"
|
||||
+ a.style("-> ", "red").join(["\n"] + repr(e).splitlines())
|
||||
)
|
||||
if "404" in repr(e):
|
||||
echo("Please check your reference is valid.", style="red")
|
||||
sys.exit(1)
|
||||
|
||||
src = r.read()
|
||||
(hash := hashlib.sha256()).update(src)
|
||||
sha256 = hash.hexdigest()
|
||||
|
||||
cached_src_file = c.srccache / f"{sha256}.py"
|
||||
|
||||
if not cached_src_file.is_file():
|
||||
with cached_src_file.open("w") as f:
|
||||
f.write(src.decode())
|
||||
|
||||
return sha256
|
||||
|
||||
|
||||
class Viv:
|
||||
def __init__(self) -> None:
|
||||
self.vivenvs = get_venvs()
|
||||
self.current_source = Path(__file__).resolve()
|
||||
self.local = not str(self.current_source).startswith("/proc/")
|
||||
self.name = (
|
||||
"python3 <(curl -fsSL gh.dayl.in/viv/viv.py)"
|
||||
if str(self.current_source).startswith("/proc/")
|
||||
else "viv"
|
||||
"python3 <(curl -fsSL gh.dayl.in/viv/viv.py)" if self.local else "viv"
|
||||
)
|
||||
|
||||
def _match_vivenv(self, name_id: str) -> ViVenv: # type: ignore[return]
|
||||
|
@ -922,7 +961,6 @@ class Viv:
|
|||
"""manage viv installation"""
|
||||
|
||||
if args.cmd == "show":
|
||||
# NOTE: could reuse the table output for this?
|
||||
echo("Current:")
|
||||
sys.stdout.write(
|
||||
SHOW_TEMPLATE.format(
|
||||
|
@ -933,56 +971,71 @@ class Viv:
|
|||
)
|
||||
|
||||
elif args.cmd == "update":
|
||||
if str(Path(__file__).resolve()).startswith("/proc/"):
|
||||
if not self.local:
|
||||
error(
|
||||
a.style("viv manage update", "bold")
|
||||
+ " should only be used with a locally installed viv",
|
||||
1,
|
||||
)
|
||||
try:
|
||||
r = urlopen(
|
||||
"https://raw.githubusercontent.com/daylinmorgan/viv/"
|
||||
+ args.reference
|
||||
+ "/src/viv/viv.py"
|
||||
)
|
||||
except HTTPError as e:
|
||||
error(
|
||||
"Issue updating viv see below:"
|
||||
+ a.style("-> ", "red").join(["\n"] + repr(e).splitlines())
|
||||
)
|
||||
if "404" in repr(e):
|
||||
echo("Please check your reference is valid.", style="red")
|
||||
sys.exit(1)
|
||||
|
||||
viv_src = r.read()
|
||||
|
||||
(hash := hashlib.sha256()).update(viv_src)
|
||||
sha256 = hash.hexdigest()
|
||||
|
||||
(
|
||||
src_cache := Path(os.getenv("XDG_CACHE_HOME", Path.home() / ".cache"))
|
||||
/ "viv"
|
||||
/ "src"
|
||||
).mkdir(exist_ok=True, parents=True)
|
||||
cached_version = src_cache / f"{sha256}.py"
|
||||
|
||||
if not cached_version.is_file():
|
||||
with cached_version.open("w") as f:
|
||||
f.write(viv_src.decode())
|
||||
|
||||
sys.path.append(str(src_cache))
|
||||
#
|
||||
# viv_src = fetch_source(args.reference)
|
||||
#
|
||||
# (hash := hashlib.sha256()).update(viv_src)
|
||||
# sha256 = hash.hexdigest()
|
||||
#
|
||||
# (
|
||||
# src_cache := Path(os.getenv("XDG_CACHE_HOME", Path.home() / ".cache"))
|
||||
# / "viv"
|
||||
# / "src"
|
||||
# ).mkdir(exist_ok=True, parents=True)
|
||||
# cached_version = src_cache / f"{sha256}.py"
|
||||
#
|
||||
# if not cached_version.is_file():
|
||||
# with cached_version.open("w") as f:
|
||||
# f.write(viv_src.decode())
|
||||
#
|
||||
sha256 = fetch_source(args.reference)
|
||||
sys.path.append(str(c.srccache))
|
||||
next_version = __import__(sha256).__version__
|
||||
|
||||
q = (
|
||||
"Update source at: "
|
||||
+ a.style(Path(__file__).resolve(), "bold")
|
||||
+ a.style(self.current_source, "bold")
|
||||
+ f" \n from version {__version__} to {next_version}?"
|
||||
)
|
||||
|
||||
if confirm(q):
|
||||
print("AWAY THEN")
|
||||
print("Holding off on the additions of this piece")
|
||||
|
||||
elif args.cmd == "install":
|
||||
echo("not yet implemented. sorry")
|
||||
if self.local:
|
||||
error(
|
||||
a.style("viv manage install", "bold")
|
||||
+ " should only be used with a remote viv",
|
||||
)
|
||||
echo(
|
||||
"use "
|
||||
+ a.style("viv manage update", "bold")
|
||||
+ " to modify current installation.",
|
||||
style="red",
|
||||
)
|
||||
sys.exit(1)
|
||||
|
||||
echo("Preparing to install viv.py")
|
||||
|
||||
sha256 = fetch_source(args.reference)
|
||||
sys.path.append(str(c.srccache))
|
||||
downloaded_version = __import__(sha256).__version__
|
||||
echo(f"Downloaded version: {downloaded_version}")
|
||||
q = (
|
||||
"Would you like to install viv with the following settings\n"
|
||||
+ INSTALL_TEMPLATE.format(
|
||||
src_location="~/.local/share/viv/viv.py", cli_location="~/bin/viv"
|
||||
)
|
||||
)
|
||||
|
||||
if confirm(q):
|
||||
print("AWAY MAN")
|
||||
|
||||
def _get_subcmd_parser(
|
||||
self,
|
||||
|
@ -1102,9 +1155,18 @@ class Viv:
|
|||
subparsers, name="manage"
|
||||
).add_subparsers(title="subcommand", metavar="<sub-cmd>", required=True)
|
||||
|
||||
p_manage_sub.add_parser(
|
||||
"install", help="install viv", aliases="i"
|
||||
# TODO: shared parser?
|
||||
(
|
||||
p_manage_install := p_manage_sub.add_parser(
|
||||
"install", help="install viv", aliases="i"
|
||||
)
|
||||
).set_defaults(func=self.manage, cmd="install")
|
||||
p_manage_install.add_argument(
|
||||
"-r",
|
||||
"--reference",
|
||||
help="git reference (branch/tag/commit)",
|
||||
default="main",
|
||||
)
|
||||
|
||||
(
|
||||
p_manage_update := p_manage_sub.add_parser(
|
||||
|
|
Loading…
Reference in a new issue