mirror of
https://github.com/daylinmorgan/viv.git
synced 2024-11-09 19:13:14 -06:00
refactor: reduce boilerplate
This commit is contained in:
parent
f522352a33
commit
78e533b0a2
2 changed files with 32 additions and 23 deletions
|
@ -5,9 +5,11 @@ from pathlib import Path
|
||||||
|
|
||||||
import nox
|
import nox
|
||||||
|
|
||||||
|
nox.options.sessions = ["lint"]
|
||||||
nox.options.reuse_existing_virtualenvs = True
|
nox.options.reuse_existing_virtualenvs = True
|
||||||
os.environ.update({"PDM_IGNORE_SAVED_PYTHON": "1"})
|
os.environ.update({"PDM_IGNORE_SAVED_PYTHON": "1"})
|
||||||
|
|
||||||
|
|
||||||
def pdm_install(session, group):
|
def pdm_install(session, group):
|
||||||
session.run_always("pdm", "install", "-G", group, external=True, silent=True)
|
session.run_always("pdm", "install", "-G", group, external=True, silent=True)
|
||||||
|
|
||||||
|
@ -18,15 +20,17 @@ def lint(session):
|
||||||
session.run("pre-commit", "run")
|
session.run("pre-commit", "run")
|
||||||
session.run("mypy", "src/")
|
session.run("mypy", "src/")
|
||||||
|
|
||||||
|
|
||||||
@nox.session
|
@nox.session
|
||||||
def svgs(session):
|
def svgs(session):
|
||||||
pdm_install(session, "docs")
|
pdm_install(session, "docs")
|
||||||
session.run("./scripts/generate-svgs.py", external=True)
|
session.run("./scripts/generate-svgs.py", external=True)
|
||||||
|
|
||||||
|
|
||||||
@nox.session
|
@nox.session
|
||||||
def docs(session):
|
def docs(session):
|
||||||
pdm_install(session, "docs")
|
pdm_install(session, "docs")
|
||||||
if not Path('docs/svgs').is_dir():
|
if not Path("docs/svgs").is_dir():
|
||||||
svgs(session)
|
svgs(session)
|
||||||
|
|
||||||
session.run("make", "docs", external=True)
|
session.run("make", "docs", external=True)
|
||||||
|
|
|
@ -52,7 +52,7 @@ from typing import (
|
||||||
Union,
|
Union,
|
||||||
)
|
)
|
||||||
|
|
||||||
__version__ = "23.8b2-10-g0cc9faa-dev"
|
__version__ = "23.8b2-11-gf522352-dev"
|
||||||
|
|
||||||
|
|
||||||
class Spinner:
|
class Spinner:
|
||||||
|
@ -939,6 +939,10 @@ class ViVenv:
|
||||||
|
|
||||||
return vivenv
|
return vivenv
|
||||||
|
|
||||||
|
def exists(self):
|
||||||
|
if self.name in (d.name for d in Cfg().cache_venv.iterdir()):
|
||||||
|
self.loaded = True
|
||||||
|
|
||||||
def set_path(self, path: Path | None = None) -> None:
|
def set_path(self, path: Path | None = None) -> None:
|
||||||
self.path = path if path else Cfg().cache_venv / self.name
|
self.path = path if path else Cfg().cache_venv / self.name
|
||||||
self.python = str((self.path / "bin" / "python").absolute())
|
self.python = str((self.path / "bin" / "python").absolute())
|
||||||
|
@ -1002,8 +1006,7 @@ class ViVenv:
|
||||||
)
|
)
|
||||||
|
|
||||||
def ensure(self) -> None:
|
def ensure(self) -> None:
|
||||||
# FIXME: doens't account for the ephemeral case...
|
self.existse()
|
||||||
# should this be encapuslated in the use method?
|
|
||||||
if not self.loaded or Env().viv_force:
|
if not self.loaded or Env().viv_force:
|
||||||
self.create()
|
self.create()
|
||||||
self.install_pkgs()
|
self.install_pkgs()
|
||||||
|
@ -1051,18 +1054,23 @@ class ViVenv:
|
||||||
_path = self.path
|
_path = self.path
|
||||||
try:
|
try:
|
||||||
if self.loaded:
|
if self.loaded:
|
||||||
|
self.ensure()
|
||||||
yield
|
yield
|
||||||
if keep or run_mode == "persist":
|
elif keep or run_mode == "persist":
|
||||||
|
self.ensure()
|
||||||
yield
|
yield
|
||||||
elif run_mode == "ephemeral":
|
elif run_mode == "ephemeral":
|
||||||
with tempfile.TemporaryDirectory(prefix="viv-") as tmpdir:
|
with tempfile.TemporaryDirectory(prefix="viv-") as tmpdir:
|
||||||
self.set_path(Path(tmpdir))
|
self.set_path(Path(tmpdir))
|
||||||
|
self.ensure()
|
||||||
yield
|
yield
|
||||||
elif run_mode == "semi-ephemeral":
|
elif run_mode == "semi-ephemeral":
|
||||||
ephemeral_cache = _path_ok(
|
ephemeral_cache = _path_ok(
|
||||||
Path(tempfile.gettempdir()) / "viv-ephemeral-cache" / "venvs"
|
Path(tempfile.gettempdir()) / "viv-ephemeral-cache"
|
||||||
)
|
)
|
||||||
self.set_path(ephemeral_cache / self.name)
|
os.environ.update(dict(VIV_CACHE=str(ephemeral_cache)))
|
||||||
|
self.set_path(ephemeral_cache / "venvs" / self.name)
|
||||||
|
self.ensure()
|
||||||
yield
|
yield
|
||||||
finally:
|
finally:
|
||||||
self.set_path(_path)
|
self.set_path(_path)
|
||||||
|
@ -1140,10 +1148,9 @@ def use(*packages: str, track_exe: bool = False, name: str = "") -> Path:
|
||||||
"""
|
"""
|
||||||
|
|
||||||
vivenv = ViVenv([*list(packages), *Env().viv_spec], track_exe=track_exe, name=name)
|
vivenv = ViVenv([*list(packages), *Env().viv_spec], track_exe=track_exe, name=name)
|
||||||
vivenv.ensure()
|
with vivenv.use():
|
||||||
vivenv.meta.addfile(get_caller_path())
|
vivenv.meta.addfile(get_caller_path())
|
||||||
vivenv.meta.write()
|
vivenv.meta.write()
|
||||||
|
|
||||||
vivenv.activate()
|
vivenv.activate()
|
||||||
|
|
||||||
return vivenv.path
|
return vivenv.path
|
||||||
|
@ -1463,7 +1470,7 @@ class Viv:
|
||||||
spec = resolve_deps(reqs, requirements)
|
spec = resolve_deps(reqs, requirements)
|
||||||
if keep:
|
if keep:
|
||||||
vivenv = ViVenv(spec)
|
vivenv = ViVenv(spec)
|
||||||
vivenv.ensure()
|
with vivenv.use():
|
||||||
vivenv.touch()
|
vivenv.touch()
|
||||||
vivenv.meta.write()
|
vivenv.meta.write()
|
||||||
|
|
||||||
|
@ -1842,8 +1849,6 @@ class Viv:
|
||||||
vivenv = ViVenv(spec)
|
vivenv = ViVenv(spec)
|
||||||
|
|
||||||
with vivenv.use(keep=keep):
|
with vivenv.use(keep=keep):
|
||||||
vivenv.ensure()
|
|
||||||
|
|
||||||
# TODO: refactor this logic elsewhere
|
# TODO: refactor this logic elsewhere
|
||||||
if keep or Env().viv_run_mode != "ephemeral":
|
if keep or Env().viv_run_mode != "ephemeral":
|
||||||
vivenv.touch()
|
vivenv.touch()
|
||||||
|
|
Loading…
Reference in a new issue