[Pkg-libvirt-commits] [libguestfs] 50/61: sysprep: Use common error/warning/info functions.

Hilko Bengen bengen at moszumanska.debian.org
Fri Oct 31 19:09:41 UTC 2014


This is an automated email from the git hooks/post-receive script.

bengen pushed a commit to annotated tag debian/1%1.28.2-1
in repository libguestfs.

commit 0252bc582064c82800e729a97678830c662a90f5
Author: Richard W.M. Jones <rjones at redhat.com>
Date:   Sat Oct 25 15:13:27 2014 +0100

    sysprep: Use common error/warning/info functions.
    
    (cherry picked from commit 6049fbdddb8c28a48632af2a7f894c93ffd8ac63)
---
 sysprep/main.ml                           | 28 ++++++++++++++--------------
 sysprep/sysprep_operation.ml              | 23 +++++++++++++----------
 sysprep/sysprep_operation.mli             |  3 +++
 sysprep/sysprep_operation_fs_uuids.ml     |  8 +++-----
 sysprep/sysprep_operation_script.ml       |  9 +++++----
 sysprep/sysprep_operation_user_account.ml | 10 +++++-----
 6 files changed, 43 insertions(+), 38 deletions(-)

diff --git a/sysprep/main.ml b/sysprep/main.ml
index f32c4ad..057f4cb 100644
--- a/sysprep/main.ml
+++ b/sysprep/main.ml
@@ -19,9 +19,10 @@
 open Unix
 open Printf
 
+open Common_utils
 open Common_gettext.Gettext
 
-open Common_utils
+open Sysprep_operation
 
 module G = Guestfs
 
@@ -29,7 +30,6 @@ module G = Guestfs
 let () = Sysprep_operation.bake ()
 
 (* Command line argument parsing. *)
-let prog = Filename.basename Sys.executable_name
 
 let () = Random.self_init ()
 
@@ -60,13 +60,13 @@ let main () =
       let uri =
         try URI.parse_uri arg
         with Invalid_argument "URI.parse_uri" ->
-          error ~prog (f_"error parsing URI '%s'. Look for error messages printed above.") arg in
+          error (f_"error parsing URI '%s'. Look for error messages printed above.") arg in
       let format = match !format with "auto" -> None | fmt -> Some fmt in
       files := (uri, format) :: !files;
       format_consumed := true
     and set_domain dom =
       if !domain <> None then
-        error ~prog (f_"--domain option can only be given once");
+        error (f_"--domain option can only be given once");
       domain := Some dom
     and dump_pod () =
       Sysprep_operation.dump_pod ();
@@ -76,15 +76,15 @@ let main () =
       exit 0
     and set_enable ops =
       if !operations <> None then
-        error ~prog (f_"--enable option can only be given once");
+        error (f_"--enable option can only be given once");
       if ops = "" then
-        error ~prog (f_"you cannot pass an empty argument to --enable");
+        error (f_"you cannot pass an empty argument to --enable");
       let ops = string_nsplit "," ops in
       let opset = List.fold_left (
         fun opset op_name ->
           try Sysprep_operation.add_to_set op_name opset
           with Not_found ->
-            error ~prog (f_"--enable: '%s' is not a known operation") op_name
+            error (f_"--enable: '%s' is not a known operation") op_name
       ) Sysprep_operation.empty_set ops in
       operations := Some opset
     and set_operations op_string =
@@ -103,7 +103,7 @@ let main () =
               `Add op_name in
           match op with
           | `Add "" | `Remove "" ->
-            error ~prog (f_"--operations: empty operation name")
+            error (f_"--operations: empty operation name")
           | `Add "defaults" -> Sysprep_operation.add_defaults_to_set opset
           | `Remove "defaults" -> Sysprep_operation.remove_defaults_from_set opset
           | `Add "all" -> Sysprep_operation.add_all_to_set opset
@@ -114,7 +114,7 @@ let main () =
               | `Remove n -> Sysprep_operation.remove_from_set in
             try f n opset with
             | Not_found ->
-              error ~prog (f_"--operations: '%s' is not a known operation") n
+              error (f_"--operations: '%s' is not a known operation") n
       ) currentopset ops in
       operations := Some opset
     and list_operations () =
@@ -173,7 +173,7 @@ read the man page virt-sysprep(1).
     Arg.parse argspec anon_fun usage_msg;
 
     if not !format_consumed then
-      error ~prog (f_"--format parameter must appear before -a parameter");
+      error (f_"--format parameter must appear before -a parameter");
 
     (* Check -a and -d options. *)
     let files = !files in
@@ -182,7 +182,7 @@ read the man page virt-sysprep(1).
     let add =
       match files, domain with
       | [], None ->
-        error ~prog (f_"you must give either -a or -d options.  Read virt-sysprep(1) man page for further information.")
+        error (f_"you must give either -a or -d options.  Read virt-sysprep(1) man page for further information.")
       | [], Some dom ->
         fun (g : Guestfs.guestfs) readonly ->
           let allowuuid = true in
@@ -193,7 +193,7 @@ read the man page virt-sysprep(1).
                     ?libvirturi ~allowuuid ~readonlydisk
                     dom)
       | _, Some _ ->
-        error ~prog (f_"you cannot give -a and -d options together.  Read virt-sysprep(1) man page for further information.")
+        error (f_"you cannot give -a and -d options together.  Read virt-sysprep(1) man page for further information.")
       | files, None ->
         fun g readonly ->
           List.iter (
@@ -247,7 +247,7 @@ read the man page virt-sysprep(1).
   (* Inspection. *)
   (match Array.to_list (g#inspect_os ()) with
   | [] ->
-    error ~prog (f_"no operating systems were found in the guest image")
+    error (f_"no operating systems were found in the guest image")
   | roots ->
     List.iter (
       fun root ->
@@ -263,7 +263,7 @@ read the man page virt-sysprep(1).
             let opts = mount_opts mp in
 
             try g#mount_options opts dev mp;
-            with Guestfs.Error msg -> eprintf (f_"%s (ignored)\n") msg
+            with Guestfs.Error msg -> warning (f_"%s (ignored)") msg
         ) mps;
 
         let side_effects = new Sysprep_operation.filesystem_side_effects in
diff --git a/sysprep/sysprep_operation.ml b/sysprep/sysprep_operation.ml
index 0d6cae4..1531268 100644
--- a/sysprep/sysprep_operation.ml
+++ b/sysprep/sysprep_operation.ml
@@ -22,7 +22,10 @@ open Printf
 
 open Common_gettext.Gettext
 
-let prog = "virt-sysprep"
+let prog = Filename.basename Sys.executable_name
+let error ?exit_code fs = error ~prog ?exit_code fs
+let warning fs = warning ~prog fs
+let info fs = info ~prog fs
 
 class filesystem_side_effects =
 object
@@ -128,42 +131,42 @@ and check_no_dupes ops =
     List.fold_left (
       fun opset op ->
         if OperationSet.mem op opset then
-          error ~prog (f_"duplicate operation name (%s)") op.name;
+          error (f_"duplicate operation name (%s)") op.name;
         add_to_set op.name opset
     ) empty_set ops
   )
 and check op =
   let n = String.length op.name in
   if n = 0 then
-    error ~prog (f_"operation name is an empty string");
+    error (f_"operation name is an empty string");
   for i = 0 to n-1 do
     match String.unsafe_get op.name i with
     | 'a'..'z' | 'A'..'Z' | '0'..'9' | '-' -> ()
     | c ->
-      error ~prog (f_"disallowed character (%c) in operation name") c
+      error (f_"disallowed character (%c) in operation name") c
   done;
   let n = String.length op.heading in
   if n = 0 then
-    error ~prog (f_"operation %s has no heading") op.name;
+    error (f_"operation %s has no heading") op.name;
   if op.heading.[n-1] = '\n' || op.heading.[n-1] = '.' then
-    error ~prog (f_"heading for %s must not end with newline or period") op.name;
+    error (f_"heading for %s must not end with newline or period") op.name;
   (match op.pod_description with
   | None -> ()
   | Some description ->
     let n = String.length description in
     if n = 0 then
-      error ~prog (f_"operation %s has no POD") op.name;
+      error (f_"operation %s has no POD") op.name;
     if description.[n-1] = '\n' then
-      error ~prog (f_"POD for %s must not end with newline") op.name;
+      error (f_"POD for %s must not end with newline") op.name;
   );
   (match op.pod_notes with
   | None -> ()
   | Some notes ->
     let n = String.length notes in
     if n = 0 then
-      error ~prog (f_"operation %s has no POD notes") op.name;
+      error (f_"operation %s has no POD notes") op.name;
     if notes.[n-1] = '\n' then
-      error ~prog (f_"POD notes for %s must not end with newline") op.name;
+      error (f_"POD notes for %s must not end with newline") op.name;
   )
 
 let extra_args () =
diff --git a/sysprep/sysprep_operation.mli b/sysprep/sysprep_operation.mli
index d10ef1e..5d3b44a 100644
--- a/sysprep/sysprep_operation.mli
+++ b/sysprep/sysprep_operation.mli
@@ -19,6 +19,9 @@
 (** Defines the interface between the main program and sysprep operations. *)
 
 val prog : string
+val error : ?exit_code:int -> ('a, unit, string, 'b) format4 -> 'a
+val warning : ('a, unit, string, unit) format4 -> 'a
+val info : ('a, unit, string, unit) format4 -> 'a
 
 class filesystem_side_effects : object
   method created_file : unit -> unit
diff --git a/sysprep/sysprep_operation_fs_uuids.ml b/sysprep/sysprep_operation_fs_uuids.ml
index af168c2..ccd8ef6 100644
--- a/sysprep/sysprep_operation_fs_uuids.ml
+++ b/sysprep/sysprep_operation_fs_uuids.ml
@@ -18,14 +18,12 @@
 
 open Printf
 
-open Sysprep_operation
-
 open Common_gettext.Gettext
 open Common_utils
 
-module G = Guestfs
+open Sysprep_operation
 
-let prog = "virt-sysprep"
+module G = Guestfs
 
 let rec fs_uuids_perform ~verbose ~quiet g root side_effects =
   let fses = g#list_filesystems () in
@@ -39,7 +37,7 @@ let rec fs_uuids_perform ~verbose ~quiet g root side_effects =
       g#set_uuid dev new_uuid
     with
       G.Error msg ->
-        warning ~prog (f_"cannot set random UUID on filesystem %s type %s: %s")
+        warning (f_"cannot set random UUID on filesystem %s type %s: %s")
           dev typ msg
   ) fses
 
diff --git a/sysprep/sysprep_operation_script.ml b/sysprep/sysprep_operation_script.ml
index d74bd5d..a8bbac5 100644
--- a/sysprep/sysprep_operation_script.ml
+++ b/sysprep/sysprep_operation_script.ml
@@ -19,16 +19,17 @@
 open Printf
 open Unix
 
+open Common_gettext.Gettext
 open Common_utils
+
 open Sysprep_operation
-open Common_gettext.Gettext
 
 module G = Guestfs
 
 let scriptdir = ref None
 let set_scriptdir dir =
   if !scriptdir <> None then
-    error ~prog (f_"--scriptdir cannot be used more than once");
+    error (f_"--scriptdir cannot be used more than once");
   scriptdir := Some dir
 
 let scripts = ref []
@@ -58,11 +59,11 @@ let rec script_perform ~verbose ~quiet (g : Guestfs.guestfs) root side_effects =
       match snd (waitpid [] pid) with
       | WEXITED 0 -> true
       | WEXITED i ->
-        eprintf (f_"virt-sysprep: script: failed (code %d)\n") i;
+        warning (f_"script: failed (code %d)") i;
         false
       | WSIGNALED i
       | WSTOPPED i ->
-        eprintf (f_"virt-sysprep: script: killed by signal (%d)\n") i;
+        warning (f_"script: killed by signal (%d)") i;
         false in
 
     (* Remote temporary directory / mountpoint. *)
diff --git a/sysprep/sysprep_operation_user_account.ml b/sysprep/sysprep_operation_user_account.ml
index 2d231cd..fda55476 100644
--- a/sysprep/sysprep_operation_user_account.ml
+++ b/sysprep/sysprep_operation_user_account.ml
@@ -20,9 +20,9 @@
 open Printf
 
 open Common_utils
+open Common_gettext.Gettext
 
 open Sysprep_operation
-open Common_gettext.Gettext
 
 module G = Guestfs
 
@@ -35,7 +35,7 @@ let add_users set users =
   List.iter (
     function
     | "" ->
-      error ~prog (f_"user-accounts: empty user name")
+      error (f_"user-accounts: empty user name")
     | user ->
       set := StringSet.add user !set
   ) users
@@ -77,7 +77,7 @@ let user_account_perform ~verbose ~quiet g root side_effects =
             try Some (g#aug_get (userpath ^ "/home"))
             with _ ->
               if verbose then
-                warning ~prog (f_"Cannot get the home directory for %s")
+                warning (f_"Cannot get the home directory for %s")
                   username;
               None in
           g#aug_rm userpath;
@@ -136,9 +136,9 @@ This option can be specified multiple times."
     perform_on_filesystems = Some user_account_perform;
     not_enabled_check_args = fun () ->
       if not (StringSet.is_empty !keep_users) then
-        error ~prog (f_"user-accounts: --keep-user-accounts parameter was used, but the \"user-account\" operation is not enabled");
+        error (f_"user-accounts: --keep-user-accounts parameter was used, but the \"user-account\" operation is not enabled");
       if not (StringSet.is_empty !remove_users) then
-        error ~prog (f_"user-accounts: --remove-user-accounts parameter was used, but the \"user-account\" operation is not enabled");
+        error (f_"user-accounts: --remove-user-accounts parameter was used, but the \"user-account\" operation is not enabled");
 }
 
 let () = register_operation op

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-libvirt/libguestfs.git



More information about the Pkg-libvirt-commits mailing list