Merge pull request 'only compute final_script and validation hashes once' (#3206) from fix-eval into main

Reviewed-on: https://git.clan.lol/clan/clan-core/pulls/3206
This commit is contained in:
Mic92
2025-04-05 06:44:28 +00:00
3 changed files with 15 additions and 17 deletions

View File

@@ -172,7 +172,7 @@ class StoreBase(ABC):
-> this provides backward and forward compatibility -> this provides backward and forward compatibility
""" """
stored_hash = self.get_validation(generator) stored_hash = self.get_validation(generator)
target_hash = generator.validation target_hash = generator.validation()
# if the hash is neither set in nix nor on disk, it is considered valid (provides backwards compat) # if the hash is neither set in nix nor on disk, it is considered valid (provides backwards compat)
if target_hash is None and stored_hash is None: if target_hash is None and stored_hash is None:
return True return True

View File

@@ -66,7 +66,6 @@ class Generator:
prompts=[Prompt.from_json(p) for p in data["prompts"].values()], prompts=[Prompt.from_json(p) for p in data["prompts"].values()],
) )
@property
def final_script(self) -> Path: def final_script(self) -> Path:
assert self._machine is not None assert self._machine is not None
final_script = self._machine.build_nix( final_script = self._machine.build_nix(
@@ -74,7 +73,6 @@ class Generator:
) )
return final_script return final_script
@property
def validation(self) -> str | None: def validation(self) -> str | None:
assert self._machine is not None assert self._machine is not None
return self._machine.eval_nix( return self._machine.eval_nix(
@@ -208,10 +206,12 @@ def execute_generator(
prompt_file.write_text(value) prompt_file.write_text(value)
from clan_cli import bwrap from clan_cli import bwrap
final_script = generator.final_script()
if sys.platform == "linux" and bwrap.bubblewrap_works(): if sys.platform == "linux" and bwrap.bubblewrap_works():
cmd = bubblewrap_cmd(str(generator.final_script), tmpdir) cmd = bubblewrap_cmd(str(final_script), tmpdir)
else: else:
cmd = ["bash", "-c", str(generator.final_script)] cmd = ["bash", "-c", str(final_script)]
run(cmd, RunOpts(env=env)) run(cmd, RunOpts(env=env))
files_to_commit = [] files_to_commit = []
# store secrets # store secrets
@@ -222,7 +222,7 @@ def execute_generator(
secret_file = tmpdir_out / file.name secret_file = tmpdir_out / file.name
if not secret_file.is_file(): if not secret_file.is_file():
msg = f"did not generate a file for '{file.name}' when running the following command:\n" msg = f"did not generate a file for '{file.name}' when running the following command:\n"
msg += str(generator.final_script) msg += str(final_script)
raise ClanError(msg) raise ClanError(msg)
if file.secret: if file.secret:
file_path = secret_vars_store.set( file_path = secret_vars_store.set(
@@ -240,18 +240,15 @@ def execute_generator(
public_changed = True public_changed = True
if file_path: if file_path:
files_to_commit.append(file_path) files_to_commit.append(file_path)
if generator.validation is not None: validation = generator.validation()
if validation is not None:
if public_changed: if public_changed:
files_to_commit.append( files_to_commit.append(
public_vars_store.set_validation( public_vars_store.set_validation(generator, validation)
generator, generator.validation
)
) )
if secret_changed: if secret_changed:
files_to_commit.append( files_to_commit.append(
secret_vars_store.set_validation( secret_vars_store.set_validation(generator, validation)
generator, generator.validation
)
) )
commit_files( commit_files(
files_to_commit, files_to_commit,

View File

@@ -14,11 +14,12 @@
pytest-subprocess, # fake the real subprocess behavior to make your tests more independent. pytest-subprocess, # fake the real subprocess behavior to make your tests more independent.
pytest-timeout, # Add timeouts to your tests pytest-timeout, # Add timeouts to your tests
pytest-xdist, # Run tests in parallel on multiple cores pytest-xdist, # Run tests in parallel on multiple cores
python3, buildPythonApplication,
runCommand, runCommand,
setuptools, setuptools,
webkitgtk_6_0, webkitgtk_6_0,
wrapGAppsHook, wrapGAppsHook,
python,
lib, lib,
stdenv, stdenv,
}: }:
@@ -48,7 +49,7 @@ let
]; ];
# Deps including python packages from the local project # Deps including python packages from the local project
allPythonDeps = [ (python3.pkgs.toPythonModule clan-cli) ] ++ externalPythonDeps; allPythonDeps = [ (python.pkgs.toPythonModule clan-cli) ] ++ externalPythonDeps;
# Runtime binary dependencies required by the application # Runtime binary dependencies required by the application
runtimeDependencies = [ runtimeDependencies = [
@@ -71,9 +72,9 @@ let
testDependencies = runtimeDependencies ++ allPythonDeps ++ externalTestDeps; testDependencies = runtimeDependencies ++ allPythonDeps ++ externalTestDeps;
# Setup Python environment with all dependencies for running tests # Setup Python environment with all dependencies for running tests
pythonWithTestDeps = python3.withPackages (_ps: testDependencies); pythonWithTestDeps = python.withPackages (_ps: testDependencies);
in in
python3.pkgs.buildPythonApplication rec { buildPythonApplication rec {
name = "clan-vm-manager"; name = "clan-vm-manager";
src = source; src = source;
format = "pyproject"; format = "pyproject";