From ee66ee9fe903f1219869c737784bfe43d557059f Mon Sep 17 00:00:00 2001 From: Marek Blaha Date: Fri, 13 Sep 2024 10:58:39 +0200 Subject: [PATCH] doc: Document arch override for API users We have documentation for CLI users (dnf5-forcearch(7)), but it's not clear how to do the same thing through API. This patch fills the gap. --- doc/misc/forcearch.7.rst | 7 ++++++ doc/tutorial/bindings/python3/force_arch.rst | 12 +++++++++ doc/tutorial/bindings/python3/index.rst | 1 + doc/tutorial/force-arch.rst | 14 +++++++++++ doc/tutorial/index.rst | 1 + .../libdnf5/tutorial/session/force_arch.py | 24 ++++++++++++++++++ .../python3/libdnf5/tutorial/test_tutorial.py | 7 ++++++ test/tutorial/session/force_arch.cpp | 25 +++++++++++++++++++ test/tutorial/test_tutorial.cpp | 5 ++++ test/tutorial/test_tutorial.hpp | 2 ++ 10 files changed, 98 insertions(+) create mode 100644 doc/tutorial/bindings/python3/force_arch.rst create mode 100644 doc/tutorial/force-arch.rst create mode 100644 test/python3/libdnf5/tutorial/session/force_arch.py create mode 100644 test/tutorial/session/force_arch.cpp diff --git a/doc/misc/forcearch.7.rst b/doc/misc/forcearch.7.rst index ba6a41fc8..c6ee75005 100644 --- a/doc/misc/forcearch.7.rst +++ b/doc/misc/forcearch.7.rst @@ -41,3 +41,10 @@ Examples ``dnf5 repoquery --forcearch=aarch64 --arch=aarch64`` Query all packages available for the AArch64 architecture. If your system has a different native architecture, then both ``--arch`` and ``--forcearch`` are necessary here. ``--arch`` will filter for only packages with the ``aarch64`` architecture, and ``--forcearch`` sets the "arch" and "basearch" substitution variables to ensure the correct repositories are queried. + + +See Also +======== + + | :ref:`Tutorial to override the system architecture for C++ API users ` + | :ref:`Tutorial to override the system architecture for Python API users ` diff --git a/doc/tutorial/bindings/python3/force_arch.rst b/doc/tutorial/bindings/python3/force_arch.rst new file mode 100644 index 000000000..e2bfb3b05 --- /dev/null +++ b/doc/tutorial/bindings/python3/force_arch.rst @@ -0,0 +1,12 @@ +.. _tutorial-bindings-python3-force-arch-label: + +Override the system architecture +================================ + +For the `dnf5` command-line tool, the +:ref:`--forcearch `, :manpage:`dnf5-forcearch(7)` +option is available. Here's how you can achieve the same effect using the Python API: + +.. literalinclude:: ../../tests/bindings/python3/session/force_arch.py + :language: python + :linenos: diff --git a/doc/tutorial/bindings/python3/index.rst b/doc/tutorial/bindings/python3/index.rst index b1b5e0a21..e8834f1cb 100644 --- a/doc/tutorial/bindings/python3/index.rst +++ b/doc/tutorial/bindings/python3/index.rst @@ -9,3 +9,4 @@ Python3 repos queries transaction + force_arch diff --git a/doc/tutorial/force-arch.rst b/doc/tutorial/force-arch.rst new file mode 100644 index 000000000..264c262a3 --- /dev/null +++ b/doc/tutorial/force-arch.rst @@ -0,0 +1,14 @@ +.. _tutorial-force-arch-label: + +Override the system architecture +================================ + +For the `dnf5` command-line tool, the +:ref:`--forcearch `, :manpage:`dnf5-forcearch(7)` +option is available. Here's how you can achieve the same effect using the API: + + +.. literalinclude:: tests/session/force_arch.cpp + :language: c++ + :linenos: + :lines: 2,4- diff --git a/doc/tutorial/index.rst b/doc/tutorial/index.rst index 833219182..99e454335 100644 --- a/doc/tutorial/index.rst +++ b/doc/tutorial/index.rst @@ -11,3 +11,4 @@ Tutorial repos queries transaction + force-arch diff --git a/test/python3/libdnf5/tutorial/session/force_arch.py b/test/python3/libdnf5/tutorial/session/force_arch.py new file mode 100644 index 000000000..d75dc4242 --- /dev/null +++ b/test/python3/libdnf5/tutorial/session/force_arch.py @@ -0,0 +1,24 @@ +import libdnf5 + +# Create a new Base object +base = libdnf5.base.Base() + +# Optionally load configuration from the config files. +base.load_config() + +# Optionally override the detected system architecture, similar to how the +# `--forcearch=aarch64` switch works in the dnf5 command line tool. +vars = base.get_vars().get() +vars.set("arch", "aarch64") + +# This is sufficient for loading repositories and querying packages using the +# `aarch64` architecture. +# However, if you also want to run a transaction (e.g., you want to modify a +# foreign system from "outside" using `installroot`), you need to set the +# `ignorearch` option to instruct RPM to permit packages that are incompatible +# with the system architecture. +base.get_config().get_ignorearch_option().set(True) + +# The base.setup() call configures the architecture for the solver, so the +# `arch` variable needs to be set beforehand. +base.setup() diff --git a/test/python3/libdnf5/tutorial/test_tutorial.py b/test/python3/libdnf5/tutorial/test_tutorial.py index 3beee9942..558a8d601 100644 --- a/test/python3/libdnf5/tutorial/test_tutorial.py +++ b/test/python3/libdnf5/tutorial/test_tutorial.py @@ -90,3 +90,10 @@ def test_transaction(self): exec(file, {'installroot': self.installroot, 'cachedir': self.cachedir, 'baseurl': self.baseurl}) + + def test_force_arch(self): + file = "" + with open("tutorial/session/force_arch.py", "r") as f: + file += f.read() + + exec(file, {'installroot': self.installroot, 'cachedir': self.cachedir}) diff --git a/test/tutorial/session/force_arch.cpp b/test/tutorial/session/force_arch.cpp new file mode 100644 index 000000000..e1175ed34 --- /dev/null +++ b/test/tutorial/session/force_arch.cpp @@ -0,0 +1,25 @@ +/* includes; won't compile in tests, in the docs we leave out the comment lines to show them +#include +*/ + +// Create a new Base object. +libdnf5::Base base; + +// Optionally load configuration from the config files. +base.load_config(); + +// Optionally override the detected system architecture, similar to how the +// `--forcearch=aarch64` switch works in the dnf5 command line tool. +base.get_vars()->set("arch", "aarch64"); + +// This is sufficient for loading repositories and querying packages using the +// `aarch64` architecture. +// However, if you also want to run a transaction (e.g., you want to modify a +// foreign system from "outside" using `installroot`), you need to set the +// `ignorearch` option to instruct RPM to permit packages that are incompatible +// with the system architecture. +base.get_config().get_ignorearch_option().set(true); + +// The base.setup() call configures the architecture for the solver, so the +// `arch` variable needs to be set beforehand. +base.setup(); diff --git a/test/tutorial/test_tutorial.cpp b/test/tutorial/test_tutorial.cpp index d0348fe2f..e19863c66 100644 --- a/test/tutorial/test_tutorial.cpp +++ b/test/tutorial/test_tutorial.cpp @@ -80,3 +80,8 @@ void TutorialTest::test_transaction() { #include "repo/load_repo.cpp" #include "transaction/transaction.cpp" } + + +void TutorialTest::test_force_arch() { +#include "session/force_arch.cpp" +} diff --git a/test/tutorial/test_tutorial.hpp b/test/tutorial/test_tutorial.hpp index bd273bda7..cdc6ddeea 100644 --- a/test/tutorial/test_tutorial.hpp +++ b/test/tutorial/test_tutorial.hpp @@ -39,6 +39,7 @@ class TutorialTest : public CppUnit::TestCase { CPPUNIT_TEST(test_query); CPPUNIT_TEST(test_transaction); + CPPUNIT_TEST(test_force_arch); CPPUNIT_TEST_SUITE_END(); @@ -51,6 +52,7 @@ class TutorialTest : public CppUnit::TestCase { void test_load_system_repos(); void test_query(); void test_transaction(); + void test_force_arch(); private: std::string baseurl = PROJECT_BINARY_DIR "/test/data/repos-rpm/rpm-repo1/";