[PATCH 02/10] Add osinfo_install_script_generate_for_media*()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Add API that allows apps to pass the media matched against the OS they
are generating the script for. Scripts can use this information to make
decisions about how installation should be setup. For example, we are
going to use this in the Fedora installer script to decide whether to
install from ISO or from network, based on whether or not media is a
network installer.
---
 osinfo/libosinfo.syms          |   4 +
 osinfo/osinfo_install_script.c | 197 ++++++++++++++++++++++++++++++++++++-----
 osinfo/osinfo_install_script.h |  16 ++++
 3 files changed, 196 insertions(+), 21 deletions(-)

diff --git a/osinfo/libosinfo.syms b/osinfo/libosinfo.syms
index 2dba411..ba4c020 100644
--- a/osinfo/libosinfo.syms
+++ b/osinfo/libosinfo.syms
@@ -507,6 +507,10 @@ LIBOSINFO_0.2.11 {
 
 LIBOSINFO_0.2.12 {
     global:
+	osinfo_install_script_generate_for_media;
+	osinfo_install_script_generate_for_media_async;
+	osinfo_install_script_generate_for_media_finish;
+
 	osinfo_media_get_volume_size;
 } LIBOSINFO_0.2.11;
 
diff --git a/osinfo/osinfo_install_script.c b/osinfo/osinfo_install_script.c
index a6a5bab..621c56f 100644
--- a/osinfo/osinfo_install_script.c
+++ b/osinfo/osinfo_install_script.c
@@ -533,6 +533,7 @@ OsinfoAvatarFormat *osinfo_install_script_get_avatar_format(OsinfoInstallScript
 struct _OsinfoInstallScriptGenerateData {
     GSimpleAsyncResult *res;
     OsinfoOs *os;
+    OsinfoMedia *media;
     OsinfoInstallConfig *config;
     OsinfoInstallScript *script;
 };
@@ -541,6 +542,8 @@ struct _OsinfoInstallScriptGenerateData {
 static void osinfo_install_script_generate_data_free(OsinfoInstallScriptGenerateData *data)
 {
     g_object_unref(data->os);
+    if (data->media != NULL)
+        g_object_unref(data->media);
     g_object_unref(data->config);
     g_object_unref(data->script);
     g_object_unref(data->res);
@@ -731,6 +734,7 @@ static xmlNodePtr osinfo_install_script_generate_entity_xml(OsinfoInstallScript
 
 static xmlDocPtr osinfo_install_script_generate_config_xml(OsinfoInstallScript *script,
                                                            OsinfoOs *os,
+                                                           OsinfoMedia *media,
                                                            OsinfoInstallConfig *config,
                                                            const gchar *node_name,
                                                            GError **error)
@@ -767,6 +771,19 @@ static xmlDocPtr osinfo_install_script_generate_config_xml(OsinfoInstallScript *
         goto error;
     }
 
+    if (media != NULL) {
+        if (!(node = osinfo_install_script_generate_entity_xml(script,
+                                                               OSINFO_ENTITY(media),
+                                                               "media",
+                                                               error)))
+            goto error;
+        if (!(xmlAddChild(root, node))) {
+            xmlErrorPtr err = xmlGetLastError();
+            g_set_error(error, 0, 0, _("Unable to set XML root '%s'"), err ? err->message : "");
+            goto error;
+        }
+    }
+
     if (!(node = osinfo_install_script_generate_entity_xml(script,
                                                            OSINFO_ENTITY(config),
                                                            "config",
@@ -819,6 +836,7 @@ static gchar *osinfo_install_script_apply_xslt(xsltStylesheetPtr ss,
 
 static gboolean osinfo_install_script_apply_template(OsinfoInstallScript *script,
                                                      OsinfoOs *os,
+                                                     OsinfoMedia *media,
                                                      const gchar *templateUri,
                                                      const gchar *template,
                                                      const gchar *node_name,
@@ -828,7 +846,7 @@ static gboolean osinfo_install_script_apply_template(OsinfoInstallScript *script
 {
     gboolean ret = FALSE;
     xsltStylesheetPtr templateXsl = osinfo_install_script_load_template(templateUri, template, error);
-    xmlDocPtr configXml = osinfo_install_script_generate_config_xml(script, os, config, node_name, error);
+    xmlDocPtr configXml = osinfo_install_script_generate_config_xml(script, os, media, config, node_name, error);
 
     if (!templateXsl || !configXml)
         goto cleanup;
@@ -871,6 +889,7 @@ static void osinfo_install_script_template_loaded(GObject *src,
 
     if (!osinfo_install_script_apply_template(data->script,
                                               data->os,
+                                              data->media,
                                               uri,
                                               input,
                                               "install-script-config",
@@ -891,43 +910,35 @@ static void osinfo_install_script_template_loaded(GObject *src,
     g_free(uri);
 }
 
-/**
- * osinfo_install_script_generate_async:
- * @script:     the install script
- * @os:         the os
- * @config:     the install script config
- * @cancellable: (allow-none): a #GCancellable, or %NULL
- * @callback: Function to call when result of this call is ready
- * @user_data: The user data to pass to @callback, or %NULL
- *
- * Asynchronous variant of #osinfo_install_script_generate(). From the callback,
- * call #osinfo_install_script_generate_finish() to conclude this call and get
- * the generated script.
- */
-void osinfo_install_script_generate_async(OsinfoInstallScript *script,
-                                          OsinfoOs *os,
-                                          OsinfoInstallConfig *config,
-                                          GCancellable *cancellable,
-                                          GAsyncReadyCallback callback,
-                                          gpointer user_data)
+
+static void osinfo_install_script_generate_async_common(OsinfoInstallScript *script,
+                                                        OsinfoOs *os,
+                                                        OsinfoMedia *media,
+                                                        OsinfoInstallConfig *config,
+                                                        GCancellable *cancellable,
+                                                        GAsyncReadyCallback callback,
+                                                        gpointer user_data)
 {
     OsinfoInstallScriptGenerateData *data = g_new0(OsinfoInstallScriptGenerateData, 1);
     const gchar *templateData = osinfo_install_script_get_template_data(script);
     const gchar *templateUri = osinfo_install_script_get_template_uri(script);
 
     data->os = g_object_ref(os);
+    if (media != NULL)
+        data->media = g_object_ref(media);
     data->config = g_object_ref(config);
     data->script = g_object_ref(script);
     data->res = g_simple_async_result_new(G_OBJECT(script),
                                           callback,
                                           user_data,
-                                          osinfo_install_script_generate_async);
+                                          osinfo_install_script_generate_async_common);
 
     if (templateData) {
         GError *error = NULL;
         gchar *output;
         if (!osinfo_install_script_apply_template(script,
                                                   os,
+                                                  media,
                                                   "<data>",
                                                   templateData,
                                                   "install-script-config",
@@ -954,6 +965,35 @@ void osinfo_install_script_generate_async(OsinfoInstallScript *script,
     }
 }
 
+/**
+ * osinfo_install_script_generate_async:
+ * @script:     the install script
+ * @os:         the os
+ * @config:     the install script config
+ * @cancellable: (allow-none): a #GCancellable, or %NULL
+ * @callback: Function to call when result of this call is ready
+ * @user_data: The user data to pass to @callback, or %NULL
+ *
+ * Asynchronous variant of #osinfo_install_script_generate(). From the callback,
+ * call #osinfo_install_script_generate_finish() to conclude this call and get
+ * the generated script.
+ */
+void osinfo_install_script_generate_async(OsinfoInstallScript *script,
+                                          OsinfoOs *os,
+                                          OsinfoInstallConfig *config,
+                                          GCancellable *cancellable,
+                                          GAsyncReadyCallback callback,
+                                          gpointer user_data)
+{
+    osinfo_install_script_generate_async_common(script,
+                                                os,
+                                                NULL,
+                                                config,
+                                                cancellable,
+                                                callback,
+                                                user_data);
+}
+
 static gpointer osinfo_install_script_generate_finish_common(OsinfoInstallScript *script,
                                                              GAsyncResult *res,
                                                              GError **error)
@@ -986,6 +1026,25 @@ gchar *osinfo_install_script_generate_finish(OsinfoInstallScript *script,
 }
 
 /**
+ * osinfo_install_script_generate_for_media_finish:
+ * @script: the install script
+ * @res:    a #GAsyncResult
+ * @error:  The location where to store any error, or NULL
+ *
+ * Returns: (transfer full): the generated script, or NULL on error
+ *
+ * Since: 0.2.12
+ */
+gchar *osinfo_install_script_generate_for_media_finish(OsinfoInstallScript *script,
+                                                       GAsyncResult *res,
+                                                       GError **error)
+{
+    return osinfo_install_script_generate_finish_common(script,
+                                                        res,
+                                                        error);
+}
+
+/**
  * osinfo_install_script_generate_output_finish:
  * @script: the install script
  * @res:    a #GAsyncResult
@@ -1094,6 +1153,101 @@ gchar *osinfo_install_script_generate(OsinfoInstallScript *script,
     return data.output;
 }
 
+/**
+ * osinfo_install_script_generate_for_media_async:
+ * @script:     the install script
+ * @media:      the media
+ * @config:     the install script config
+ * @cancellable: (allow-none): a #GCancellable, or %NULL
+ * @callback: Function to call when result of this call is ready
+ * @user_data: The user data to pass to @callback, or %NULL
+ *
+ * Asynchronous variant of #osinfo_install_script_generate_for_media(). From the
+ * callback, call #osinfo_install_script_generate_for_media_finish() to
+ * conclude this call and get the generated script.
+ *
+ * Since: 0.2.12
+ */
+void osinfo_install_script_generate_for_media_async(OsinfoInstallScript *script,
+                                                    OsinfoMedia *media,
+                                                    OsinfoInstallConfig *config,
+                                                    GCancellable *cancellable,
+                                                    GAsyncReadyCallback callback,
+                                                    gpointer user_data) {
+    OsinfoOs *os;
+
+    g_return_if_fail (media != NULL);
+
+    os = osinfo_media_get_os (media);
+    g_return_if_fail (os != NULL);
+
+    osinfo_install_script_generate_async_common(script,
+                                                os,
+                                                media,
+                                                config,
+                                                cancellable,
+                                                callback,
+                                                user_data);
+}
+
+static void osinfo_install_script_generate_for_media_done(GObject *src,
+                                                          GAsyncResult *res,
+                                                          gpointer user_data)
+{
+    OsinfoInstallScriptGenerateSyncData *data = user_data;
+
+    data->output =
+        osinfo_install_script_generate_for_media_finish(OSINFO_INSTALL_SCRIPT(src),
+                                                        res,
+                                                        &data->error);
+    g_main_loop_quit(data->loop);
+}
+
+/**
+ * osinfo_install_script_generate_for_media:
+ * @script:     the install script
+ * @media:      the media
+ * @config:     the install script config
+ * @cancellable: (allow-none): a #GCancellable, or %NULL
+ * @error: The location where to store any error, or %NULL
+ *
+ * Creates an install script. The media @media must have been identified
+ * successfully using #osinfo_db_identify_media() before calling this function.
+ *
+ * Returns: (transfer full): the script as string.
+ *
+ * Since: 0.2.12
+ */
+gchar *osinfo_install_script_generate_for_media(OsinfoInstallScript *script,
+                                                OsinfoMedia *media,
+                                                OsinfoInstallConfig *config,
+                                                GCancellable *cancellable,
+                                                GError **error)
+{
+    GMainLoop *loop = g_main_loop_new(g_main_context_get_thread_default(),
+                                      TRUE);
+    OsinfoInstallScriptGenerateSyncData data = {
+        loop, NULL, NULL, NULL
+    };
+
+    osinfo_install_script_generate_for_media_async(script,
+                                                   media,
+                                                   config,
+                                                   cancellable,
+                                                   osinfo_install_script_generate_for_media_done,
+                                                   &data);
+
+    if (g_main_loop_is_running(loop))
+        g_main_loop_run(loop);
+
+    if (data.error)
+        g_propagate_error(error, data.error);
+
+    g_main_loop_unref(loop);
+
+    return data.output;
+}
+
 static void osinfo_install_script_generate_output_write_file(GObject *src,
                                                              GAsyncResult *res,
                                                              gpointer user_data)
@@ -1264,6 +1418,7 @@ gchar *osinfo_install_script_generate_command_line(OsinfoInstallScript *script,
         GError *error = NULL;
         if (!osinfo_install_script_apply_template(script,
                                                   os,
+                                                  NULL,
                                                   "<data>",
                                                   templateData,
                                                   "command-line",
diff --git a/osinfo/osinfo_install_script.h b/osinfo/osinfo_install_script.h
index f8083b4..cd0b033 100644
--- a/osinfo/osinfo_install_script.h
+++ b/osinfo/osinfo_install_script.h
@@ -40,6 +40,7 @@
 #define OSINFO_INSTALL_SCRIPT_GET_CLASS(obj)        (G_TYPE_INSTANCE_GET_CLASS ((obj), OSINFO_TYPE_INSTALL_SCRIPT, OsinfoInstallScriptClass))
 
 typedef struct _OsinfoOs        OsinfoOs;
+typedef struct _OsinfoMedia     OsinfoMedia;
 typedef struct _OsinfoInstallScript        OsinfoInstallScript;
 typedef struct _OsinfoInstallScriptClass   OsinfoInstallScriptClass;
 typedef struct _OsinfoInstallScriptPrivate OsinfoInstallScriptPrivate;
@@ -199,6 +200,21 @@ GFile *osinfo_install_script_generate_output(OsinfoInstallScript *script,
                                              GCancellable *cancellable,
                                              GError **error);
 
+void osinfo_install_script_generate_for_media_async(OsinfoInstallScript *script,
+                                                    OsinfoMedia *media,
+                                                    OsinfoInstallConfig *config,
+                                                    GCancellable *cancellable,
+                                                    GAsyncReadyCallback callback,
+                                                    gpointer user_data);
+gchar *osinfo_install_script_generate_for_media_finish(OsinfoInstallScript *script,
+                                                       GAsyncResult *res,
+                                                       GError **error);
+gchar *osinfo_install_script_generate_for_media(OsinfoInstallScript *script,
+                                                OsinfoMedia *media,
+                                                OsinfoInstallConfig *config,
+                                                GCancellable *cancellable,
+                                                GError **error);
+
 gchar *osinfo_install_script_generate_command_line(OsinfoInstallScript *script,
                                                    OsinfoOs *os,
                                                    OsinfoInstallConfig *config);
-- 
2.1.0

_______________________________________________
Libosinfo mailing list
Libosinfo@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libosinfo




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Fedora Users]     [Fedora Maintainers]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]

  Powered by Linux