-
Notifications
You must be signed in to change notification settings - Fork 4
/
exec.ml
105 lines (92 loc) · 3.5 KB
/
exec.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
(* Copyright (c) Anil Madhavapeddy <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*)
open Bos
open Rresult
open Astring
let rec iter fn l =
match l with hd :: tl -> fn hd >>= fun () -> iter fn tl | [] -> Ok ()
let stream cmd =
let bin, args =
Cmd.to_list cmd |> function
| hd :: tl -> (hd, Array.of_list (hd :: tl))
| _ -> failwith "invalid command"
in
let open Unix in
Logs.debug (fun l ->
l "Exec: %s" (String.concat ~sep:" " (Array.to_list args)));
let _ = create_process bin args stdin stdout stderr in
match wait () with
| _, WEXITED 0 -> Ok ()
| _, WEXITED e -> Error (`Msg (Fmt.strf "Exited with code %d" e))
| _, _ -> Error (`Msg "Process terminated abruptly")
let run_and_log_s cmd =
OS.File.tmp "opam-tools-run-%s.stderr" >>= fun tmp_file ->
let err = OS.Cmd.err_file tmp_file in
let res = OS.Cmd.(run_out ~err cmd |> out_string) in
match res with
| Ok (stdout, (_, `Exited 0)) -> Ok stdout
| Ok (stdout, _) ->
OS.File.read tmp_file >>= fun stderr ->
Logs.err (fun l ->
l "%a failed. Output was:@.%a%a"
Fmt.(styled `Cyan Cmd.pp)
cmd
Fmt.(styled `Red text)
stderr Fmt.text (String.trim stdout));
Error (`Msg "Command execution failed")
| Error (`Msg m) -> Error (`Msg m)
let run_and_log cmd = run_and_log_s cmd >>= fun _ -> Ok ()
let run_and_log_l cmd =
run_and_log_s cmd >>= fun out ->
R.ok (String.cuts ~sep:"\n" out |> List.map String.trim)
let map fn l =
List.map fn l
|> List.fold_left
(fun acc b ->
match (acc, b) with
| Ok acc, Ok v -> Ok (v :: acc)
| Ok _acc, Error v -> Error v
| (Error _ as e), _ -> e)
(Ok [])
|> function
| Ok v -> Ok (List.rev v)
| e -> e
let opam_version () =
match run_and_log_s Cmd.(v "opam" % "--version") with
| Ok v -> Ok v
| Error (`Msg _) -> Error (`Msg "opam not installed on system")
let ocaml_version ?ocamlc () =
let oc =
match ocamlc with None -> Cmd.v "ocamlc" | Some x -> Cmd.(v @@ p x)
in
match run_and_log_s Cmd.(oc % "-version") with
| Ok s -> (
match Ocaml_version.of_string s with
| Ok v -> Ok v
| Error (`Msg _) ->
Error (`Msg "unable to parse OCaml string from ocamlc") )
| Error (`Msg _) -> Error (`Msg "unable to find an installed ocamlc")
let run_opam args = run_and_log Cmd.(v "opam" %% args % "--color=never")
let run_opam_s args = run_and_log_s Cmd.(v "opam" %% args % "--color=never")
let run_opam_l args = run_and_log_l Cmd.(v "opam" %% args % "--color=never")
let install_ocaml_to ~prefix ~src () =
OS.Dir.with_current src
(fun () ->
run_and_log Cmd.(v "./configure" % "--prefix" % p prefix) >>= fun () ->
run_and_log Cmd.(v "make" % "-j" % "world.opt") >>= fun () ->
run_and_log Cmd.(v "make" % "install"))
()
>>= fun x -> x