From: Alexander Kanavin <alex.kanavin@gmail.com>
To: openembedded-core@lists.openembedded.org
Cc: Alexander Kanavin <alex@linutronix.de>
Subject: [PATCH v3 3/3] bitbake-config-build: add a plugin for config fragments
Date: Mon, 18 Nov 2024 17:26:43 +0100 [thread overview]
Message-ID: <20241118162643.1423409-3-alex.kanavin@gmail.com> (raw)
In-Reply-To: <20241118162643.1423409-1-alex.kanavin@gmail.com>
From: Alexander Kanavin <alex@linutronix.de>
This allows fine-tuning local configurations with pre-frabricated
configuration snippets in a structured, controlled way. It's also
an important building block for bitbake-setup.
There are three (and a half) operations (list/enable/disable/disable all), and here's the 'list' output:
alex@Zen2:/srv/storage/alex/yocto/build-64$ bitbake-config-build list-fragments
NOTE: Starting bitbake server...
Available fragments in selftest layer located in /srv/work/alex/poky/meta-selftest:
selftest/test-fragment (disabled) This is a configuration fragment intended for testing in oe-selftest context
selftest/more-fragments-here/test-another-fragment (disabled) This is a second configuration fragment intended for testing in oe-selftest context
The tool requires that each fragment contains a one-line summary, and one or more
lines of description, as BB_CONF_FRAGMENT_SUMMARY[layerid/fragmentname] style metadata.
Signed-off-by: Alexander Kanavin <alex@linutronix.de>
---
.../test-another-fragment.conf | 3 +
.../conf/fragments/test-fragment.conf | 3 +
meta/lib/bbconfigbuild/configfragments.py | 147 ++++++++++++++++++
meta/lib/oeqa/selftest/cases/bblayers.py | 31 ++++
4 files changed, 184 insertions(+)
create mode 100644 meta-selftest/conf/fragments/more-fragments-here/test-another-fragment.conf
create mode 100644 meta-selftest/conf/fragments/test-fragment.conf
create mode 100644 meta/lib/bbconfigbuild/configfragments.py
diff --git a/meta-selftest/conf/fragments/more-fragments-here/test-another-fragment.conf b/meta-selftest/conf/fragments/more-fragments-here/test-another-fragment.conf
new file mode 100644
index 00000000000..cf9ba6a6132
--- /dev/null
+++ b/meta-selftest/conf/fragments/more-fragments-here/test-another-fragment.conf
@@ -0,0 +1,3 @@
+BB_CONF_FRAGMENT_SUMMARY[selftest/more-fragments-here/test-another-fragment] = "This is a second configuration fragment intended for testing in oe-selftest context"
+BB_CONF_FRAGMENT_DESCRIPTION[selftest/more-fragments-here/test-another-fragment] = "It defines another variable that can be checked inside the test."
+SELFTEST_FRAGMENT_ANOTHER_VARIABLE = "someothervalue"
diff --git a/meta-selftest/conf/fragments/test-fragment.conf b/meta-selftest/conf/fragments/test-fragment.conf
new file mode 100644
index 00000000000..63ebc1fca68
--- /dev/null
+++ b/meta-selftest/conf/fragments/test-fragment.conf
@@ -0,0 +1,3 @@
+BB_CONF_FRAGMENT_SUMMARY[selftest/test-fragment] = "This is a configuration fragment intended for testing in oe-selftest context"
+BB_CONF_FRAGMENT_DESCRIPTION[selftest/test-fragment] = "It defines a variable that can be checked inside the test."
+SELFTEST_FRAGMENT_VARIABLE = "somevalue"
diff --git a/meta/lib/bbconfigbuild/configfragments.py b/meta/lib/bbconfigbuild/configfragments.py
new file mode 100644
index 00000000000..39031b43435
--- /dev/null
+++ b/meta/lib/bbconfigbuild/configfragments.py
@@ -0,0 +1,147 @@
+#
+# Copyright OpenEmbedded Contributors
+#
+# SPDX-License-Identifier: GPL-2.0-only
+#
+
+import logging
+import os
+import sys
+import os.path
+
+import bb.utils
+
+from bblayers.common import LayerPlugin
+
+logger = logging.getLogger('bitbake-config-layers')
+
+sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
+
+def plugin_init(plugins):
+ return ConfigFragmentsPlugin()
+
+class ConfigFragmentsPlugin(LayerPlugin):
+ def get_fragment_info(self, path, name):
+ d = bb.data.init()
+ bb.parse.handle(path, d, True)
+ summary = d.getVarFlag('BB_CONF_FRAGMENT_SUMMARY', name)
+ description = d.getVarFlag('BB_CONF_FRAGMENT_DESCRIPTION', name)
+ if not summary:
+ raise Exception('Please add a one-line summary as BB_CONF_FRAGMENT_SUMMARY[{}] = \"...\" variable at the beginning of {}'.format(name, path))
+
+ if not description:
+ raise Exception('Please add a description as BB_CONF_FRAGMENT_DESCRIPTION[{}] = \"...\" variable at the beginning of {}'.format(name, path))
+
+ return summary, description
+
+ def discover_fragments(self):
+ fragments_path_prefix = self.tinfoil.config_data.getVar('OE_FRAGMENTS_PREFIX')
+ allfragments = {}
+ for layername in self.bbfile_collections:
+ layerdir = self.bbfile_collections[layername]
+ fragments = []
+ for topdir, dirs, files in os.walk(os.path.join(layerdir, fragments_path_prefix)):
+ fragmentdir = os.path.relpath(topdir, os.path.join(layerdir, fragments_path_prefix))
+ for fragmentfile in sorted(files):
+ fragmentname = os.path.normpath("/".join((layername, fragmentdir, fragmentfile.split('.')[0])))
+ fragmentpath = os.path.join(topdir, fragmentfile)
+ fragmentsummary, fragmentdesc = self.get_fragment_info(fragmentpath, fragmentname)
+ fragments.append({'path':fragmentpath, 'name':fragmentname, 'summary':fragmentsummary, 'description':fragmentdesc})
+ if fragments:
+ allfragments[layername] = {'layerdir':layerdir,'fragments':fragments}
+ return allfragments
+
+ def do_list_fragments(self, args):
+ """ List available configuration fragments """
+ enabled_fragments = (self.tinfoil.config_data.getVar('OE_FRAGMENTS') or "").split()
+
+ for layername, layerdata in self.discover_fragments().items():
+ layerdir = layerdata['layerdir']
+ fragments = layerdata['fragments']
+
+ print('Available fragments in {} layer located in {}:\n'.format(layername, layerdir))
+ for f in fragments:
+ if not args.verbose:
+ print('{}\t{}\t{}'.format(f['name'], '(enabled)' if f['name'] in enabled_fragments else '(disabled)', f['summary']))
+ else:
+ print('Name: {}\nPath: {}\nEnabled: {}\nSummary: {}\nDescription:\n{}\n'.format(f['name'], f['path'], 'yes' if f['name'] in enabled_fragments else 'no', f['summary'],''.join(f['description'])))
+ print('')
+
+ def fragment_exists(self, fragmentname):
+ for layername, layerdata in self.discover_fragments().items():
+ for f in layerdata['fragments']:
+ if f['name'] == fragmentname:
+ return True
+ return False
+
+ def create_conf(self, confpath):
+ if not os.path.exists(confpath):
+ with open(confpath, 'w') as f:
+ f.write('')
+ with open(confpath, 'r') as f:
+ lines = f.read()
+ if "OE_FRAGMENTS += " not in lines:
+ lines += "\nOE_FRAGMENTS += \"\"\n"
+ with open(confpath, 'w') as f:
+ f.write(lines)
+
+ def do_enable_fragment(self, args):
+ """ Enable a fragment in the local build configuration """
+ def enable_helper(varname, origvalue, op, newlines):
+ enabled_fragments = origvalue.split()
+ if args.fragmentname in enabled_fragments:
+ print("Fragment {} already included in {}".format(args.fragmentname, args.confpath))
+ else:
+ enabled_fragments.append(args.fragmentname)
+ return " ".join(enabled_fragments), None, 0, True
+
+ if not self.fragment_exists(args.fragmentname):
+ raise Exception("Fragment {} does not exist; use 'list-fragments' to see the full list.".format(args.fragmentname))
+
+ self.create_conf(args.confpath)
+ modified = bb.utils.edit_metadata_file(args.confpath, ["OE_FRAGMENTS"], enable_helper)
+ if modified:
+ print("Fragment {} added to {}.".format(args.fragmentname, args.confpath))
+
+ def do_disable_fragment(self, args):
+ """ Disable a fragment in the local build configuration """
+ def disable_helper(varname, origvalue, op, newlines):
+ enabled_fragments = origvalue.split()
+ if args.fragmentname in enabled_fragments:
+ enabled_fragments.remove(args.fragmentname)
+ else:
+ print("Fragment {} not currently enabled in {}".format(args.fragmentname, args.confpath))
+ return " ".join(enabled_fragments), None, 0, True
+
+ self.create_conf(args.confpath)
+ modified = bb.utils.edit_metadata_file(args.confpath, ["OE_FRAGMENTS"], disable_helper)
+ if modified:
+ print("Fragment {} removed from {}.".format(args.fragmentname, args.confpath))
+
+ def do_disable_all_fragments(self, args):
+ """ Disable all fragments in the local build configuration """
+ def disable_all_helper(varname, origvalue, op, newlines):
+ return "", None, 0, True
+
+ self.create_conf(args.confpath)
+ modified = bb.utils.edit_metadata_file(args.confpath, ["OE_FRAGMENTS"], disable_all_helper)
+ if modified:
+ print("All fragments removed from {}.".format(args.confpath))
+
+ def register_commands(self, sp):
+ default_confpath = os.path.join(os.environ["BBPATH"], "conf/auto.conf")
+
+ parser_list_fragments = self.add_command(sp, 'list-fragments', self.do_list_fragments, parserecipes=False)
+ parser_list_fragments.add_argument("--confpath", default=default_confpath, help='Configuration file which contains a list of enabled fragments (default is {}).'.format(default_confpath))
+ parser_list_fragments.add_argument('--verbose', '-v', action='store_true', help='Print extended descriptions of the fragments')
+
+ parser_enable_fragment = self.add_command(sp, 'enable-fragment', self.do_enable_fragment, parserecipes=False)
+ parser_enable_fragment.add_argument("--confpath", default=default_confpath, help='Configuration file which contains a list of enabled fragments (default is {}).'.format(default_confpath))
+ parser_enable_fragment.add_argument('fragmentname', help='The name of the fragment (use list-fragments to see them)')
+
+ parser_disable_fragment = self.add_command(sp, 'disable-fragment', self.do_disable_fragment, parserecipes=False)
+ parser_disable_fragment.add_argument("--confpath", default=default_confpath, help='Configuration file which contains a list of enabled fragments (default is {}).'.format(default_confpath))
+ parser_disable_fragment.add_argument('fragmentname', help='The name of the fragment')
+
+ parser_disable_all = self.add_command(sp, 'disable-all-fragments', self.do_disable_all_fragments, parserecipes=False)
+ parser_disable_all.add_argument("--confpath", default=default_confpath, help='Configuration file which contains a list of enabled fragments (default is {}).'.format(default_confpath))
diff --git a/meta/lib/oeqa/selftest/cases/bblayers.py b/meta/lib/oeqa/selftest/cases/bblayers.py
index 695d17377d4..68b03777201 100644
--- a/meta/lib/oeqa/selftest/cases/bblayers.py
+++ b/meta/lib/oeqa/selftest/cases/bblayers.py
@@ -240,3 +240,34 @@ class BitbakeLayers(OESelftestTestCase):
self.assertEqual(first_desc_2, '', "Describe not cleared: '{}'".format(first_desc_2))
self.assertEqual(second_rev_2, second_rev_1, "Revision should not be updated: '{}'".format(second_rev_2))
self.assertEqual(second_desc_2, second_desc_1, "Describe should not be updated: '{}'".format(second_desc_2))
+
+class BitbakeConfigBuild(OESelftestTestCase):
+ def test_enable_disable_fragments(self):
+ self.assertEqual(get_bb_var('SELFTEST_FRAGMENT_VARIABLE'), None)
+ self.assertEqual(get_bb_var('SELFTEST_FRAGMENT_ANOTHER_VARIABLE'), None)
+
+ runCmd('bitbake-config-build enable-fragment selftest/test-fragment')
+ self.assertEqual(get_bb_var('SELFTEST_FRAGMENT_VARIABLE'), 'somevalue')
+ self.assertEqual(get_bb_var('SELFTEST_FRAGMENT_ANOTHER_VARIABLE'), None)
+
+ runCmd('bitbake-config-build enable-fragment selftest/more-fragments-here/test-another-fragment')
+ self.assertEqual(get_bb_var('SELFTEST_FRAGMENT_VARIABLE'), 'somevalue')
+ self.assertEqual(get_bb_var('SELFTEST_FRAGMENT_ANOTHER_VARIABLE'), 'someothervalue')
+
+ fragment_metadata_command = "bitbake-getvar -f {} --value {}"
+ result = runCmd(fragment_metadata_command.format("selftest/test-fragment", "BB_CONF_FRAGMENT_SUMMARY"))
+ self.assertIn("This is a configuration fragment intended for testing in oe-selftest context", result.output)
+ result = runCmd(fragment_metadata_command.format("selftest/test-fragment", "BB_CONF_FRAGMENT_DESCRIPTION"))
+ self.assertIn("It defines a variable that can be checked inside the test.", result.output)
+ result = runCmd(fragment_metadata_command.format("selftest/more-fragments-here/test-another-fragment", "BB_CONF_FRAGMENT_SUMMARY"))
+ self.assertIn("This is a second configuration fragment intended for testing in oe-selftest context", result.output)
+ result = runCmd(fragment_metadata_command.format("selftest/more-fragments-here/test-another-fragment", "BB_CONF_FRAGMENT_DESCRIPTION"))
+ self.assertIn("It defines another variable that can be checked inside the test.", result.output)
+
+ runCmd('bitbake-config-build disable-fragment selftest/test-fragment')
+ self.assertEqual(get_bb_var('SELFTEST_FRAGMENT_VARIABLE'), None)
+ self.assertEqual(get_bb_var('SELFTEST_FRAGMENT_ANOTHER_VARIABLE'), 'someothervalue')
+
+ runCmd('bitbake-config-build disable-fragment selftest/more-fragments-here/test-another-fragment')
+ self.assertEqual(get_bb_var('SELFTEST_FRAGMENT_VARIABLE'), None)
+ self.assertEqual(get_bb_var('SELFTEST_FRAGMENT_ANOTHER_VARIABLE'), None)
--
2.39.5
next prev parent reply other threads:[~2024-11-18 16:27 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-11-18 16:26 [PATCH v3 1/3] patchtest: use HEAD commit as base for the selftest and not master Alexander Kanavin
2024-11-18 16:26 ` [PATCH v3 2/3] bitbake.conf: add an addfragments directive for oe-core and dependent layers Alexander Kanavin
2024-11-18 16:26 ` Alexander Kanavin [this message]
2024-11-18 16:32 ` Patchtest results for [PATCH v3 3/3] bitbake-config-build: add a plugin for config fragments patchtest
2024-12-09 16:33 ` [OE-core] " Richard Purdie
2024-12-10 12:22 ` Alexander Kanavin
2024-12-09 17:00 ` Richard Purdie
2024-12-10 13:05 ` Alexander Kanavin
2024-12-10 15:27 ` Richard Purdie
2024-12-11 11:24 ` Alexander Kanavin
2024-12-16 16:58 ` Richard Purdie
2024-12-20 17:39 ` Joshua Watt
2024-12-25 15:29 ` Alexander Kanavin
2024-12-25 15:33 ` Joshua Watt
2024-12-27 18:12 ` Joshua Watt
2024-12-27 18:44 ` Alexander Kanavin
2024-12-27 19:47 ` Joshua Watt
2024-11-18 16:33 ` [OE-core] [PATCH v3 1/3] patchtest: use HEAD commit as base for the selftest and not master Trevor Gamblin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20241118162643.1423409-3-alex.kanavin@gmail.com \
--to=alex.kanavin@gmail.com \
--cc=alex@linutronix.de \
--cc=openembedded-core@lists.openembedded.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox