blob: 4f27c565eeb41c91a6182b6fa9f255097963a2e3 [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
Patrick Williams92b42cb2022-09-03 06:53:57 -05009import sys
Patrick Williams92b42cb2022-09-03 06:53:57 -050010
11import bb.utils
12import bb.process
13
14from bblayers.common import LayerPlugin
15
16logger = logging.getLogger('bitbake-layers')
17
18sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
19
20import oe.buildcfg
21
22def plugin_init(plugins):
23 return MakeSetupPlugin()
24
25class MakeSetupPlugin(LayerPlugin):
26
27 def _get_repo_path(self, layer_path):
28 repo_path, _ = bb.process.run('git rev-parse --show-toplevel', cwd=layer_path)
29 return repo_path.strip()
30
31 def _get_remotes(self, repo_path):
32 remotes = {}
33 remotes_list,_ = bb.process.run('git remote', cwd=repo_path)
34 for r in remotes_list.split():
35 uri,_ = bb.process.run('git remote get-url {r}'.format(r=r), cwd=repo_path)
36 remotes[r] = {'uri':uri.strip()}
37 return remotes
38
39 def _get_describe(self, repo_path):
40 try:
41 describe,_ = bb.process.run('git describe --tags', cwd=repo_path)
42 except bb.process.ExecutionError:
43 return ""
44 return describe.strip()
45
Andrew Geissler6aa7eec2023-03-03 12:41:14 -060046 def _is_submodule(self, repo_path):
47 # This is slightly brittle: git does not offer a way to tell whether
48 # a given repo dir is a submodule checkout, so we need to rely on .git
49 # being a file (rather than a dir like it is in standalone checkouts).
50 # The file typically contains a gitdir pointer to elsewhere.
51 return os.path.isfile(os.path.join(repo_path,".git"))
52
Patrick Williams92b42cb2022-09-03 06:53:57 -050053 def make_repo_config(self, destdir):
Andrew Geissler517393d2023-01-13 08:55:19 -060054 """ This is a helper function for the writer plugins that discovers currently configured layers.
Patrick Williams92b42cb2022-09-03 06:53:57 -050055 The writers do not have to use it, but it can save a bit of work and avoid duplicated code, hence it is
56 available here. """
57 repos = {}
58 layers = oe.buildcfg.get_layer_revisions(self.tinfoil.config_data)
59 try:
60 destdir_repo = self._get_repo_path(destdir)
61 except bb.process.ExecutionError:
62 destdir_repo = None
63
64 for (l_path, l_name, l_branch, l_rev, l_ismodified) in layers:
65 if l_name == 'workspace':
66 continue
67 if l_ismodified:
68 logger.error("Layer {name} in {path} has uncommitted modifications or is not in a git repository.".format(name=l_name,path=l_path))
69 return
70 repo_path = self._get_repo_path(l_path)
Andrew Geissler6aa7eec2023-03-03 12:41:14 -060071
72 if self._is_submodule(repo_path):
73 continue
Patrick Williams92b42cb2022-09-03 06:53:57 -050074 if repo_path not in repos.keys():
75 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)}}
76 if repo_path == destdir_repo:
77 repos[repo_path]['contains_this_file'] = True
78 if not repos[repo_path]['git-remote']['remotes'] and not repos[repo_path]['contains_this_file']:
79 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))
80 return
81
82 top_path = os.path.commonpath([os.path.dirname(r) for r in repos.keys()])
83
84 repos_nopaths = {}
85 for r in repos.keys():
86 r_nopath = os.path.basename(r)
87 repos_nopaths[r_nopath] = repos[r]
88 r_relpath = os.path.relpath(r, top_path)
89 repos_nopaths[r_nopath]['path'] = r_relpath
90 return repos_nopaths
91
92 def do_make_setup(self, args):
93 """ Writes out a configuration file and/or a script that replicate the directory structure and revisions of the layers in a current build. """
94 for p in self.plugins:
95 if str(p) == args.writer:
96 p.do_write(self, args)
97
98 def register_commands(self, sp):
99 parser_setup_layers = self.add_command(sp, 'create-layers-setup', self.do_make_setup, parserecipes=False)
100 parser_setup_layers.add_argument('destdir',
101 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).')
102 parser_setup_layers.add_argument('--output-prefix', '-o',
103 help='File name prefix for the output files, if the default (setup-layers) is undesirable.')
104
105 self.plugins = []
106
107 for path in (self.tinfoil.config_data.getVar('BBPATH').split(':')):
108 pluginpath = os.path.join(path, 'lib', 'bblayers', 'setupwriters')
109 bb.utils.load_plugins(logger, self.plugins, pluginpath)
110
111 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")
112
113 for plugin in self.plugins:
114 if hasattr(plugin, 'register_arguments'):
115 plugin.register_arguments(parser_setup_layers)