fix: handle viv_run_modes appropriately with script mode

This commit is contained in:
Daylin Morgan 2023-08-21 09:48:22 -05:00
parent c7bcdfefee
commit f88a7bd15f
Signed by: daylin
GPG key ID: C1E52E7DD81DF79F

View file

@ -52,7 +52,7 @@ from typing import (
Union, Union,
) )
__version__ = "23.8b1-7-g3031e5f-dev" __version__ = "23.8b1-8-gc7bcdfe-dev"
class Spinner: class Spinner:
@ -1002,11 +1002,9 @@ class ViVenv:
self.set_path(Path(tmpdir)) self.set_path(Path(tmpdir))
yield yield
elif run_mode == "semi-ephemeral": elif run_mode == "semi-ephemeral":
( ephemeral_cache = _path_ok(
ephemeral_cache := ( Path(tempfile.gettempdir()) / "viv-ephemeral-cache" / "venvs"
Path(tempfile.gettempdir()) / "viv-ephemeral-cache" / "venvs" )
)
).mkdir(exist_ok=True, parents=True)
self.set_path(ephemeral_cache / self.name) self.set_path(ephemeral_cache / self.name)
yield yield
finally: finally:
@ -1678,6 +1676,20 @@ class Viv:
make_executable(output) make_executable(output)
@staticmethod
def _update_cache(env: Dict[str, str], keep: bool, tmpdir: str):
run_mode = Env().viv_run_mode
if not keep:
if run_mode == "ephemeral":
new_cache = tmpdir
elif run_mode == "semi-ephemeral":
new_cache = str(
Path(tempfile.gettempdir()) / "viv-ephemeral-cache" / "venvs"
)
env.update({"VIV_CACHE": new_cache})
os.environ["VIV_CACHE"] = new_cache
def _run_script( def _run_script(
self, spec: List[str], script: str, keep: bool, rest: List[str] self, spec: List[str], script: str, keep: bool, rest: List[str]
) -> None: ) -> None:
@ -1711,10 +1723,7 @@ class Viv:
) )
scriptpath.write_text(script_text) scriptpath.write_text(script_text)
self._update_cache(env, keep, tmpdir)
if not keep:
env.update({"VIV_CACHE": tmpdir})
os.environ["VIV_CACHE"] = tmpdir
if viv_used: if viv_used:
log.debug(f"script invokes viv.use passing along spec: \n '{spec}'") log.debug(f"script invokes viv.use passing along spec: \n '{spec}'")
@ -1727,18 +1736,19 @@ class Viv:
subprocess_run_quit([sys.executable, "-S", scriptpath, *rest]) subprocess_run_quit([sys.executable, "-S", scriptpath, *rest])
else: else:
vivenv = ViVenv(spec + deps) vivenv = ViVenv(spec + deps)
vivenv.ensure() with vivenv.use(keep=keep):
vivenv.touch() vivenv.ensure()
vivenv.meta.write() vivenv.touch()
subprocess_run_quit( vivenv.meta.write()
[vivenv.python, "-S", scriptpath, *rest], subprocess_run_quit(
env=dict( [vivenv.python, "-S", scriptpath, *rest],
env, env=dict(
PYTHONPATH=":".join( env,
filter(None, (vivenv.site_packages, Env().pythonpath)) PYTHONPATH=":".join(
filter(None, (vivenv.site_packages, Env().pythonpath))
),
), ),
), )
)
def cmd_run( def cmd_run(
self, self,