From baacd3a8b97662712e20ab6484629102263d5178 Mon Sep 17 00:00:00 2001 From: Paul Barker Date: Fri, 13 Nov 2020 19:34:53 +0100 Subject: [PATCH] for-all-repos: New plugin This plugin runs a specified command in all checked out repositories. Signed-off-by: Paul Barker Signed-off-by: Jan Kiszka --- docs/userguide.rst | 5 +++ kas/plugins/__init__.py | 2 + kas/plugins/forallrepos.py | 85 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 kas/plugins/forallrepos.py diff --git a/docs/userguide.rst b/docs/userguide.rst index e2f2e76..12a0efa 100644 --- a/docs/userguide.rst +++ b/docs/userguide.rst @@ -76,6 +76,11 @@ typically provides a single command. .. automodule:: kas.plugins.build +``forallrepos`` plugin +~~~~~~~~~~~~~~~~~~~~~~ + +.. automodule:: kas.plugins.forallrepos + ``shell`` plugin ~~~~~~~~~~~~~~~~ diff --git a/kas/plugins/__init__.py b/kas/plugins/__init__.py index 425ed55..587d0b4 100644 --- a/kas/plugins/__init__.py +++ b/kas/plugins/__init__.py @@ -39,9 +39,11 @@ def load(): Import all kas plugins """ from . import build + from . import forallrepos from . import shell register_plugins(build) + register_plugins(forallrepos) register_plugins(shell) diff --git a/kas/plugins/forallrepos.py b/kas/plugins/forallrepos.py new file mode 100644 index 0000000..917a3af --- /dev/null +++ b/kas/plugins/forallrepos.py @@ -0,0 +1,85 @@ +# kas - setup tool for bitbake based projects +# +# Copyright (c) Konsulko Group, 2020 +# +# 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 plugin implements the ``kas for-all-repos`` command. + + When this command is executed, kas will checkout the repositories listed + in the chosen config file and then execute a specified command in each + repository. It can be used to query the repository status, automate + actions such as archiving the layers used in a build or to execute any + other required commands. + + For example, to print the commit hashes used by each repository used in + the file ``kas-project.yml`` (assuming they are all git repositories) you + could run:: + + kas for-all-repos kas-project.yml 'git rev-parse HEAD' +""" + +import logging +import subprocess +from kas.context import create_global_context +from kas.config import Config +from kas.libcmds import Macro, Command + +__license__ = 'MIT' +__copyright__ = 'Copyright (c) Siemens AG, 2017-2018' + + +class ForAllRepos: + name = 'for-all-repos' + helpmsg = ( + 'Runs a specified command in all checked out repositories.' + ) + + @classmethod + def setup_parser(cls, parser): + parser.add_argument('command', + help='Command to be executed as a string.') + + def run(self, args): + ctx = create_global_context(args) + ctx.config = Config(args.config) + + macro = Macro() + macro.add(ForAllReposCommand(args.command)) + macro.run(ctx, args.skip) + + +class ForAllReposCommand(Command): + def __init__(self, command): + super().__init__() + self.command = command + + def __str__(self): + return 'for-all-repos' + + def execute(self, ctx): + for repo in ctx.config.get_repos(): + logging.info('%s$ %s', repo.path, self.command) + retcode = subprocess.call(self.command, shell=True, cwd=repo.path, + env=ctx.environ) + if retcode != 0: + logging.error('Command failed with return code %d', retcode) + + +__KAS_PLUGINS__ = [ForAllRepos]