[Libguestfs] [PATCH 4/5] mllib: add libosinfo DB reading helpers

Cédric Bosdonnat
Tue, 03 Jan 2017 10:18:55 -0000

There is already a libosinfo reading function located in src folder to
get the iso informations. Provide a similar but more generic function
to be used in ocaml tools.
---
 mllib/Makefile.am   | 12 ++++++++--
 mllib/osinfo.ml     | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 mllib/osinfo.mli    | 21 ++++++++++++++++++
 mllib/osinfopath.ml |  1 +
 4 files changed, 96 insertions(+), 2 deletions(-)
 create mode 100644 mllib/osinfo.ml
 create mode 100644 mllib/osinfo.mli
 create mode 100644 mllib/osinfopath.ml

diff --git a/mllib/Makefile.am b/mllib/Makefile.am
index 1a21f825f..eb9753e45 100644
--- a/mllib/Makefile.am
+++ b/mllib/Makefile.am
@@ -19,7 +19,7 @@ include $(top_srcdir)/subdir-rules.mk

 EXTRA_DIST = \
 	$(SOURCES_MLI) \
-	$(filter-out guestfs_config.ml libdir.ml,$(SOURCES_ML)) \
+	$(filter-out guestfs_config.ml libdir.ml osinfopath.ml,$(SOURCES_ML)) \
 	$(SOURCES_C) \
 	common_utils_tests.ml \
 	getopt_tests.ml \
@@ -40,6 +40,7 @@ SOURCES_MLI = \
 	getopt.mli \
 	JSON.mli \
 	mkdtemp.mli \
+	osinfo.mli \
 	planner.mli \
 	progress.mli \
 	regedit.mli \
@@ -71,7 +72,9 @@ SOURCES_ML = \
 	exit.ml \
 	checksums.ml \
 	xml.ml \
-	xpath_helpers.ml
+	xpath_helpers.ml \
+	osinfopath.ml \
+	osinfo.ml

 SOURCES_C = \
 	../cat/visit.c \
@@ -173,6 +176,11 @@ libdir.ml: Makefile
 	echo 'let libdir = "$(libdir)"' > $@-t
 	mv $@-t $@

+osinfopath.ml: Makefile
+	echo 'let osinfopath = "$(datadir)/libosinfo/db"' > $@-t
+	mv $@-t $@
+
+
 # Tests.

 common_utils_tests_SOURCES = dummy.c
diff --git a/mllib/osinfo.ml b/mllib/osinfo.ml
new file mode 100644
index 000000000..7630c4f9c
--- /dev/null
+++ b/mllib/osinfo.ml
@@ -0,0 +1,64 @@
+(* virt-builder
+ * Copyright (C) 2016 - SUSE 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_utils
+open Osinfopath
+
+let osinfo_db_read_three_levels os_path filter +  let distros = Array.to_list
(Sys.readdir os_path) in
+  remove_duplicates (
+    List.concat (
+      List.map (
+        fun distro ->
+          let distro_path = os_path // distro in
+          let os_files = Array.to_list (Sys.readdir distro_path) in
+          List.map (
+            fun os_file ->
+              let file_path = distro_path // os_file in
+              let xml = read_whole_file file_path in
+              let doc = Xml.parse_memory xml in
+              let xpathctx = Xml.xpath_new_context doc in
+              filter xpathctx
+          ) os_files
+      ) distros
+    )
+  )
+
+let osinfo_db_read_flat os_path filter +  let os_files = Array.to_list
(Sys.readdir os_path) in
+  remove_duplicates (
+    List.map (
+      fun os_file ->
+        let file_path = os_path // os_file in
+        let xml = read_whole_file file_path in
+        let doc = Xml.parse_memory xml in
+        let xpathctx = Xml.xpath_new_context doc in
+        filter xpathctx
+    ) os_files
+  )
+
+let osinfo_read_db filter +  let path = try
+    Sys.getenv "OSINFO_SYSTEM_DIR"
+  with Not_found -> "/usr/share/osinfo" in
+
+  try osinfo_db_read_three_levels (path // "os") filter with
+  | _ ->
+    try osinfo_db_read_three_levels (osinfopath // "os") filter with
+    | _ ->
+      try osinfo_db_read_flat (osinfopath // "oses") filter with
+      | _ -> []
diff --git a/mllib/osinfo.mli b/mllib/osinfo.mli
new file mode 100644
index 000000000..4b881408e
--- /dev/null
+++ b/mllib/osinfo.mli
@@ -0,0 +1,21 @@
+(* virt-builder
+ * Copyright (C) 2016 - SUSE 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.
+ *)
+
+val osinfo_read_db : (Xml.xpathctx -> 'a) -> 'a list
+(** [osinfo_read_db filter] runs the [filter] function on the
+    XPath context of every osinfo os description XML file. *)
diff --git a/mllib/osinfopath.ml b/mllib/osinfopath.ml
new file mode 100644
index 000000000..a58b8d60f
--- /dev/null
+++ b/mllib/osinfopath.ml
@@ -0,0 +1 @@
+let osinfopath = "/usr/local/share/libosinfo/db"
--
2.11.0