blob: 22f89d81a416169bc4d8d5195ddbe3d31d52e505 [file] [log] [blame]
Patrick Williams92b42cb2022-09-03 06:53:57 -05001#
2# Copyright OpenEmbedded Contributors
3#
4# SPDX-License-Identifier: GPL-2.0-only
5#
6
7import logging
8import os
9import stat
10import sys
11import shutil
12
13import bb.utils
14import bb.process
15
16from bblayers.common import LayerPlugin
17
18logger = logging.getLogger('bitbake-layers')
19
20sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
21
22import oe.buildcfg
23
24def plugin_init(plugins):
25 return MakeSetupPlugin()
26
27class 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),'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
74 top_path = os.path.commonpath([os.path.dirname(r) for r in repos.keys()])
75
76 repos_nopaths = {}
77 for r in repos.keys():
78 r_nopath = os.path.basename(r)
79 repos_nopaths[r_nopath] = repos[r]
80 r_relpath = os.path.relpath(r, top_path)
81 repos_nopaths[r_nopath]['path'] = r_relpath
82 return repos_nopaths
83
84 def do_make_setup(self, args):
85 """ Writes out a configuration file and/or a script that replicate the directory structure and revisions of the layers in a current build. """
86 for p in self.plugins:
87 if str(p) == args.writer:
88 p.do_write(self, args)
89
90 def register_commands(self, sp):
91 parser_setup_layers = self.add_command(sp, 'create-layers-setup', self.do_make_setup, parserecipes=False)
92 parser_setup_layers.add_argument('destdir',
93 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).')
94 parser_setup_layers.add_argument('--output-prefix', '-o',
95 help='File name prefix for the output files, if the default (setup-layers) is undesirable.')
96
97 self.plugins = []
98
99 for path in (self.tinfoil.config_data.getVar('BBPATH').split(':')):
100 pluginpath = os.path.join(path, 'lib', 'bblayers', 'setupwriters')
101 bb.utils.load_plugins(logger, self.plugins, pluginpath)
102
103 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")
104
105 for plugin in self.plugins:
106 if hasattr(plugin, 'register_arguments'):
107 plugin.register_arguments(parser_setup_layers)