From fc9ebf1104031ab9692fe761a935301fb7bf983b Mon Sep 17 00:00:00 2001 From: Andreas Reichel Date: Wed, 5 Sep 2018 11:13:00 +0200 Subject: [PATCH] macros: Add loop class for loop functionality To keep the macro style of the program flow and explicitely state the command sequence with Macro.add, we need a mechanism that repeats several steps of the Macro depending on a given condition. This will be used by the repo checkout logic. Signed-off-by: Andreas Reichel --- kas/libcmds.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/kas/libcmds.py b/kas/libcmds.py index f2c11b8..a98190d 100644 --- a/kas/libcmds.py +++ b/kas/libcmds.py @@ -74,6 +74,38 @@ class Command: pass +class Loop(Command): + """ + A class that defines a set of commands as a loop. + """ + def __init__(self, name): + self.commands = [] + self.name = name + + def __str__(self): + return self.name + + def add(self, command): + """ + Appends commands to the loop. + """ + self.commands.append(command) + + def execute(self, ctx): + """ + Executes the loop. + """ + loop_name = str(self) + + def executor(command): + command_name = str(command) + logging.debug('Loop %s: execute %s', loop_name, command_name) + return command.execute(ctx) + + while all(executor(c) for c in self.commands): + pass + + class SetupHome(Command): """ Setups the home directory of kas.