Source file make_runtime.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
open! Import
module Unix = UnixLabels
module type S = sig
type t
val create : unit -> t
(** {1 I/O} *)
include Vcs.Trait.File_system.S with type t := t
(** {1 Running the git command line} *)
type process_output
val vcs_cli
: ?env:string array
-> t
-> cwd:Absolute_path.t
-> args:string list
-> f:(process_output -> ('a, Err.t) Result.t)
-> ('a, Err.t) Result.t
end
module type M = sig
val executable_basename : string
module Output : sig
type t
module Private : sig
val of_process_output : Vcs.Private.Process_output.t -> t
end
end
end
module Found_executable = struct
type t =
{ filename : string
; path : string
}
end
let find_executable ~path ~executable_basename =
let rec loop = function
| [] -> None
| path :: rest ->
let fn = Filename.concat path executable_basename in
if Sys.file_exists fn then Some fn else loop rest
in
loop (String.split path ~on:':')
;;
type t =
{ executable_basename : string
; executable : Found_executable.t option
}
let load_file (_ : t) ~path =
Vcs.Private.try_with (fun () ->
In_channel.with_open_bin (Absolute_path.to_string path) In_channel.input_all
|> Vcs.File_contents.create)
;;
let save_file (_ : t) ?(perms = 0o666) () ~path ~(file_contents : Vcs.File_contents.t) =
Vcs.Private.try_with (fun () ->
let oc =
open_out_gen
[ Open_wronly; Open_creat; Open_trunc; Open_binary ]
perms
(Absolute_path.to_string path)
in
Fun.protect
~finally:(fun () -> close_out_noerr oc)
(fun () -> Out_channel.output_string oc (file_contents :> string)))
;;
let read_dir (_ : t) ~dir =
Vcs.Private.try_with (fun () ->
let entries = Sys.readdir (Absolute_path.to_string dir) in
Array.sort entries ~compare:String.compare;
entries |> Array.map ~f:Fsegment.v |> Array.to_list)
;;
module Exit_status = struct
[@@@coverage off]
type t =
[ `Exited of int
| `Signaled of int
| `Stopped of int
| `Unknown
]
[@@deriving_inline sexp_of]
let sexp_of_t =
(function
| `Exited v__001_ ->
Sexplib0.Sexp.List [ Sexplib0.Sexp.Atom "Exited"; sexp_of_int v__001_ ]
| `Signaled v__002_ ->
Sexplib0.Sexp.List [ Sexplib0.Sexp.Atom "Signaled"; sexp_of_int v__002_ ]
| `Stopped v__003_ ->
Sexplib0.Sexp.List [ Sexplib0.Sexp.Atom "Stopped"; sexp_of_int v__003_ ]
| `Unknown -> Sexplib0.Sexp.Atom "Unknown"
: t -> Sexplib0.Sexp.t)
;;
[@@@deriving.end]
end
module Lines = struct
type t = string list
let sexp_of_t (t : t) =
match t with
| [] -> Sexp.Atom ""
| [ hd ] -> Sexp.Atom (hd : string)
| _ :: _ :: _ as lines -> Sexp.List (List.map lines ~f:(fun line -> Sexp.Atom line))
;;
let create string : t = String.split_lines string
end
exception Uncaught_user_exn of exn * Printexc.raw_backtrace
let create ~executable_basename =
let executable =
match Stdlib.Sys.getenv_opt "PATH" with
| None -> None [@coverage off]
| Some path ->
(match find_executable ~path ~executable_basename with
| None -> None
| Some filename -> Some { Found_executable.filename; path })
in
{ executable_basename; executable }
;;
let rec waitpid_non_intr pid =
try Unix.waitpid ~mode:[] pid with
| Unix.Unix_error (EINTR, _, _) -> waitpid_non_intr pid [@coverage off]
;;
let read_all_from_fd fd =
let out = In_channel.input_all (Unix.in_channel_of_descr fd) in
Unix.close fd;
out
;;
let vcs_cli ~of_process_output ?env t ~cwd ~args ~f =
let env = Option.map env ~f:Array.to_list in
let executable_basename = t.executable_basename in
let prog =
match t.executable with
| None -> executable_basename
| Some { filename; path } ->
(match env with
| None -> filename
| Some bindings ->
(match
List.find_map bindings ~f:(fun var -> String.chop_prefix var ~prefix:"PATH=")
with
| None -> filename
| Some path_override ->
if String.equal path path_override
then filename
else (
match find_executable ~path:path_override ~executable_basename with
| None -> executable_basename
| Some filename -> filename)))
in
let exit_status_r : Exit_status.t ref = ref `Unknown in
let stdout_r = ref "" in
let stderr_r = ref "" in
try
let stdin_reader, stdin_writer = Spawn.safe_pipe () in
let stdout_reader, stdout_writer = Spawn.safe_pipe () in
let stderr_reader, stderr_writer = Spawn.safe_pipe () in
let pid =
Spawn.spawn
?env:(env |> Option.map ~f:Spawn.Env.of_list)
~cwd:(Path (Absolute_path.to_string cwd))
~prog
~argv:(executable_basename :: args)
~stdin:stdin_reader
~stdout:stdout_writer
~stderr:stderr_writer
()
in
Unix.close stdin_reader;
Unix.close stdin_writer;
Unix.close stdout_writer;
Unix.close stderr_writer;
let stdout = read_all_from_fd stdout_reader in
let stderr = read_all_from_fd stderr_reader in
let pid', process_status = waitpid_non_intr pid in
assert (pid = pid');
let exit_status =
match process_status with
| Unix.WEXITED n -> `Exited n
| Unix.WSIGNALED n -> `Signaled n [@coverage off]
| Unix.WSTOPPED n -> `Stopped n [@coverage off]
in
exit_status_r := exit_status;
stdout_r := stdout;
stderr_r := stderr;
let exit_code =
match exit_status with
| `Exited n -> n
| (`Signaled _ | `Stopped _) as exit_status ->
raise_notrace
(Err.E
(Err.create
[ Err.sexp
(List
[ Atom "process terminated abnormally"
; sexp_field (module Exit_status) "exit_status" exit_status
])
])) [@coverage off]
in
match
f (of_process_output { Vcs.Private.Process_output.exit_code; stdout; stderr })
with
| Ok _ as ok -> ok
| Error err -> raise_notrace (Err.E err) [@coverage off]
| exception exn ->
let bt = Printexc.get_raw_backtrace () in
(raise_notrace (Uncaught_user_exn (exn, bt)) [@coverage off])
with
| Uncaught_user_exn (exn, bt) -> Printexc.raise_with_backtrace exn bt
| exn ->
let err = Err.of_exn exn in
Error
(Err.add_context
err
[ Err.sexp
(List
[ sexp_field (module String) "prog" executable_basename
; sexp_field' (List.sexp_of_t String.sexp_of_t) "args" args
; sexp_field (module Exit_status) "exit_status" !exit_status_r
; sexp_field (module Absolute_path) "cwd" cwd
; sexp_field (module Lines) "stdout" (Lines.create !stdout_r)
; sexp_field (module Lines) "stderr" (Lines.create !stderr_r)
])
])
;;
module Make (M : M) = struct
type nonrec t = t
let create () = create ~executable_basename:M.executable_basename
let load_file = load_file
let save_file = save_file
let read_dir = read_dir
let vcs_cli ?env t ~cwd ~args ~f =
vcs_cli ~of_process_output:M.Output.Private.of_process_output ?env t ~cwd ~args ~f
;;
end
module Private = struct
let find_executable = find_executable
end