From 1aedabd0caf7a0562a3f314dddd13d49e0165c92 Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Wed, 3 Nov 2021 11:47:13 +0000 Subject: [PATCH] Remove dependency on ocaml-extlib We only used a handful of small functions from this library, so we can completely remove the dependency easily. --- README | 3 +-- configure.ac | 5 ----- src/.depend | 2 ++ src/Makefile.am | 2 +- src/collect.ml | 1 - src/csv_output.ml | 6 +++--- src/opt_calendar.ml | 8 ++++++-- src/opt_xml.ml | 2 -- src/redraw.ml | 9 ++++----- src/stream_output.ml | 3 +-- src/top.ml | 1 - src/utils.ml | 23 ++++++++++++++++++++++- src/utils.mli | 6 ++++++ 13 files changed, 46 insertions(+), 25 deletions(-) diff --git a/README b/README index d97fa62..1ad45a2 100644 --- a/README +++ b/README @@ -1,7 +1,7 @@ virt-top ---------------------------------------------------------------------- -Copyright (C) 2007-2016 Richard W.M. Jones, Red Hat Inc. +Copyright (C) 2007-2021 Richard W.M. Jones, Red Hat Inc. http://et.redhat.com/~rjones/virt-top/ http://libvirt.org/ocaml/ http://libvirt.org/ @@ -19,7 +19,6 @@ Requirements OCaml >= 3.11.0 ocaml-libvirt >= 0.6.1.1 (for virDomainGetCPUStats support) -OCaml extlib OCaml curses Optional: diff --git a/configure.ac b/configure.ac index 3286941..7eca0f6 100644 --- a/configure.ac +++ b/configure.ac @@ -57,11 +57,6 @@ if test "x$OCAML_PKG_unix" = "xno"; then AC_MSG_ERROR([Cannot find required OCaml package 'unix']) fi -AC_CHECK_OCAML_PKG(extlib) -if test "x$OCAML_PKG_extlib" = "xno"; then - AC_MSG_ERROR([Cannot find required OCaml package 'extlib']) -fi - AC_CHECK_OCAML_PKG(libvirt) if test "x$OCAML_PKG_libvirt" = "xno"; then AC_MSG_ERROR([Cannot find required OCaml package 'libvirt']) diff --git a/src/.depend b/src/.depend index ce785dd..997a051 100644 --- a/src/.depend +++ b/src/.depend @@ -11,9 +11,11 @@ collect.cmx : \ collect.cmi : \ types.cmi csv_output.cmo : \ + utils.cmi \ collect.cmi \ csv_output.cmi csv_output.cmx : \ + utils.cmx \ collect.cmx \ csv_output.cmi csv_output.cmi : \ diff --git a/src/Makefile.am b/src/Makefile.am index d10f223..aa55078 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -45,7 +45,7 @@ EXTRA_DIST = \ version.ml \ virt-top.pod -OCAMLPACKAGES = -package unix,extlib,curses,str,libvirt +OCAMLPACKAGES = -package unix,curses,str,libvirt if HAVE_PKG_GETTEXT OCAMLPACKAGES += -package gettext-stub diff --git a/src/collect.ml b/src/collect.ml index a1e50a1..0c673e1 100644 --- a/src/collect.ml +++ b/src/collect.ml @@ -21,7 +21,6 @@ module C = Libvirt.Connect module D = Libvirt.Domain open Printf -open ExtList open Utils open Types diff --git a/src/csv_output.ml b/src/csv_output.ml index f23d673..cb373d5 100644 --- a/src/csv_output.ml +++ b/src/csv_output.ml @@ -20,8 +20,8 @@ (* CSV output functions. *) open Printf -open ExtList +open Utils open Collect module C = Libvirt.Connect @@ -92,9 +92,9 @@ let append_csv (_, _, _, _, _, node_info, hostname, _) (* setup *) let cmp (_, { rd_domid = rd_domid1 }) (_, { rd_domid = rd_domid2 }) = compare rd_domid1 rd_domid2 in - let doms = List.sort ~cmp doms in + let doms = List.sort cmp doms in - let string_of_int64_option = Option.map_default Int64.to_string "" in + let string_of_int64_option = map_default Int64.to_string "" in let domain_fields = List.map ( fun (domname, rd) -> diff --git a/src/opt_calendar.ml b/src/opt_calendar.ml index fd93704..89c5598 100644 --- a/src/opt_calendar.ml +++ b/src/opt_calendar.ml @@ -22,14 +22,18 @@ open CalendarLib open Printf -open ExtString open Opt_gettext.Gettext ;; Top.parse_date_time := fun time -> let cal : Calendar.t = - if String.starts_with time "+" then ( (* +something *) + (* time is "+something" *) + let is_plus = + let n = String.length time in + n >= 1 && time.[0] = '+' + in + if is_plus then ( let period = String.sub time 1 (String.length time - 1) in let period = if String.contains period ':' then ( (* +HH:MM:SS *) diff --git a/src/opt_xml.ml b/src/opt_xml.ml index 1037b85..25ef0ad 100644 --- a/src/opt_xml.ml +++ b/src/opt_xml.ml @@ -19,8 +19,6 @@ This file contains all code which requires xml-light. *) -open ExtList - open Opt_gettext.Gettext module C = Libvirt.Connect diff --git a/src/redraw.ml b/src/redraw.ml index 0403158..7031e66 100644 --- a/src/redraw.ml +++ b/src/redraw.ml @@ -17,7 +17,6 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *) -open ExtList open Curses open Printf @@ -137,7 +136,7 @@ let redraw display_mode sort_order if r <> 0 then r else compare name1 name2 in - List.sort ~cmp doms in + List.sort cmp doms in (* Print domains. *) attron A.reverse; @@ -316,7 +315,7 @@ let redraw display_mode sort_order if r <> 0 then r else compare (dev1, name1) (dev2, name2) in - List.sort ~cmp devs in + List.sort cmp devs in (* Print the header for network devices. *) attron A.reverse; @@ -428,7 +427,7 @@ let redraw display_mode sort_order if r <> 0 then r else compare (dev1, name1) (dev2, name2) in - List.sort ~cmp devs in + List.sort cmp devs in (* Print the header for block devices. *) attron A.reverse; @@ -491,7 +490,7 @@ let redraw display_mode sort_order (* Time to grab another historical %CPU for the list? *) if time >= !historical_cpu_last_time +. float historical_cpu_delay then ( - historical_cpu := percent_cpu :: List.take 10 !historical_cpu; + historical_cpu := percent_cpu :: list_take 10 !historical_cpu; historical_cpu_last_time := time ); diff --git a/src/stream_output.ml b/src/stream_output.ml index c3af99b..2b9e087 100644 --- a/src/stream_output.ml +++ b/src/stream_output.ml @@ -20,7 +20,6 @@ (* [--stream] mode output functions. *) open Printf -open ExtList open Utils open Collect @@ -54,7 +53,7 @@ let append_stream (_, _, _, _, _, node_info, hostname, _) (* setup *) | Inactive, Inactive -> 0) in let cmp (name1, dom1) (name2, dom2) = compare(dom1, dom2) in - List.sort ~cmp doms in + List.sort cmp doms in (*Print domains *) let dump_domain = fun name rd -> begin diff --git a/src/top.ml b/src/top.ml index c38239c..5fb6e03 100644 --- a/src/top.ml +++ b/src/top.ml @@ -18,7 +18,6 @@ *) open Printf -open ExtList open Curses open Opt_gettext.Gettext diff --git a/src/utils.ml b/src/utils.ml index 4332ff7..1f00803 100644 --- a/src/utils.ml +++ b/src/utils.ml @@ -75,6 +75,14 @@ let trimr ?(test = isspace) str = let trim ?(test = isspace) str = trimr (triml str) +(* Split string on the first instance of 'sep' character. *) +let split str sep = + try + let i = String.index str sep in + String.sub str 0 i, String.sub str (i+1) (String.length str - 1) + with + Not_found -> str, "" + (* Read a configuration file as a list of (key, value) pairs. * If the config file is missing this returns an empty list. *) @@ -103,7 +111,7 @@ let read_config_file filename = (* Convert to key, value pairs. *) List.map ( fun (lineno, line) -> - let key, value = ExtString.String.split line " " in + let key, value = split line ' ' in lineno, trim key, trim value ) lines @@ -117,6 +125,19 @@ let pad width str = else (* if n < width then *) str ^ String.make (width-n) ' ' ) +(* Take up to n elements of xs, if available. *) +let rec list_take n xs = + if n <= 0 then [] + else ( + match xs with + | [] -> [] + | x :: xs -> x :: list_take (n-1) xs + ) + +let map_default f def = function + | None -> def + | Some v -> f v + module Show = struct (* Show a percentage in 4 chars. *) let percent percent = diff --git a/src/utils.mli b/src/utils.mli index 3c966f8..2679dc9 100644 --- a/src/utils.mli +++ b/src/utils.mli @@ -36,6 +36,12 @@ val read_config_file : string -> (int * string * string) list (* Pad or truncate a string to a fixed width. *) val pad : int -> string -> string +(* Take up to n elements of xs, if available. *) +val list_take : int -> 'a list -> 'a list + +(* Apply function f to [Some v], return default for [None] *) +val map_default : ('a -> 'b) -> 'b -> 'a option -> 'b + (* Int64 operators for convenience. *) val (+^) : int64 -> int64 -> int64 val (-^) : int64 -> int64 -> int64 -- 1.8.3.1