[virt-tools-list] [PATCH 09/10] Change osinfo_install_script_generate to write the output in a file

Fabiano Fidêncio fabiano at fidencio.org
Tue Jun 12 02:23:10 UTC 2012


    For libosinfo API:
        Now, instead of return a string, writes that string in a file,
        passed as argument

    For osinfo-install-script tool:
        The output will be wrote in:
            - Linuxes: <short_id>,ks
            - Windows 2k3r2 and older: windows.sif
            - Windows 2k8 and newer: windows.xml

    It will be used to create, easily, multiple scripts, as used in:
    http://bugzilla-attachments.gnome.org/attachment.cgi?id=214681
---
 osinfo/osinfo_install_script.c |   35 ++++++++++++++++++++------
 osinfo/osinfo_install_script.h |   11 +++++----
 tools/osinfo-install-script.c  |   53 ++++++++++++++++++++++++++--------------
 3 files changed, 69 insertions(+), 30 deletions(-)

diff --git a/osinfo/osinfo_install_script.c b/osinfo/osinfo_install_script.c
index 0d5fdaa..53b0b4c 100644
--- a/osinfo/osinfo_install_script.c
+++ b/osinfo/osinfo_install_script.c
@@ -688,12 +688,14 @@ static void osinfo_install_script_generate_done(GObject *src,
 }
 
 
-gchar *osinfo_install_script_generate(OsinfoInstallScript *script,
-                                      OsinfoOs *os,
-                                      OsinfoInstallConfig *config,
-                                      GCancellable *cancellable,
-                                      GError **error)
+gboolean osinfo_install_script_generate(OsinfoInstallScript *script,
+                                    OsinfoOs *os,
+                                    OsinfoInstallConfig *config,
+                                    gchar *path,
+                                    GCancellable *cancellable,
+                                    GError **error)
 {
+    gboolean ret = FALSE;
     GMainLoop *loop = g_main_loop_new(g_main_context_get_thread_default(),
                                       TRUE);
     struct OsinfoInstallScriptGenerateSync data = {
@@ -713,12 +715,31 @@ gchar *osinfo_install_script_generate(OsinfoInstallScript *script,
     if (data.error)
         g_propagate_error(error, data.error);
 
+    if (data.output) {
+        GFile *script_file = g_file_new_for_path(path);
+        GFileOutputStream *script_stream = g_file_replace(script_file,
+                                                          NULL, /*etag*/
+                                                          TRUE, /*backup*/
+                                                          0, /*flags*/
+                                                          NULL, /*cancellable*/
+                                                          NULL); /*error*/
+
+        g_output_stream_write (G_OUTPUT_STREAM (script_stream),
+                               data.output,
+                               strlen (data.output),
+                               NULL,
+                               NULL);
+        ret = TRUE;
+        if (script_stream)
+            g_object_unref(script_stream);
+        if (script_file)
+            g_object_unref(script_file);
+    }
     g_main_loop_unref(loop);
 
-    return data.output;
+    return ret;
 }
 
-
 /*
  * Local variables:
  *  indent-tabs-mode: nil
diff --git a/osinfo/osinfo_install_script.h b/osinfo/osinfo_install_script.h
index 30d5fb7..341d427 100644
--- a/osinfo/osinfo_install_script.h
+++ b/osinfo/osinfo_install_script.h
@@ -100,11 +100,12 @@ gchar *osinfo_install_script_generate_finish(OsinfoInstallScript *script,
                                              GAsyncResult *res,
                                              GError **error);
 
-gchar *osinfo_install_script_generate(OsinfoInstallScript *script,
-                                      OsinfoOs *os,
-                                      OsinfoInstallConfig *config,
-                                      GCancellable *cancellable,
-                                      GError **error);
+gboolean osinfo_install_script_generate(OsinfoInstallScript *script,
+                                        OsinfoOs *os,
+                                        OsinfoInstallConfig *config,
+                                        gchar *path,
+                                        GCancellable *cancellable,
+                                        GError **error);
 
 
 #endif /* __OSINFO_INSTALL_SCRIPT_H__ */
diff --git a/tools/osinfo-install-script.c b/tools/osinfo-install-script.c
index cedce10..85151b8 100644
--- a/tools/osinfo-install-script.c
+++ b/tools/osinfo-install-script.c
@@ -27,6 +27,7 @@
 #include <string.h>
 
 static gchar *profile;
+static gchar *output;
 
 static OsinfoInstallConfig *config;
 
@@ -55,7 +56,6 @@ static gboolean handle_config(const gchar *option_name G_GNUC_UNUSED,
     return TRUE;
 }
 
-
 static GOptionEntry entries[] =
 {
     { "profile", 'p', 0, G_OPTION_ARG_STRING, (void*)&profile,
@@ -99,16 +99,30 @@ static OsinfoOs *find_os(OsinfoDb *db,
     return os;
 }
 
+static void script_file_name_get(char *distro)
+{
+    if (!(strcmp(distro, "win2k")) || !(strcmp(distro, "winxp")) ||
+        !(strcmp(distro, "win2k3")) || !(strcmp(distro, "win2k3r3"))) {
+        output = g_strdup("windows.sif");
+    } else if (!(strcmp(distro, "win2k8")) || !(strcmp(distro, "win2k8r2")) ||
+               !(strcmp(distro, "vista")) || !(strcmp(distro, "win7")) ||
+               !(strcmp(distro, "win8"))) {
+        output = g_strdup("windows.xml");
+    } else {
+        gsize len = sizeof(distro) + sizeof(".ks");
+        output = g_malloc(len);
+        g_snprintf(output, len, "%s.ks", distro);
+    }
+}
 
 static gboolean generate_script(OsinfoOs *os)
 {
     OsinfoInstallScriptList *scripts = osinfo_os_get_install_script_list(os);
-    OsinfoInstallScriptList *jeosScripts;
+    OsinfoInstallScriptList *profileScripts;
     OsinfoFilter *filter;
     OsinfoInstallScript *script;
-    gboolean ret = FALSE;
     GError *error = NULL;
-    gchar *data;
+    gboolean ret = FALSE;
 
     filter = osinfo_filter_new();
     osinfo_filter_add_constraint(filter,
@@ -116,34 +130,35 @@ static gboolean generate_script(OsinfoOs *os)
                                  profile ? profile :
                                  OSINFO_INSTALL_SCRIPT_PROFILE_JEOS);
 
-    jeosScripts = osinfo_install_scriptlist_new_filtered(scripts,
+    profileScripts = osinfo_install_scriptlist_new_filtered(scripts,
                                                          filter);
-    if (osinfo_list_get_length(OSINFO_LIST(jeosScripts)) != 1) {
+    if (osinfo_list_get_length(OSINFO_LIST(profileScripts)) != 1) {
         g_printerr("Cannot find any install script for profile '%s'\n",
                    profile ? profile :
                    OSINFO_INSTALL_SCRIPT_PROFILE_JEOS);
         goto cleanup;
     }
 
-    script = OSINFO_INSTALL_SCRIPT(osinfo_list_get_nth(OSINFO_LIST(jeosScripts), 0));
-    if (!(data = osinfo_install_script_generate(script,
-                                                os,
-                                                config,
-                                                NULL,
-                                                &error))) {
+    script = OSINFO_INSTALL_SCRIPT(
+            osinfo_list_get_nth(OSINFO_LIST(profileScripts), 0));
+
+    if (!osinfo_install_script_generate(script,
+                                        os,
+                                        config,
+                                        output,
+                                        NULL,
+                                        &error)) {
         g_printerr("Unable to generate install script: %s\n",
-                   error ? error->message : "unknown");
+                error ? error->message : "unknown");
         goto cleanup;
     }
 
-    g_print("%s\n", data);
-
     ret = TRUE;
+    g_print("Install script was generated successfully as: %s\n", output);
 
  cleanup:
-    g_free(data);
     g_object_unref(scripts);
-    g_object_unref(jeosScripts);
+    g_object_unref(profileScripts);
     g_object_unref(filter);
     return ret;
 }
@@ -197,7 +212,7 @@ gint main(gint argc, gchar **argv)
         goto EXIT;
     }
 
-
+    script_file_name_get(argv[1]);
     if (!generate_script(os)) {
         ret = -5;
         goto EXIT;
@@ -206,6 +221,8 @@ gint main(gint argc, gchar **argv)
 EXIT:
     if (config)
         g_object_unref(config);
+    if (output)
+        g_free(output);
     g_clear_error(&error);
     g_clear_object(&loader);
     g_option_context_free(context);
-- 
1.7.10.2




More information about the virt-tools-list mailing list