[Libguestfs] [PATCH 06/10] builder: split Index_parser.index in an own module

Pino Toscano
Tue, 28 Jul 2015 09:24:46 -0000

Move the index and entry definitions in an own Index module, together
with the (previously internal to Index_parser) print_entry debugging
function.
---
 builder/Makefile.am      |   2 +
 builder/builder.ml       |  36 +++++++--------
 builder/index.ml         | 117 +++++++++++++++++++++++++++++++++++++++++++++++
 builder/index.mli        |  41 +++++++++++++++++
 builder/index_parser.ml  |  96 +-------------------------------------
 builder/index_parser.mli |  24 +---------
 builder/list_entries.ml  |   6 +--
 builder/list_entries.mli |   2 +-
 8 files changed, 185 insertions(+), 139 deletions(-)
 create mode 100644 builder/index.ml
 create mode 100644 builder/index.mli

diff --git a/builder/Makefile.am b/builder/Makefile.am
index 28afeee..597b943 100644
--- a/builder/Makefile.am
+++ b/builder/Makefile.am
@@ -40,6 +40,7 @@ SOURCES_MLI = \
 	cache.mli \
 	downloader.mli \
 	checksums.mli \
+	index.mli \
 	index_parser.mli \
 	ini_reader.mli \
 	languages.mli \
@@ -54,6 +55,7 @@ SOURCES_ML = \
 	pxzcat.ml \
 	setlocale.ml \
 	checksums.ml \
+	index.ml \
 	ini_reader.ml \
 	paths.ml \
 	languages.ml \
diff --git a/builder/builder.ml b/builder/builder.ml
index 6f2b4bd..a30dbd1 100644
--- a/builder/builder.ml
+++ b/builder/builder.ml
@@ -40,7 +40,7 @@ let remove_duplicates index     *)
   let nseen = Hashtbl.create 13 in
   List.iter (
-    fun (name, { Index_parser.arch = arch; revision = revision }) ->
+    fun (name, { Index.arch = arch; revision = revision }) ->
       let id = name, arch in
       try
         let rev = Hashtbl.find nseen id in
@@ -50,7 +50,7 @@ let remove_duplicates index          Hashtbl.add nseen id
revision
   ) index;
   List.filter (
-    fun (name, { Index_parser.arch = arch; revision = revision }) ->
+    fun (name, { Index.arch = arch; revision = revision }) ->
       let id = name, arch in
       try
         let rev = Hashtbl.find nseen (name, arch) in
@@ -165,7 +165,7 @@ let main ()        }
   ) sources in
   let sources = List.append sources repos in
-  let index : Index_parser.index +  let index : Index.index      List.concat (
       List.map (
         fun source ->
@@ -190,11 +190,11 @@ let main ()        (match cache with
       | Some cache ->
         let l = List.filter (
-          fun (_, { Index_parser.hidden = hidden }) ->
+          fun (_, { Index.hidden = hidden }) ->
             hidden <> true
         ) index in
         let l = List.map (
-          fun (name, { Index_parser.revision = revision; arch = arch }) ->
+          fun (name, { Index.revision = revision; arch = arch }) ->
             (name, arch, revision)
         ) l in
         Cache.print_item_status cache ~header:true l
@@ -209,7 +209,7 @@ let main ()        | Some _ ->
         List.iter (
           fun (name,
-               { Index_parser.revision = revision; file_uri = file_uri;
+               { Index.revision = revision; file_uri = file_uri;
                  proxy = proxy }) ->
             let template = name, arch, revision in
             message (f_"Downloading: %s") file_uri;
@@ -228,7 +228,7 @@ let main ()      try
       let item          List.find (
-          fun (name, { Index_parser.aliases = aliases }) ->
+          fun (name, { Index.aliases = aliases }) ->
             match aliases with
             | None -> false
             | Some l -> List.mem arg l
@@ -237,19 +237,19 @@ let main ()      with Not_found -> arg in
   let item      try List.find (
-      fun (name, { Index_parser.arch = a }) ->
+      fun (name, { Index.arch = a }) ->
         name = arg && arch = a
     ) index
     with Not_found ->
       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
+  let sigchecker = entry.Index.sigchecker in

   (match mode with
   | `Notes ->                           (* --notes *)
     let notes -      Languages.find_notes (Languages.languages ())
entry.Index_parser.notes in
+      Languages.find_notes (Languages.languages ()) entry.Index.notes in
     (match notes with
     | notes :: _ ->
       print_endline notes
@@ -267,7 +267,7 @@ let main ()    (* Download the template, or it may be in the
cache. *)
   let template      let template, delete_on_exit -      let {
Index_parser.revision = revision; file_uri = file_uri;
+      let { Index.revision = revision; file_uri = file_uri;
             proxy = proxy } = entry in
       let template = arg, arch, revision in
       message (f_"Downloading: %s") file_uri;
@@ -281,15 +281,15 @@ let main ()    let ()      match entry with
     (* New-style: Using a checksum. *)
-    | { Index_parser.checksums = Some csums } ->
+    | { Index.checksums = Some csums } ->
       Checksums.verify_checksums csums template

-    | { Index_parser.checksums = None } ->
+    | { Index.checksums = None } ->
       (* Old-style: detached signature. *)
       let sigfile          match entry with
-        | { Index_parser.signature_uri = None } -> None
-        | { Index_parser.signature_uri = Some signature_uri } ->
+        | { Index.signature_uri = None } -> None
+        | { Index.signature_uri = Some signature_uri } ->
           let sigfile, delete_on_exit              Downloader.download
downloader signature_uri in
           if delete_on_exit then unlink_on_exit sigfile;
@@ -303,7 +303,7 @@ let main ()
   (* Planner: Input tags. *)
   let itags -    let { Index_parser.size = size; format = format } = entry in
+    let { Index.size = size; format = format } = entry in
     let format_tag        match format with
       | None -> []
@@ -341,7 +341,7 @@ let main ()      b, sz in

   let output_size -    let { Index_parser.size = original_image_size } = entry
in
+    let { Index.size = original_image_size } = entry in

     let size        match size with
@@ -557,7 +557,7 @@ let main ()        let osize = Int64.of_string (List.assoc
`Size otags) in
       let osize = roundup64 osize 512L in
       let oformat = List.assoc `Format otags in
-      let { Index_parser.expand = expand; lvexpand = lvexpand } = entry in
+      let { Index.expand = expand; lvexpand = lvexpand } = entry in
       message (f_"Resizing (using virt-resize) to expand the disk to
%s")
         (human_size osize);
       let preallocation = if oformat = "qcow2" then Some
"metadata" else None in
diff --git a/builder/index.ml b/builder/index.ml
new file mode 100644
index 0000000..3e8cb85
--- /dev/null
+++ b/builder/index.ml
@@ -0,0 +1,117 @@
+(* virt-builder
+ * Copyright (C) 2013-2015 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.
+ *)
+
+open Common_gettext.Gettext
+open Common_utils
+
+open Utils
+
+open Printf
+open Unix
+
+type index = (string * entry) list      (* string = "os-version" *)
+and entry = {
+  printable_name : string option;       (* the name= field *)
+  osinfo : string option;
+  file_uri : string;
+  arch : string;
+  signature_uri : string option;        (* deprecated, will be removed in 1.26
*)
+  checksums : Checksums.csum_t list option;
+  revision : int;
+  format : string option;
+  size : int64;
+  compressed_size : int64 option;
+  expand : string option;
+  lvexpand : string option;
+  notes : (string * string) list;
+  hidden : bool;
+  aliases : string list option;
+
+  sigchecker : Sigchecker.t;
+  proxy : Downloader.proxy_mode;
+}
+
+let print_entry chan (name, { printable_name = printable_name;
+                              file_uri = file_uri;
+                              arch = arch;
+                              osinfo = osinfo;
+                              signature_uri = signature_uri;
+                              checksums = checksums;
+                              revision = revision;
+                              format = format;
+                              size = size;
+                              compressed_size = compressed_size;
+                              expand = expand;
+                              lvexpand = lvexpand;
+                              notes = notes;
+                              aliases = aliases;
+                              hidden = hidden }) +  let fp fs = fprintf chan fs
in
+  fp "[%s]\n" name;
+  (match printable_name with
+  | None -> ()
+  | Some name -> fp "name=%s\n" name
+  );
+  (match osinfo with
+  | None -> ()
+  | Some id -> fp "osinfo=%s\n" id
+  );
+  fp "file=%s\n" file_uri;
+  fp "arch=%s\n" arch;
+  (match signature_uri with
+  | None -> ()
+  | Some uri -> fp "sig=%s\n" uri
+  );
+  (match checksums with
+  | None -> ()
+  | Some checksums ->
+    List.iter (
+      fun c ->
+        fp "checksum[%s]=%s\n"
+          (Checksums.string_of_csum_t c) (Checksums.string_of_csum c)
+    ) checksums
+  );
+  fp "revision=%d\n" revision;
+  (match format with
+  | None -> ()
+  | Some format -> fp "format=%s\n" format
+  );
+  fp "size=%Ld\n" size;
+  (match compressed_size with
+  | None -> ()
+  | Some size -> fp "compressed_size=%Ld\n" size
+  );
+  (match expand with
+  | None -> ()
+  | Some expand -> fp "expand=%s\n" expand
+  );
+  (match lvexpand with
+  | None -> ()
+  | Some lvexpand -> fp "lvexpand=%s\n" lvexpand
+  );
+  List.iter (
+    fun (lang, notes) ->
+      match lang with
+      | "" -> fp "notes=%s\n" notes
+      | lang -> fp "notes[%s]=%s\n" lang notes
+  ) notes;
+  (match aliases with
+  | None -> ()
+  | Some l -> fp "aliases=%s\n" (String.concat " " l)
+  );
+  if hidden then fp "hidden=true\n"
diff --git a/builder/index.mli b/builder/index.mli
new file mode 100644
index 0000000..10ed15a
--- /dev/null
+++ b/builder/index.mli
@@ -0,0 +1,41 @@
+(* virt-builder
+ * Copyright (C) 2013-2015 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 index = (string * entry) list      (* string = "os-version" *)
+and entry = {
+  printable_name : string option;       (* the name= field *)
+  osinfo : string option;
+  file_uri : string;
+  arch : string;
+  signature_uri : string option;        (* deprecated, will be removed in 1.26
*)
+  checksums : Checksums.csum_t list option;
+  revision : int;
+  format : string option;
+  size : int64;
+  compressed_size : int64 option;
+  expand : string option;
+  lvexpand : string option;
+  notes : (string * string) list;
+  hidden : bool;
+  aliases : string list option;
+
+  sigchecker : Sigchecker.t;
+  proxy : Downloader.proxy_mode;
+}
+
+val print_entry : out_channel -> (string * entry) -> unit
diff --git a/builder/index_parser.ml b/builder/index_parser.ml
index 1164ab5..845d0e9 100644
--- a/builder/index_parser.ml
+++ b/builder/index_parser.ml
@@ -24,98 +24,6 @@ open Utils
 open Printf
 open Unix

-type index = (string * entry) list      (* string = "os-version" *)
-and entry = {
-  printable_name : string option;       (* the name= field *)
-  osinfo : string option;
-  file_uri : string;
-  arch : string;
-  signature_uri : string option;        (* deprecated, will be removed in 1.26
*)
-  checksums : Checksums.csum_t list option;
-  revision : int;
-  format : string option;
-  size : int64;
-  compressed_size : int64 option;
-  expand : string option;
-  lvexpand : string option;
-  notes : (string * string) list;
-  hidden : bool;
-  aliases : string list option;
-
-  sigchecker : Sigchecker.t;
-  proxy : Downloader.proxy_mode;
-}
-
-let print_entry chan (name, { printable_name = printable_name;
-                              file_uri = file_uri;
-                              arch = arch;
-                              osinfo = osinfo;
-                              signature_uri = signature_uri;
-                              checksums = checksums;
-                              revision = revision;
-                              format = format;
-                              size = size;
-                              compressed_size = compressed_size;
-                              expand = expand;
-                              lvexpand = lvexpand;
-                              notes = notes;
-                              aliases = aliases;
-                              hidden = hidden }) -  let fp fs = fprintf chan fs
in
-  fp "[%s]\n" name;
-  (match printable_name with
-  | None -> ()
-  | Some name -> fp "name=%s\n" name
-  );
-  (match osinfo with
-  | None -> ()
-  | Some id -> fp "osinfo=%s\n" id
-  );
-  fp "file=%s\n" file_uri;
-  fp "arch=%s\n" arch;
-  (match signature_uri with
-  | None -> ()
-  | Some uri -> fp "sig=%s\n" uri
-  );
-  (match checksums with
-  | None -> ()
-  | Some checksums ->
-    List.iter (
-      fun c ->
-        fp "checksum[%s]=%s\n"
-          (Checksums.string_of_csum_t c) (Checksums.string_of_csum c)
-    ) checksums
-  );
-  fp "revision=%d\n" revision;
-  (match format with
-  | None -> ()
-  | Some format -> fp "format=%s\n" format
-  );
-  fp "size=%Ld\n" size;
-  (match compressed_size with
-  | None -> ()
-  | Some size -> fp "compressed_size=%Ld\n" size
-  );
-  (match expand with
-  | None -> ()
-  | Some expand -> fp "expand=%s\n" expand
-  );
-  (match lvexpand with
-  | None -> ()
-  | Some lvexpand -> fp "lvexpand=%s\n" lvexpand
-  );
-  List.iter (
-    fun (lang, notes) ->
-      match lang with
-      | "" -> fp "notes=%s\n" notes
-      | lang -> fp "notes[%s]=%s\n" lang notes
-  ) notes;
-  (match aliases with
-  | None -> ()
-  | Some l -> fp "aliases=%s\n" (String.concat " " l)
-  );
-  if hidden then fp "hidden=true\n"
-
 let get_index ~downloader ~sigchecker
   { Sources.uri = uri; proxy = proxy }    let corrupt_file () @@ -268,7 +176,7
@@ let get_index ~downloader ~sigchecker
             | Some c -> Some [Checksums.SHA512 c]
             | None -> None in

-          let entry = { printable_name = printable_name;
+          let entry = { Index.printable_name = printable_name;
                         osinfo = osinfo;
                         file_uri = file_uri;
                         arch = arch;
@@ -290,7 +198,7 @@ let get_index ~downloader ~sigchecker

     if verbose () then (
       printf "index file (%s) after parsing (C parser):\n" uri;
-      List.iter (print_entry Pervasives.stdout) entries
+      List.iter (Index.print_entry Pervasives.stdout) entries
     );

     entries
diff --git a/builder/index_parser.mli b/builder/index_parser.mli
index f5b98b7..b8d8ddf 100644
--- a/builder/index_parser.mli
+++ b/builder/index_parser.mli
@@ -16,26 +16,4 @@
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  *)

-type index = (string * entry) list      (* string = "os-version" *)
-and entry = {
-  printable_name : string option;       (* the name= field *)
-  osinfo : string option;
-  file_uri : string;
-  arch : string;
-  signature_uri : string option;        (* deprecated, will be removed in 1.26
*)
-  checksums : Checksums.csum_t list option;
-  revision : int;
-  format : string option;
-  size : int64;
-  compressed_size : int64 option;
-  expand : string option;
-  lvexpand : string option;
-  notes : (string * string) list;
-  hidden : bool;
-  aliases : string list option;
-
-  sigchecker : Sigchecker.t;
-  proxy : Downloader.proxy_mode;
-}
-
-val get_index : downloader:Downloader.t -> sigchecker:Sigchecker.t ->
Sources.source -> index
+val get_index : downloader:Downloader.t -> sigchecker:Sigchecker.t ->
Sources.source -> Index.index
diff --git a/builder/list_entries.ml b/builder/list_entries.ml
index 45c7e8b..4bb899c 100644
--- a/builder/list_entries.ml
+++ b/builder/list_entries.ml
@@ -29,7 +29,7 @@ let rec list_entries ~list_format ~sources index
 and list_entries_short index    List.iter (
-    fun (name, { Index_parser.printable_name = printable_name;
+    fun (name, { Index.printable_name = printable_name;
                  arch = arch;
                  hidden = hidden }) ->
       if not hidden then (
@@ -60,7 +60,7 @@ and list_entries_long ~sources index    ) sources;

   List.iter (
-    fun (name, { Index_parser.printable_name = printable_name;
+    fun (name, { Index.printable_name = printable_name;
                  arch = arch;
                  size = size;
                  compressed_size = compressed_size;
@@ -112,7 +112,7 @@ and list_entries_json ~sources index      ) sources in
   let json_templates      List.map (
-      fun (name, { Index_parser.printable_name = printable_name;
+      fun (name, { Index.printable_name = printable_name;
                    arch = arch;
                    size = size;
                    compressed_size = compressed_size;
diff --git a/builder/list_entries.mli b/builder/list_entries.mli
index 4765f67..a3f35d3 100644
--- a/builder/list_entries.mli
+++ b/builder/list_entries.mli
@@ -16,4 +16,4 @@
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  *)

-val list_entries : list_format:([ `Short | `Long | `Json ]) ->
sources:Sources.source list -> Index_parser.index -> unit
+val list_entries : list_format:([ `Short | `Long | `Json ]) ->
sources:Sources.source list -> Index.index -> unit
--
2.1.0