Skip to content

Commit

Permalink
lib: Expose new API around basearch
Browse files Browse the repository at this point in the history
For https://pagure.io/atomic-wg/issue/299 we need to make it
more convenient to substitute the architecture in an installation
context.  I plan to use this API inside `rpmostreepayload` in Anaconda,
so we can substitute the same value of `${basearch}` we use in treefiles
since #305

Now, you might wonder - why do we need an API wrapping libdnf? It's because
libdnf is not API stable yet. We're just exposing a tiny subset. In theory we
could use the Python dnf bindings in Anaconda, but things get slightly weird if
rpmostreepayload depends on dnf. Perhaps we'll do that down the road, but for
now this a small API surface to maintain (forever).

This change reworks the internal `varsubst` bits to take a pure `DnfContext`,
since we don't want to spin up a whole `RpmOstreeContext` just to do some
string substitutions.

Closes: #877
Approved by: jlebon
  • Loading branch information
cgwalters authored and rh-atomic-bot committed Jul 21, 2017
1 parent b46fc35 commit fee6d06
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 7 deletions.
1 change: 1 addition & 0 deletions Makefile-lib.am
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ librpmostreeincludedir = $(includedir)/rpm-ostree-1
librpmostreeinclude_HEADERS = $(librpmostree_public_headers)

librpmostree_1_la_SOURCES = \
src/lib/rpmostree.c \
src/lib/rpmostree-db.c \
src/lib/rpmostree-package.c \
$(NULL)
Expand Down
3 changes: 2 additions & 1 deletion Makefile-tests.am
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ AM_TESTS_ENVIRONMENT = \
# we consume libdnf as a submodule, but we may not have installed it yet (and we
# don't want it to fall back to the system libhif if it's also installed)
AM_TESTS_ENVIRONMENT += \
LD_LIBRARY_PATH=$(abs_builddir)/libdnf-build/libdnf \
LD_LIBRARY_PATH=$(abs_builddir)/libdnf-build/libdnf:$$(cd $(top_builddir)/.libs && pwd)$${LD_LIBRARY_PATH:+:$${LD_LIBRARY_PATH}} \
GI_TYPELIB_PATH=$$(cd $(top_builddir) && pwd)$${GI_TYPELIB_PATH:+:$$GI_TYPELIB_PATH} \
$(NULL)
if BUILDOPT_ASAN
AM_TESTS_ENVIRONMENT += ASAN_OPTIONS=detect_leaks=false
Expand Down
6 changes: 6 additions & 0 deletions api-doc/rpmostree-sections.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
<SECTION>
<FILE>librpmostree-core</FILE>
rpm_ostree_get_basearch
rpm_ostree_varsubst_basearch
</SECTION>

<SECTION>
<FILE>librpmostree-dbquery</FILE>
rpm_ostree_db_query
Expand Down
4 changes: 3 additions & 1 deletion ci/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ fi

install_builddeps rpm-ostree

yum install -y /usr/bin/g-ir-scanner # Accidentally omitted
# Mostly dependencies for tests
yum install -y ostree{,-devel,-grub2} createrepo_c /usr/bin/jq PyYAML clang \
libubsan libasan libtsan elfutils fuse sudo
libubsan libasan libtsan elfutils fuse sudo python-gobject-base

# create an unprivileged user for testing
adduser testuser
Expand Down
1 change: 1 addition & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ echo "

nts name: $enable_new_name
compose tooling: $enable_compose_tooling
introspection: $found_introspection
bubblewrap: $with_bubblewrap
gtk-doc: $enable_gtk_doc
"
2 changes: 1 addition & 1 deletion src/app/rpmostree-compose-builtin-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ rpmostree_compose_builtin_tree (int argc,
if (!corectx)
goto out;

varsubsts = rpmostree_context_get_varsubsts (corectx);
varsubsts = rpmostree_dnfcontext_get_varsubsts (rpmostree_context_get_hif (corectx));

treefile_parser = json_parser_new ();
if (!json_parser_load_from_file (treefile_parser,
Expand Down
64 changes: 64 additions & 0 deletions src/lib/rpmostree.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
*
* Copyright (C) 2017 Colin Walters <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation; either version 2 of the licence or (at
* your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*/

#include "config.h"

#include "string.h"

#include "rpmostree.h"
#include "rpmostree-core.h"
#include "rpmostree-util.h"

/**
* SECTION:librpmostree
* @title: Global high level APIs
* @short_description: APIs for accessing global state
*
* These APIs access generic global state.
*/

/**
* rpm_ostree_get_basearch:
*
* Returns: A string for RPM's architecture, commonly used for e.g. $basearch in URLs
* Since: 2017.8
*/
char *
rpm_ostree_get_basearch (void)
{
g_autoptr(DnfContext) ctx = dnf_context_new ();
/* Need to strdup since we unref the context */
return g_strdup (dnf_context_get_base_arch (ctx));
}

/**
* rpm_ostree_varsubst_basearch:
* @src: String (commonly a URL)
*
* Returns: A copy of @src with all references for `${basearch}` replaced with `rpmostree_get_basearch()`, or %NULL on error
* Since: 2017.8
*/
char *
rpm_ostree_varsubst_basearch (const char *src, GError **error)
{
g_autoptr(DnfContext) ctx = dnf_context_new ();
g_autoptr(GHashTable) varsubsts = rpmostree_dnfcontext_get_varsubsts (ctx);
return _rpmostree_varsubst_string (src, varsubsts, error);
}
10 changes: 10 additions & 0 deletions src/lib/rpmostree.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,13 @@

#include <rpmostree-db.h>
#include <rpmostree-package.h>

G_BEGIN_DECLS

_RPMOSTREE_EXTERN
char *rpm_ostree_get_basearch (void);

_RPMOSTREE_EXTERN
char *rpm_ostree_varsubst_basearch (const char *src, GError **error);

G_END_DECLS
4 changes: 2 additions & 2 deletions src/libpriv/rpmostree-core.c
Original file line number Diff line number Diff line change
Expand Up @@ -481,11 +481,11 @@ rpmostree_context_get_hif (RpmOstreeContext *self)
}

GHashTable *
rpmostree_context_get_varsubsts (RpmOstreeContext *context)
rpmostree_dnfcontext_get_varsubsts (DnfContext *context)
{
GHashTable *r = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);

g_hash_table_insert (r, g_strdup ("basearch"), g_strdup (dnf_context_get_base_arch (context->hifctx)));
g_hash_table_insert (r, g_strdup ("basearch"), g_strdup (dnf_context_get_base_arch (context)));

return r;
}
Expand Down
2 changes: 1 addition & 1 deletion src/libpriv/rpmostree-core.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ RpmOstreeTreespec *rpmostree_treespec_new_from_keyfile (GKeyFile *keyfile, GErro
RpmOstreeTreespec *rpmostree_treespec_new_from_path (const char *path, GError **error);
RpmOstreeTreespec *rpmostree_treespec_new (GVariant *variant);

GHashTable *rpmostree_context_get_varsubsts (RpmOstreeContext *context);
GHashTable *rpmostree_dnfcontext_get_varsubsts (DnfContext *context);

GVariant *rpmostree_treespec_to_variant (RpmOstreeTreespec *spec);
const char *rpmostree_treespec_get_ref (RpmOstreeTreespec *spec);
Expand Down
17 changes: 16 additions & 1 deletion tests/check/test-basic.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export RPMOSTREE_SUPPRESS_REQUIRES_ROOT_CHECK=yes

ensure_dbus

echo "1..21"
echo "1..22"

setup_os_repository "archive-z2" "syslinux"

Expand Down Expand Up @@ -181,3 +181,18 @@ if rpm-ostree nosuchcommand --nosuchoption 2>err.txt; then
fi
assert_file_has_content err.txt 'Unknown.*command'
echo "ok error on unknown command"

cat >test-rpmostree-gi <<EOF
#!/usr/bin/python2
import gi
gi.require_version("RpmOstree", "1.0")
from gi.repository import RpmOstree
assert RpmOstree.get_basearch() == 'x86_64'
assert RpmOstree.varsubst_basearch('http://example.com/foo/\${basearch}/bar') == 'http://example.com/foo/x86_64/bar'
EOF
chmod a+x test-rpmostree-gi
case $(arch) in
x86_64) ./test-rpmostree-gi;;
*) echo "Skipping RPM architecture test on $(arch)"
esac
echo "ok rpmostree arch"

0 comments on commit fee6d06

Please sign in to comment.