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

[Libguestfs] [PATCH v2 1/7] v2v: Move augeas_debug_errors (renamed debug_augeas_errors) to common code.



Since the function uses the StringMap module, I also had to
move this to mllib.

The function is renamed and the 'if verbose ()' part is moved into the
function, but apart from that, it's all just code motion.
---
 builder/Makefile.am          |  1 +
 customize/Makefile.am        |  1 +
 dib/Makefile.am              |  1 +
 get-kernel/Makefile.am       |  1 +
 mllib/Makefile.am            |  3 +++
 mllib/common_utils.ml        | 63 +++++++++++++++++++++++++++++++++++++++++++
 mllib/common_utils.mli       | 14 ++++++++++
 mllib/stringMap.ml           | 21 +++++++++++++++
 mllib/stringMap.mli          | 54 +++++++++++++++++++++++++++++++++++++
 resize/Makefile.am           |  1 +
 sparsify/Makefile.am         |  1 +
 sysprep/Makefile.am          |  1 +
 v2v/Makefile.am              |  6 ++---
 v2v/linux.ml                 | 64 ++------------------------------------------
 v2v/stringMap.ml             | 21 ---------------
 v2v/stringMap.mli            | 54 -------------------------------------
 v2v/test-harness/Makefile.am |  1 +
 17 files changed, 168 insertions(+), 140 deletions(-)
 create mode 100644 mllib/stringMap.ml
 create mode 100644 mllib/stringMap.mli
 delete mode 100644 v2v/stringMap.ml
 delete mode 100644 v2v/stringMap.mli

diff --git a/builder/Makefile.am b/builder/Makefile.am
index 5c41cfa..cc9e51d 100644
--- a/builder/Makefile.am
+++ b/builder/Makefile.am
@@ -137,6 +137,7 @@ BOBJECTS = \
 	$(top_builddir)/mllib/guestfs_config.cmo \
 	$(top_builddir)/mllib/common_gettext.cmo \
 	$(top_builddir)/mllib/dev_t.cmo \
+	$(top_builddir)/mllib/stringMap.cmo \
 	$(top_builddir)/mllib/common_utils.cmo \
 	$(top_builddir)/mllib/fsync.cmo \
 	$(top_builddir)/mllib/planner.cmo \
diff --git a/customize/Makefile.am b/customize/Makefile.am
index de3d7e0..fbd584d 100644
--- a/customize/Makefile.am
+++ b/customize/Makefile.am
@@ -96,6 +96,7 @@ BOBJECTS = \
 	$(top_builddir)/mllib/guestfs_config.cmo \
 	$(top_builddir)/mllib/common_gettext.cmo \
 	$(top_builddir)/mllib/dev_t.cmo \
+	$(top_builddir)/mllib/stringMap.cmo \
 	$(top_builddir)/mllib/common_utils.cmo \
 	$(top_builddir)/mllib/regedit.cmo \
 	$(top_builddir)/mllib/URI.cmo \
diff --git a/dib/Makefile.am b/dib/Makefile.am
index ae6e878..3bb992a 100644
--- a/dib/Makefile.am
+++ b/dib/Makefile.am
@@ -60,6 +60,7 @@ BOBJECTS = \
 	$(top_builddir)/mllib/guestfs_config.cmo \
 	$(top_builddir)/mllib/common_gettext.cmo \
 	$(top_builddir)/mllib/dev_t.cmo \
+	$(top_builddir)/mllib/stringMap.cmo \
 	$(top_builddir)/mllib/common_utils.cmo \
 	$(top_builddir)/mllib/mkdtemp.cmo \
 	$(SOURCES_ML:.ml=.cmo)
diff --git a/get-kernel/Makefile.am b/get-kernel/Makefile.am
index 6892fbb..875075f 100644
--- a/get-kernel/Makefile.am
+++ b/get-kernel/Makefile.am
@@ -59,6 +59,7 @@ BOBJECTS = \
 	$(top_builddir)/mllib/guestfs_config.cmo \
 	$(top_builddir)/mllib/common_gettext.cmo \
 	$(top_builddir)/mllib/dev_t.cmo \
+	$(top_builddir)/mllib/stringMap.cmo \
 	$(top_builddir)/mllib/common_utils.cmo \
 	$(top_builddir)/mllib/URI.cmo \
 	$(SOURCES_ML:.ml=.cmo)
diff --git a/mllib/Makefile.am b/mllib/Makefile.am
index e728d54..f533749 100644
--- a/mllib/Makefile.am
+++ b/mllib/Makefile.am
@@ -37,12 +37,14 @@ SOURCES_MLI = \
 	progress.mli \
 	regedit.mli \
 	StatVFS.mli \
+	stringMap.mli \
 	URI.mli
 
 SOURCES_ML = \
 	guestfs_config.ml \
 	$(OCAML_BYTES_COMPAT_ML) \
 	libdir.ml \
+	stringMap.ml \
 	common_gettext.ml \
 	dev_t.ml \
 	common_utils.ml \
@@ -151,6 +153,7 @@ common_utils_tests_BOBJECTS = \
 	guestfs_config.cmo \
 	common_gettext.cmo \
 	dev_t.cmo \
+	stringMap.cmo \
 	common_utils.cmo \
 	common_utils_tests.cmo
 common_utils_tests_XOBJECTS = $(common_utils_tests_BOBJECTS:.cmo=.cmx)
diff --git a/mllib/common_utils.ml b/mllib/common_utils.ml
index 69118da..041f88b 100644
--- a/mllib/common_utils.ml
+++ b/mllib/common_utils.ml
@@ -812,6 +812,69 @@ let truncate_recursive (g : Guestfs.guestfs) dir =
   let files = List.filter g#is_file files in
   List.iter g#truncate files
 
+let debug_augeas_errors g =
+  if verbose () then (
+    try
+      let errors = g#aug_match "/augeas/files//error" in
+      let errors = Array.to_list errors in
+      let map =
+        List.fold_left (
+          fun map error ->
+            let detail_paths = g#aug_match (error ^ "//*") in
+            let detail_paths = Array.to_list detail_paths in
+            List.fold_left (
+              fun map path ->
+                (* path is "/augeas/files/<filename>/error/<field>".  Put
+                 * <filename>, <field> and the value of this Augeas field
+                 * into a map.
+                 *)
+                let i = String.find path "/error/" in
+                assert (i >= 0);
+                let filename = String.sub path 13 (i-13) in
+                let field =
+                  String.sub path (i+7) (String.length path - (i+7)) in
+
+                let detail = g#aug_get path in
+
+                let fmap : string StringMap.t =
+                  try StringMap.find filename map
+                  with Not_found -> StringMap.empty in
+                let fmap = StringMap.add field detail fmap in
+                StringMap.add filename fmap map
+            ) map detail_paths
+        ) StringMap.empty errors in
+
+      let filenames = StringMap.keys map in
+      let filenames = List.sort compare filenames in
+
+      List.iter (
+        fun filename ->
+          eprintf "augeas failed to parse %s:\n" filename;
+          let fmap = StringMap.find filename map in
+          (try
+            let msg = StringMap.find "message" fmap in
+            eprintf " error \"%s\"" msg
+          with Not_found -> ()
+          );
+          (try
+            let line = StringMap.find "line" fmap
+            and char = StringMap.find "char" fmap in
+            eprintf " at line %s char %s" line char
+          with Not_found -> ()
+          );
+          (try
+            let lens = StringMap.find "lens" fmap in
+            eprintf " in lens %s" lens
+          with Not_found -> ()
+          );
+          eprintf "\n"
+      ) filenames;
+
+      flush stderr
+    with
+      Guestfs.Error msg -> eprintf "%s: augeas: %s (ignored)\n" prog msg
+  )
+
 (* Detect type of a file. *)
 let detect_file_type filename =
   let chan = open_in filename in
diff --git a/mllib/common_utils.mli b/mllib/common_utils.mli
index 9b1086c..572719b 100644
--- a/mllib/common_utils.mli
+++ b/mllib/common_utils.mli
@@ -318,6 +318,20 @@ val truncate_recursive : Guestfs.guestfs -> string -> unit
 (** Using the libguestfs API, recurse into the given directory and
     truncate all files found to zero size. *)
 
+val debug_augeas_errors : Guestfs.guestfs -> unit
+(** In verbose mode, any Augeas errors which happened most recently
+    on the handle and printed on standard error.  You should usually
+    call this just after either [g#aug_init] or [g#aug_load].
+
+    Note this doesn't call {!error} if there were any errors on the
+    handle.  It is just for debugging.  It is expected that a
+    subsequent Augeas command will fail, eg. when trying to match
+    an Augeas path which is expected to exist but does not exist
+    because of a parsing error.  In that case turning on debugging
+    will reveal the parse error.
+
+    If not in verbose mode, this does nothing. *)
+
 val detect_file_type : string -> [`GZip | `Tar | `XZ | `Zip | `Unknown]
 (** Detect type of a file (for a very limited range of file types). *)
 
diff --git a/mllib/stringMap.ml b/mllib/stringMap.ml
new file mode 100644
index 0000000..ea47a6e
--- /dev/null
+++ b/mllib/stringMap.ml
@@ -0,0 +1,21 @@
+(* virt-v2v
+ * Copyright (C) 2009-2016 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.
+ *)
+
+include Map.Make (String)
+
+let keys map = fold (fun k _ ks -> k :: ks) map []
diff --git a/mllib/stringMap.mli b/mllib/stringMap.mli
new file mode 100644
index 0000000..b668538
--- /dev/null
+++ b/mllib/stringMap.mli
@@ -0,0 +1,54 @@
+(* virt-v2v
+ * Copyright (C) 2009-2016 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.
+ *)
+
+type key = String.t
+type 'a t = 'a Map.Make(String).t
+
+val empty : 'a t
+val is_empty : 'a t -> bool
+val mem : key -> 'a t -> bool
+val add : key -> 'a -> 'a t -> 'a t
+(*
+val singleton : key -> 'a -> 'a t
+*)
+val remove : key -> 'a t -> 'a t
+(*
+val merge : (key -> 'a option -> 'b option -> 'c option) -> 'a t -> 'b t -> 'c t
+*)
+val compare : ('a -> 'a -> int) -> 'a t -> 'a t -> int
+val equal : ('a -> 'a -> bool) -> 'a t -> 'a t -> bool
+val iter : (key -> 'a -> unit) -> 'a t -> unit
+val fold : (key -> 'a -> 'b -> 'b) -> 'a t -> 'b -> 'b
+(*
+val for_all : (key -> 'a -> bool) -> 'a t -> bool
+val exists : (key -> 'a -> bool) -> 'a t -> bool
+val filter : (key -> 'a -> bool) -> 'a t -> 'a t
+val partition : (key -> 'a -> bool) -> 'a t -> 'a t * 'a t
+val cardinal : 'a t -> int
+val bindings : 'a t -> (key * 'a) list
+val min_binding : 'a t -> key * 'a
+val max_binding : 'a t -> key * 'a
+val choose : 'a t -> key * 'a
+val split : key -> 'a t -> 'a t * 'a option * 'a t
+*)
+val find : key -> 'a t -> 'a
+val map : ('a -> 'b) -> 'a t -> 'b t
+(*
+val mapi : (key -> 'a -> 'b) -> 'a t -> 'b t
+*)
+val keys : 'a t -> key list
diff --git a/resize/Makefile.am b/resize/Makefile.am
index da5d42d..ac9ccc3 100644
--- a/resize/Makefile.am
+++ b/resize/Makefile.am
@@ -61,6 +61,7 @@ BOBJECTS = \
 	$(top_builddir)/mllib/guestfs_config.cmo \
 	$(top_builddir)/mllib/common_gettext.cmo \
 	$(top_builddir)/mllib/dev_t.cmo \
+	$(top_builddir)/mllib/stringMap.cmo \
 	$(top_builddir)/mllib/common_utils.cmo \
 	$(SOURCES_ML:.ml=.cmo)
 XOBJECTS = $(BOBJECTS:.cmo=.cmx)
diff --git a/sparsify/Makefile.am b/sparsify/Makefile.am
index f9f0f8e..133042f 100644
--- a/sparsify/Makefile.am
+++ b/sparsify/Makefile.am
@@ -60,6 +60,7 @@ BOBJECTS = \
 	$(top_builddir)/mllib/guestfs_config.cmo \
 	$(top_builddir)/mllib/common_gettext.cmo \
 	$(top_builddir)/mllib/dev_t.cmo \
+	$(top_builddir)/mllib/stringMap.cmo \
 	$(top_builddir)/mllib/common_utils.cmo \
 	$(top_builddir)/mllib/progress.cmo \
 	$(top_builddir)/mllib/StatVFS.cmo \
diff --git a/sysprep/Makefile.am b/sysprep/Makefile.am
index d4f1173..90f6b04 100644
--- a/sysprep/Makefile.am
+++ b/sysprep/Makefile.am
@@ -109,6 +109,7 @@ BOBJECTS = \
 	$(top_builddir)/mllib/guestfs_config.cmo \
 	$(top_builddir)/mllib/common_gettext.cmo \
 	$(top_builddir)/mllib/dev_t.cmo \
+	$(top_builddir)/mllib/stringMap.cmo \
 	$(top_builddir)/mllib/common_utils.cmo \
 	$(top_builddir)/mllib/URI.cmo \
 	$(top_builddir)/mllib/mkdtemp.cmo \
diff --git a/v2v/Makefile.am b/v2v/Makefile.am
index fedc84d..fc83600 100644
--- a/v2v/Makefile.am
+++ b/v2v/Makefile.am
@@ -51,7 +51,6 @@ SOURCES_MLI = \
 	output_rhev.mli \
 	output_vdsm.mli \
 	OVF.mli \
-	stringMap.mli \
 	target_bus_assignment.mli \
 	types.mli \
 	utils.mli \
@@ -61,7 +60,6 @@ SOURCES_MLI = \
 	xml.mli
 
 SOURCES_ML = \
-	stringMap.ml \
 	types.ml \
 	xml.ml \
 	utils.ml \
@@ -124,6 +122,7 @@ BOBJECTS = \
 	$(top_builddir)/mllib/guestfs_config.cmo \
 	$(top_builddir)/mllib/common_gettext.cmo \
 	$(top_builddir)/mllib/dev_t.cmo \
+	$(top_builddir)/mllib/stringMap.cmo \
 	$(top_builddir)/mllib/common_utils.cmo \
 	$(top_builddir)/mllib/regedit.cmo \
 	$(top_builddir)/mllib/mkdtemp.cmo \
@@ -195,6 +194,7 @@ COPY_TO_LOCAL_BOBJECTS = \
 	$(top_builddir)/mllib/guestfs_config.cmo \
 	$(top_builddir)/mllib/common_gettext.cmo \
 	$(top_builddir)/mllib/dev_t.cmo \
+	$(top_builddir)/mllib/stringMap.cmo \
 	$(top_builddir)/mllib/common_utils.cmo \
 	$(top_builddir)/mllib/JSON.cmo \
 	$(top_builddir)/mllib/StatVFS.cmo \
@@ -409,9 +409,9 @@ v2v_unit_tests_BOBJECTS = \
 	$(top_builddir)/mllib/guestfs_config.cmo \
 	$(top_builddir)/mllib/common_gettext.cmo \
 	$(top_builddir)/mllib/dev_t.cmo \
+	$(top_builddir)/mllib/stringMap.cmo \
 	$(top_builddir)/mllib/common_utils.cmo \
 	$(top_builddir)/mllib/regedit.cmo \
-	stringMap.cmo \
 	types.cmo \
 	xml.cmo \
 	utils.cmo \
diff --git a/v2v/linux.ml b/v2v/linux.ml
index e1451a4..d20194b 100644
--- a/v2v/linux.ml
+++ b/v2v/linux.ml
@@ -31,71 +31,11 @@ module G = Guestfs
  *)
 let rec augeas_init g =
   g#aug_init "/" 1;
-  if verbose () then augeas_debug_errors g
+  debug_augeas_errors g
 
 and augeas_reload g =
   g#aug_load ();
-  if verbose () then augeas_debug_errors g
-
-and augeas_debug_errors g =
-  try
-    let errors = g#aug_match "/augeas/files//error" in
-    let errors = Array.to_list errors in
-    let map =
-      List.fold_left (
-        fun map error ->
-          let detail_paths = g#aug_match (error ^ "//*") in
-          let detail_paths = Array.to_list detail_paths in
-          List.fold_left (
-            fun map path ->
-              (* path is "/augeas/files/<filename>/error/<field>".  Put
-               * <filename>, <field> and the value of this Augeas field
-               * into a map.
-               *)
-              let i = String.find path "/error/" in
-              assert (i >= 0);
-              let filename = String.sub path 13 (i-13) in
-              let field = String.sub path (i+7) (String.length path - (i+7)) in
-
-              let detail = g#aug_get path in
-
-              let fmap : string StringMap.t =
-                try StringMap.find filename map
-                with Not_found -> StringMap.empty in
-              let fmap = StringMap.add field detail fmap in
-              StringMap.add filename fmap map
-          ) map detail_paths
-      ) StringMap.empty errors in
-
-    let filenames = StringMap.keys map in
-    let filenames = List.sort compare filenames in
-
-    List.iter (
-      fun filename ->
-        eprintf "augeas failed to parse %s:\n" filename;
-        let fmap = StringMap.find filename map in
-        (try
-           let msg = StringMap.find "message" fmap in
-           eprintf " error \"%s\"" msg
-         with Not_found -> ()
-        );
-        (try
-           let line = StringMap.find "line" fmap
-           and char = StringMap.find "char" fmap in
-           eprintf " at line %s char %s" line char
-         with Not_found -> ()
-        );
-        (try
-           let lens = StringMap.find "lens" fmap in
-           eprintf " in lens %s" lens
-         with Not_found -> ()
-        );
-        eprintf "\n"
-    ) filenames;
-
-    flush stderr
-  with
-    Guestfs.Error msg -> eprintf "%s: augeas: %s (ignored)\n" prog msg
+  debug_augeas_errors g
 
 let remove g inspect packages =
   if packages <> [] then (
diff --git a/v2v/stringMap.ml b/v2v/stringMap.ml
deleted file mode 100644
index ea47a6e..0000000
--- a/v2v/stringMap.ml
+++ /dev/null
@@ -1,21 +0,0 @@
-(* virt-v2v
- * Copyright (C) 2009-2016 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.
- *)
-
-include Map.Make (String)
-
-let keys map = fold (fun k _ ks -> k :: ks) map []
diff --git a/v2v/stringMap.mli b/v2v/stringMap.mli
deleted file mode 100644
index b668538..0000000
--- a/v2v/stringMap.mli
+++ /dev/null
@@ -1,54 +0,0 @@
-(* virt-v2v
- * Copyright (C) 2009-2016 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.
- *)
-
-type key = String.t
-type 'a t = 'a Map.Make(String).t
-
-val empty : 'a t
-val is_empty : 'a t -> bool
-val mem : key -> 'a t -> bool
-val add : key -> 'a -> 'a t -> 'a t
-(*
-val singleton : key -> 'a -> 'a t
-*)
-val remove : key -> 'a t -> 'a t
-(*
-val merge : (key -> 'a option -> 'b option -> 'c option) -> 'a t -> 'b t -> 'c t
-*)
-val compare : ('a -> 'a -> int) -> 'a t -> 'a t -> int
-val equal : ('a -> 'a -> bool) -> 'a t -> 'a t -> bool
-val iter : (key -> 'a -> unit) -> 'a t -> unit
-val fold : (key -> 'a -> 'b -> 'b) -> 'a t -> 'b -> 'b
-(*
-val for_all : (key -> 'a -> bool) -> 'a t -> bool
-val exists : (key -> 'a -> bool) -> 'a t -> bool
-val filter : (key -> 'a -> bool) -> 'a t -> 'a t
-val partition : (key -> 'a -> bool) -> 'a t -> 'a t * 'a t
-val cardinal : 'a t -> int
-val bindings : 'a t -> (key * 'a) list
-val min_binding : 'a t -> key * 'a
-val max_binding : 'a t -> key * 'a
-val choose : 'a t -> key * 'a
-val split : key -> 'a t -> 'a t * 'a option * 'a t
-*)
-val find : key -> 'a t -> 'a
-val map : ('a -> 'b) -> 'a t -> 'b t
-(*
-val mapi : (key -> 'a -> 'b) -> 'a t -> 'b t
-*)
-val keys : 'a t -> key list
diff --git a/v2v/test-harness/Makefile.am b/v2v/test-harness/Makefile.am
index 97d62f0..dcbe884 100644
--- a/v2v/test-harness/Makefile.am
+++ b/v2v/test-harness/Makefile.am
@@ -62,6 +62,7 @@ BOBJECTS = \
 	$(top_builddir)/mllib/guestfs_config.cmo \
 	$(top_builddir)/mllib/common_gettext.cmo \
 	$(top_builddir)/mllib/dev_t.cmo \
+	$(top_builddir)/mllib/stringMap.cmo \
 	$(top_builddir)/mllib/common_utils.cmo \
 	$(top_builddir)/v2v/xml.cmo \
 	$(SOURCES_ML:.ml=.cmo) \
-- 
2.7.4


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