-
Notifications
You must be signed in to change notification settings - Fork 12
/
ousMisc.ml
67 lines (53 loc) · 1.77 KB
/
ousMisc.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
open OusTypes
let msg fmt = Printf.kprintf print_endline fmt
let (|>) a f = f a
let (@@) f a = f a
let (/) = Filename.concat
let (@>) f g x = g (f x)
module StringMap = Map.Make(struct type t = string let compare = compare end)
type 'a stringmap = 'a StringMap.t
let lines_of_string s =
let rex = Re.(compile (char '\n')) in
let s = Re.(replace_string (compile @@ seq [ bos; rep space]) "" s) in
let s = Re.(replace_string (compile @@ seq [ rep space; eos]) "" s) in
Re_pcre.split ~rex s
let lines_of_channel ic =
let rec aux acc =
let l = try Some (input_line ic) with End_of_file -> None in
match l with
| Some s -> aux (s::acc)
| None -> acc
in
List.rev (aux [])
let lines_of_file f =
if not (Sys.file_exists f) then [] else
let ic = open_in f in
let lines = lines_of_channel ic in
close_in ic;
lines
let lines_of_command c =
let ic = Unix.open_process_in c in
let lines = lines_of_channel ic in
close_in ic;
lines
let rec mkdir_p dir =
if Sys.file_exists dir then () else
(mkdir_p (Filename.dirname dir);
Unix.mkdir dir 0o777)
let lines_to_file ?(remove_if_empty=false) lines f =
if remove_if_empty && lines = [] && Sys.file_exists f then Unix.unlink f else
mkdir_p (Filename.dirname f);
let oc = open_out f in
List.iter (fun line -> output_string oc line; output_char oc '\n') lines;
close_out oc
let opam_var v =
let cmd = Printf.sprintf "opam var %s" v in
match lines_of_command cmd with
| [value] -> value
| _ -> failwith (Printf.sprintf "Bad answer from '%s'" cmd)
let home =
try Sys.getenv "HOME"
with Not_found -> failwith "Could not get the HOME variable"
let has_command c =
let cmd = Printf.sprintf "/bin/sh -c \"command -v %s\" >/dev/null" c in
try Sys.command cmd = 0 with Sys_error _ -> false