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
include Base
module Result = struct
include Base.Result
module Let_syntax = struct
let ( let+ ) = ( >>| )
let ( let* ) = ( >>= )
let ( and+ ) = Let_syntax.Let_syntax.both
end
let fold_left ~f l =
List.fold_left l ~init:(Ok ()) ~f:(fun acc el ->
bind acc ~f:(fun () -> f el))
let fold_right ~f l =
List.fold_right l ~init:(Ok ()) ~f:(fun el acc ->
bind acc ~f:(fun () -> f el))
end
module Filename = struct
include struct
open Caml.Filename
let check_suffix = check_suffix
let chop_extension = chop_extension
let chop_suffix = chop_suffix
let current_dir_name = current_dir_name
let is_implicit = is_implicit
let is_relative = is_relative
let parent_dir_name = parent_dir_name
let dir_sep = dir_sep
let quote = quote
let temp_dir_name = get_temp_dir_name ()
let dirname = dirname
let basename = basename
end
let of_parts = function
| [] ->
failwith "Filename.of_parts: empty parts list"
| root :: rest ->
List.fold rest ~init:root ~f:Caml.Filename.concat
let concat = Caml.Filename.concat
end
module Glob = Glob
module Spin_unix = struct
open Unix
let mkdir ?(perm = 0o777) dirname = mkdir dirname perm
let rec mkdir_p ?perm dir =
let mkdir_idempotent ?perm dir =
match mkdir ?perm dir with
| () ->
()
| exception Unix_error ((EEXIST | EISDIR), _, _) ->
()
in
match mkdir_idempotent ?perm dir with
| () ->
()
| exception (Unix_error (ENOENT, _, _) as exn) ->
let parent = Filename.dirname dir in
if String.equal parent dir then
raise exn
else (
mkdir_p ?perm parent;
mkdir_idempotent ?perm dir)
let rec rm_p path =
match Caml.Sys.is_directory path with
| true ->
Caml.Sys.readdir path
|> Array.iter ~f:(fun name -> rm_p (Filename.concat path name));
Unix.rmdir path
| false ->
Caml.Sys.remove path
end
module Spin_lwt = struct
include Lwt
let fold_left ~f l =
let open Syntax in
let+ l =
List.fold_left l ~init:(Lwt.return []) ~f:(fun acc el ->
let* acc = acc in
let+ result = f el in
result :: acc)
in
List.rev l
let fold_right ~f l =
let open Syntax in
let+ l =
List.fold_right l ~init:(Lwt.return []) ~f:(fun el acc ->
let* acc = acc in
let+ result = f el in
result :: acc)
in
List.rev l
let result_fold_left ~f l =
let open Lwt_result.Syntax in
let+ l =
List.fold_left l ~init:(Lwt_result.return []) ~f:(fun acc el ->
let* acc = acc in
let+ result = f el in
result :: acc)
in
List.rev l
let result_fold_right ~f l =
let open Lwt_result.Syntax in
let+ l =
List.fold_right l ~init:(Lwt_result.return []) ~f:(fun el acc ->
let* acc = acc in
let+ result = f el in
result :: acc)
in
List.rev l
type command_result =
{ stdout : string list
; stderr : string list
; status : Unix.process_status
}
let command_result_of_process process =
let open Lwt.Syntax in
let* status = process#status in
let* stdout = Lwt_io.read_lines process#stdout |> Lwt_stream.to_list in
let+ stderr = Lwt_io.read_lines process#stderr |> Lwt_stream.to_list in
{ stdout; stderr; status }
let prepare_args cmd args = "", Array.of_list (cmd :: args)
let exec cmd args =
Lwt_process.with_process_full
(prepare_args cmd args)
command_result_of_process
let exec_with_logs cmd args =
let open Lwt.Syntax in
let* p_output = exec cmd args in
let* _ =
fold_left p_output.stdout ~f:(fun line ->
Logs_lwt.debug (fun m -> m "stdout of %s: %s" cmd line))
in
match p_output.status with
| WEXITED 0 ->
let+ _ =
fold_left p_output.stderr ~f:(fun line ->
Logs_lwt.debug (fun m -> m "stderr of %s: %s" cmd line))
in
Ok ()
| _ ->
let+ _ =
fold_left p_output.stderr ~f:(fun line ->
Logs_lwt.err (fun m -> m "stderr of %s: %s" cmd line))
in
Error
(Caml.Format.asprintf
"The command %s did not run successfully: %a"
cmd
(Caml.Format.pp_print_list Caml.Format.pp_print_string)
p_output.stderr)
let exec_with_stdout cmd args =
let open Lwt.Syntax in
let* p_output = exec cmd args in
let* _ =
fold_left p_output.stdout ~f:(fun line ->
Logs_lwt.debug (fun m -> m "stdout of %s: %s" cmd line))
in
match p_output.status with
| WEXITED 0 ->
let+ _ =
fold_left p_output.stderr ~f:(fun line ->
Logs_lwt.debug (fun m -> m "stderr of %s: %s" cmd line))
in
let stdout = String.concat ~sep:"\n" p_output.stdout in
Ok stdout
| _ ->
let+ _ =
fold_left p_output.stderr ~f:(fun line ->
Logs_lwt.err (fun m -> m "stderr of %s: %s" cmd line))
in
Error
(Caml.Format.asprintf
"The command %s did not run successfully: %a"
cmd
(Caml.Format.pp_print_list Caml.Format.pp_print_string)
p_output.stderr)
let with_chdir ~dir t =
let open Lwt.Syntax in
let old_cwd = Caml.Sys.getcwd () in
let* () = Lwt_unix.chdir dir in
Lwt.finalize t (fun () -> Lwt_unix.chdir old_cwd)
end
module Spin_sys = struct
include Sys
let rand_digits () =
let rand = Random.State.(bits (make_self_init ()) land 0xFFFFFF) in
Printf.sprintf "%06x" rand
let mk_temp_dir ?(mode = 0o700) ?dir pat =
let dir =
match dir with Some d -> d | None -> Caml.Filename.get_temp_dir_name ()
in
let raise_err msg = raise (Sys_error msg) in
let rec loop count =
if count < 0 then
raise_err "mk_temp_dir: too many failing attemps"
else
let dir = Printf.sprintf "%s/%s%s" dir pat (rand_digits ()) in
try
Unix.mkdir dir mode;
dir
with
| Unix.Unix_error (Unix.EEXIST, _, _) ->
loop (count - 1)
| Unix.Unix_error (Unix.EINTR, _, _) ->
loop count
| Unix.Unix_error (e, _, _) ->
raise_err ("mk_temp_dir: " ^ Unix.error_message e)
in
loop 1000
let ls_dir ?(recursive = true) directory =
if recursive then
let rec loop result = function
| f :: fs when Caml.Sys.is_directory f ->
Caml.Sys.readdir f
|> Array.to_list
|> List.map ~f:(Caml.Filename.concat f)
|> List.append fs
|> loop result
| f :: fs ->
loop (f :: result) fs
| [] ->
result
in
loop [] [ directory ] |> List.rev
else
Caml.Sys.readdir directory
|> Array.to_list
|> List.map ~f:(Caml.Filename.concat directory)
end