PTU5KAS/kas/config.py
Andreas Reichel 7b18e5ec3b 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>
2018-08-23 16:29:56 +02:00

191 lines
5.9 KiB
Python

# kas - setup tool for bitbake based projects
#
# Copyright (c) Siemens AG, 2017
#
# 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 configuration.
"""
import os
try:
import distro
def get_distro_id_base():
"""
Returns a compatible distro id.
"""
return distro.like() or distro.id()
except ImportError:
import platform
def get_distro_id_base():
"""
Wrapper around platform.dist to simulate distro.id
platform.dist is deprecated and will be removed in python 3.7
Use the 'distro' package instead.
"""
# pylint: disable=deprecated-method
return platform.dist()[0]
from .repos import Repo
__license__ = 'MIT'
__copyright__ = 'Copyright (c) Siemens AG, 2017'
class Config:
"""
Implements the kas configuration based on config files.
"""
def __init__(self, filename, target, task=None):
from .includehandler import GlobalIncludes
self._config = {}
self.filename = os.path.abspath(filename)
self.handler = GlobalIncludes(self.filename)
self.repo_dict = self._get_repo_dict()
self.context = None
if target:
self._config['target'] = target
if task:
self._config['task'] = task
def set_context(self, ctx):
"""
Set a reference to the ctx that includes this config
"""
self.context = ctx
def find_missing_repos(self):
"""
Returns repos that are in config but not on disk
"""
repo_paths = {}
(self._config, missing_repo_names) = \
self.handler.get_config(repos=repo_paths)
return missing_repo_names
def get_repos(self):
"""
Returns the list of repos.
"""
# 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):
"""
Returns a dictionary containing the repositories with
their name (as it is defined in the config file) as key
and the `Repo` instances as value.
"""
repo_config_dict = self._config.get('repos', {})
repo_dict = {}
for repo in repo_config_dict:
repo_config_dict[repo] = repo_config_dict[repo] or {}
repo_dict[repo] = Repo.factory(repo, repo_config_dict[repo], self)
return repo_dict
def get_bitbake_targets(self):
"""
Returns a list of bitbake targets
"""
environ_targets = [i
for i in os.environ.get('KAS_TARGET', '').split()
if i]
if environ_targets:
return environ_targets
target = self._config.get('target', 'core-image-minimal')
if isinstance(target, str):
return [target]
return target
def get_bitbake_task(self):
"""
Return the bitbake task
"""
return os.environ.get('KAS_TASK',
self._config.get('task', 'build'))
def _get_conf_header(self, header_name):
"""
Returns the local.conf header
"""
header = ''
for key, value in sorted(self._config.get(header_name, {}).items()):
header += '# {}\n{}\n'.format(key, value)
return header
def get_bblayers_conf_header(self):
"""
Returns the bblayers.conf header
"""
return self._get_conf_header('bblayers_conf_header')
def get_local_conf_header(self):
"""
Returns the local.conf header
"""
return self._get_conf_header('local_conf_header')
def get_machine(self):
"""
Returns the machine
"""
return os.environ.get('KAS_MACHINE',
self._config.get('machine', 'qemu'))
def get_distro(self):
"""
Returns the distro
"""
return os.environ.get('KAS_DISTRO',
self._config.get('distro', 'poky'))
def get_environment(self):
"""
Returns the configured environment variables from the configuration
file, with possible overwritten values from the environment.
"""
env = self._config.get('env', {})
return {var: os.environ.get(var, env[var]) for var in env}
def get_multiconfig(self):
"""
Returns the multiconfig array as bitbake string
"""
return ' '.join(set(i.split(':')[1]
for i in
self.get_bitbake_targets()
if i.startswith('multiconfig')))
def get_gitlabci_config(self):
"""
Returns the GitlabCI configuration
"""
return self._config.get('gitlabci_config', '')