Skip to content

Commit 5d29686

Browse files
sgallagherppisar
authored andcommitted
Add support for stripping XMD from an Index
By specification, the XMD contents are not meant to be used by consumers. This will provide a simple method of stripping that content out from the metadata of all streams. This could be used to sanitize the module metadata prior to including it in a composed RPM repository. Signed-off-by: Stephen Gallagher <[email protected]>
1 parent 5265493 commit 5d29686

File tree

7 files changed

+170
-0
lines changed

7 files changed

+170
-0
lines changed

modulemd/include/modulemd-2.0/modulemd-module-index.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,4 +667,19 @@ modulemd_module_index_upgrade_defaults (ModulemdModuleIndex *self,
667667
GError **error);
668668

669669

670+
/**
671+
* modulemd_module_index_clear_xmds:
672+
* @self: This #ModulemdModuleIndex object.
673+
*
674+
* Iterates through all #ModulemdModuleStream entries in this
675+
* #ModulemdModuleIndex and removes any XMD sections that are present. This is
676+
* generally done to trim down the metadata to only the portions that are
677+
* useful to the package manager.
678+
*
679+
* Since: 2.14.0
680+
*/
681+
void
682+
modulemd_module_index_clear_xmds (ModulemdModuleIndex *self);
683+
684+
670685
G_END_DECLS

modulemd/include/modulemd-2.0/modulemd-module-stream-v2.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -959,6 +959,16 @@ GVariant *
959959
modulemd_module_stream_v2_get_xmd (ModulemdModuleStreamV2 *self);
960960

961961

962+
/**
963+
* modulemd_module_stream_v2_clear_xmd:
964+
* @self: (in): This #ModulemdModuleStreamV2 object.
965+
*
966+
* Removes all XMD data from this #ModulemdModuleStreamV2
967+
*/
968+
void
969+
modulemd_module_stream_v2_clear_xmd (ModulemdModuleStreamV2 *self);
970+
971+
962972
/**
963973
* modulemd_module_stream_v2_set_static_context:
964974
* @self: (in): This #ModulemdModuleStreamV2 object.

modulemd/include/modulemd-2.0/modulemd-module.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,4 +336,20 @@ modulemd_module_get_newest_active_obsoletes (ModulemdModule *self,
336336
const gchar *stream,
337337
const gchar *context);
338338

339+
340+
/**
341+
* modulemd_module_clear_xmds:
342+
* @self: (in): This #ModulemdModule object.
343+
*
344+
* Iterates through all #ModulemdModuleStream entries in this
345+
* #ModulemdModule and removes any XMD sections that are present. This is
346+
* generally done to trim down the metadata to only the portions that are
347+
* useful to the package manager.
348+
*
349+
* Since: 2.14.0
350+
*/
351+
void
352+
modulemd_module_clear_xmds (ModulemdModule *self);
353+
354+
339355
G_END_DECLS

modulemd/modulemd-module-index.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1439,6 +1439,26 @@ modulemd_module_index_add_translation (ModulemdModuleIndex *self,
14391439
}
14401440

14411441

1442+
static void
1443+
clear_xmds (gpointer key, gpointer value, gpointer user_data)
1444+
{
1445+
MODULEMD_INIT_TRACE ();
1446+
1447+
g_return_if_fail (MODULEMD_IS_MODULE (value));
1448+
1449+
modulemd_module_clear_xmds (MODULEMD_MODULE (value));
1450+
}
1451+
1452+
1453+
void
1454+
modulemd_module_index_clear_xmds (ModulemdModuleIndex *self)
1455+
{
1456+
MODULEMD_INIT_TRACE ();
1457+
1458+
g_hash_table_foreach (self->modules, clear_xmds, NULL);
1459+
}
1460+
1461+
14421462
gboolean
14431463
modulemd_module_index_merge (ModulemdModuleIndex *from,
14441464
ModulemdModuleIndex *into,

modulemd/modulemd-module-stream-v2.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,6 +1263,15 @@ modulemd_module_stream_v2_get_xmd (ModulemdModuleStreamV2 *self)
12631263
}
12641264

12651265

1266+
void
1267+
modulemd_module_stream_v2_clear_xmd (ModulemdModuleStreamV2 *self)
1268+
{
1269+
g_return_if_fail (MODULEMD_IS_MODULE_STREAM_V2 (self));
1270+
1271+
g_clear_pointer (&self->xmd, g_variant_unref);
1272+
}
1273+
1274+
12661275
gboolean
12671276
modulemd_module_stream_v2_includes_nevra (ModulemdModuleStreamV2 *self,
12681277
const gchar *nevra_pattern)

modulemd/modulemd-module.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,3 +1088,23 @@ modulemd_module_get_newest_active_obsoletes (ModulemdModule *self,
10881088

10891089
return newestActiveObsoletes;
10901090
}
1091+
1092+
1093+
static void
1094+
clear_xmds (gpointer data, gpointer user_data)
1095+
{
1096+
g_return_if_fail (MODULEMD_IS_MODULE_STREAM_V2 (data));
1097+
1098+
modulemd_module_stream_v2_clear_xmd (MODULEMD_MODULE_STREAM_V2 (data));
1099+
}
1100+
1101+
1102+
void
1103+
modulemd_module_clear_xmds (ModulemdModule *self)
1104+
{
1105+
MODULEMD_INIT_TRACE ();
1106+
1107+
g_return_if_fail (MODULEMD_IS_MODULE (self));
1108+
1109+
g_ptr_array_foreach (self->streams, clear_xmds, NULL);
1110+
}

modulemd/tests/ModulemdTests/moduleindex.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,86 @@ def test_update_from_defaults_directory(self):
224224
)
225225
self.assertFalse(ret)
226226

227+
def test_clear_xmds(self):
228+
if "_overrides_module" in dir(Modulemd) and hasattr(
229+
gi.overrides.Modulemd, "ModuleStreamV2"
230+
):
231+
idx = Modulemd.ModuleIndex.new()
232+
self.assertIsNotNone(idx)
233+
234+
ret, failures = idx.update_from_string(
235+
"""
236+
---
237+
document: modulemd
238+
version: 2
239+
data:
240+
name: foo
241+
stream: latest
242+
version: 1
243+
static_context: true
244+
context: c0ffee43
245+
arch: s390x
246+
summary: An example module
247+
description: A longer description
248+
license:
249+
module: MIT
250+
xmd:
251+
a_key: a_value
252+
another_key: another_value
253+
an_array:
254+
- a
255+
- b
256+
...
257+
---
258+
document: modulemd
259+
version: 2
260+
data:
261+
name: foo
262+
stream: latest
263+
version: 2
264+
static_context: true
265+
context: c0ffee43
266+
arch: s390x
267+
summary: An example module
268+
description: A longer description
269+
license:
270+
module: MIT
271+
xmd:
272+
a_key: a_value
273+
another_key: another_value
274+
an_array:
275+
- a
276+
- b
277+
...
278+
""",
279+
strict=True,
280+
)
281+
self.assertEqual(len(failures), 0)
282+
283+
module_names = idx.get_module_names()
284+
self.assertEqual(len(module_names), 1)
285+
self.assertEqual(module_names[0], "foo")
286+
287+
module = idx.get_module(module_names[0])
288+
self.assertEqual(len(module.get_all_streams()), 2)
289+
290+
ref_xmd = {
291+
"a_key": "a_value",
292+
"another_key": "another_value",
293+
"an_array": ["a", "b"],
294+
}
295+
296+
for stream in module.get_all_streams():
297+
# Verify that the XMD data is present.
298+
xmd = stream.get_xmd()
299+
self.assertEqual(xmd, ref_xmd)
300+
301+
idx.clear_xmds()
302+
303+
for stream in module.get_all_streams():
304+
# Verify that the XMD data is gone now.
305+
self.assertEqual(stream.get_xmd(), {})
306+
227307

228308
if __name__ == "__main__":
229309
unittest.main()

0 commit comments

Comments
 (0)