Blob Blame History Raw
From 9ad21a8ae34fc60198afb46cb64a3325b2720b07 Mon Sep 17 00:00:00 2001
From: Pino Toscano <ptoscano@redhat.com>
Date: Tue, 2 Aug 2016 18:34:09 +0200
Subject: [PATCH] mllib: move which and its exception from dib

Rename it from "tool" to "executable" in the process, but otherwise
it is just code motion (with minimal adjustments in dib).

(cherry picked from commit 398b940ea4a6069b1d0233c8a6f5d6ed823eddb9)
---
 dib/dib.ml             |  2 +-
 dib/utils.ml           | 16 +---------------
 mllib/common_utils.ml  | 14 ++++++++++++++
 mllib/common_utils.mli |  9 +++++++++
 4 files changed, 25 insertions(+), 16 deletions(-)

diff --git a/dib/dib.ml b/dib/dib.ml
index de4f242..87af4eb 100644
--- a/dib/dib.ml
+++ b/dib/dib.ml
@@ -297,7 +297,7 @@ $cmd \"$@\"
   (try
     let loc = which "dib-run-parts" in
     do_cp loc (destdir // "fake-bin")
-  with Tool_not_found _ ->
+  with Executable_not_found _ ->
     let fake_dib_run_parts = "\
 #!/bin/sh
 echo \"Please install dib-run-parts on the host\"
diff --git a/dib/utils.ml b/dib/utils.ml
index f316264..a2046cb 100644
--- a/dib/utils.ml
+++ b/dib/utils.ml
@@ -21,8 +21,6 @@ open Common_utils
 
 open Printf
 
-exception Tool_not_found of string (* tool *)
-
 let quote = Filename.quote
 
 let unit_GB howmany =
@@ -97,21 +95,9 @@ let rec remove_dups = function
   | [] -> []
   | x :: xs -> x :: (remove_dups (List.filter ((<>) x) xs))
 
-let which tool =
-  let paths = String.nsplit ":" (Sys.getenv "PATH") in
-  let paths = filter_map (
-    fun p ->
-      let path = p // tool in
-      try Unix.access path [Unix.X_OK]; Some path
-      with Unix.Unix_error _ -> None
-  ) paths in
-  match paths with
-  | [] -> raise (Tool_not_found tool)
-  | x :: _ -> x
-
 let require_tool tool =
   try ignore (which tool)
-  with Tool_not_found tool ->
+  with Executable_not_found tool ->
     error (f_"%s needed but not found") tool
 
 let do_cp src destdir =
diff --git a/mllib/common_utils.ml b/mllib/common_utils.ml
index 2dfe475..7e44029 100644
--- a/mllib/common_utils.ml
+++ b/mllib/common_utils.ml
@@ -141,6 +141,8 @@ module String = struct
       )
 end
 
+exception Executable_not_found of string (* executable *)
+
 let (//) = Filename.concat
 
 let ( +^ ) = Int64.add
@@ -299,6 +301,18 @@ let protect ~f ~finally =
   finally ();
   match r with Either ret -> ret | Or exn -> raise exn
 
+let which executable =
+  let paths = String.nsplit ":" (Sys.getenv "PATH") in
+  let paths = filter_map (
+    fun p ->
+      let path = p // executable in
+      try Unix.access path [Unix.X_OK]; Some path
+      with Unix.Unix_error _ -> None
+  ) paths in
+  match paths with
+  | [] -> raise (Executable_not_found executable)
+  | x :: _ -> x
+
 (* Program name. *)
 let prog = Filename.basename Sys.executable_name
 
diff --git a/mllib/common_utils.mli b/mllib/common_utils.mli
index c66efa7..8179dde 100644
--- a/mllib/common_utils.mli
+++ b/mllib/common_utils.mli
@@ -85,6 +85,10 @@ module String : sig
 end
 (** Override the String module from stdlib. *)
 
+(** Exception thrown by [which] when the specified executable is not found
+    in [$PATH]. *)
+exception Executable_not_found of string (* executable *)
+
 val ( // ) : string -> string -> string
 (** Concatenate directory and filename. *)
 
@@ -372,3 +376,8 @@ val read_first_line_from_file : string -> string
 
 val is_regular_file : string -> bool
 (** Checks whether the file is a regular file. *)
+
+val which : string -> string
+(** Return the full path of the specified executable from [$PATH].
+
+    Throw [Executable_not_found] if not available. *)
-- 
2.7.4