Skip to content

Commit

Permalink
Add Module.remove_streams_by_NSVCA()
Browse files Browse the repository at this point in the history
Add functionality to remove streams matching certain search
criteria from a ModulemdModule object. This will be useful for
createrepo, which will use this to trim input modulemd down to
only the content present in the repository.

Signed-off-by: Stephen Gallagher <sgallagh@redhat.com>
  • Loading branch information
sgallagher committed Apr 22, 2019
1 parent 86a7644 commit 5bf0685
Show file tree
Hide file tree
Showing 3 changed files with 213 additions and 0 deletions.
38 changes: 38 additions & 0 deletions modulemd/v2/include/modulemd-2.0/modulemd-module.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,44 @@ modulemd_module_get_stream_by_NSVCA (ModulemdModule *self,
GError **error);


/**
* modulemd_module_remove_streams_by_NSVCA:
* @self: This #ModulemdModule object
* @stream_name: (not nullable): The name of the stream to remove
* @version: The version of the stream to remove. If set to zero, matches all
* versions.
* @context: (nullable): The context of the stream to remove. If NULL, matches
* all stream contexts.
* @arch: (nullable): The processor architecture of the stream to remove. If
* NULL, matches all architectures.
*
* Remove one or more #ModulemdModuleStream objects from this #ModulemdModule
* that match the provided parameters.
*
* Since: 2.3
*/
void
modulemd_module_remove_streams_by_NSVCA (ModulemdModule *self,
const gchar *stream_name,
const guint64 version,
const gchar *context,
const gchar *arch);


/**
* modulemd_module_remove_streams_by_name:
* @self: This #ModulemdModule object
* @stream_name: (not nullable): The name of the stream to remove
*
* Remove one or more #ModulemdModuleStream objects from this #ModulemdModule
* that match the provided stream name.
*
* Since: 2.3
*/
#define modulemd_module_remove_streams_by_name(self, stream_name) \
modulemd_module_remove_streams_by_NSVCA (self, stream_name, 0, NULL, NULL)


/**
* modulemd_module_get_defaults:
* @self: This #ModulemdModule object
Expand Down
85 changes: 85 additions & 0 deletions modulemd/v2/modulemd-module.c
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,91 @@ modulemd_module_get_stream_by_NSVCA (ModulemdModule *self,
}


typedef struct _modulemd_nsvca
{
const gchar *stream_name;
guint64 version;
const gchar *context;
const gchar *arch;
} modulemd_nsvca;


static void
modulemd_nsvca_free (gpointer nsvca)
{
/* All of the fields are just pointers to static data,
* so nothing to free here.
*/

g_free ((modulemd_nsvca *)nsvca);
}

G_DEFINE_AUTOPTR_CLEANUP_FUNC (modulemd_nsvca, modulemd_nsvca_free);


static gboolean
match_nsvca (gconstpointer haystraw, gconstpointer needle)
{
ModulemdModuleStream *stream = (ModulemdModuleStream *)haystraw;
modulemd_nsvca *nsvca = (modulemd_nsvca *)needle;

if (!g_str_equal (nsvca->stream_name,
modulemd_module_stream_get_stream_name (stream)))
return FALSE;

if (nsvca->version)
{
if (nsvca->version != modulemd_module_stream_get_version (stream))
return FALSE;
}

if (nsvca->context)
{
if (!g_str_equal (nsvca->context,
modulemd_module_stream_get_context (stream)))
return FALSE;
}

if (nsvca->arch)
{
if (!g_str_equal (nsvca->arch, modulemd_module_stream_get_arch (stream)))
return FALSE;
}

return TRUE;
}


void
modulemd_module_remove_streams_by_NSVCA (ModulemdModule *self,
const gchar *stream_name,
const guint64 version,
const gchar *context,
const gchar *arch)
{
gboolean found = FALSE;
guint index;
g_autoptr (modulemd_nsvca) nsvca = g_malloc0_n (1, sizeof (modulemd_nsvca));

nsvca->stream_name = stream_name;
nsvca->version = version;
nsvca->context = context;
nsvca->arch = arch;

/* Iterate through the streams and remove any that match the requested
* parameters
*/
do
{
found = g_ptr_array_find_with_equal_func (
self->streams, nsvca, match_nsvca, &index);
if (found)
g_ptr_array_remove_index (self->streams, index);
}
while (found);
}


void
modulemd_module_add_translation (ModulemdModule *self,
ModulemdTranslation *translation)
Expand Down
90 changes: 90 additions & 0 deletions modulemd/v2/tests/test-modulemd-module.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

#include "modulemd-defaults.h"
#include "modulemd-module.h"
#include "modulemd-module-index.h"
#include "modulemd-module-index-merger.h"
#include "modulemd-module-stream.h"
#include "modulemd-module-stream-v2.h"
#include "private/glib-extensions.h"
Expand Down Expand Up @@ -328,6 +330,91 @@ module_test_streams (ModuleFixture *fixture, gconstpointer user_data)
}


static void
modulemd_test_remove_streams (void)
{
g_autoptr (ModulemdModuleIndex) f29 = NULL;
g_autoptr (ModulemdModuleIndex) f29_updates = NULL;
g_autoptr (ModulemdModuleIndex) index = NULL;
g_autoptr (ModulemdModuleIndexMerger) merger = NULL;
ModulemdModule *nodejs_module = NULL;
g_autoptr (GError) error = NULL;
g_autoptr (GPtrArray) failures = NULL;
g_autofree gchar *yaml_path = NULL;

/* Get the f29 and f29-updates indexes. They have multiple streams and
* versions for the 'dwm' module
*/
f29 = modulemd_module_index_new ();
yaml_path = g_strdup_printf ("%s/modulemd/v2/tests/test_data/f29.yaml",
g_getenv ("MESON_SOURCE_ROOT"));
g_assert_true (modulemd_module_index_update_from_file (
f29, yaml_path, TRUE, &failures, &error));
g_assert_no_error (error);
g_assert_cmpint (failures->len, ==, 0);
g_clear_pointer (&yaml_path, g_free);
g_clear_pointer (&failures, g_ptr_array_unref);

f29_updates = modulemd_module_index_new ();
yaml_path =
g_strdup_printf ("%s/modulemd/v2/tests/test_data/f29-updates.yaml",
g_getenv ("MESON_SOURCE_ROOT"));
g_assert_true (modulemd_module_index_update_from_file (
f29_updates, yaml_path, TRUE, &failures, &error));
g_assert_no_error (error);
g_assert_cmpint (failures->len, ==, 0);
g_clear_pointer (&yaml_path, g_free);
g_clear_pointer (&failures, g_ptr_array_unref);


/* Merge them so we're operating on a combined index */
merger = modulemd_module_index_merger_new ();
modulemd_module_index_merger_associate_index (merger, f29, 0);
modulemd_module_index_merger_associate_index (merger, f29_updates, 0);

index = modulemd_module_index_merger_resolve (merger, &error);
g_assert_nonnull (index);
g_assert_no_error (error);

/* Now get the 'nodejs' module */
nodejs_module = modulemd_module_index_get_module (index, "nodejs");
g_assert_nonnull (nodejs_module);
g_assert_true (MODULEMD_IS_MODULE (nodejs_module));

g_assert_cmpuint (
modulemd_module_get_all_streams (nodejs_module)->len, ==, 4);

/* Remove the `nodejs:10:20181101171344:6c81f848:x86_64` item from the
* index.
*/
modulemd_module_remove_streams_by_NSVCA (
nodejs_module, "10", 20181101171344, "6c81f848", "x86_64");

/* This should remove exactly one item */
g_assert_cmpuint (
modulemd_module_get_all_streams (nodejs_module)->len, ==, 3);


/* Try to remove the same stream from the index a second time, which should
* do nothing.
*/
modulemd_module_remove_streams_by_NSVCA (
nodejs_module, "10", 20181101171344, "6c81f848", "x86_64");

/* This should remove exactly one item */
g_assert_cmpuint (
modulemd_module_get_all_streams (nodejs_module)->len, ==, 3);


/* Remove all dwm stream objects for the "11" stream from the index. */
modulemd_module_remove_streams_by_name (nodejs_module, "11");

/* This should remove two items */
g_assert_cmpuint (
modulemd_module_get_all_streams (nodejs_module)->len, ==, 2);
}


int
main (int argc, char *argv[])
{
Expand Down Expand Up @@ -359,5 +446,8 @@ main (int argc, char *argv[])
module_test_streams,
NULL);

g_test_add_func ("/modulemd/v2/module/streams/remove",
modulemd_test_remove_streams);

return g_test_run ();
}

0 comments on commit 5bf0685

Please sign in to comment.