diff --git a/modulemd/include/modulemd-2.0/modulemd.h b/modulemd/include/modulemd-2.0/modulemd.h index 3f1ee0e32..7141eeb53 100644 --- a/modulemd/include/modulemd-2.0/modulemd.h +++ b/modulemd/include/modulemd-2.0/modulemd.h @@ -361,6 +361,33 @@ modulemd_read_packager_file (const gchar *yaml_path, GObject **object, GError **error); +/** + * modulemd_read_packager_file_ext: + * @yaml_path: (in): A path to a YAML file containing a packager document. + * @object: (out): (transfer full): A newly allocated #ModulemdModuleStreamV2 or + * #ModulemdPackagerV3 object initialized with the content from @yaml_path. + * @module_name: (in) (nullable): An optional module name to override the + * document on disk. Mostly useful in cases where the name is being + * auto-detected from git. + * @module_stream: (in) (nullable): An optional module stream name to override + * the document on disk. Mostly useful in cases where the name is being + * auto-detected from git. + * @error: (out): A #GError containing additional information if this function + * fails in a way that prevents program continuation. + * + * Returns: @MODULEMD_TYPE_MODULE_STREAM_V2, @MODULEMD_TYPE_PACKAGER_V3, or + * @G_TYPE_INVALID. Returns the matching GObject through the @object parameter. + * If the return value is @G_TYPE_INVALID, returns the reason as @error. + * + * Since: 2.11 + */ +GType +modulemd_read_packager_file_ext (const gchar *yaml_path, + GObject **object, + const gchar *module_name, + const gchar *module_stream, + GError **error); + /** * modulemd_read_packager_string: * @yaml_string: (in): A YAML string containing a packager document. @@ -380,4 +407,31 @@ modulemd_read_packager_string (const gchar *yaml_string, GObject **object, GError **error); +/** + * modulemd_read_packager_string_ext: + * @yaml_string: (in): A YAML string containing a packager document. + * @object: (out): (transfer full): A newly allocated #ModulemdModuleStreamV2 or + * #ModulemdPackagerV3 object initialized with the content from @yaml_string. + * @module_name: (in) (nullable): An optional module name to override the + * document on disk. Mostly useful in cases where the name is being + * auto-detected from git. + * @module_stream: (in) (nullable): An optional module stream name to override + * the document on disk. Mostly useful in cases where the name is being + * auto-detected from git. + * @error: (out): A #GError containing additional information if this function + * fails in a way that prevents program continuation. + * + * Returns: @MODULEMD_TYPE_MODULE_STREAM_V2, @MODULEMD_TYPE_PACKAGER_V3, or + * @G_TYPE_INVALID. Returns the matching GObject through the @object parameter. + * If the return value is @G_TYPE_INVALID, returns the reason as @error. + * + * Since: 2.11 + */ +GType +modulemd_read_packager_string_ext (const gchar *yaml_string, + GObject **object, + const gchar *module_name, + const gchar *module_stream, + GError **error); + G_END_DECLS diff --git a/modulemd/modulemd.c b/modulemd/modulemd.c index e6a595c26..f61d20d09 100644 --- a/modulemd/modulemd.c +++ b/modulemd/modulemd.c @@ -111,12 +111,25 @@ verify_load (gboolean ret, static GType modulemd_read_packager_from_parser (yaml_parser_t *parser, GObject **object, + const gchar *module_name, + const gchar *module_stream, GError **error); GType modulemd_read_packager_file (const gchar *yaml_path, GObject **object, GError **error) +{ + return modulemd_read_packager_file_ext ( + yaml_path, object, NULL, NULL, error); +} + +GType +modulemd_read_packager_file_ext (const gchar *yaml_path, + GObject **object, + const gchar *module_name, + const gchar *module_stream, + GError **error) { MMD_INIT_YAML_PARSER (parser); g_autoptr (FILE) yaml_stream = NULL; @@ -142,13 +155,25 @@ modulemd_read_packager_file (const gchar *yaml_path, yaml_parser_set_input_file (&parser, yaml_stream); - return modulemd_read_packager_from_parser (&parser, object, error); + return modulemd_read_packager_from_parser ( + &parser, object, module_name, module_stream, error); } GType modulemd_read_packager_string (const gchar *yaml_string, GObject **object, GError **error) +{ + return modulemd_read_packager_string_ext ( + yaml_string, object, NULL, NULL, error); +} + +GType +modulemd_read_packager_string_ext (const gchar *yaml_string, + GObject **object, + const gchar *module_name, + const gchar *module_stream, + GError **error) { MMD_INIT_YAML_PARSER (parser); @@ -159,12 +184,15 @@ modulemd_read_packager_string (const gchar *yaml_string, yaml_parser_set_input_string ( &parser, (const unsigned char *)yaml_string, strlen (yaml_string)); - return modulemd_read_packager_from_parser (&parser, object, error); + return modulemd_read_packager_from_parser ( + &parser, object, module_name, module_stream, error); } static GType modulemd_read_packager_from_parser (yaml_parser_t *parser, GObject **object, + const gchar *module_name, + const gchar *module_stream, GError **error) { MMD_INIT_YAML_EVENT (event); @@ -252,6 +280,17 @@ modulemd_read_packager_from_parser (yaml_parser_t *parser, return G_TYPE_INVALID; } + if (module_name) + { + modulemd_packager_v3_set_module_name (packager_v3, module_name); + } + + if (module_stream) + { + modulemd_packager_v3_set_stream_name (packager_v3, + module_stream); + } + return_object = (GObject *)g_steal_pointer (&packager_v3); return_type = MODULEMD_TYPE_PACKAGER_V3; break; @@ -285,6 +324,18 @@ modulemd_read_packager_from_parser (yaml_parser_t *parser, return G_TYPE_INVALID; } + if (module_name) + { + modulemd_module_stream_set_module_name ( + MODULEMD_MODULE_STREAM (stream_v2), module_name); + } + + if (module_stream) + { + modulemd_module_stream_set_stream_name ( + MODULEMD_MODULE_STREAM (stream_v2), module_stream); + } + return_object = (GObject *)g_steal_pointer (&stream_v2); return_type = MODULEMD_TYPE_MODULE_STREAM_V2; break; @@ -297,6 +348,18 @@ modulemd_read_packager_from_parser (yaml_parser_t *parser, return G_TYPE_INVALID; } + if (module_name) + { + modulemd_module_stream_set_module_name ( + MODULEMD_MODULE_STREAM (stream_v2), module_name); + } + + if (module_stream) + { + modulemd_module_stream_set_stream_name ( + MODULEMD_MODULE_STREAM (stream_v2), module_stream); + } + return_object = (GObject *)g_steal_pointer (&stream_v2); return_type = MODULEMD_TYPE_MODULE_STREAM_V2; break; diff --git a/modulemd/tests/test-modulemd-common.c b/modulemd/tests/test-modulemd-common.c index db021f921..3f693a22d 100644 --- a/modulemd/tests/test-modulemd-common.c +++ b/modulemd/tests/test-modulemd-common.c @@ -176,6 +176,28 @@ test_packager_read_file (void) g_assert_true (MODULEMD_IS_MODULE_STREAM_V2 (object)); + /* Valid packager v2 file with module/stream name overrides */ + g_clear_error (&error); + g_clear_object (&object); + g_clear_pointer (&yaml_file, g_free); + yaml_file = g_strdup_printf ("%s/yaml_specs/modulemd_packager_v2.yaml", + g_getenv ("MESON_SOURCE_ROOT")); + otype = modulemd_read_packager_file_ext ( + yaml_file, &object, "modulename-override", "streamname-override", &error); + g_assert_no_error (error); + g_assert_true (otype == MODULEMD_TYPE_MODULE_STREAM_V2); + g_assert_nonnull (object); + g_assert_true (MODULEMD_IS_MODULE_STREAM_V2 (object)); + g_assert_cmpstr ( + modulemd_module_stream_get_module_name (MODULEMD_MODULE_STREAM (object)), + ==, + "modulename-override"); + g_assert_cmpstr ( + modulemd_module_stream_get_stream_name (MODULEMD_MODULE_STREAM (object)), + ==, + "streamname-override"); + + /* Valid packager v3 file */ g_clear_error (&error); g_clear_object (&object); @@ -189,6 +211,28 @@ test_packager_read_file (void) g_assert_true (MODULEMD_IS_PACKAGER_V3 (object)); + /* Valid packager v3 file with module/stream name overrides */ + g_clear_error (&error); + g_clear_object (&object); + g_clear_pointer (&yaml_file, g_free); + yaml_file = g_strdup_printf ("%s/yaml_specs/modulemd_packager_v3.yaml", + g_getenv ("MESON_SOURCE_ROOT")); + otype = modulemd_read_packager_file_ext ( + yaml_file, &object, "modulename-override", "streamname-override", &error); + g_assert_no_error (error); + g_assert_true (otype == MODULEMD_TYPE_PACKAGER_V3); + g_assert_nonnull (object); + g_assert_true (MODULEMD_IS_PACKAGER_V3 (object)); + g_assert_cmpstr ( + modulemd_packager_v3_get_module_name (MODULEMD_PACKAGER_V3 (object)), + ==, + "modulename-override"); + g_assert_cmpstr ( + modulemd_packager_v3_get_stream_name (MODULEMD_PACKAGER_V3 (object)), + ==, + "streamname-override"); + + /* Valid stream v2 file */ g_clear_error (&error); g_clear_object (&object); @@ -202,6 +246,28 @@ test_packager_read_file (void) g_assert_true (MODULEMD_IS_MODULE_STREAM_V2 (object)); + /* Valid stream v2 file with module/stream name overrides */ + g_clear_error (&error); + g_clear_object (&object); + g_clear_pointer (&yaml_file, g_free); + yaml_file = g_strdup_printf ("%s/yaml_specs/modulemd_stream_v2.yaml", + g_getenv ("MESON_SOURCE_ROOT")); + otype = modulemd_read_packager_file_ext ( + yaml_file, &object, "modulename-override", "streamname-override", &error); + g_assert_no_error (error); + g_assert_true (otype == MODULEMD_TYPE_MODULE_STREAM_V2); + g_assert_nonnull (object); + g_assert_true (MODULEMD_IS_MODULE_STREAM_V2 (object)); + g_assert_cmpstr ( + modulemd_module_stream_get_module_name (MODULEMD_MODULE_STREAM (object)), + ==, + "modulename-override"); + g_assert_cmpstr ( + modulemd_module_stream_get_stream_name (MODULEMD_MODULE_STREAM (object)), + ==, + "streamname-override"); + + /* Valid stream v1 file, should get upgraded to v2 */ g_clear_error (&error); g_clear_object (&object); @@ -215,6 +281,30 @@ test_packager_read_file (void) g_assert_true (MODULEMD_IS_MODULE_STREAM_V2 (object)); + /* Valid stream v1 file, should get upgraded to v2, + * with module/stream name overrides + */ + g_clear_error (&error); + g_clear_object (&object); + g_clear_pointer (&yaml_file, g_free); + yaml_file = g_strdup_printf ("%s/yaml_specs/modulemd_stream_v1.yaml", + g_getenv ("MESON_SOURCE_ROOT")); + otype = modulemd_read_packager_file_ext ( + yaml_file, &object, "modulename-override", "streamname-override", &error); + g_assert_no_error (error); + g_assert_true (otype == MODULEMD_TYPE_MODULE_STREAM_V2); + g_assert_nonnull (object); + g_assert_true (MODULEMD_IS_MODULE_STREAM_V2 (object)); + g_assert_cmpstr ( + modulemd_module_stream_get_module_name (MODULEMD_MODULE_STREAM (object)), + ==, + "modulename-override"); + g_assert_cmpstr ( + modulemd_module_stream_get_stream_name (MODULEMD_MODULE_STREAM (object)), + ==, + "streamname-override"); + + /* Nonexistent file */ g_clear_error (&error); g_clear_object (&object); @@ -295,7 +385,26 @@ test_packager_read_string (void) g_assert_true (otype == MODULEMD_TYPE_MODULE_STREAM_V2); g_assert_nonnull (object); g_assert_true (MODULEMD_IS_MODULE_STREAM_V2 (object)); - + /* with module/stream name overrides */ + g_clear_error (&error); + g_clear_object (&object); + otype = modulemd_read_packager_string_ext (yaml_string, + &object, + "modulename-override", + "streamname-override", + &error); + g_assert_no_error (error); + g_assert_true (otype == MODULEMD_TYPE_MODULE_STREAM_V2); + g_assert_nonnull (object); + g_assert_true (MODULEMD_IS_MODULE_STREAM_V2 (object)); + g_assert_cmpstr ( + modulemd_module_stream_get_module_name (MODULEMD_MODULE_STREAM (object)), + ==, + "modulename-override"); + g_assert_cmpstr ( + modulemd_module_stream_get_stream_name (MODULEMD_MODULE_STREAM (object)), + ==, + "streamname-override"); /* Trivial modulemd packager v3 */ g_clear_error (&error); @@ -317,6 +426,26 @@ test_packager_read_string (void) g_assert_true (otype == MODULEMD_TYPE_PACKAGER_V3); g_assert_nonnull (object); g_assert_true (MODULEMD_IS_PACKAGER_V3 (object)); + /* with module/stream name overrides */ + g_clear_error (&error); + g_clear_object (&object); + otype = modulemd_read_packager_string_ext (yaml_string, + &object, + "modulename-override", + "streamname-override", + &error); + g_assert_no_error (error); + g_assert_true (otype == MODULEMD_TYPE_PACKAGER_V3); + g_assert_nonnull (object); + g_assert_true (MODULEMD_IS_PACKAGER_V3 (object)); + g_assert_cmpstr ( + modulemd_packager_v3_get_module_name (MODULEMD_PACKAGER_V3 (object)), + ==, + "modulename-override"); + g_assert_cmpstr ( + modulemd_packager_v3_get_stream_name (MODULEMD_PACKAGER_V3 (object)), + ==, + "streamname-override"); /* Trivial modulemd stream v2 */ g_clear_error (&error); @@ -339,6 +468,26 @@ test_packager_read_string (void) g_assert_true (otype == MODULEMD_TYPE_MODULE_STREAM_V2); g_assert_nonnull (object); g_assert_true (MODULEMD_IS_MODULE_STREAM_V2 (object)); + /* with module/stream name overrides */ + g_clear_error (&error); + g_clear_object (&object); + otype = modulemd_read_packager_string_ext (yaml_string, + &object, + "modulename-override", + "streamname-override", + &error); + g_assert_no_error (error); + g_assert_true (otype == MODULEMD_TYPE_MODULE_STREAM_V2); + g_assert_nonnull (object); + g_assert_true (MODULEMD_IS_MODULE_STREAM_V2 (object)); + g_assert_cmpstr ( + modulemd_module_stream_get_module_name (MODULEMD_MODULE_STREAM (object)), + ==, + "modulename-override"); + g_assert_cmpstr ( + modulemd_module_stream_get_stream_name (MODULEMD_MODULE_STREAM (object)), + ==, + "streamname-override"); /* Trivial modulemd stream v1, should get upgraded to v2 */ g_clear_error (&error); @@ -361,6 +510,26 @@ test_packager_read_string (void) g_assert_true (otype == MODULEMD_TYPE_MODULE_STREAM_V2); g_assert_nonnull (object); g_assert_true (MODULEMD_IS_MODULE_STREAM_V2 (object)); + /* with module/stream name overrides */ + g_clear_error (&error); + g_clear_object (&object); + otype = modulemd_read_packager_string_ext (yaml_string, + &object, + "modulename-override", + "streamname-override", + &error); + g_assert_no_error (error); + g_assert_true (otype == MODULEMD_TYPE_MODULE_STREAM_V2); + g_assert_nonnull (object); + g_assert_true (MODULEMD_IS_MODULE_STREAM_V2 (object)); + g_assert_cmpstr ( + modulemd_module_stream_get_module_name (MODULEMD_MODULE_STREAM (object)), + ==, + "modulename-override"); + g_assert_cmpstr ( + modulemd_module_stream_get_stream_name (MODULEMD_MODULE_STREAM (object)), + ==, + "streamname-override"); /* NULL string should raise an exception */ g_clear_error (&error);