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).
This commit is contained in:
Pino Toscano
2016-08-02 18:34:09 +02:00
parent c8c181e8d9
commit 398b940ea4
4 changed files with 25 additions and 16 deletions

View File

@@ -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\"

View File

@@ -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 =

View File

@@ -142,6 +142,8 @@ module String = struct
)
end
exception Executable_not_found of string (* executable *)
let (//) = Filename.concat
let ( +^ ) = Int64.add
@@ -316,6 +318,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

View File

@@ -78,6 +78,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. *)
@@ -379,3 +383,8 @@ val inspect_mount_root_ro : Guestfs.guestfs -> string -> unit
val is_btrfs_subvolume : Guestfs.guestfs -> string -> bool
(** Checks if a filesystem is a btrfs subvolume. *)
val which : string -> string
(** Return the full path of the specified executable from [$PATH].
Throw [Executable_not_found] if not available. *)