[Pkg-libvirt-commits] [libguestfs] 147/179: builder: Add Utils module and use common error/warning/info functions.

Hilko Bengen bengen at moszumanska.debian.org
Fri Oct 31 19:08:49 UTC 2014


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

bengen pushed a commit to branch experimental
in repository libguestfs.

commit b99983480ac3d75e2cb62dacf6042b3cd4cdf65e
Author: Richard W.M. Jones <rjones at redhat.com>
Date:   Sat Oct 25 11:36:15 2014 +0100

    builder: Add Utils module and use common error/warning/info functions.
    
    Add a Utils module.  This contains common error/warning/info functions,
    and also quote = Filename.quote.
    
    Examine every existing call to printf/eprintf and change where
    necessary so that:
    
     - error is used instead of eprintf + exit 1
    
     - warning no longer needs ~prog argument (it is added by Utils module)
    
     - any verbose output should go to stdout, not stderr
    
     - info is used to print general informational messages
---
 builder/Makefile.am     |   4 +-
 builder/builder.ml      | 126 ++++++++++++++++++++----------------------------
 builder/cache.ml        |   4 +-
 builder/cmdline.ml      |  51 +++++++-------------
 builder/downloader.ml   |  42 ++++++----------
 builder/get_kernel.ml   |  22 ++++-----
 builder/index_parser.ml |  43 ++++++++---------
 builder/sigchecker.ml   |  63 +++++++++---------------
 builder/sources.ml      |  12 ++---
 builder/utils.ml        |  30 ++++++++++++
 po/POTFILES-ml          |   1 +
 11 files changed, 180 insertions(+), 218 deletions(-)

diff --git a/builder/Makefile.am b/builder/Makefile.am
index fd5c8e2..b53e183 100644
--- a/builder/Makefile.am
+++ b/builder/Makefile.am
@@ -75,7 +75,8 @@ SOURCES = \
 	sources.ml \
 	uname.ml \
 	uname.mli \
-	uname-c.c
+	uname-c.c \
+	utils.ml
 
 man_MANS =
 noinst_DATA =
@@ -113,6 +114,7 @@ deps = \
 	$(top_builddir)/customize/customize_run.cmx \
 	$(top_builddir)/fish/guestfish-uri.o \
 	$(top_builddir)/fish/guestfish-file-edit.o \
+	utils.cmx \
 	index-scan.o \
 	index-struct.o \
 	index-parse.o \
diff --git a/builder/builder.ml b/builder/builder.ml
index 121c5fb..fdf9334 100644
--- a/builder/builder.ml
+++ b/builder/builder.ml
@@ -23,6 +23,7 @@ module G = Guestfs
 open Common_utils
 open Password
 open Planner
+open Utils
 
 open Cmdline
 open Customize_cmdline
@@ -30,8 +31,6 @@ open Customize_cmdline
 open Unix
 open Printf
 
-let quote = Filename.quote
-
 let prog = Filename.basename Sys.executable_name
 
 let () = Random.self_init ()
@@ -85,12 +84,12 @@ let main () =
 
   (* If debugging, echo the command line arguments and the sources. *)
   if verbose then (
-    eprintf "command line:";
-    List.iter (eprintf " %s") (Array.to_list Sys.argv);
-    prerr_newline ();
+    printf "command line:";
+    List.iter (printf " %s") (Array.to_list Sys.argv);
+    print_newline ();
     iteri (
       fun i (source, fingerprint) ->
-        eprintf "source[%d] = (%S, %S)\n" i source fingerprint
+        printf "source[%d] = (%S, %S)\n" i source fingerprint
     ) sources
   );
 
@@ -108,9 +107,7 @@ let main () =
         Cache.clean_cachedir cachedir;
         exit 0
       | None ->
-        eprintf (f_"%s: error: could not find cache directory. Is $HOME set?\n")
-          prog;
-        exit 1
+        error (f_"could not find cache directory. Is $HOME set?")
       )
 
     | (`Install|`List|`Notes|`Print_cache|`Cache_all) as mode -> mode in
@@ -122,27 +119,21 @@ let main () =
    *)
   let cmd = sprintf "%s --help >/dev/null 2>&1" gpg in
   if Sys.command cmd <> 0 then (
-    if check_signature then (
-      eprintf (f_"%s: gpg is not installed (or does not work)\nYou should install gpg, or use --gpg option, or use --no-check-signature.\n") prog;
-      exit 1
-    )
+    if check_signature then
+      error (f_"gpg is not installed (or does not work)\nYou should install gpg, or use --gpg option, or use --no-check-signature.")
     else if verbose then
-      warning ~prog (f_"gpg program is not available")
+      warning (f_"gpg program is not available")
   );
 
   (* Check that curl works. *)
   let cmd = sprintf "%s --help >/dev/null 2>&1" curl in
-  if Sys.command cmd <> 0 then (
-    eprintf (f_"%s: curl is not installed (or does not work)\n") prog;
-    exit 1
-  );
+  if Sys.command cmd <> 0 then
+    error (f_"curl is not installed (or does not work)");
 
   (* Check that virt-resize works. *)
   let cmd = "virt-resize --help >/dev/null 2>&1" in
-  if Sys.command cmd <> 0 then (
-    eprintf (f_"%s: virt-resize is not installed (or does not work)\n") prog;
-    exit 1
-  );
+  if Sys.command cmd <> 0 then
+    error (f_"virt-resize is not installed (or does not work)");
 
   (* Create the cache. *)
   let cache =
@@ -151,8 +142,8 @@ let main () =
     | Some dir ->
       try Some (Cache.create ~verbose ~directory:dir)
       with exn ->
-        warning ~prog (f_"cache %s: %s") dir (Printexc.to_string exn);
-        warning ~prog (f_"disabling the cache");
+        warning (f_"cache %s: %s") dir (Printexc.to_string exn);
+        warning (f_"disabling the cache");
         None
   in
 
@@ -209,8 +200,7 @@ let main () =
     | `Cache_all ->                     (* --cache-all-templates *)
       (match cache with
       | None ->
-        eprintf (f_"%s: error: no cache directory\n") prog;
-        exit 1
+        error (f_"no cache directory")
       | Some _ ->
         List.iter (
           fun (name,
@@ -246,9 +236,8 @@ let main () =
         name = arg && arch = Architecture.filter_arch a
     ) index
     with Not_found ->
-      eprintf (f_"%s: cannot find os-version '%s' with architecture '%s'.\nUse --list to list available guest types.\n")
-        prog arg arch;
-      exit 1 in
+      error (f_"cannot find os-version '%s' with architecture '%s'.\nUse --list to list available guest types.")
+        arg arch in
   let entry = snd item in
   let sigchecker = entry.Index_parser.sigchecker in
 
@@ -318,9 +307,7 @@ let main () =
       match detect_file_type template with
       | `XZ -> [ `XZ, "" ]
       | `GZip | `Tar | `Zip ->
-        eprintf (f_"%s: input file (%s) has an unsupported type\n")
-          prog template;
-        exit 1
+        error (f_"input file (%s) has an unsupported type") template
       | `Unknown -> [] in
     [ `Template, ""; `Filename, template; `Size, Int64.to_string size ] @
       format_tag @ compression_tag in
@@ -334,10 +321,8 @@ let main () =
     | Some output, None -> output, "raw"
     | Some output, Some format -> output, format in
 
-  if is_char_device output_filename then (
-    eprintf (f_"%s: cannot output to a character device or /dev/null\n") prog;
-    exit 1
-  );
+  if is_char_device output_filename then
+    error (f_"cannot output to a character device or /dev/null");
 
   let blockdev_getsize64 dev =
     let cmd = sprintf "blockdev --getsize64 %s" (quote dev) in
@@ -361,16 +346,12 @@ let main () =
       (* --size parameter missing, block device: use block device size *)
       | None -> blockdev_size in
 
-    if size < original_image_size then (
-      eprintf (f_"%s: images cannot be shrunk, the output size is too small for this image.  Requested size = %s, minimum size = %s\n")
-        prog (human_size size) (human_size original_image_size);
-      exit 1
-    )
-    else if output_is_block_dev && output_format = "raw" && size > blockdev_size then (
-      eprintf (f_"%s: output size is too large for this block device.  Requested size = %s, output block device = %s, output block device size = %s\n")
-        prog (human_size size) output_filename (human_size blockdev_size);
-      exit 1
-    );
+    if size < original_image_size then
+      error (f_"images cannot be shrunk, the output size is too small for this image.  Requested size = %s, minimum size = %s")
+        (human_size size) (human_size original_image_size)
+    else if output_is_block_dev && output_format = "raw" && size > blockdev_size then
+      error (f_"output size is too large for this block device.  Requested size = %s, output block device = %s, output block device size = %s")
+        (human_size size) output_filename (human_size blockdev_size);
     size in
 
   let goal =
@@ -487,45 +468,44 @@ let main () =
     try plan ~max_depth:5 transitions itags goal
     with
       Failure "plan" ->
-        eprintf (f_"%s: no plan could be found for making a disk image with\nthe required size, format etc. This is a bug in libguestfs!\nPlease file a bug, giving the command line arguments you used.\n") prog;
-        exit 1
+        error (f_"no plan could be found for making a disk image with\nthe required size, format etc. This is a bug in libguestfs!\nPlease file a bug, giving the command line arguments you used.");
   in
 
   (* Print out the plan. *)
   if verbose then (
     let print_tags tags =
       (try
-         let v = List.assoc `Filename tags in eprintf " +filename=%s" v
+         let v = List.assoc `Filename tags in printf " +filename=%s" v
        with Not_found -> ());
       (try
-         let v = List.assoc `Size tags in eprintf " +size=%s" v
+         let v = List.assoc `Size tags in printf " +size=%s" v
        with Not_found -> ());
       (try
-         let v = List.assoc `Format tags in eprintf " +format=%s" v
+         let v = List.assoc `Format tags in printf " +format=%s" v
        with Not_found -> ());
-      if List.mem_assoc `Template tags then eprintf " +template";
-      if List.mem_assoc `XZ tags then eprintf " +xz"
+      if List.mem_assoc `Template tags then printf " +template";
+      if List.mem_assoc `XZ tags then printf " +xz"
     in
     let print_task = function
-      | `Copy -> eprintf "cp"
-      | `Rename -> eprintf "mv"
-      | `Pxzcat -> eprintf "pxzcat"
-      | `Virt_resize -> eprintf "virt-resize"
-      | `Disk_resize -> eprintf "qemu-img resize"
-      | `Convert -> eprintf "qemu-img convert"
+      | `Copy -> printf "cp"
+      | `Rename -> printf "mv"
+      | `Pxzcat -> printf "pxzcat"
+      | `Virt_resize -> printf "virt-resize"
+      | `Disk_resize -> printf "qemu-img resize"
+      | `Convert -> printf "qemu-img convert"
     in
 
     iteri (
       fun i (itags, task, otags) ->
-        eprintf "%d: itags:" i;
+        printf "%d: itags:" i;
         print_tags itags;
-        eprintf "\n";
-        eprintf "%d: task : " i;
+        printf "\n";
+        printf "%d: task : " i;
         print_task task;
-        eprintf "\n";
-        eprintf "%d: otags:" i;
+        printf "\n";
+        printf "%d: otags:" i;
         print_tags otags;
-        eprintf "\n\n%!"
+        printf "\n\n%!"
     ) plan
   );
 
@@ -548,14 +528,14 @@ let main () =
       let ofile = List.assoc `Filename otags in
       msg (f_"Copying");
       let cmd = sprintf "cp %s %s" (quote ifile) (quote ofile) in
-      if verbose then eprintf "%s\n%!" cmd;
+      if verbose then printf "%s\n%!" cmd;
       if Sys.command cmd <> 0 then exit 1
 
     | itags, `Rename, otags ->
       let ifile = List.assoc `Filename itags in
       let ofile = List.assoc `Filename otags in
       let cmd = sprintf "mv %s %s" (quote ifile) (quote ofile) in
-      if verbose then eprintf "%s\n%!" cmd;
+      if verbose then printf "%s\n%!" cmd;
       if Sys.command cmd <> 0 then exit 1
 
     | itags, `Pxzcat, otags ->
@@ -595,7 +575,7 @@ let main () =
           | None -> ""
           | Some lvexpand -> sprintf " --lv-expand %s" (quote lvexpand))
           (quote ifile) (quote ofile) in
-      if verbose then eprintf "%s\n%!" cmd;
+      if verbose then printf "%s\n%!" cmd;
       if Sys.command cmd <> 0 then exit 1
 
     | itags, `Disk_resize, otags ->
@@ -606,7 +586,7 @@ let main () =
         (human_size osize);
       let cmd = sprintf "qemu-img resize %s %Ld%s"
         (quote ofile) osize (if verbose then "" else " >/dev/null") in
-      if verbose then eprintf "%s\n%!" cmd;
+      if verbose then printf "%s\n%!" cmd;
       if Sys.command cmd <> 0 then exit 1
 
     | itags, `Convert, otags ->
@@ -623,7 +603,7 @@ let main () =
         | Some iformat -> sprintf " -f %s" (quote iformat))
         (quote ifile) (quote oformat) (quote ofile)
         (if verbose then "" else " >/dev/null 2>&1") in
-      if verbose then eprintf "%s\n%!" cmd;
+      if verbose then printf "%s\n%!" cmd;
       if Sys.command cmd <> 0 then exit 1
   ) plan;
 
@@ -667,12 +647,12 @@ let main () =
       List.iter (
         fun (mp, dev) ->
           try g#mount dev mp
-          with G.Error msg -> eprintf (f_"%s: %s (ignored)\n") prog msg
+          with G.Error msg -> warning (f_"%s (ignored)") msg
       ) mps;
       root
     | _ ->
-      eprintf (f_"%s: no guest operating systems or multiboot OS found in this disk image\nThis is a failure of the source repository.  Use -v for more information.\n") prog;
-      exit 1 in
+      error (f_"no guest operating systems or multiboot OS found in this disk image\nThis is a failure of the source repository.  Use -v for more information.")
+  in
 
   Customize_run.run ~prog ~verbose ~quiet g root ops;
 
diff --git a/builder/cache.ml b/builder/cache.ml
index 5471d49..d1fb053 100644
--- a/builder/cache.ml
+++ b/builder/cache.ml
@@ -19,11 +19,11 @@
 open Common_gettext.Gettext
 open Common_utils
 
+open Utils
+
 open Unix
 open Printf
 
-let quote = Filename.quote
-
 let clean_cachedir dir =
   let cmd = sprintf "rm -rf %s" (quote dir) in
   ignore (Sys.command cmd);
diff --git a/builder/cmdline.ml b/builder/cmdline.ml
index 1242aaa..14706a9 100644
--- a/builder/cmdline.ml
+++ b/builder/cmdline.ml
@@ -23,13 +23,13 @@ open Common_utils
 
 open Customize_cmdline
 
+open Utils
+
 module G = Guestfs
 
 open Unix
 open Printf
 
-let prog = Filename.basename Sys.executable_name
-
 let parse_cmdline () =
   let display_version () =
     printf "virt-builder %s\n" Config.package_version;
@@ -77,8 +77,7 @@ let parse_cmdline () =
     | "long" -> `Long
     | "json" -> `Json
     | fmt ->
-      eprintf (f_"%s: invalid --list-format type '%s', see the man page.\n") prog fmt;
-      exit 1 in
+      error (f_"invalid --list-format type '%s', see the man page") fmt in
 
   let machine_readable = ref false in
 
@@ -236,32 +235,25 @@ read the man page virt-builder(1).
       (match args with
       | [arg] -> arg
       | [] ->
-        eprintf (f_"%s: virt-builder os-version\nMissing 'os-version'. Use '--list' to list available template names.\n") prog;
-        exit 1
+        error (f_"virt-builder os-version\nMissing 'os-version'. Use '--list' to list available template names.")
       | _ ->
-        eprintf (f_"%s: virt-builder: too many parameters, expecting 'os-version'\n") prog;
-        exit 1
+        error (f_"too many parameters, expecting 'os-version'")
       )
     | `List ->
-      if format <> None then (
-        eprintf (f_"%s: virt-builder --list: use '--list-format', not '--format'.\n") prog;
-        exit 1
-      );
+      if format <> None then
+        error (f_"virt-builder --list: use '--list-format', not '--format'");
       (match args with
       | [] -> ""
       | _ ->
-        eprintf (f_"%s: virt-builder --list does not need any extra arguments.\n") prog;
-        exit 1
+        error (f_"virt-builder --list does not need any extra arguments")
       )
     | `Notes ->
       (match args with
       | [arg] -> arg
       | [] ->
-        eprintf (f_"%s: virt-builder --notes os-version\nMissing 'os-version'. Use '--list' to list available template names.\n") prog;
-        exit 1
+        error (f_"virt-builder --notes os-version\nMissing 'os-version'. Use '--list' to list available template names.")
       | _ ->
-        eprintf (f_"%s: virt-builder: too many parameters, expecting 'os-version'\n") prog;
-        exit 1
+        error (f_"virt-builder: too many parameters, expecting 'os-version'");
       )
     | `Cache_all
     | `Print_cache
@@ -269,18 +261,15 @@ read the man page virt-builder(1).
       (match args with
       | [] -> ""
       | _ ->
-        eprintf (f_"%s: virt-builder --cache-all-templates/--print-cache/--delete-cache does not need any extra arguments.\n") prog;
-        exit 1
+        error (f_"virt-builder --cache-all-templates/--print-cache/--delete-cache does not need any extra arguments")
       )
     | `Get_kernel ->
       (match args with
       | [arg] -> arg
       | [] ->
-        eprintf (f_"%s: virt-builder --get-kernel image\nMissing 'image' (disk image file) argument.\n") prog;
-        exit 1
+        error (f_"virt-builder --get-kernel image\nMissing 'image' (disk image file) argument")
       | _ ->
-        eprintf (f_"%s: virt-builder --get-kernel: too many parameters\n") prog;
-        exit 1
+        error (f_"virt-builder --get-kernel: too many parameters")
       ) in
 
   (* Check source(s) and fingerprint(s). *)
@@ -300,11 +289,8 @@ read the man page virt-builder(1).
         repeat fingerprint nr_sources
       | xs -> xs in
 
-    if List.length fingerprints <> nr_sources then (
-      eprintf (f_"%s: source and fingerprint lists are not the same length\n")
-        prog;
-      exit 1
-    );
+    if List.length fingerprints <> nr_sources then
+      error (f_"source and fingerprint lists are not the same length");
 
     (* Combine the sources and fingerprints into a single list of pairs. *)
     List.combine sources fingerprints in
@@ -324,11 +310,8 @@ read the man page virt-builder(1).
           | `Password _ | `RootPassword _ | `Scrub _ | `Timezone _ | `Upload _
           | `Write _ | `Chmod _ -> false
         ) ops.ops in
-        if requires_execute_on_guest then (
-          eprintf (f_"%s: sorry, cannot run commands on a guest with a different architecture\n")
-            prog;
-          exit 1
-        );
+        if requires_execute_on_guest then
+          error (f_"sorry, cannot run commands on a guest with a different architecture");
       );
       target_arch in
 
diff --git a/builder/downloader.ml b/builder/downloader.ml
index 011ed1c..8a23bdc 100644
--- a/builder/downloader.ml
+++ b/builder/downloader.ml
@@ -19,11 +19,11 @@
 open Common_gettext.Gettext
 open Common_utils
 
+open Utils
+
 open Unix
 open Printf
 
-let quote = Filename.quote
-
 type uri = string
 type filename = string
 
@@ -73,8 +73,8 @@ and download_to ~prog t ?(progress_bar = false) ~proxy uri filename =
   let parseduri =
     try URI.parse_uri uri
     with Invalid_argument "URI.parse_uri" ->
-      eprintf (f_"Error parsing URI '%s'. Look for error messages printed above.\n") uri;
-      exit 1 in
+      error (f_"error parsing URI '%s'. Look for error messages printed above.")
+        uri in
 
   (* Note because there may be parallel virt-builder instances running
    * and also to avoid partial downloads in the cache if the network
@@ -91,11 +91,8 @@ and download_to ~prog t ?(progress_bar = false) ~proxy uri filename =
       (if t.verbose then " -v" else "")
       (quote path) (quote filename_new) in
     let r = Sys.command cmd in
-    if r <> 0 then (
-      eprintf (f_"%s: cp (download) command failed copying '%s'\n")
-        prog path;
-      exit 1
-    )
+    if r <> 0 then
+      error (f_"cp (download) command failed copying '%s'") path;
   | _ as protocol -> (* Any other protocol. *)
     let outenv = proxy_envvar protocol proxy in
     (* Get the status code first to ensure the file exists. *)
@@ -104,13 +101,10 @@ and download_to ~prog t ?(progress_bar = false) ~proxy uri filename =
       t.curl
       (if t.verbose then "" else " -s -S")
       (quote uri) in
-    if t.verbose then eprintf "%s\n%!" cmd;
+    if t.verbose then printf "%s\n%!" cmd;
     let lines = external_command ~prog cmd in
-    if List.length lines < 1 then (
-      eprintf (f_"%s: unexpected output from curl command, enable debug and look at previous messages\n")
-        prog;
-      exit 1
-    );
+    if List.length lines < 1 then
+      error (f_"unexpected output from curl command, enable debug and look at previous messages");
     let status_code = List.hd lines in
     let bad_status_code = function
       | "" -> true
@@ -118,11 +112,8 @@ and download_to ~prog t ?(progress_bar = false) ~proxy uri filename =
       | s when s.[0] = '5' -> true (* 5xx *)
       | _ -> false
     in
-    if bad_status_code status_code then (
-      eprintf (f_"%s: failed to download %s: HTTP status code %s\n")
-        prog uri status_code;
-      exit 1
-    );
+    if bad_status_code status_code then
+      error (f_"failed to download %s: HTTP status code %s") uri status_code;
 
     (* Now download the file. *)
     let cmd = sprintf "%s%s%s -g -o %s %s"
@@ -130,13 +121,10 @@ and download_to ~prog t ?(progress_bar = false) ~proxy uri filename =
       t.curl
       (if t.verbose then "" else if progress_bar then " -#" else " -s -S")
       (quote filename_new) (quote uri) in
-    if t.verbose then eprintf "%s\n%!" cmd;
+    if t.verbose then printf "%s\n%!" cmd;
     let r = Sys.command cmd in
-    if r <> 0 then (
-      eprintf (f_"%s: curl (download) command failed downloading '%s'\n")
-        prog uri;
-      exit 1
-    )
+    if r <> 0 then
+      error (f_"curl (download) command failed downloading '%s'") uri;
   );
 
   (* Rename the file if the download was successful. *)
@@ -154,7 +142,7 @@ and proxy_envvar protocol = function
     (* No changes required. *)
     ""
   | ForcedProxy proxy ->
-    let proxy = Filename.quote proxy in
+    let proxy = quote proxy in
     (match protocol with
     | "http" -> sprintf "env http_proxy=%s no_proxy= " proxy
     | "https" -> sprintf "env https_proxy=%s no_proxy= " proxy
diff --git a/builder/get_kernel.ml b/builder/get_kernel.ml
index 47518d4..9ac37b9 100644
--- a/builder/get_kernel.ml
+++ b/builder/get_kernel.ml
@@ -19,6 +19,8 @@
 open Common_gettext.Gettext
 open Common_utils
 
+open Utils
+
 module G = Guestfs
 
 open Printf
@@ -34,14 +36,10 @@ let rec get_kernel ~trace ~verbose ?format ?output disk =
   g#launch ();
 
   let roots = g#inspect_os () in
-  if Array.length roots = 0 then (
-    eprintf (f_"virt-builder: get-kernel: no operating system found\n");
-    exit 1
-  );
-  if Array.length roots > 1 then (
-    eprintf (f_"virt-builder: get-kernel: dual/multi-boot images are not supported by this tool\n");
-    exit 1
-  );
+  if Array.length roots = 0 then
+    error (f_"get-kernel: no operating system found");
+  if Array.length roots > 1 then
+    error (f_"get-kernel: dual/multi-boot images are not supported by this tool");
   let root = roots.(0) in
 
   (* Mount up the disks. *)
@@ -51,7 +49,7 @@ let rec get_kernel ~trace ~verbose ?format ?output disk =
   List.iter (
     fun (mp, dev) ->
       try g#mount_ro dev mp
-      with Guestfs.Error msg -> eprintf "%s (ignored)\n" msg
+      with Guestfs.Error msg -> warning (f_"%s (ignored)") msg
   ) mps;
 
   (* Get all kernels and initramfses. *)
@@ -69,10 +67,8 @@ let rec get_kernel ~trace ~verbose ?format ?output disk =
   let kernels = List.rev (List.sort compare_version kernels) in
   let initrds = List.rev (List.sort compare_version initrds) in
 
-  if kernels = [] then (
-    eprintf (f_"virt-builder: no kernel found\n");
-    exit 1
-  );
+  if kernels = [] then
+    error (f_"no kernel found");
 
   (* Download the latest. *)
   let outputdir =
diff --git a/builder/index_parser.ml b/builder/index_parser.ml
index 0c8bf1a..00b0e49 100644
--- a/builder/index_parser.ml
+++ b/builder/index_parser.ml
@@ -19,6 +19,8 @@
 open Common_gettext.Gettext
 open Common_utils
 
+open Utils
+
 open Printf
 open Unix
 
@@ -111,9 +113,7 @@ let print_entry chan (name, { printable_name = printable_name;
 
 let get_index ~prog ~verbose ~downloader ~sigchecker ~proxy source =
   let corrupt_file () =
-    eprintf (f_"\nThe index file downloaded from '%s' is corrupt.\nYou need to ask the supplier of this file to fix it and upload a fixed version.\n")
-      source;
-    exit 1
+    error (f_"The index file downloaded from '%s' is corrupt.\nYou need to ask the supplier of this file to fix it and upload a fixed version.") source
   in
 
   let rec get_index () =
@@ -145,7 +145,7 @@ let get_index ~prog ~verbose ~downloader ~sigchecker ~proxy source =
       fun (n, arch) ->
         let id = n, arch in
         if Hashtbl.mem nseen id then (
-          eprintf (f_"virt-builder: index is corrupt: os-version '%s' with architecture '%s' appears two or more times\n") n arch;
+          eprintf (f_"%s: index is corrupt: os-version '%s' with architecture '%s' appears two or more times\n") prog n arch;
           corrupt_file ()
         );
         Hashtbl.add nseen id true
@@ -161,9 +161,9 @@ let get_index ~prog ~verbose ~downloader ~sigchecker ~proxy source =
             if Hashtbl.mem fseen hashkey then (
               (match subkey with
               | Some value ->
-                eprintf (f_"virt-builder: index is corrupt: %s: field '%s[%s]' appears two or more times\n") n field value
+                eprintf (f_"%s: index is corrupt: %s: field '%s[%s]' appears two or more times\n") prog n field value
               | None ->
-                eprintf (f_"virt-builder: index is corrupt: %s: field '%s' appears two or more times\n") n field);
+                eprintf (f_"%s: index is corrupt: %s: field '%s' appears two or more times\n") prog n field);
               corrupt_file ()
             );
             Hashtbl.add fseen hashkey true
@@ -182,12 +182,12 @@ let get_index ~prog ~verbose ~downloader ~sigchecker ~proxy source =
           let file_uri =
             try make_absolute_uri (List.assoc ("file", None) fields)
             with Not_found ->
-              eprintf (f_"virt-builder: no 'file' (URI) entry for '%s'\n") n;
+              eprintf (f_"%s: no 'file' (URI) entry for '%s'\n") prog n;
             corrupt_file () in
           let arch =
             try List.assoc ("arch", None) fields
             with Not_found ->
-              eprintf (f_"virt-builder: no 'arch' entry for '%s'\n") n;
+              eprintf (f_"%s: no 'arch' entry for '%s'\n") prog n;
             corrupt_file () in
           let signature_uri =
             try Some (make_absolute_uri (List.assoc ("sig", None) fields))
@@ -202,8 +202,7 @@ let get_index ~prog ~verbose ~downloader ~sigchecker ~proxy source =
             with
             | Not_found -> 1
             | Failure "int_of_string" ->
-              eprintf (f_"virt-builder: cannot parse 'revision' field for '%s'\n")
-                n;
+              eprintf (f_"%s: cannot parse 'revision' field for '%s'\n") prog n;
               corrupt_file () in
           let format =
             try Some (List.assoc ("format", None) fields) with Not_found -> None in
@@ -211,11 +210,10 @@ let get_index ~prog ~verbose ~downloader ~sigchecker ~proxy source =
             try Int64.of_string (List.assoc ("size", None) fields)
             with
             | Not_found ->
-              eprintf (f_"virt-builder: no 'size' field for '%s'\n") n;
+              eprintf (f_"%s: no 'size' field for '%s'\n") prog n;
               corrupt_file ()
             | Failure "int_of_string" ->
-              eprintf (f_"virt-builder: cannot parse 'size' field for '%s'\n")
-                n;
+              eprintf (f_"%s: cannot parse 'size' field for '%s'\n") prog n;
               corrupt_file () in
           let compressed_size =
             try Some (Int64.of_string (List.assoc ("compressed_size", None) fields))
@@ -223,8 +221,8 @@ let get_index ~prog ~verbose ~downloader ~sigchecker ~proxy source =
             | Not_found ->
               None
             | Failure "int_of_string" ->
-              eprintf (f_"virt-builder: cannot parse 'compressed_size' field for '%s'\n")
-                n;
+              eprintf (f_"%s: cannot parse 'compressed_size' field for '%s'\n")
+                prog n;
               corrupt_file () in
           let expand =
             try Some (List.assoc ("expand", None) fields) with Not_found -> None in
@@ -248,8 +246,8 @@ let get_index ~prog ~verbose ~downloader ~sigchecker ~proxy source =
             with
             | Not_found -> false
             | Failure "bool_of_string" ->
-              eprintf (f_"virt-builder: cannot parse 'hidden' field for '%s'\n")
-                n;
+              eprintf (f_"%s: cannot parse 'hidden' field for '%s'\n")
+                prog n;
               corrupt_file () in
           let aliases =
             let l =
@@ -280,8 +278,8 @@ let get_index ~prog ~verbose ~downloader ~sigchecker ~proxy source =
       ) sections in
 
     if verbose then (
-      eprintf "index file (%s) after parsing (C parser):\n" source;
-      List.iter (print_entry Pervasives.stderr) entries
+      printf "index file (%s) after parsing (C parser):\n" source;
+      List.iter (print_entry Pervasives.stdout) entries
     );
 
     entries
@@ -289,16 +287,15 @@ let get_index ~prog ~verbose ~downloader ~sigchecker ~proxy source =
   (* Verify same-origin policy for the file= and sig= fields. *)
   and make_absolute_uri path =
     if String.length path = 0 then (
-      eprintf (f_"virt-builder: zero length path in the index file\n");
+      eprintf (f_"%s: zero length path in the index file\n") prog;
       corrupt_file ()
     )
     else if string_find path "://" >= 0 then (
-      eprintf (f_"virt-builder: cannot use a URI ('%s') in the index file\n")
-        path;
+      eprintf (f_"%s: cannot use a URI ('%s') in the index file\n") prog path;
       corrupt_file ()
     )
     else if path.[0] = '/' then (
-      eprintf (f_"virt-builder: you must use relative paths (not '%s') in the index file\n") path;
+      eprintf (f_"%s: you must use relative paths (not '%s') in the index file\n") prog path;
       corrupt_file ()
     )
     else (
diff --git a/builder/sigchecker.ml b/builder/sigchecker.ml
index 08efa5d..489da28 100644
--- a/builder/sigchecker.ml
+++ b/builder/sigchecker.ml
@@ -19,11 +19,11 @@
 open Common_gettext.Gettext
 open Common_utils
 
+open Utils
+
 open Printf
 open Unix
 
-let quote = Filename.quote
-
 type gpgkey_type =
   | No_Key
   | Fingerprint of string
@@ -44,12 +44,10 @@ let import_keyfile ~gpg ~gpghome ~verbose keyfile =
   let cmd = sprintf "%s --homedir %s --status-file %s --import %s%s"
     gpg gpghome (quote status_file) (quote keyfile)
     (if verbose then "" else " >/dev/null 2>&1") in
-  if verbose then eprintf "%s\n%!" cmd;
+  if verbose then printf "%s\n%!" cmd;
   let r = Sys.command cmd in
-  if r <> 0 then (
-    eprintf (f_"virt-builder: error: could not import public key\nUse the '-v' option and look for earlier error messages.\n");
-    exit 1
-  );
+  if r <> 0 then
+    error (f_"could not import public key\nUse the '-v' option and look for earlier error messages.");
   status_file
 
 let rec create ~verbose ~gpg ~gpgkey ~check_signature =
@@ -68,12 +66,10 @@ let rec create ~verbose ~gpg ~gpgkey ~check_signature =
        *)
       let cmd = sprintf "%s --homedir %s --list-keys%s"
         gpg tmpdir (if verbose then "" else " >/dev/null 2>&1") in
-      if verbose then eprintf "%s\n%!" cmd;
+      if verbose then printf "%s\n%!" cmd;
       let r = Sys.command cmd in
-      if r <> 0 then (
-        eprintf (f_"virt-builder: error: GPG failure: could not run GPG the first time\nUse the '-v' option and look for earlier error messages.\n");
-        exit 1
-      );
+      if r <> 0 then
+        error (f_"GPG failure: could not run GPG the first time\nUse the '-v' option and look for earlier error messages.");
       match gpgkey with
       | No_Key ->
         assert false
@@ -96,12 +92,10 @@ let rec create ~verbose ~gpg ~gpgkey ~check_signature =
         let cmd = sprintf "%s --yes --armor --output %s --export %s%s"
           gpg (quote filename) (quote fp)
           (if verbose then "" else " >/dev/null 2>&1") in
-        if verbose then eprintf "%s\n%!" cmd;
+        if verbose then printf "%s\n%!" cmd;
         let r = Sys.command cmd in
-        if r <> 0 then (
-          eprintf (f_"virt-builder: error: could not export public key\nUse the '-v' option and look for earlier error messages.\n");
-          exit 1
-        );
+        if r <> 0 then
+          error (f_"could not export public key\nUse the '-v' option and look for earlier error messages.");
         ignore (import_keyfile gpg tmpdir verbose filename);
         fp
     ) else
@@ -148,8 +142,7 @@ and verify_detached t filename sigfile =
   if t.check_signature then (
     match sigfile with
     | None ->
-      eprintf (f_"virt-builder: error: there is no detached signature file\nThis probably means the index file is missing a sig=... line.\nYou can use --no-check-signature to ignore this error, but that means\nyou are susceptible to man-in-the-middle attacks.\n");
-      exit 1
+      error (f_"there is no detached signature file\nThis probably means the index file is missing a sig=... line.\nYou can use --no-check-signature to ignore this error, but that means\nyou are susceptible to man-in-the-middle attacks.\n")
     | Some sigfile ->
       let args = sprintf "%s %s" (quote sigfile) (quote filename) in
       do_verify t args
@@ -163,12 +156,10 @@ and do_verify t args =
         t.gpg t.gpghome
         (if t.verbose then "" else " -q --logger-file /dev/null")
         (quote status_file) args in
-  if t.verbose then eprintf "%s\n%!" cmd;
+  if t.verbose then printf "%s\n%!" cmd;
   let r = Sys.command cmd in
-  if r <> 0 then (
-    eprintf (f_"virt-builder: error: GPG failure: could not verify digital signature of file\nTry:\n - Use the '-v' option and look for earlier error messages.\n - Delete the cache: virt-builder --delete-cache\n - Check no one has tampered with the website or your network!\n");
-    exit 1
-  );
+  if r <> 0 then
+    error (f_"GPG failure: could not verify digital signature of file\nTry:\n - Use the '-v' option and look for earlier error messages.\n - Delete the cache: virt-builder --delete-cache\n - Check no one has tampered with the website or your network!");
 
   (* Check the fingerprint is who it should be. *)
   let status = read_whole_file status_file in
@@ -183,11 +174,9 @@ and do_verify t args =
       | _ -> ()
   ) status;
 
-  if not (equal_fingerprints !fingerprint t.fingerprint) then (
-    eprintf (f_"virt-builder: error: fingerprint of signature does not match the expected fingerprint!\n  found fingerprint: %s\n  expected fingerprint: %s\n")
-      !fingerprint t.fingerprint;
-    exit 1
-  )
+  if not (equal_fingerprints !fingerprint t.fingerprint) then
+    error (f_"fingerprint of signature does not match the expected fingerprint!\n  found fingerprint: %s\n  expected fingerprint: %s")
+      !fingerprint t.fingerprint
 
 type csum_t = SHA512 of string
 
@@ -196,12 +185,10 @@ let verify_checksum t (SHA512 csum) filename =
   unlink_on_exit csum_file;
   let cmd = sprintf "sha512sum %s | awk '{print $1}' > %s"
     (quote filename) (quote csum_file) in
-  if t.verbose then eprintf "%s\n%!" cmd;
+  if t.verbose then printf "%s\n%!" cmd;
   let r = Sys.command cmd in
-  if r <> 0 then (
-    eprintf (f_"virt-builder: error: could not run sha512sum command to verify checksum\n");
-    exit 1
-  );
+  if r <> 0 then
+    error (f_"could not run sha512sum command to verify checksum");
 
   let csum_actual = read_whole_file csum_file in
 
@@ -212,8 +199,6 @@ let verify_checksum t (SHA512 csum) filename =
     else
       csum_actual in
 
-  if csum <> csum_actual then (
-    eprintf (f_"virt-builder: error: checksum of template did not match the expected checksum!\n  found checksum: %s\n  expected checksum: %s\nTry:\n - Use the '-v' option and look for earlier error messages.\n - Delete the cache: virt-builder --delete-cache\n - Check no one has tampered with the website or your network!\n")
-      csum_actual csum;
-    exit 1
-  )
+  if csum <> csum_actual then
+    error (f_"checksum of template did not match the expected checksum!\n  found checksum: %s\n  expected checksum: %s\nTry:\n - Use the '-v' option and look for earlier error messages.\n - Delete the cache: virt-builder --delete-cache\n - Check no one has tampered with the website or your network!")
+      csum_actual csum
diff --git a/builder/sources.ml b/builder/sources.ml
index a752edc..9b81a3a 100644
--- a/builder/sources.ml
+++ b/builder/sources.ml
@@ -33,7 +33,7 @@ module StringSet = Set.Make (String)
 
 let parse_conf ~prog ~verbose file =
   if verbose then (
-    eprintf (f_"%s: trying to read %s\n") prog file;
+    printf (f_"%s: trying to read %s\n") prog file;
   );
   let sections = Ini_reader.read_ini ~prog ~error_suffix:"[ignored]" file in
 
@@ -52,7 +52,7 @@ let parse_conf ~prog ~verbose file =
             | Not_found -> None
             | Invalid_argument "URI.parse_uri" as ex ->
               if verbose then (
-                eprintf (f_"%s: '%s' has invalid gpgkey URI\n") prog n;
+                printf (f_"%s: '%s' has invalid gpgkey URI\n") prog n;
               );
               raise ex in
           match k with
@@ -62,7 +62,7 @@ let parse_conf ~prog ~verbose file =
             | "file" -> Some uri.URI.path
             | _ ->
               if verbose then (
-                eprintf (f_"%s: '%s' has non-local gpgkey URI\n") prog n;
+                printf (f_"%s: '%s' has non-local gpgkey URI\n") prog n;
               );
               None
             ) in
@@ -84,7 +84,7 @@ let parse_conf ~prog ~verbose file =
   ) sections [] in
 
   if verbose then (
-    eprintf (f_"%s: ... read %d sources\n") prog (List.length sources);
+    printf (f_"%s: ... read %d sources\n") prog (List.length sources);
   );
 
   sources
@@ -127,12 +127,12 @@ let read_sources ~prog ~verbose =
           ) with
           | Unix_error (code, fname, _) ->
             if verbose then (
-              eprintf (f_"%s: file error: %s: %s\n") prog fname (error_message code)
+              printf (f_"%s: file error: %s: %s\n") prog fname (error_message code)
             );
             acc
           | Invalid_argument msg ->
             if verbose then (
-              eprintf (f_"%s: internal error: invalid argument: %s\n") prog msg
+              printf (f_"%s: internal error: invalid argument: %s\n") prog msg
             );
             acc
       ) acc files
diff --git a/builder/utils.ml b/builder/utils.ml
new file mode 100644
index 0000000..f4f290d
--- /dev/null
+++ b/builder/utils.ml
@@ -0,0 +1,30 @@
+(* virt-builder
+ * Copyright (C) 2013-2014 Red Hat Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *)
+
+(* Utilities/common functions used in virt-builder only. *)
+
+open Printf
+
+open Common_utils
+
+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
+
+let quote = Filename.quote
diff --git a/po/POTFILES-ml b/po/POTFILES-ml
index 7403497..1f08e47 100644
--- a/po/POTFILES-ml
+++ b/po/POTFILES-ml
@@ -14,6 +14,7 @@ builder/setlocale.ml
 builder/sigchecker.ml
 builder/sources.ml
 builder/uname.ml
+builder/utils.ml
 customize/crypt.ml
 customize/customize_cmdline.ml
 customize/customize_main.ml

-- 
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