Refactor repo checkout

If a kas-file inside a repo includes another kas-file form a repo
which is not checked-out, and this 2nd repo can only be checked-out
with an SSH-key, kas fails. This is, because the constructor of
the Config class used to fetch missing repos already before the
SSH-Agent was setup.

This patch refactors the way in which kas checks-out repositories. This
also required the Config class to be split into Context and Config,
where the new Context is the build-Context, i.e. containing the
environment of commands executed by kas and the new Config is the Config
of kas itself, i.e. containing the repo dictionary.

This way it is possible to initialize the context needed for SSH setup
independently of the kas configuration.

The commands ReposFetch and ReposCheckout are refactored into a
SetupRepos command. This command parses the include files and
successively checks-out needed repos and updates the config as long
as repos are missing. The logic is taken directly from the constructor
of the former Config class (with minor adaptations).

Further refactoring will be needed to clean up the conceptual
programming patterns (i.e. to keep the Macro-Style consistent),
and to re-add the macro pattern regarding the above ReposFetch.

Signed-off-by: Andreas Reichel <andreas.reichel.ext@siemens.com>
This commit is contained in:
Andreas Reichel 2018-08-13 14:11:22 +02:00 committed by Daniel Wagner
parent 3bada55d30
commit 7b18e5ec3b
7 changed files with 258 additions and 197 deletions

View File

@ -24,12 +24,11 @@
"""
import os
from .config import Config
from .context import Context
from .libkas import find_program, run_cmd, kasplugin
from .libcmds import (Macro, Command, SetupDir, SetupProxy,
CleanupSSHAgent, SetupSSHAgent, SetupEnviron,
WriteConfig, SetupHome, ReposFetch,
ReposApplyPatches, ReposCheckout)
from .libcmds import (Macro, Command, SetupDir, CleanupSSHAgent,
SetupSSHAgent, SetupEnviron, SetupRepos,
WriteBBConfig, SetupHome, ReposApplyPatches)
__license__ = 'MIT'
__copyright__ = 'Copyright (c) Siemens AG, 2017'
@ -72,24 +71,22 @@ class Build:
if args.cmd != 'build':
return False
cfg = Config(args.config, args.target, args.task)
ctx = Context(args.config, args.target, args.task)
macro = Macro()
# Prepare
macro.add(SetupDir())
macro.add(SetupProxy())
if 'SSH_PRIVATE_KEY' in os.environ:
macro.add(SetupSSHAgent())
macro.add(ReposFetch())
macro.add(ReposCheckout())
macro.add(SetupRepos())
macro.add(SetupEnviron())
macro.add(SetupHome())
macro.add(ReposApplyPatches())
macro.add(WriteConfig())
macro.add(WriteBBConfig())
# Build
macro.add(BuildCommand(args.task))
@ -97,7 +94,7 @@ class Build:
if 'SSH_PRIVATE_KEY' in os.environ:
macro.add(CleanupSSHAgent())
macro.run(cfg, args.skip)
macro.run(ctx, args.skip)
return True
@ -114,12 +111,12 @@ class BuildCommand(Command):
def __str__(self):
return 'build'
def execute(self, config):
def execute(self, ctx):
"""
Executes the bitbake build command.
"""
# Start bitbake build of image
bitbake = find_program(config.environ['PATH'], 'bitbake')
run_cmd(([bitbake, '-k', '-c', config.get_bitbake_task()]
+ config.get_bitbake_targets()),
env=config.environ, cwd=config.build_dir)
bitbake = find_program(ctx.environ['PATH'], 'bitbake')
run_cmd(([bitbake, '-k', '-c', ctx.config.get_bitbake_task()]
+ ctx.config.get_bitbake_targets()),
env=ctx.environ, cwd=ctx.build_dir)

View File

@ -24,8 +24,6 @@
"""
import os
import logging
import pprint
try:
import distro
@ -49,7 +47,6 @@ except ImportError:
return platform.dist()[0]
from .repos import Repo
from .libkas import repos_fetch
__license__ = 'MIT'
__copyright__ = 'Copyright (c) Siemens AG, 2017'
@ -60,108 +57,33 @@ class Config:
Implements the kas configuration based on config files.
"""
def __init__(self, filename, target, task=None):
from .includehandler import GlobalIncludes, IncludeException
self.__kas_work_dir = os.environ.get('KAS_WORK_DIR', os.getcwd())
self.environ = {}
from .includehandler import GlobalIncludes
self._config = {}
self.setup_environ()
self.filename = os.path.abspath(filename)
self.handler = GlobalIncludes(self.filename)
repo_paths = {}
missing_repo_names_old = []
(self._config, missing_repo_names) = \
self.handler.get_config(repos=repo_paths)
self.environ.update(self.get_proxy_config())
self.repo_dict = self._get_repo_dict()
while missing_repo_names:
if missing_repo_names == missing_repo_names_old:
raise IncludeException('Could not fetch all repos needed by '
'includes.')
logging.debug('Missing repos for complete config:\n%s',
pprint.pformat(missing_repo_names))
self.repo_dict = self._get_repo_dict()
missing_repos = [self.repo_dict[repo_name]
for repo_name in missing_repo_names
if repo_name in self.repo_dict]
repos_fetch(self, missing_repos)
for repo in missing_repos:
repo.checkout(self)
repo_paths = {r: self.repo_dict[r].path for r in self.repo_dict}
missing_repo_names_old = missing_repo_names
(self._config, missing_repo_names) = \
self.handler.get_config(repos=repo_paths)
self.repo_dict = self._get_repo_dict()
logging.debug('Configuration from config file:\n%s',
pprint.pformat(self._config))
self.context = None
if target:
self._config['target'] = target
if task:
self._config['task'] = task
@property
def build_dir(self):
def set_context(self, ctx):
"""
The path of the build directory.
Set a reference to the ctx that includes this config
"""
return os.path.join(self.__kas_work_dir, 'build')
self.context = ctx
@property
def kas_work_dir(self):
def find_missing_repos(self):
"""
The path to the kas work directory.
Returns repos that are in config but not on disk
"""
return self.__kas_work_dir
repo_paths = {}
(self._config, missing_repo_names) = \
self.handler.get_config(repos=repo_paths)
def setup_environ(self):
"""
Sets the environment variables for process that are
started by kas.
"""
distro_base = get_distro_id_base().lower()
if distro_base in ['fedora', 'suse', 'opensuse']:
self.environ = {'LC_ALL': 'en_US.utf8',
'LANG': 'en_US.utf8',
'LANGUAGE': 'en_US'}
elif distro_base in ['debian', 'ubuntu', 'gentoo']:
self.environ = {'LC_ALL': 'en_US.UTF-8',
'LANG': 'en_US.UTF-8',
'LANGUAGE': 'en_US:en'}
else:
logging.warning('kas: "%s" is not a supported distro. '
'No default locales set.', distro_base)
self.environ = {}
def get_repo_ref_dir(self):
"""
The path to the directory that contains the repository references.
"""
# pylint: disable=no-self-use
return os.environ.get('KAS_REPO_REF_DIR', None)
def get_proxy_config(self):
"""
Returns the proxy settings
"""
proxy_config = self._config.get('proxy_config', {})
return {var_name: os.environ.get(var_name,
proxy_config.get(var_name, ''))
for var_name in ['http_proxy',
'https_proxy',
'ftp_proxy',
'no_proxy']}
return missing_repo_names
def get_repos(self):
"""
@ -169,6 +91,9 @@ class Config:
"""
# pylint: disable=no-self-use
# Always keep repo_dict and repos synchronous
# when calling get_repos
self.repo_dict = self._get_repo_dict()
return list(self.repo_dict.values())
def _get_repo_dict(self):

92
kas/context.py Normal file
View File

@ -0,0 +1,92 @@
# kas - setup tool for bitbake based projects
#
# Copyright (c) Siemens AG, 2018
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
"""
This module contains the implementation of the kas context.
"""
import os
import logging
from .config import Config, get_distro_id_base
# pylint: disable=too-many-instance-attributes
class Context:
"""
Implements the kas build context.
"""
def __init__(self, config_filename, bitbake_target, bitbake_task):
self.config_filename = config_filename
self.bitbake_target = bitbake_target
self.bitbake_task = bitbake_task
self.__kas_work_dir = os.environ.get('KAS_WORK_DIR', os.getcwd())
self.__kas_repo_ref_dir = os.environ.get('KAS_REPO_REF_DIR', None)
self.setup_initial_environ()
self.keep_config = False
self.config = Config(config_filename, bitbake_target, bitbake_task)
self.config.set_context(self)
def setup_initial_environ(self):
"""
Sets the environment variables for process that are
started by kas.
"""
distro_base = get_distro_id_base().lower()
if distro_base in ['fedora', 'suse', 'opensuse']:
self.environ = {'LC_ALL': 'en_US.utf8',
'LANG': 'en_US.utf8',
'LANGUAGE': 'en_US'}
elif distro_base in ['debian', 'ubuntu', 'gentoo']:
self.environ = {'LC_ALL': 'en_US.UTF-8',
'LANG': 'en_US.UTF-8',
'LANGUAGE': 'en_US:en'}
else:
logging.warning('kas: "%s" is not a supported distro. '
'No default locales set.', distro_base)
self.environ = {}
for key in ['http_proxy', 'https_proxy', 'ftp_proxy', 'no_proxy']:
val = os.environ.get(key, None)
if val:
self.environ[key] = val
@property
def build_dir(self):
"""
The path of the build directory
"""
return os.path.join(self.__kas_work_dir, 'build')
@property
def kas_work_dir(self):
"""
The path to the kas work directory
"""
return self.__kas_work_dir
@property
def kas_repo_ref_dir(self):
"""
The reference directory for the repo
"""
return self.__kas_repo_ref_dir

View File

@ -27,8 +27,10 @@ import tempfile
import logging
import shutil
import os
import pprint
from .libkas import (ssh_cleanup_agent, ssh_setup_agent, ssh_no_host_key_check,
get_build_environ, repos_fetch, repos_apply_patches)
from .includehandler import IncludeException
__license__ = 'MIT'
__copyright__ = 'Copyright (c) Siemens AG, 2017'
@ -47,7 +49,7 @@ class Macro:
"""
self.commands.append(command)
def run(self, config, skip=None):
def run(self, ctx, skip=None):
"""
Runs command from the command list respective to the configuration.
"""
@ -57,7 +59,7 @@ class Macro:
if command_name in skip:
continue
logging.debug('execute %s', command_name)
command.execute(config)
command.execute(ctx)
class Command:
@ -65,7 +67,7 @@ class Command:
An abstract class that defines the interface of a command.
"""
def execute(self, config):
def execute(self, ctx):
"""
This method executes the command.
"""
@ -87,7 +89,7 @@ class SetupHome(Command):
def __str__(self):
return 'setup_home'
def execute(self, config):
def execute(self, ctx):
with open(self.tmpdirname + '/.wgetrc', 'w') as fds:
fds.write('\n')
with open(self.tmpdirname + '/.netrc', 'w') as fds:
@ -96,7 +98,7 @@ class SetupHome(Command):
fds.write('[User]\n'
'\temail = kas@example.com\n'
'\tname = Kas User\n')
config.environ['HOME'] = self.tmpdirname
ctx.environ['HOME'] = self.tmpdirname
class SetupDir(Command):
@ -107,10 +109,10 @@ class SetupDir(Command):
def __str__(self):
return 'setup_dir'
def execute(self, config):
os.chdir(config.kas_work_dir)
if not os.path.exists(config.build_dir):
os.mkdir(config.build_dir)
def execute(self, ctx):
os.chdir(ctx.kas_work_dir)
if not os.path.exists(ctx.build_dir):
os.mkdir(ctx.build_dir)
class SetupSSHAgent(Command):
@ -121,9 +123,9 @@ class SetupSSHAgent(Command):
def __str__(self):
return 'setup_ssh_agent'
def execute(self, config):
ssh_setup_agent(config)
ssh_no_host_key_check(config)
def execute(self, ctx):
ssh_setup_agent(ctx)
ssh_no_host_key_check(ctx)
class CleanupSSHAgent(Command):
@ -134,20 +136,8 @@ class CleanupSSHAgent(Command):
def __str__(self):
return 'cleanup_ssh_agent'
def execute(self, config):
ssh_cleanup_agent(config)
class SetupProxy(Command):
"""
Setups proxy configuration in the kas environment.
"""
def __str__(self):
return 'setup_proxy'
def execute(self, config):
config.environ.update(config.get_proxy_config())
def execute(self, ctx):
ssh_cleanup_agent(ctx)
class SetupEnviron(Command):
@ -158,42 +148,44 @@ class SetupEnviron(Command):
def __str__(self):
return 'setup_environ'
def execute(self, config):
config.environ.update(get_build_environ(config, config.build_dir))
def execute(self, ctx):
ctx.environ.update(get_build_environ(ctx, ctx.build_dir))
class WriteConfig(Command):
class WriteBBConfig(Command):
"""
Writes bitbake configuration files into the build directory.
"""
def __str__(self):
return 'write_config'
return 'write_bbconfig'
def execute(self, config):
def _write_bblayers_conf(config):
filename = config.build_dir + '/conf/bblayers.conf'
def execute(self, ctx):
def _write_bblayers_conf(ctx):
filename = ctx.build_dir + '/conf/bblayers.conf'
if not os.path.isdir(os.path.dirname(filename)):
os.makedirs(os.path.dirname(filename))
with open(filename, 'w') as fds:
fds.write(config.get_bblayers_conf_header())
fds.write(ctx.config.get_bblayers_conf_header())
fds.write('BBLAYERS ?= " \\\n ')
fds.write(' \\\n '.join(
sorted(layer for repo in config.get_repos()
sorted(layer for repo in ctx.config.get_repos()
for layer in repo.layers)))
fds.write('"\n')
def _write_local_conf(config):
filename = config.build_dir + '/conf/local.conf'
def _write_local_conf(ctx):
filename = ctx.build_dir + '/conf/local.conf'
with open(filename, 'w') as fds:
fds.write(config.get_local_conf_header())
fds.write('MACHINE ??= "{}"\n'.format(config.get_machine()))
fds.write('DISTRO ??= "{}"\n'.format(config.get_distro()))
fds.write(ctx.config.get_local_conf_header())
fds.write('MACHINE ??= "{}"\n'.format(
ctx.config.get_machine()))
fds.write('DISTRO ??= "{}"\n'.format(
ctx.config.get_distro()))
fds.write('BBMULTICONFIG ?= "{}"\n'.format(
config.get_multiconfig()))
ctx.config.get_multiconfig()))
_write_bblayers_conf(config)
_write_local_conf(config)
_write_bblayers_conf(ctx)
_write_local_conf(ctx)
class ReposFetch(Command):
@ -204,8 +196,8 @@ class ReposFetch(Command):
def __str__(self):
return 'repos_fetch'
def execute(self, config):
repos_fetch(config, config.get_repos())
def execute(self, ctx):
repos_fetch(ctx, ctx.config.get_repos())
class ReposApplyPatches(Command):
@ -216,8 +208,8 @@ class ReposApplyPatches(Command):
def __str__(self):
return 'repos_apply_patches'
def execute(self, config):
repos_apply_patches(config, config.get_repos())
def execute(self, ctx):
repos_apply_patches(ctx, ctx.config.get_repos())
class ReposCheckout(Command):
@ -228,6 +220,62 @@ class ReposCheckout(Command):
def __str__(self):
return 'repos_checkout'
def execute(self, config):
for repo in config.get_repos():
repo.checkout(config)
def execute(self, ctx):
for repo in ctx.config.get_repos():
repo.checkout(ctx)
class SetupRepos(Command):
"""
Setup repos including the include logic
"""
def __str__(self):
return 'setup_repos'
def execute(self, ctx):
missing_repo_names = ctx.config.find_missing_repos()
missing_repo_names_old = None
# pylint: disable=pointless-string-statement
"""XXX This will be refactored"""
# pylint: disable=protected-access
while missing_repo_names:
if missing_repo_names == missing_repo_names_old:
raise IncludeException('Could not fetch all repos needed by '
'includes.')
logging.debug('Missing repos for complete config:\n%s',
pprint.pformat(missing_repo_names))
ctx.config.repo_dict = ctx.config._get_repo_dict()
missing_repos = [ctx.config.repo_dict[repo_name]
for repo_name in missing_repo_names
if repo_name in ctx.config.repo_dict]
repos_fetch(ctx, missing_repos)
for repo in missing_repos:
repo.checkout(ctx)
ctx.config.repo_dict = ctx.config._get_repo_dict()
repo_paths = {r: ctx.config.repo_dict[r].path for r
in ctx.config.repo_dict}
missing_repo_names_old = missing_repo_names
(ctx.config._config, missing_repo_names) = \
ctx.config.handler.get_config(repos=repo_paths)
# now fetch everything with complete config and check out layers
# except if keep_config is set
if not ctx.keep_config:
repos_fetch(ctx, ctx.config.get_repos())
for repo in ctx.config.get_repos():
repo.checkout(ctx)
logging.debug('Configuration from config file:\n%s',
pprint.pformat(ctx.config._config))

View File

@ -209,7 +209,7 @@ def repos_apply_patches(config, repos):
sys.exit(task.result())
def get_build_environ(config, build_dir):
def get_build_environ(context, build_dir):
"""
Create the build environment variables.
"""
@ -219,7 +219,7 @@ def get_build_environ(config, build_dir):
init_repo = None
permutations = \
[(repo, script) for repo in config.get_repos()
[(repo, script) for repo in context.config.get_repos()
for script in ['oe-init-build-env', 'isar-init-build-env']]
for (repo, script) in permutations:
if os.path.exists(repo.path + '/' + script):
@ -260,7 +260,7 @@ def get_build_environ(config, build_dir):
except ValueError:
pass
conf_env = config.get_environment()
conf_env = context.config.get_environment()
env_vars = ['SSTATE_DIR', 'DL_DIR', 'TMPDIR']
env_vars.extend(conf_env)
@ -324,6 +324,7 @@ def ssh_setup_agent(config, envkeys=None):
for envkey in envkeys:
key = os.environ.get(envkey)
if key:
logging.info("adding SSH key")
ssh_add_key(config.environ, key)
else:
logging.warning('%s is missing', envkey)

View File

@ -75,6 +75,7 @@ class Repo:
"""
Return an instance Repo depending on params.
"""
ctx = config.context
layers_dict = repo_config.get('layers', {})
layers = list(filter(lambda x, laydict=layers_dict:
str(laydict[x]).lower() not in
@ -100,7 +101,7 @@ class Repo:
# No version control operation on repository
if path is None:
path = Repo.get_root_path(os.path.dirname(config.filename),
config.environ)
config.context.environ)
logging.info('Using %s as root for repository %s', path,
name)
@ -108,11 +109,11 @@ class Repo:
disable_operations = True
else:
if path is None:
path = os.path.join(config.kas_work_dir, name)
path = os.path.join(ctx.kas_work_dir, name)
else:
if not os.path.isabs(path):
# Relative pathes are assumed to start from work_dir
path = os.path.join(config.kas_work_dir, path)
path = os.path.join(ctx.kas_work_dir, path)
if typ == 'git':
return GitRepo(url, path, refspec, layers, patches,
@ -148,7 +149,7 @@ class RepoImpl(Repo):
"""
@asyncio.coroutine
def fetch_async(self, config):
def fetch_async(self, ctx):
"""
Start asynchronous repository fetch.
"""
@ -157,13 +158,13 @@ class RepoImpl(Repo):
if not os.path.exists(self.path):
os.makedirs(os.path.dirname(self.path), exist_ok=True)
sdir = os.path.join(config.get_repo_ref_dir() or '',
sdir = os.path.join(ctx.kas_repo_ref_dir or '',
self.qualified_name)
logging.debug('Looking for repo ref dir in %s', sdir)
(retc, _) = yield from run_cmd_async(self.clone_cmd(sdir, config),
env=config.environ,
cwd=config.kas_work_dir)
(retc, _) = yield from run_cmd_async(self.clone_cmd(sdir, ctx),
env=ctx.environ,
cwd=ctx.kas_work_dir)
if retc == 0:
logging.info('Repository %s cloned', self.name)
return retc
@ -174,7 +175,7 @@ class RepoImpl(Repo):
# Does refspec exist in the current repository?
(retc, output) = yield from run_cmd_async(self.contains_refspec_cmd(),
env=config.environ,
env=ctx.environ,
cwd=self.path,
fail=False,
liveupdate=False)
@ -185,7 +186,7 @@ class RepoImpl(Repo):
# No it is missing, try to fetch
(retc, output) = yield from run_cmd_async(self.fetch_cmd(),
env=config.environ,
env=ctx.environ,
cwd=self.path,
fail=False)
if retc:
@ -195,7 +196,7 @@ class RepoImpl(Repo):
logging.info('Repository %s updated', self.name)
return 0
def checkout(self, config):
def checkout(self, ctx):
"""
Checks out the correct revision of the repo.
"""
@ -204,7 +205,7 @@ class RepoImpl(Repo):
# Check if repos is dirty
(_, output) = run_cmd(self.is_dirty_cmd(),
env=config.environ, cwd=self.path,
env=ctx.environ, cwd=self.path,
fail=False)
if output:
logging.warning('Repo %s is dirty. no checkout', self.name)
@ -212,7 +213,7 @@ class RepoImpl(Repo):
# Check if current HEAD is what in the config file is defined.
(_, output) = run_cmd(self.current_rev_cmd(),
env=config.environ, cwd=self.path)
env=ctx.environ, cwd=self.path)
if output.strip() == self.refspec:
logging.info('Repo %s has already checkout out correct '
@ -222,7 +223,7 @@ class RepoImpl(Repo):
run_cmd(self.checkout_cmd(), cwd=self.path)
@asyncio.coroutine
def apply_patches_async(self, config):
def apply_patches_async(self, ctx):
"""
Applies patches to repository asynchronously.
"""
@ -230,7 +231,7 @@ class RepoImpl(Repo):
return 0
for patch in self._patches:
other_repo = config.repo_dict.get(patch['repo'], None)
other_repo = ctx.config.repo_dict.get(patch['repo'], None)
if not other_repo:
logging.warning('Could not find referenced repo. '
@ -257,20 +258,20 @@ class RepoImpl(Repo):
continue
(retc, output) = yield from run_cmd_async(cmd,
env=config.environ,
env=ctx.environ,
cwd=self.path,
fail=False)
# pylint: disable=no-else-return
if retc:
logging.error('Could not apply patch. Please fix repos and '
'patches. (patch path: %s, repo: %s, patch '
'entry: %s, vcs output: %s)',
path, self.name, patch['id'], output)
return 1
logging.info('Patch applied. '
'(patch path: %s, repo: %s, patch entry: %s)',
path, self.name, patch['id'])
else:
logging.info('Patch applied. '
'(patch path: %s, repo: %s, patch entry: %s)',
path, self.name, patch['id'])
return 0
@ -280,9 +281,9 @@ class GitRepo(RepoImpl):
"""
# pylint: disable=no-self-use,missing-docstring
def clone_cmd(self, gitsrcdir, config):
def clone_cmd(self, gitsrcdir, ctx):
cmd = ['git', 'clone', '-q', self.url, self.path]
if config.get_repo_ref_dir() and os.path.exists(gitsrcdir):
if ctx.kas_repo_ref_dir and os.path.exists(gitsrcdir):
cmd.extend(['--reference', gitsrcdir])
return cmd

View File

@ -27,10 +27,10 @@
import subprocess
import os
from kas.libkas import kasplugin
from kas.config import Config
from kas.libcmds import (Macro, Command, SetupDir, SetupProxy, SetupEnviron,
WriteConfig, SetupHome, ReposFetch, ReposCheckout,
ReposApplyPatches, CleanupSSHAgent, SetupSSHAgent)
from kas.context import Context
from kas.libcmds import (Macro, Command, SetupDir, SetupEnviron,
WriteBBConfig, SetupHome, ReposApplyPatches,
CleanupSSHAgent, SetupSSHAgent, SetupRepos)
__license__ = 'MIT'
__copyright__ = 'Copyright (c) Siemens AG, 2017'
@ -75,7 +75,7 @@ class Shell:
if args.cmd != 'shell':
return False
cfg = Config(args.config, args.target)
ctx = Context(args.config, args.target, None)
macro = Macro()
@ -83,21 +83,18 @@ class Shell:
if not args.keep_config_unchanged:
macro.add(SetupDir())
macro.add(SetupProxy())
if 'SSH_PRIVATE_KEY' in os.environ:
macro.add(SetupSSHAgent())
if not args.keep_config_unchanged:
macro.add(ReposFetch())
macro.add(ReposCheckout())
ctx.keep_config = args.keep_config_unchanged
macro.add(SetupRepos())
macro.add(SetupEnviron())
macro.add(SetupHome())
if not args.keep_config_unchanged:
macro.add(ReposApplyPatches())
macro.add(WriteConfig())
macro.add(WriteBBConfig())
# Shell
macro.add(ShellCommand(args.command))
@ -105,7 +102,7 @@ class Shell:
if 'SSH_PRIVATE_KEY' in os.environ:
macro.add(CleanupSSHAgent())
macro.run(cfg, args.skip)
macro.run(ctx, args.skip)
return True
@ -124,10 +121,10 @@ class ShellCommand(Command):
def __str__(self):
return 'shell'
def execute(self, config):
cmd = [config.environ.get('SHELL', '/bin/sh')]
def execute(self, ctx):
cmd = [ctx.environ.get('SHELL', '/bin/sh')]
if self.cmd:
cmd.append('-c')
cmd.append(self.cmd)
subprocess.call(cmd, env=config.environ,
cwd=config.build_dir)
subprocess.call(cmd, env=ctx.environ,
cwd=ctx.build_dir)