Skip to content

Commit

Permalink
Early out after an action is found
Browse files Browse the repository at this point in the history
Don't load the other environments after an action has been found in one
of them.
  • Loading branch information
rikardg committed Jan 16, 2025
1 parent 8543f57 commit 224b567
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/bygg/cmd/dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import subprocess
import sys
import tempfile
from typing import Optional
from typing import Optional, TypeAlias

from bygg.cmd.argument_parsing import ByggNamespace, create_argument_parser
from bygg.cmd.argument_unparsing import unparse_args
Expand Down Expand Up @@ -216,8 +216,13 @@ def parent_dispatcher(
sys.exit(1)


DoerType: TypeAlias = Callable[[ByggContext, str], tuple[SubProcessIpcData, bool]]
"""Takes a ByggContext and an action name as arguments and returns a tuple of the
subprocess IPC data and whether the action was handled."""


def do_in_all_environments(
ctx: ByggContext, doer: Callable[[ByggContext, str], SubProcessIpcData]
ctx: ByggContext, doer: DoerType
) -> dict[str, SubProcessIpcData]:
"""
Runs doer for all environments and returns the environment data.
Expand All @@ -226,7 +231,9 @@ def do_in_all_environments(
environment_names = [DEFAULT_ENVIRONMENT_NAME, *ctx.configuration.environments]

for environment_name in environment_names:
environment_data[environment_name] = doer(ctx, environment_name)
environment_data[environment_name], early_out = doer(ctx, environment_name)
if early_out:
break
return environment_data


Expand All @@ -235,7 +242,7 @@ def run_or_collect_in_environment(
environment_name: str,
*,
action: Optional[str] = None,
) -> SubProcessIpcData:
) -> tuple[SubProcessIpcData, bool]:
"""
Runs the given action in the given environment or just collects data about the
environment. These two flows are roughly the same.
Expand All @@ -253,12 +260,14 @@ def run_or_collect_in_environment(

logger.info("Running action: should restart with %s", subprocess_bygg_path)
if subprocess_bygg_path:
return spawn_subprocess(
result = spawn_subprocess(
ctx,
environment_name=environment_name,
subprocess_bygg_path=subprocess_bygg_path,
action=action,
)
action_found = action is not None and action in result.found_actions
return (result, action_found)

logger.info("Running-collecting: in ambient environment")
load_environment(ctx, environment_name)
Expand All @@ -283,7 +292,7 @@ def run_or_collect_in_environment(

# Our work here is done, or, we had nothing to do
if action is None or action not in subprocess_data.found_actions:
return subprocess_data
return (subprocess_data, False)

# Build or clean handled below. These are the only commands handled here.
status = False
Expand All @@ -301,7 +310,7 @@ def run_or_collect_in_environment(
sys.exit(1)

# All critical errors will already have done sys.exit
return subprocess_data
return (subprocess_data, True)


@contextlib.contextmanager
Expand Down

0 comments on commit 224b567

Please sign in to comment.