fix(#1): get the spec then re-make only when necessary

This commit is contained in:
Daylin Morgan 2023-05-25 15:28:40 -05:00
parent e8a77fe77c
commit ddb03df8a2
Signed by: daylin
GPG key ID: C1E52E7DD81DF79F

View file

@ -49,7 +49,7 @@ from typing import (
Generator, Generator,
) )
__version__ = "22.12a3-39-gee7fe64-dev" __version__ = "22.12a3-41-g5c40210-dev"
@dataclass @dataclass
@ -578,36 +578,29 @@ def generate_import(
keep: bool, keep: bool,
standalone: bool, standalone: bool,
) -> None: ) -> None:
# TODO: make compatible with Venv class for now just use the name /tmp/
reqs_from_file = [] reqs_from_file = []
if requirements: if requirements:
with requirements.open("r") as f: with requirements.open("r") as f:
reqs_from_file = f.readlines() reqs_from_file = f.readlines()
# refactor to make the below steps context dependent with tmpdir path with tempfile.TemporaryDirectory() as tmpdir:
echo("generating frozen spec")
vivenv, resolved_spec = freeze_venv(reqs + reqs_from_file, path=Path(tmpdir))
if keep: if keep:
# TODO: remove directory if any errors occur? # create env again since path's are hard-coded
echo("generating new vivenv") vivenv = ViVenv(resolved_spec.splitlines())
vivenv, resolved_spec = freeze_venv(reqs + reqs_from_file)
# update id and move vivenv if vivenv.name not in [d.name for d in c.venvcache.iterdir()] or os.getenv(
vivenv.spec = resolved_spec.splitlines() "VIV_FORCE"
vivenv.id = get_hash(resolved_spec.splitlines()) ):
echo(f"updated hash -> {vivenv.id}") vivenv.create()
vivenv.install_pkgs()
if not (c.venvcache / vivenv.id).exists():
vivenv.path = vivenv.path.rename(c.venvcache / vivenv.id)
vivenv.dump_info(write=True) vivenv.dump_info(write=True)
else: else:
echo("this vivenv already exists cleaning up temporary vivenv") echo("re-using existing vivenv")
shutil.rmtree(vivenv.path)
else:
with tempfile.TemporaryDirectory() as tmpdir: #
echo("generating temporary vivenv ")
vivenv, resolved_spec = freeze_venv(
reqs + reqs_from_file, path=Path(tmpdir)
)
echo("see below for import statements\n") echo("see below for import statements\n")