2017-06-14 13:36:37 +02:00
|
|
|
# kas - setup tool for bitbake based projects
|
|
|
|
#
|
2018-09-05 11:13:03 +02:00
|
|
|
# Copyright (c) Siemens AG, 2017-2018
|
2017-06-14 13:36:37 +02:00
|
|
|
#
|
|
|
|
# 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.
|
2017-06-21 13:32:56 +02:00
|
|
|
"""
|
2018-09-05 11:13:03 +02:00
|
|
|
This module contains common commands used by kas plugins.
|
2017-06-21 13:32:56 +02:00
|
|
|
"""
|
2017-06-14 13:36:37 +02:00
|
|
|
|
|
|
|
import tempfile
|
|
|
|
import logging
|
|
|
|
import shutil
|
|
|
|
import os
|
2018-08-13 14:11:22 +02:00
|
|
|
import pprint
|
2017-06-14 13:36:37 +02:00
|
|
|
from .libkas import (ssh_cleanup_agent, ssh_setup_agent, ssh_no_host_key_check,
|
2018-03-09 08:22:52 +01:00
|
|
|
get_build_environ, repos_fetch, repos_apply_patches)
|
2018-08-13 14:11:22 +02:00
|
|
|
from .includehandler import IncludeException
|
2017-06-14 13:36:37 +02:00
|
|
|
|
|
|
|
__license__ = 'MIT'
|
2018-09-05 11:13:03 +02:00
|
|
|
__copyright__ = 'Copyright (c) Siemens AG, 2017-2018'
|
2017-06-14 13:36:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Macro:
|
2017-06-21 13:32:56 +02:00
|
|
|
"""
|
2018-09-05 11:13:03 +02:00
|
|
|
Contains commands and provides method to run them.
|
2017-06-21 13:32:56 +02:00
|
|
|
"""
|
2017-06-14 13:36:37 +02:00
|
|
|
def __init__(self):
|
|
|
|
self.commands = []
|
|
|
|
|
|
|
|
def add(self, command):
|
2017-06-21 13:32:56 +02:00
|
|
|
"""
|
|
|
|
Appends commands to the command list.
|
|
|
|
"""
|
2017-06-14 13:36:37 +02:00
|
|
|
self.commands.append(command)
|
|
|
|
|
2018-08-13 14:11:22 +02:00
|
|
|
def run(self, ctx, skip=None):
|
2017-06-21 13:32:56 +02:00
|
|
|
"""
|
2018-09-05 11:13:03 +02:00
|
|
|
Runs a command from the command list with respect to the
|
|
|
|
configuration.
|
2017-06-21 13:32:56 +02:00
|
|
|
"""
|
|
|
|
skip = skip or []
|
|
|
|
for command in self.commands:
|
|
|
|
command_name = str(command)
|
|
|
|
if command_name in skip:
|
2017-06-14 13:36:37 +02:00
|
|
|
continue
|
2017-09-26 09:33:31 +02:00
|
|
|
logging.debug('execute %s', command_name)
|
2018-08-13 14:11:22 +02:00
|
|
|
command.execute(ctx)
|
2017-06-14 13:36:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Command:
|
2017-06-21 13:32:56 +02:00
|
|
|
"""
|
|
|
|
An abstract class that defines the interface of a command.
|
|
|
|
"""
|
|
|
|
|
2018-08-13 14:11:22 +02:00
|
|
|
def execute(self, ctx):
|
2017-06-21 13:32:56 +02:00
|
|
|
"""
|
|
|
|
This method executes the command.
|
|
|
|
"""
|
2017-06-14 13:36:37 +02:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2018-09-05 11:13:00 +02:00
|
|
|
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):
|
|
|
|
"""
|
2018-09-05 11:13:03 +02:00
|
|
|
Appends a command to the loop.
|
2018-09-05 11:13:00 +02:00
|
|
|
"""
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2017-06-14 13:36:37 +02:00
|
|
|
class SetupHome(Command):
|
2017-06-21 13:32:56 +02:00
|
|
|
"""
|
2018-09-05 11:13:03 +02:00
|
|
|
Sets up the home directory of kas.
|
2017-06-21 13:32:56 +02:00
|
|
|
"""
|
|
|
|
|
2017-06-14 13:36:37 +02:00
|
|
|
def __init__(self):
|
2017-06-21 13:32:56 +02:00
|
|
|
super().__init__()
|
2017-06-14 13:36:37 +02:00
|
|
|
self.tmpdirname = tempfile.mkdtemp()
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
shutil.rmtree(self.tmpdirname)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return 'setup_home'
|
|
|
|
|
2018-08-13 14:11:22 +02:00
|
|
|
def execute(self, ctx):
|
2017-06-21 13:32:56 +02:00
|
|
|
with open(self.tmpdirname + '/.wgetrc', 'w') as fds:
|
|
|
|
fds.write('\n')
|
|
|
|
with open(self.tmpdirname + '/.netrc', 'w') as fds:
|
|
|
|
fds.write('\n')
|
2018-03-21 16:41:10 +01:00
|
|
|
with open(self.tmpdirname + '/.gitconfig', 'w') as fds:
|
|
|
|
fds.write('[User]\n'
|
|
|
|
'\temail = kas@example.com\n'
|
|
|
|
'\tname = Kas User\n')
|
2020-05-05 14:07:25 +02:00
|
|
|
|
2020-06-17 08:09:14 +02:00
|
|
|
if os.environ.get('AWS_CONFIG_FILE', False) \
|
|
|
|
and os.environ.get('AWS_SHARED_CREDENTIALS_FILE', False):
|
2020-05-05 14:07:25 +02:00
|
|
|
os.makedirs(self.tmpdirname + "/.aws")
|
|
|
|
|
|
|
|
shutil.copy(os.environ['AWS_CONFIG_FILE'],
|
|
|
|
self.tmpdirname + "/.aws/config")
|
|
|
|
shutil.copy(os.environ['AWS_SHARED_CREDENTIALS_FILE'],
|
|
|
|
self.tmpdirname + "/.aws/credentials")
|
|
|
|
|
2018-08-13 14:11:22 +02:00
|
|
|
ctx.environ['HOME'] = self.tmpdirname
|
2017-06-14 13:36:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
class SetupDir(Command):
|
2017-06-21 13:32:56 +02:00
|
|
|
"""
|
|
|
|
Creates the build directory.
|
|
|
|
"""
|
|
|
|
|
2017-06-14 13:36:37 +02:00
|
|
|
def __str__(self):
|
|
|
|
return 'setup_dir'
|
|
|
|
|
2018-08-13 14:11:22 +02:00
|
|
|
def execute(self, ctx):
|
|
|
|
os.chdir(ctx.kas_work_dir)
|
|
|
|
if not os.path.exists(ctx.build_dir):
|
|
|
|
os.mkdir(ctx.build_dir)
|
2017-06-14 13:36:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
class SetupSSHAgent(Command):
|
2017-06-21 13:32:56 +02:00
|
|
|
"""
|
2018-09-05 11:13:03 +02:00
|
|
|
Sets up the ssh agent configuration.
|
2017-06-21 13:32:56 +02:00
|
|
|
"""
|
|
|
|
|
2017-06-14 13:36:37 +02:00
|
|
|
def __str__(self):
|
|
|
|
return 'setup_ssh_agent'
|
|
|
|
|
2018-08-13 14:11:22 +02:00
|
|
|
def execute(self, ctx):
|
2018-08-24 19:18:04 +02:00
|
|
|
ssh_setup_agent()
|
|
|
|
ssh_no_host_key_check()
|
2017-06-14 13:36:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
class CleanupSSHAgent(Command):
|
2017-06-21 13:32:56 +02:00
|
|
|
"""
|
2018-09-05 11:13:03 +02:00
|
|
|
Removes all the identities and stops the ssh-agent instance.
|
2017-06-21 13:32:56 +02:00
|
|
|
"""
|
2017-06-14 13:36:37 +02:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return 'cleanup_ssh_agent'
|
|
|
|
|
2018-08-13 14:11:22 +02:00
|
|
|
def execute(self, ctx):
|
2018-08-24 19:18:04 +02:00
|
|
|
ssh_cleanup_agent()
|
2017-06-14 13:36:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
class SetupEnviron(Command):
|
2017-06-21 13:32:56 +02:00
|
|
|
"""
|
2018-09-05 11:13:03 +02:00
|
|
|
Sets up the kas environment.
|
2017-06-21 13:32:56 +02:00
|
|
|
"""
|
|
|
|
|
2017-06-14 13:36:37 +02:00
|
|
|
def __str__(self):
|
|
|
|
return 'setup_environ'
|
|
|
|
|
2018-08-13 14:11:22 +02:00
|
|
|
def execute(self, ctx):
|
2018-08-24 19:18:05 +02:00
|
|
|
ctx.environ.update(get_build_environ())
|
2017-06-14 13:36:37 +02:00
|
|
|
|
|
|
|
|
2018-08-13 14:11:22 +02:00
|
|
|
class WriteBBConfig(Command):
|
2017-06-21 13:32:56 +02:00
|
|
|
"""
|
|
|
|
Writes bitbake configuration files into the build directory.
|
|
|
|
"""
|
|
|
|
|
2017-06-14 13:36:37 +02:00
|
|
|
def __str__(self):
|
2018-08-13 14:11:22 +02:00
|
|
|
return 'write_bbconfig'
|
2017-06-14 13:36:37 +02:00
|
|
|
|
2018-08-13 14:11:22 +02:00
|
|
|
def execute(self, ctx):
|
|
|
|
def _write_bblayers_conf(ctx):
|
|
|
|
filename = ctx.build_dir + '/conf/bblayers.conf'
|
2017-12-21 17:21:40 +01:00
|
|
|
if not os.path.isdir(os.path.dirname(filename)):
|
|
|
|
os.makedirs(os.path.dirname(filename))
|
2017-06-21 13:32:56 +02:00
|
|
|
with open(filename, 'w') as fds:
|
2018-08-13 14:11:22 +02:00
|
|
|
fds.write(ctx.config.get_bblayers_conf_header())
|
2017-07-17 22:38:07 +02:00
|
|
|
fds.write('BBLAYERS ?= " \\\n ')
|
|
|
|
fds.write(' \\\n '.join(
|
2018-08-13 14:11:22 +02:00
|
|
|
sorted(layer for repo in ctx.config.get_repos()
|
2017-07-17 22:38:07 +02:00
|
|
|
for layer in repo.layers)))
|
2017-06-21 13:32:56 +02:00
|
|
|
fds.write('"\n')
|
|
|
|
|
2018-08-13 14:11:22 +02:00
|
|
|
def _write_local_conf(ctx):
|
|
|
|
filename = ctx.build_dir + '/conf/local.conf'
|
2017-06-21 13:32:56 +02:00
|
|
|
with open(filename, 'w') as fds:
|
2018-08-13 14:11:22 +02:00
|
|
|
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()))
|
2017-11-07 17:20:17 +01:00
|
|
|
fds.write('BBMULTICONFIG ?= "{}"\n'.format(
|
2018-08-13 14:11:22 +02:00
|
|
|
ctx.config.get_multiconfig()))
|
2017-06-21 13:32:56 +02:00
|
|
|
|
2018-08-13 14:11:22 +02:00
|
|
|
_write_bblayers_conf(ctx)
|
|
|
|
_write_local_conf(ctx)
|
2017-06-14 13:36:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
class ReposFetch(Command):
|
2017-06-21 13:32:56 +02:00
|
|
|
"""
|
|
|
|
Fetches repositories defined in the configuration
|
|
|
|
"""
|
|
|
|
|
2017-06-14 13:36:37 +02:00
|
|
|
def __str__(self):
|
|
|
|
return 'repos_fetch'
|
|
|
|
|
2018-08-13 14:11:22 +02:00
|
|
|
def execute(self, ctx):
|
2018-08-24 19:18:06 +02:00
|
|
|
repos_fetch(ctx.config.get_repos())
|
2017-06-14 13:36:37 +02:00
|
|
|
|
|
|
|
|
2018-03-09 08:22:52 +01:00
|
|
|
class ReposApplyPatches(Command):
|
|
|
|
"""
|
|
|
|
Applies the patches defined in the configuration to the repositories.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return 'repos_apply_patches'
|
|
|
|
|
2018-08-13 14:11:22 +02:00
|
|
|
def execute(self, ctx):
|
2018-08-24 19:18:06 +02:00
|
|
|
repos_apply_patches(ctx.config.get_repos())
|
2018-03-09 08:22:52 +01:00
|
|
|
|
|
|
|
|
2017-06-14 13:36:37 +02:00
|
|
|
class ReposCheckout(Command):
|
2017-06-21 13:32:56 +02:00
|
|
|
"""
|
2018-09-05 11:13:03 +02:00
|
|
|
Ensures that the right revision of each repo is checked out.
|
2017-06-21 13:32:56 +02:00
|
|
|
"""
|
|
|
|
|
2017-06-14 13:36:37 +02:00
|
|
|
def __str__(self):
|
|
|
|
return 'repos_checkout'
|
|
|
|
|
2018-08-13 14:11:22 +02:00
|
|
|
def execute(self, ctx):
|
|
|
|
for repo in ctx.config.get_repos():
|
2018-08-24 19:18:06 +02:00
|
|
|
repo.checkout()
|
2018-08-13 14:11:22 +02:00
|
|
|
|
|
|
|
|
2018-09-05 11:13:01 +02:00
|
|
|
class InitSetupRepos(Command):
|
2018-08-13 14:11:22 +02:00
|
|
|
"""
|
2018-09-05 11:13:03 +02:00
|
|
|
Prepares setting up repos including the include logic
|
2018-08-13 14:11:22 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
def __str__(self):
|
2018-09-05 11:13:01 +02:00
|
|
|
return 'init_setup_repos'
|
2018-08-13 14:11:22 +02:00
|
|
|
|
|
|
|
def execute(self, ctx):
|
2018-09-05 11:13:01 +02:00
|
|
|
ctx.missing_repo_names = ctx.config.find_missing_repos()
|
|
|
|
ctx.missing_repo_names_old = None
|
2018-08-13 14:11:22 +02:00
|
|
|
|
2018-09-05 11:13:01 +02:00
|
|
|
|
|
|
|
class SetupReposStep(Command):
|
|
|
|
"""
|
|
|
|
Single step of the checkout repos loop
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return 'setup_repos_step'
|
|
|
|
|
|
|
|
def execute(self, ctx):
|
|
|
|
""" TODO refactor protected-access """
|
|
|
|
if not ctx.missing_repo_names:
|
|
|
|
return False
|
2018-08-13 14:11:22 +02:00
|
|
|
|
2018-09-05 11:13:01 +02:00
|
|
|
if ctx.missing_repo_names == ctx.missing_repo_names_old:
|
|
|
|
raise IncludeException('Could not fetch all repos needed by '
|
|
|
|
'includes.')
|
2018-08-13 14:11:22 +02:00
|
|
|
|
2018-09-05 11:13:01 +02:00
|
|
|
logging.debug('Missing repos for complete config:\n%s',
|
|
|
|
pprint.pformat(ctx.missing_repo_names))
|
2018-08-13 14:11:22 +02:00
|
|
|
|
2018-09-05 11:13:01 +02:00
|
|
|
ctx.config.repo_dict = ctx.config._get_repo_dict()
|
2018-08-13 14:11:22 +02:00
|
|
|
|
2018-09-05 11:13:01 +02:00
|
|
|
ctx.missing_repos = [ctx.config.repo_dict[repo_name]
|
|
|
|
for repo_name in ctx.missing_repo_names
|
2018-08-13 14:11:22 +02:00
|
|
|
if repo_name in ctx.config.repo_dict]
|
|
|
|
|
2018-09-05 11:13:01 +02:00
|
|
|
repos_fetch(ctx.missing_repos)
|
2018-08-13 14:11:22 +02:00
|
|
|
|
2018-09-05 11:13:01 +02:00
|
|
|
for repo in ctx.missing_repos:
|
|
|
|
repo.checkout()
|
2018-08-13 14:11:22 +02:00
|
|
|
|
2018-09-05 11:13:01 +02:00
|
|
|
ctx.config.repo_dict = ctx.config._get_repo_dict()
|
2018-08-13 14:11:22 +02:00
|
|
|
|
2018-09-05 11:13:01 +02:00
|
|
|
repo_paths = {r: ctx.config.repo_dict[r].path for r
|
|
|
|
in ctx.config.repo_dict}
|
|
|
|
ctx.missing_repo_names_old = ctx.missing_repo_names
|
2018-08-13 14:11:22 +02:00
|
|
|
|
2018-09-05 11:13:01 +02:00
|
|
|
(ctx.config._config, ctx.missing_repo_names) = \
|
|
|
|
ctx.config.handler.get_config(repos=repo_paths)
|
2018-08-13 14:11:22 +02:00
|
|
|
|
2018-09-05 11:13:01 +02:00
|
|
|
return ctx.missing_repo_names
|
|
|
|
|
|
|
|
|
|
|
|
class FinishSetupRepos(Command):
|
|
|
|
"""
|
2018-09-05 11:13:03 +02:00
|
|
|
Finalizes the repo setup loop
|
2018-09-05 11:13:01 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return 'finish_setup_repos'
|
|
|
|
|
|
|
|
def execute(self, ctx):
|
|
|
|
""" TODO refactor protected-access """
|
2018-08-13 14:11:22 +02:00
|
|
|
# now fetch everything with complete config and check out layers
|
|
|
|
# except if keep_config is set
|
|
|
|
if not ctx.keep_config:
|
2018-08-24 19:18:06 +02:00
|
|
|
repos_fetch(ctx.config.get_repos())
|
2018-08-13 14:11:22 +02:00
|
|
|
|
|
|
|
for repo in ctx.config.get_repos():
|
2018-08-24 19:18:06 +02:00
|
|
|
repo.checkout()
|
2018-08-13 14:11:22 +02:00
|
|
|
|
|
|
|
logging.debug('Configuration from config file:\n%s',
|
|
|
|
pprint.pformat(ctx.config._config))
|