[Pkg-libvirt-commits] [libguestfs] 28/63: v2v: Move json-generating code into separate module.

Hilko Bengen bengen at moszumanska.debian.org
Fri Oct 3 14:43:27 UTC 2014


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

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

commit 27a5ef9131317e4a715d1dfb62b155ee081c2888
Author: Richard W.M. Jones <rjones at redhat.com>
Date:   Thu Sep 4 12:07:36 2014 +0100

    v2v: Move json-generating code into separate module.
    
    Just code motion so we can reuse the same code for making
    `json:' remote SSH URLs.
---
 po/POTFILES-ml  |  1 +
 v2v/JSON.ml     | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 v2v/JSON.mli    | 26 ++++++++++++++++++++++++++
 v2v/Makefile.am |  2 ++
 v2v/lib_esx.ml  | 36 +++++++++---------------------------
 v2v/utils.ml    | 13 -------------
 6 files changed, 91 insertions(+), 40 deletions(-)

diff --git a/po/POTFILES-ml b/po/POTFILES-ml
index 4b33dd9..70e1c9c 100644
--- a/po/POTFILES-ml
+++ b/po/POTFILES-ml
@@ -83,6 +83,7 @@ sysprep/sysprep_operation_user_account.ml
 sysprep/sysprep_operation_utmp.ml
 sysprep/sysprep_operation_yum_uuid.ml
 v2v/DOM.ml
+v2v/JSON.ml
 v2v/cmdline.ml
 v2v/convert_linux.ml
 v2v/convert_windows.ml
diff --git a/v2v/JSON.ml b/v2v/JSON.ml
new file mode 100644
index 0000000..5e3a879
--- /dev/null
+++ b/v2v/JSON.ml
@@ -0,0 +1,53 @@
+(* virt-v2v
+ * Copyright (C) 2009-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.
+ *)
+
+(* Poor man's JSON generator. *)
+
+open Printf
+
+open Common_utils
+
+type field = string * json_t
+and json_t = String of string | Int of int
+and doc = field list
+
+(* JSON quoting. *)
+let json_quote str =
+  let str = replace_str str "\\" "\\\\" in
+  let str = replace_str str "\"" "\\\"" in
+  let str = replace_str str "'" "\\'" in
+  let str = replace_str str "\008" "\\b" in
+  let str = replace_str str "\012" "\\f" in
+  let str = replace_str str "\n" "\\n" in
+  let str = replace_str str "\r" "\\r" in
+  let str = replace_str str "\t" "\\t" in
+  let str = replace_str str "\011" "\\v" in
+  str
+
+let string_of_doc fields =
+  "{ " ^
+    String.concat ", " (
+      List.map (
+        function
+        | (n, String v) ->
+          sprintf "\"%s\" : \"%s\"" n (json_quote v)
+        | (n, Int v) ->
+          sprintf "\"%s\" : %d" n v
+      ) fields
+    )
+  ^ " }"
diff --git a/v2v/JSON.mli b/v2v/JSON.mli
new file mode 100644
index 0000000..1e3a1b3
--- /dev/null
+++ b/v2v/JSON.mli
@@ -0,0 +1,26 @@
+(* virt-v2v
+ * Copyright (C) 2009-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.
+ *)
+
+(** Poor man's JSON generator. *)
+
+type field = string * json_t
+and json_t = String of string | Int of int
+and doc = field list
+
+val string_of_doc : doc -> string
+  (** Serialize {!doc} object as a string. *)
diff --git a/v2v/Makefile.am b/v2v/Makefile.am
index ae65046..416f00b 100644
--- a/v2v/Makefile.am
+++ b/v2v/Makefile.am
@@ -34,6 +34,7 @@ SOURCES_MLI = \
 	input_disk.mli \
 	input_libvirt.mli \
 	input_libvirtxml.mli \
+	JSON.mli \
 	lib_esx.mli \
 	lib_linux.mli \
 	modules_list.mli \
@@ -52,6 +53,7 @@ SOURCES_ML = \
 	xml.ml \
 	domainxml.ml \
 	DOM.ml \
+	JSON.ml \
 	lib_esx.ml \
 	lib_linux.ml \
 	modules_list.ml \
diff --git a/v2v/lib_esx.ml b/v2v/lib_esx.ml
index 7966358..dde2b79 100644
--- a/v2v/lib_esx.ml
+++ b/v2v/lib_esx.ml
@@ -81,45 +81,27 @@ let rec map_path_to_uri verbose uri scheme server path format =
 
     (* Construct the JSON parameters. *)
     let json_params = [
-      "file.driver", "https";
-      "file.url", url;
-      "file.timeout", "60";
+      "file.driver", JSON.String "https";
+      "file.url", JSON.String url;
+      "file.timeout", JSON.Int 60;
     ] in
 
     let json_params =
       if sslverify then json_params
-      else ("file.sslverify", "off") :: json_params in
+      else ("file.sslverify", JSON.String "off") :: json_params in
 
     let json_params =
       match session_cookie with
       | None -> json_params
-      | Some cookie -> ("file.cookie", cookie) :: json_params in
+      | Some cookie -> ("file.cookie", JSON.String cookie) :: json_params in
 
-    if verbose then (
-      printf "esx: json parameters:\n";
-      List.iter (fun (n, v) -> printf "  %s : %s\n" n v) json_params
-    );
+    if verbose then
+      printf "esx: json parameters: %s\n" (JSON.string_of_doc json_params);
 
     (* Turn the JSON parameters into a 'json:' protocol string.
-     *
-     * Notes:
-     *
-     * (1) This requires qemu-img >= 2.1.0.
-     *
-     * (2) We don't 'quote' the commas, because in {!V2v} we pass the
-     * option to qemu-img create -b (not -o backing_file=...) and so
-     * extra quoting of commas is not required.  However if we changed
-     * things in future then we might requiring quoting of commas to be
-     * added somewhere.
+     * Note this requires qemu-img >= 2.1.0.
      *)
-    let qemu_uri =
-      sprintf "json: { %s }"
-        (String.concat " , " (
-          List.map
-            (fun (n, v) -> sprintf "\"%s\" : \"%s\"" n (json_quote v))
-            json_params
-         )
-        ) in
+    let qemu_uri = "json: " ^ JSON.string_of_doc json_params in
 
     (* The libvirt ESX driver doesn't normally specify a format, but
      * the format of the -flat file is *always* raw, so force it here.
diff --git a/v2v/utils.ml b/v2v/utils.ml
index 38443fa..dd46835 100644
--- a/v2v/utils.ml
+++ b/v2v/utils.ml
@@ -61,19 +61,6 @@ let uri_quote str =
   done;
   String.concat "" (List.rev !xs)
 
-(* JSON quoting. *)
-let json_quote str =
-  let str = replace_str str "\\" "\\\\" in
-  let str = replace_str str "\"" "\\\"" in
-  let str = replace_str str "'" "\\'" in
-  let str = replace_str str "\008" "\\b" in
-  let str = replace_str str "\012" "\\f" in
-  let str = replace_str str "\n" "\\n" in
-  let str = replace_str str "\r" "\\r" in
-  let str = replace_str str "\t" "\\t" in
-  let str = replace_str str "\011" "\\v" in
-  str
-
 external drive_name : int -> string = "v2v_utils_drive_name"
 
 let compare_app2_versions app1 app2 =

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