kas: Factor out common setup & cleanup steps

The list of steps to be executed within a macro is split into setup
commands, main commands and cleanup commands to ensure that new steps
are added to the appropriate place in the execution order by plugins.

By default, the setup and cleanup commands are set to the default list
of steps which the build and shell plugins have in common. It's expected
that most future plugins will also share these common setup and cleanup
steps but we do provide arguments to the Macro initialiser to skip these
if customisation is needed.

Signed-off-by: Paul Barker <pbarker@konsulko.com>
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
This commit is contained in:
Paul Barker 2020-11-13 19:34:51 +01:00 committed by Jan Kiszka
parent 451a51f94a
commit bcb748c22a
3 changed files with 36 additions and 66 deletions

View File

@ -41,7 +41,37 @@ class Macro:
"""
Contains commands and provides method to run them.
"""
def __init__(self):
def __init__(self, use_common_setup=True, use_common_cleanup=True):
if use_common_setup:
repo_loop = Loop('repo_setup_loop')
repo_loop.add(SetupReposStep())
self.setup_commands = [
SetupDir(),
]
if 'SSH_PRIVATE_KEY' in os.environ:
self.setup_commands.append(SetupSSHAgent())
self.setup_commands += [
InitSetupRepos(),
repo_loop,
FinishSetupRepos(),
SetupEnviron(),
SetupHome(),
ReposApplyPatches(),
WriteBBConfig(),
]
else:
self.setup_commands = []
if use_common_cleanup and 'SSH_PRIVATE_KEY' in os.environ:
self.cleanup_commands = [
CleanupSSHAgent(),
]
else:
self.cleanup_commands = []
self.commands = []
def add(self, command):
@ -56,7 +86,9 @@ class Macro:
configuration.
"""
skip = skip or []
for command in self.commands:
joined_commands = self.setup_commands + \
self.commands + self.cleanup_commands
for command in joined_commands:
command_name = str(command)
if command_name in skip:
continue

View File

@ -33,17 +33,12 @@
"""
import logging
import os
import subprocess
import sys
from kas.context import create_global_context
from kas.config import Config
from kas.libkas import find_program, run_cmd
from kas.libcmds import (Macro, Command, SetupDir, CleanupSSHAgent,
SetupSSHAgent, SetupEnviron,
WriteBBConfig, SetupHome, ReposApplyPatches,
Loop, InitSetupRepos, FinishSetupRepos,
SetupReposStep)
from kas.libcmds import Macro, Command
__license__ = 'MIT'
__copyright__ = 'Copyright (c) Siemens AG, 2017-2018'
@ -84,33 +79,7 @@ class Build:
ctx.config = Config(args.config, args.target, args.task)
macro = Macro()
# Prepare
macro.add(SetupDir())
if 'SSH_PRIVATE_KEY' in os.environ:
macro.add(SetupSSHAgent())
macro.add(InitSetupRepos())
repo_loop = Loop('repo_setup_loop')
repo_loop.add(SetupReposStep())
macro.add(repo_loop)
macro.add(FinishSetupRepos())
macro.add(SetupEnviron())
macro.add(SetupHome())
macro.add(ReposApplyPatches())
macro.add(WriteBBConfig())
# Build
macro.add(BuildCommand(args.extra_bitbake_args))
if 'SSH_PRIVATE_KEY' in os.environ:
macro.add(CleanupSSHAgent())
macro.run(ctx, args.skip)

View File

@ -38,16 +38,11 @@
"""
import logging
import os
import subprocess
import sys
from kas.context import create_global_context
from kas.config import Config
from kas.libcmds import (Macro, Command, SetupDir, SetupEnviron,
WriteBBConfig, SetupHome, ReposApplyPatches,
CleanupSSHAgent, SetupSSHAgent,
Loop, InitSetupRepos, FinishSetupRepos,
SetupReposStep)
from kas.libcmds import Macro, Command
__license__ = 'MIT'
__copyright__ = 'Copyright (c) Siemens AG, 2017-2018'
@ -93,33 +88,7 @@ class Shell:
]
macro = Macro()
# Prepare
macro.add(SetupDir())
if 'SSH_PRIVATE_KEY' in os.environ:
macro.add(SetupSSHAgent())
macro.add(InitSetupRepos())
repo_loop = Loop('repo_setup_loop')
repo_loop.add(SetupReposStep())
macro.add(repo_loop)
macro.add(FinishSetupRepos())
macro.add(SetupEnviron())
macro.add(SetupHome())
macro.add(ReposApplyPatches())
macro.add(WriteBBConfig())
# Shell
macro.add(ShellCommand(args.command))
if 'SSH_PRIVATE_KEY' in os.environ:
macro.add(CleanupSSHAgent())
macro.run(ctx, args.skip)