diff options
| author | Alexander Kanavin <alex.kanavin@gmail.com> | 2022-08-31 13:13:58 +0200 |
|---|---|---|
| committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2022-09-01 10:07:02 +0100 |
| commit | e561fc1cbec02b4002e33c63281e63691d4a2725 (patch) | |
| tree | cee69a1004baee92628c243cc29ef6acf08dc9e2 | |
| parent | f5d6792d68a12db9c512522ef576b5a8d4204952 (diff) | |
| download | poky-e561fc1cbec02b4002e33c63281e63691d4a2725.tar.gz | |
bitbake-layers: add ability to save current layer repository configuration into a file
This addresses a long standing gap in the core offering:
there is no tooling to capture the currently configured layers
with their revisions, or restore the layers from a configuration
file (without using external tools, some of which aren't particularly
suitable for the task). This plugin addresses the 'capture' part.
Note that the actual writing is performed by a sub-plugin; one such
sub-plugin is provided (for the json + python script format), but
more can be added (e.g. kas, repo, etc.).
How to save a layer configuration:
a) Running with default choices:
$ bitbake-layers create-layers-setup /srv/work/alex/meta-alex/
NOTE: Starting bitbake server...
NOTE: Created /srv/work/alex/meta-alex/setup-layers.json
NOTE: Created /srv/work/alex/meta-alex/setup-layers
b) Command line options:
NOTE: Starting bitbake server...
usage: bitbake-layers create-layers-setup [-h] [--output-prefix OUTPUT_PREFIX] [--writer {oe-setup-layers}] [--json-only] destdir
Writes out a configuration file and/or a script that replicate the directory structure and revisions of the layers in a current build.
positional arguments:
destdir Directory where to write the output
(if it is inside one of the layers, the layer becomes a bootstrap repository and thus will be excluded from fetching).
optional arguments:
-h, --help show this help message and exit
--output-prefix OUTPUT_PREFIX, -o OUTPUT_PREFIX
File name prefix for the output files, if the default (setup-layers) is undesirable.
--writer {oe-setup-layers}, -w {oe-setup-layers}
Choose the output format (defaults to oe-setup-layers).
Currently supported options are:
oe-setup-layers - a self-contained python script and a json config for it.
--json-only When using the oe-setup-layers writer, write only the layer configuruation in json format. Otherwise, also a copy of scripts/oe-setup-layers (from oe-core or poky) is provided, which is a self contained python script that fetches all the needed layers and sets them to correct revisions using the data from the json.
(From OE-Core rev: 5606d1a123a3816ab45e49ee7707ed84c9c23c5c)
Signed-off-by: Alexander Kanavin <alex@linutronix.de>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
| -rw-r--r-- | meta/lib/bblayers/makesetup.py | 108 | ||||
| -rw-r--r-- | meta/lib/bblayers/setupwriters/oe-setup-layers.py | 50 |
2 files changed, 158 insertions, 0 deletions
diff --git a/meta/lib/bblayers/makesetup.py b/meta/lib/bblayers/makesetup.py new file mode 100644 index 0000000000..bef6da0ea8 --- /dev/null +++ b/meta/lib/bblayers/makesetup.py | |||
| @@ -0,0 +1,108 @@ | |||
| 1 | # | ||
| 2 | # Copyright OpenEmbedded Contributors | ||
| 3 | # | ||
| 4 | # SPDX-License-Identifier: GPL-2.0-only | ||
| 5 | # | ||
| 6 | |||
| 7 | import logging | ||
| 8 | import os | ||
| 9 | import stat | ||
| 10 | import sys | ||
| 11 | import shutil | ||
| 12 | |||
| 13 | import bb.utils | ||
| 14 | import bb.process | ||
| 15 | |||
| 16 | from bblayers.common import LayerPlugin | ||
| 17 | |||
| 18 | logger = logging.getLogger('bitbake-layers') | ||
| 19 | |||
| 20 | sys.path.insert(0, os.path.dirname(os.path.dirname(__file__))) | ||
| 21 | |||
| 22 | import oe.buildcfg | ||
| 23 | |||
| 24 | def plugin_init(plugins): | ||
| 25 | return MakeSetupPlugin() | ||
| 26 | |||
| 27 | class MakeSetupPlugin(LayerPlugin): | ||
| 28 | |||
| 29 | def _get_repo_path(self, layer_path): | ||
| 30 | repo_path, _ = bb.process.run('git rev-parse --show-toplevel', cwd=layer_path) | ||
| 31 | return repo_path.strip() | ||
| 32 | |||
| 33 | def _get_remotes(self, repo_path): | ||
| 34 | remotes = {} | ||
| 35 | remotes_list,_ = bb.process.run('git remote', cwd=repo_path) | ||
| 36 | for r in remotes_list.split(): | ||
| 37 | uri,_ = bb.process.run('git remote get-url {r}'.format(r=r), cwd=repo_path) | ||
| 38 | remotes[r] = {'uri':uri.strip()} | ||
| 39 | return remotes | ||
| 40 | |||
| 41 | def _get_describe(self, repo_path): | ||
| 42 | try: | ||
| 43 | describe,_ = bb.process.run('git describe --tags', cwd=repo_path) | ||
| 44 | except bb.process.ExecutionError: | ||
| 45 | return "" | ||
| 46 | return describe.strip() | ||
| 47 | |||
| 48 | def make_repo_config(self, destdir): | ||
| 49 | """ This is a helper function for the writer plugins that discovers currently confugured layers. | ||
| 50 | The writers do not have to use it, but it can save a bit of work and avoid duplicated code, hence it is | ||
| 51 | available here. """ | ||
| 52 | repos = {} | ||
| 53 | layers = oe.buildcfg.get_layer_revisions(self.tinfoil.config_data) | ||
| 54 | try: | ||
| 55 | destdir_repo = self._get_repo_path(destdir) | ||
| 56 | except bb.process.ExecutionError: | ||
| 57 | destdir_repo = None | ||
| 58 | |||
| 59 | for (l_path, l_name, l_branch, l_rev, l_ismodified) in layers: | ||
| 60 | if l_name == 'workspace': | ||
| 61 | continue | ||
| 62 | if l_ismodified: | ||
| 63 | logger.error("Layer {name} in {path} has uncommitted modifications or is not in a git repository.".format(name=l_name,path=l_path)) | ||
| 64 | return | ||
| 65 | repo_path = self._get_repo_path(l_path) | ||
| 66 | if repo_path not in repos.keys(): | ||
| 67 | repos[repo_path] = {'path':os.path.basename(repo_path),'layers':{},'git-remote':{'rev':l_rev, 'branch':l_branch, 'remotes':self._get_remotes(repo_path), 'describe':self._get_describe(repo_path)}} | ||
| 68 | if repo_path == destdir_repo: | ||
| 69 | repos[repo_path]['contains_this_file'] = True | ||
| 70 | if not repos[repo_path]['git-remote']['remotes'] and not repos[repo_path]['contains_this_file']: | ||
| 71 | logger.error("Layer repository in {path} does not have any remotes configured. Please add at least one with 'git remote add'.".format(path=repo_path)) | ||
| 72 | return | ||
| 73 | repos[repo_path]['layers'][l_name] = {'subpath':l_path.replace(repo_path,'')[1:]} | ||
| 74 | |||
| 75 | top_path = os.path.commonpath([os.path.dirname(r) for r in repos.keys()]) | ||
| 76 | |||
| 77 | repos_nopaths = {} | ||
| 78 | for r in repos.keys(): | ||
| 79 | r_nopath = os.path.basename(r) | ||
| 80 | repos_nopaths[r_nopath] = repos[r] | ||
| 81 | r_relpath = os.path.relpath(r, top_path) | ||
| 82 | repos_nopaths[r_nopath]['path'] = r_relpath | ||
| 83 | return repos_nopaths | ||
| 84 | |||
| 85 | def do_make_setup(self, args): | ||
| 86 | """ Writes out a configuration file and/or a script that replicate the directory structure and revisions of the layers in a current build. """ | ||
| 87 | for p in self.plugins: | ||
| 88 | if str(p) == args.writer: | ||
| 89 | p.do_write(self, args) | ||
| 90 | |||
| 91 | def register_commands(self, sp): | ||
| 92 | parser_setup_layers = self.add_command(sp, 'create-layers-setup', self.do_make_setup, parserecipes=False) | ||
| 93 | parser_setup_layers.add_argument('destdir', | ||
| 94 | help='Directory where to write the output\n(if it is inside one of the layers, the layer becomes a bootstrap repository and thus will be excluded from fetching).') | ||
| 95 | parser_setup_layers.add_argument('--output-prefix', '-o', | ||
| 96 | help='File name prefix for the output files, if the default (setup-layers) is undesirable.') | ||
| 97 | |||
| 98 | self.plugins = [] | ||
| 99 | |||
| 100 | for path in (self.tinfoil.config_data.getVar('BBPATH').split(':')): | ||
| 101 | pluginpath = os.path.join(path, 'lib', 'bblayers', 'setupwriters') | ||
| 102 | bb.utils.load_plugins(logger, self.plugins, pluginpath) | ||
| 103 | |||
| 104 | parser_setup_layers.add_argument('--writer', '-w', choices=[str(p) for p in self.plugins], help="Choose the output format (defaults to oe-setup-layers).\n\nCurrently supported options are:\noe-setup-layers - a self-contained python script and a json config for it.\n\n", default="oe-setup-layers") | ||
| 105 | |||
| 106 | for plugin in self.plugins: | ||
| 107 | if hasattr(plugin, 'register_arguments'): | ||
| 108 | plugin.register_arguments(parser_setup_layers) | ||
diff --git a/meta/lib/bblayers/setupwriters/oe-setup-layers.py b/meta/lib/bblayers/setupwriters/oe-setup-layers.py new file mode 100644 index 0000000000..f6a484b766 --- /dev/null +++ b/meta/lib/bblayers/setupwriters/oe-setup-layers.py | |||
| @@ -0,0 +1,50 @@ | |||
| 1 | # | ||
| 2 | # Copyright OpenEmbedded Contributors | ||
| 3 | # | ||
| 4 | # SPDX-License-Identifier: GPL-2.0-only | ||
| 5 | # | ||
| 6 | |||
| 7 | import logging | ||
| 8 | import os | ||
| 9 | import json | ||
| 10 | import stat | ||
| 11 | |||
| 12 | logger = logging.getLogger('bitbake-layers') | ||
| 13 | |||
| 14 | def plugin_init(plugins): | ||
| 15 | return OeSetupLayersWriter() | ||
| 16 | |||
| 17 | class OeSetupLayersWriter(): | ||
| 18 | |||
| 19 | def __str__(self): | ||
| 20 | return "oe-setup-layers" | ||
| 21 | |||
| 22 | def _write_python(self, input, output): | ||
| 23 | with open(input) as f: | ||
| 24 | script = f.read() | ||
| 25 | with open(output, 'w') as f: | ||
| 26 | f.write(script) | ||
| 27 | st = os.stat(output) | ||
| 28 | os.chmod(output, st.st_mode | stat.S_IEXEC | stat.S_IXGRP | stat.S_IXOTH) | ||
| 29 | |||
| 30 | def _write_json(self, repos, output): | ||
| 31 | with open(output, 'w') as f: | ||
| 32 | json.dump(repos, f, sort_keys=True, indent=4) | ||
| 33 | |||
| 34 | def do_write(self, parent, args): | ||
| 35 | """ Writes out a python script and a json config that replicate the directory structure and revisions of the layers in a current build. """ | ||
| 36 | repos = parent.make_repo_config(args.destdir) | ||
| 37 | json = {"version":"1.0","sources":repos} | ||
| 38 | if not repos: | ||
| 39 | raise Exception("Could not determine layer sources") | ||
| 40 | output = args.output_prefix or "setup-layers" | ||
| 41 | output = os.path.join(os.path.abspath(args.destdir),output) | ||
| 42 | self._write_json(json, output + ".json") | ||
| 43 | logger.info('Created {}.json'.format(output)) | ||
| 44 | if not args.json_only: | ||
| 45 | self._write_python(os.path.join(os.path.dirname(__file__),'../../../../scripts/oe-setup-layers'), output) | ||
| 46 | logger.info('Created {}'.format(output)) | ||
| 47 | |||
| 48 | def register_arguments(self, parser): | ||
| 49 | parser.add_argument('--json-only', action='store_true', | ||
| 50 | help='When using the oe-setup-layers writer, write only the layer configuruation in json format. Otherwise, also a copy of scripts/oe-setup-layers (from oe-core or poky) is provided, which is a self contained python script that fetches all the needed layers and sets them to correct revisions using the data from the json.') | ||
