Source file common.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
let (+>) o f = f o
let spf = Printf.sprintf
exception Timeout
exception UnixExit of int
let rec drop n xs =
match (n,xs) with
| (0,_) -> xs
| (_,[]) -> failwith "drop: not enough"
| (n,x::xs) -> drop (n-1) xs
let take n xs =
let rec next n xs acc =
match (n,xs) with
| (0,_) -> List.rev acc
| (_,[]) -> failwith "Common.take: not enough"
| (n,x::xs) -> next (n-1) xs (x::acc) in
next n xs []
let rec enum_orig x n =
if x = n then [n] else x::enum_orig (x+1) n
let enum x n =
if not(x <= n)
then failwith (Printf.sprintf "bad values in enum, expect %d <= %d" x n);
let rec enum_aux acc x n =
if x = n then n::acc else enum_aux (x::acc) (x+1) n
in
List.rev (enum_aux [] x n)
let push v l =
l := v :: !l
let debugger = ref false
let unwind_protect f cleanup =
if !debugger then f () else
try f ()
with e -> begin cleanup e; raise e end
let finalize f cleanup =
try
let res = f () in
cleanup ();
res
with e ->
cleanup ();
raise e
let (unlines: string list -> string) = fun s ->
(String.concat "\n" s) ^ "\n"
let (lines: string -> string list) = fun s ->
let rec lines_aux = function
| [] -> []
| [x] -> if x = "" then [] else [x]
| x::xs ->
x::lines_aux xs
in
Str.split_delim (Str.regexp "\n") s +> lines_aux
let save_excursion reference newv f =
let old = !reference in
reference := newv;
finalize f (fun _ -> reference := old;)
let memoized ?(use_cache=true) h k f =
if not use_cache
then f ()
else
try Hashtbl.find h k
with Not_found ->
let v = f () in
begin
Hashtbl.add h k v;
v
end
exception Todo
exception Impossible
exception Multi_found
let exn_to_s exn =
Printexc.to_string exn
let pr s =
print_string s;
print_string "\n";
flush stdout
let pr2 s =
prerr_string s;
prerr_string "\n";
flush stderr
let pr_xxxxxxxxxxxxxxxxx () =
pr "-----------------------------------------------------------------------"
let pr2_xxxxxxxxxxxxxxxxx () =
pr2 "-----------------------------------------------------------------------"
let _already_printed = Hashtbl.create 101
let disable_pr2_once = ref false
let xxx_once f s =
if !disable_pr2_once then pr2 s
else
if not (Hashtbl.mem _already_printed s)
then begin
Hashtbl.add _already_printed s true;
f ("(ONCE) " ^ s);
end
let pr2_once s = xxx_once pr2 s
open Printf
open Obj
let rec dump2 r =
if is_int r then
string_of_int (magic r : int)
else (
let rec get_fields acc = function
| 0 -> acc
| n -> let n = n-1 in get_fields (field r n :: acc) n
in
let rec is_list r =
if is_int r then (
if (magic r : int) = 0 then true
else false
) else (
let s = size r and t = tag r in
if t = 0 && s = 2 then is_list (field r 1)
else false
)
in
let rec get_list r =
if is_int r then []
else let h = field r 0 and t = get_list (field r 1) in h :: t
in
let opaque name =
"<" ^ name ^ ">"
in
let s = size r and t = tag r in
if is_list r then (
let fields = get_list r in
"[" ^ String.concat "; " (List.map dump2 fields) ^ "]"
)
else if t = 0 then (
let fields = get_fields [] s in
"(" ^ String.concat ", " (List.map dump2 fields) ^ ")"
)
else if t = lazy_tag then opaque "lazy"
else if t = closure_tag then opaque "closure"
else if t = object_tag then (
let fields = get_fields [] s in
let clasz, id, slots =
match fields with h::h'::t -> h, h', t | _ -> assert false in
"Object #" ^ dump2 id ^
" (" ^ String.concat ", " (List.map dump2 slots) ^ ")"
)
else if t = infix_tag then opaque "infix"
else if t = forward_tag then opaque "forward"
else if t < no_scan_tag then (
let fields = get_fields [] s in
"Tag" ^ string_of_int t ^
" (" ^ String.concat ", " (List.map dump2 fields) ^ ")"
)
else if t = string_tag then (
"\"" ^ String.escaped (magic r : string) ^ "\""
)
else if t = double_tag then (
string_of_float (magic r : float)
)
else if t = abstract_tag then opaque "abstract"
else if t = custom_tag then opaque "custom"
else failwith ("dump: impossible tag (" ^ string_of_int t ^ ")")
)
let dump v = dump2 (repr v)
let pr2_gen x = pr2 (dump x)
type prof = ProfAll | ProfNone | ProfSome of string list
let profile = ref ProfNone
let show_trace_profile = ref false
let check_profile category =
match !profile with
| ProfAll -> true
| ProfNone -> false
| ProfSome l -> List.mem category l
let _profile_table = ref (Hashtbl.create 100)
let adjust_profile_entry category difftime =
let (xtime, xcount) =
(try Hashtbl.find !_profile_table category
with Not_found ->
let xtime = ref 0.0 in
let xcount = ref 0 in
Hashtbl.add !_profile_table category (xtime, xcount);
(xtime, xcount)
) in
xtime := !xtime +. difftime;
xcount := !xcount + 1;
()
let profile_start category = failwith "todo"
let profile_end category = failwith "todo"
let profile_code category f =
if not (check_profile category)
then f ()
else begin
if !show_trace_profile then pr2 (spf "> %s" category);
let t = Unix.gettimeofday () in
let res, prefix =
try Some (f ()), ""
with Timeout -> None, "*"
in
let category = prefix ^ category in
let t' = Unix.gettimeofday () in
if !show_trace_profile then pr2 (spf "< %s" category);
adjust_profile_entry category (t' -. t);
(match res with
| Some res -> res
| None -> raise Timeout
);
end
let _is_in_exclusif = ref (None: string option)
let profile_code_exclusif category f =
if not (check_profile category)
then f ()
else begin
match !_is_in_exclusif with
| Some s ->
failwith (spf "profile_code_exclusif: %s but already in %s " category s);
| None ->
_is_in_exclusif := (Some category);
finalize
(fun () ->
profile_code category f
)
(fun () ->
_is_in_exclusif := None
)
end
let profile_code_inside_exclusif_ok category f =
failwith "Todo"
let (with_open_stringbuf: (((string -> unit) * Buffer.t) -> unit) -> string) =
fun f ->
let buf = Buffer.create 1000 in
let pr s = Buffer.add_string buf (s ^ "\n") in
f (pr, buf);
Buffer.contents buf
let profile_diagnostic () =
if !profile = ProfNone then "" else
let xs =
Hashtbl.fold (fun k v acc -> (k,v)::acc) !_profile_table []
+> List.sort (fun (k1, (t1,n1)) (k2, (t2,n2)) -> compare t2 t1)
in
with_open_stringbuf (fun (pr,_) ->
pr "---------------------";
pr "profiling result";
pr "---------------------";
xs +> List.iter (fun (k, (t,n)) ->
pr (Printf.sprintf "%-40s : %10.3f sec %10d count" k !t !n)
)
)
let report_if_take_time timethreshold s f =
let t = Unix.gettimeofday () in
let res = f () in
let t' = Unix.gettimeofday () in
if (t' -. t > float_of_int timethreshold)
then pr2 (Printf.sprintf "Note: processing took %7.1fs: %s" (t' -. t) s);
res
let profile_code2 category f =
profile_code category (fun () ->
if !profile = ProfAll
then pr2 ("starting: " ^ category);
let t = Unix.gettimeofday () in
let res = f () in
let t' = Unix.gettimeofday () in
if !profile = ProfAll
then pr2 (spf "ending: %s, %fs" category (t' -. t));
res
)
let get_value filename =
let chan = open_in filename in
let x = input_value chan in
(close_in chan; x)
let write_value valu filename =
let chan = open_out filename in
(output_value chan valu;
close_out chan)
type arg_spec_full = Arg.key * Arg.spec * Arg.doc
type cmdline_options = arg_spec_full list
type options_with_title = string * string * arg_spec_full list
type cmdline_sections = options_with_title list
let parse_options options usage_msg argv =
let args = ref [] in
(try
Arg.parse_argv argv options (fun file -> args := file::!args) usage_msg;
args := List.rev !args;
!args
with
| Arg.Bad msg -> Printf.eprintf "%s" msg; exit 2
| Arg.Help msg -> Printf.printf "%s" msg; exit 0
)
let usage usage_msg options =
Arg.usage (Arg.align options) usage_msg
let arg_align2 xs =
Arg.align xs +> List.rev +> drop 2 +> List.rev
let short_usage usage_msg ~short_opt =
usage usage_msg short_opt
let long_usage usage_msg ~short_opt ~long_opt =
pr usage_msg;
pr "";
let all_options_with_title =
(("main options", "", short_opt)::long_opt) in
all_options_with_title +> List.iter
(fun (title, explanations, xs) ->
pr title;
pr_xxxxxxxxxxxxxxxxx();
if explanations <> ""
then begin pr explanations; pr "" end;
arg_align2 xs +> List.iter (fun (key,action,s) ->
pr (" " ^ key ^ s)
);
pr "";
);
()
let arg_parse2 l msg short_usage_fun =
let args = ref [] in
let f = (fun file -> args := file::!args) in
let l = Arg.align l in
(try begin
Arg.parse_argv Sys.argv l f msg;
args := List.rev !args;
!args
end
with
| Arg.Bad msg ->
let xs = lines msg in
pr2 (List.hd xs);
short_usage_fun();
raise (UnixExit (2))
| Arg.Help msg ->
raise Impossible
)
type flag_spec = Arg.key * Arg.spec * Arg.doc
type action_spec = Arg.key * Arg.doc * action_func
and action_func = (string list -> unit)
type cmdline_actions = action_spec list
exception WrongNumberOfArguments
let options_of_actions action_ref actions =
actions +> List.map (fun (key, doc, _func) ->
(key, (Arg.Unit (fun () -> action_ref := key)), doc)
)
let (action_list: cmdline_actions -> Arg.key list) = fun xs ->
List.map (fun (a,b,c) -> a) xs
let (do_action: Arg.key -> string list -> cmdline_actions -> unit) =
fun key args xs ->
let assoc = xs +> List.map (fun (a,b,c) -> (a,c)) in
let action_func = List.assoc key assoc in
action_func args
let mk_action_0_arg f =
(function
| [] -> f ()
| _ -> raise WrongNumberOfArguments
)
let mk_action_1_arg f =
(function
| [file] -> f file
| _ -> raise WrongNumberOfArguments
)
let mk_action_2_arg f =
(function
| [file1;file2] -> f file1 file2
| _ -> raise WrongNumberOfArguments
)
let mk_action_3_arg f =
(function
| [file1;file2;file3] -> f file1 file2 file3
| _ -> raise WrongNumberOfArguments
)
let mk_action_4_arg f =
(function
| [file1;file2;file3;file4] -> f file1 file2 file3 file4
| _ -> raise WrongNumberOfArguments
)
let mk_action_n_arg f = f
let (=|=) : int -> int -> bool = (=)
let (=<=) : char -> char -> bool = (=)
let (=$=) : string -> string -> bool = (=)
let (=:=) : bool -> bool -> bool = (=)
let (=*=) = (=)
let (>>=) m1 m2 =
match m1 with
| None -> None
| Some x -> m2 x
let map_opt f = function
| None -> None
| Some x -> Some (f x)
let do_option f = function
| None -> ()
| Some x -> f x
let opt = do_option
let (|||) a b =
match a with
| Some x -> x
| None -> b
type ('a,'b) either = Left of 'a | Right of 'b
type ('a, 'b, 'c) either3 = Left3 of 'a | Middle3 of 'b | Right3 of 'c
let partition_either f l =
let rec part_either left right = function
| [] -> (List.rev left, List.rev right)
| x :: l ->
(match f x with
| Left e -> part_either (e :: left) right l
| Right e -> part_either left (e :: right) l) in
part_either [] [] l
let partition_either3 f l =
let rec part_either left middle right = function
| [] -> (List.rev left, List.rev middle, List.rev right)
| x :: l ->
(match f x with
| Left3 e -> part_either (e :: left) middle right l
| Middle3 e -> part_either left (e :: middle) right l
| Right3 e -> part_either left middle (e :: right) l) in
part_either [] [] [] l
let rec filter_some = function
| [] -> []
| None :: l -> filter_some l
| Some e :: l -> e :: filter_some l
let map_filter f xs = xs +> List.map f +> filter_some
let rec find_some_opt p = function
| [] -> None
| x :: l ->
match p x with
| Some v -> Some v
| None -> find_some_opt p l
let find_some p xs =
match find_some_opt p xs with
| None -> raise Not_found
| Some x -> x
let rec find_opt f xs =
find_some_opt (fun x -> if f x then Some x else None) xs
let (matched: int -> string -> string) = fun i s ->
Str.matched_group i s
let matched1 = fun s -> matched 1 s
let matched2 = fun s -> (matched 1 s, matched 2 s)
let matched3 = fun s -> (matched 1 s, matched 2 s, matched 3 s)
let matched4 = fun s -> (matched 1 s, matched 2 s, matched 3 s, matched 4 s)
let matched5 = fun s -> (matched 1 s, matched 2 s, matched 3 s, matched 4 s, matched 5 s)
let matched6 = fun s -> (matched 1 s, matched 2 s, matched 3 s, matched 4 s, matched 5 s, matched 6 s)
let matched7 = fun s -> (matched 1 s, matched 2 s, matched 3 s, matched 4 s, matched 5 s, matched 6 s, matched 7 s)
let _memo_compiled_regexp = Hashtbl.create 101
let candidate_match_func s re =
let compile_re =
memoized _memo_compiled_regexp re (fun () -> Str.regexp re)
in
Str.string_match compile_re s 0
let match_func s re =
profile_code "Common.=~" (fun () -> candidate_match_func s re)
let (=~) s re =
match_func s re
let split sep s = Str.split (Str.regexp sep) s
let join sep xs = String.concat sep xs
let i_to_s = string_of_int
let s_to_i = int_of_string
let null_string s =
s =$= ""
type filename = string
type dirname = string
type path = string
let chop_dirsymbol = function
| s when s =~ "\\(.*\\)/$" -> matched1 s
| s -> s
let filename_without_leading_path prj_path s =
let prj_path = chop_dirsymbol prj_path in
if s =$= prj_path
then "."
else
if s =~ ("^" ^ prj_path ^ "/\\(.*\\)$")
then matched1 s
else
failwith
(spf "cant find filename_without_project_path: %s %s" prj_path s)
let readable ~root s =
filename_without_leading_path root s
let is_directory file =
(Unix.stat file).Unix.st_kind =*= Unix.S_DIR
let command2 s = ignore(Sys.command s)
exception CmdError of Unix.process_status * string
let process_output_to_list2 ?(verbose=false) command =
let chan = Unix.open_process_in command in
let res = ref ([] : string list) in
let rec process_otl_aux () =
let e = input_line chan in
res := e::!res;
if verbose then pr2 e;
process_otl_aux() in
try process_otl_aux ()
with End_of_file ->
let stat = Unix.close_process_in chan in (List.rev !res,stat)
let cmd_to_list ?verbose command =
let (l,exit_status) = process_output_to_list2 ?verbose command in
match exit_status with
| Unix.WEXITED 0 -> l
| _ -> raise (CmdError (exit_status,
(spf "CMD = %s, RESULT = %s"
command (String.concat "\n" l))))
let cmd_to_list_and_status = process_output_to_list2
let cat file =
let chan = open_in file in
let rec cat_aux acc () =
let (b, l) = try (true, input_line chan) with End_of_file -> (false, "") in
if b
then cat_aux (l::acc) ()
else acc
in
cat_aux [] () +> List.rev +> (fun x -> close_in chan; x)
let read_file file =
let ic = open_in file in
let size = in_channel_length ic in
let buf = Bytes.create size in
really_input ic buf 0 size;
close_in ic;
buf |> Bytes.to_string
let write_file ~file s =
let chan = open_out file in
(output_string chan s; close_out chan)
let filemtime file =
(Unix.stat file).Unix.st_mtime
let fullpath file =
if not (Sys.file_exists file)
then failwith (spf "fullpath: file (or directory) %s does not exist" file);
let dir, base =
if Sys.is_directory file
then file, None
else Filename.dirname file, Some (Filename.basename file)
in
let old = Sys.getcwd () in
Sys.chdir dir;
let here = Sys.getcwd () in
Sys.chdir old;
match base with
| None -> here
| Some x -> Filename.concat here x
let cache_computation2 ?(verbose=false) ?(use_cache=true) file ext_cache f =
if not use_cache
then f ()
else begin
if not (Sys.file_exists file)
then begin
pr2 ("WARNING: cache_computation: can't find file " ^ file);
pr2 ("defaulting to calling the function");
f ()
end else begin
let file_cache = (file ^ ext_cache) in
if Sys.file_exists file_cache &&
filemtime file_cache >= filemtime file
then begin
if verbose then pr2 ("using cache: " ^ file_cache);
get_value file_cache
end
else begin
let res = f () in
write_value res file_cache;
res
end
end
end
let cache_computation ?verbose ?use_cache a b c =
profile_code "Common.cache_computation" (fun () ->
cache_computation2 ?verbose ?use_cache a b c)
let (with_open_outfile: filename -> (((string -> unit) * out_channel) -> 'a) -> 'a) =
fun file f ->
let chan = open_out file in
let pr s = output_string chan s in
unwind_protect (fun () ->
let res = f (pr, chan) in
close_out chan;
res)
(fun e -> close_out chan)
let (with_open_infile: filename -> ((in_channel) -> 'a) -> 'a) = fun file f ->
let chan = open_in file in
unwind_protect (fun () ->
let res = f chan in
close_in chan;
res)
(fun e -> close_in chan)
let timeout_function ?(verbose=false) timeoutval = fun f ->
try
begin
Sys.set_signal Sys.sigalrm (Sys.Signal_handle (fun _ -> raise Timeout ));
ignore(Unix.alarm timeoutval);
let x = f () in
ignore(Unix.alarm 0);
x
end
with Timeout ->
begin
if verbose then pr2 "timeout (we abort)";
raise Timeout;
end
| e ->
begin
ignore(Unix.alarm 0);
if verbose then pr2 "exn while in timeout_function";
raise e
end
let _temp_files_created = ref ([] : filename list)
let new_temp_file prefix suffix =
let processid = i_to_s (Unix.getpid ()) in
let tmp_file = Filename.temp_file (prefix ^ "-" ^ processid ^ "-") suffix in
push tmp_file _temp_files_created;
tmp_file
let save_tmp_files = ref false
let erase_temp_files () =
if not !save_tmp_files then begin
!_temp_files_created +> List.iter (fun s ->
command2 ("rm -f " ^ s)
);
_temp_files_created := []
end
let erase_this_temp_file f =
if not !save_tmp_files then begin
_temp_files_created :=
List.filter (function x -> not (x =$= f)) !_temp_files_created;
command2 ("rm -f " ^ f)
end
let exclude p xs =
List.filter (fun x -> not (p x)) xs
let rec (span: ('a -> bool) -> 'a list -> 'a list * 'a list) =
fun p -> function
| [] -> ([], [])
| x::xs ->
if p x then
let (l1, l2) = span p xs in
(x::l1, l2)
else ([], x::xs)
let rec take_safe n xs =
match (n,xs) with
| (0,_) -> []
| (_,[]) -> []
| (n,x::xs) -> x::take_safe (n-1) xs
let group_by f xs =
let h = Hashtbl.create 101 in
let hkeys = Hashtbl.create 101 in
xs |> List.iter (fun x ->
let k = f x in
Hashtbl.replace hkeys k true;
Hashtbl.add h k x
);
Hashtbl.fold (fun k _ acc -> (k, Hashtbl.find_all h k)::acc) hkeys []
let group_by_multi fkeys xs =
let h = Hashtbl.create 101 in
let hkeys = Hashtbl.create 101 in
xs |> List.iter (fun x ->
let ks = fkeys x in
ks |> List.iter (fun k ->
Hashtbl.replace hkeys k true;
Hashtbl.add h k x;
)
);
Hashtbl.fold (fun k _ acc -> (k, Hashtbl.find_all h k)::acc) hkeys []
let rec group_by_mapped_key fkey l =
match l with
| [] -> []
| x::xs ->
let k = fkey x in
let (xs1,xs2) = List.partition (fun x' -> let k2 = fkey x' in k=*=k2) xs
in
(k, (x::xs1))::(group_by_mapped_key fkey xs2)
let rec zip xs ys =
match (xs,ys) with
| ([],[]) -> []
| ([],_) -> failwith "zip: not same length"
| (_,[]) -> failwith "zip: not same length"
| (x::xs,y::ys) -> (x,y)::zip xs ys
let null xs =
match xs with [] -> true | _ -> false
let index_list xs =
if null xs then []
else zip xs (enum 0 ((List.length xs) -1))
let index_list_0 xs = index_list xs
let index_list_1 xs =
xs +> index_list +> List.map (fun (x,i) -> x, i+1)
let sort_prof a b =
profile_code "Common.sort_by_xxx" (fun () -> List.sort a b)
type order = HighFirst | LowFirst
let compare_order order a b =
match order with
| HighFirst -> compare b a
| LowFirst -> compare a b
let sort_by_val_highfirst xs =
sort_prof (fun (k1,v1) (k2,v2) -> compare v2 v1) xs
let sort_by_val_lowfirst xs =
sort_prof (fun (k1,v1) (k2,v2) -> compare v1 v2) xs
let sort_by_key_highfirst xs =
sort_prof (fun (k1,v1) (k2,v2) -> compare k2 k1) xs
let sort_by_key_lowfirst xs =
sort_prof (fun (k1,v1) (k2,v2) -> compare k1 k2) xs
type ('a, 'b) assoc = ('a * 'b) list
let hash_to_list h =
Hashtbl.fold (fun k v acc -> (k,v)::acc) h []
+> List.sort compare
let hash_of_list xs =
let h = Hashtbl.create 101 in
xs +> List.iter (fun (k, v) -> Hashtbl.replace h k v);
h
type 'a hashset = ('a, bool) Hashtbl.t
let hashset_to_list h =
hash_to_list h +> List.map fst
let hashset_of_list xs =
xs +> List.map (fun x -> x, true) +> hash_of_list
let hkeys h =
let hkey = Hashtbl.create 101 in
h +> Hashtbl.iter (fun k v -> Hashtbl.replace hkey k true);
hashset_to_list hkey
let group_assoc_bykey_eff2 xs =
let h = Hashtbl.create 101 in
xs +> List.iter (fun (k, v) -> Hashtbl.add h k v);
let keys = hkeys h in
keys +> List.map (fun k -> k, Hashtbl.find_all h k)
let group_assoc_bykey_eff xs =
profile_code "Common.group_assoc_bykey_eff" (fun () ->
group_assoc_bykey_eff2 xs)
type 'a stack = 'a list
let sort xs = List.sort compare xs
let exn_to_real_unixexit f =
try f ()
with UnixExit x -> exit x
let pp_do_in_zero_box f = Format.open_box 0; f (); Format.close_box ()
let main_boilerplate f =
if not (!Sys.interactive) then
exn_to_real_unixexit (fun () ->
Sys.set_signal Sys.sigint (Sys.Signal_handle (fun _ ->
pr2 "C-c intercepted, will do some cleaning before exiting";
Sys.set_signal Sys.sigint Sys.Signal_default;
raise (UnixExit (-1))
));
if Sys.argv +> Array.to_list +> List.exists (fun x -> x =$= "-debugger")
then debugger := true;
finalize (fun ()->
pp_do_in_zero_box (fun () ->
try
f ();
with Unix.Unix_error (e, fm, argm) ->
pr2 (spf "exn Unix_error: %s %s %s\n"
(Unix.error_message e) fm argm);
raise (Unix.Unix_error (e, fm, argm))
))
(fun()->
if !profile <> ProfNone
then begin
pr2 (profile_diagnostic ());
Gc.print_stat stderr;
end;
erase_temp_files ();
)
)
let follow_symlinks = ref false
let arg_symlink () =
if !follow_symlinks
then " -L "
else ""
let grep_dash_v_str =
"| grep -v /.hg/ |grep -v /CVS/ | grep -v /.git/ |grep -v /_darcs/" ^
"| grep -v /.svn/ | grep -v .git_annot | grep -v .marshall"
let files_of_dir_or_files_no_vcs_nofilter xs =
xs +> List.map (fun x ->
if is_directory x
then
let cmd = (spf "find %s '%s' -noleaf -type f %s"
(arg_symlink()) x grep_dash_v_str) in
let (xs, status) =
cmd_to_list_and_status cmd in
(match status with
| Unix.WEXITED 0 -> xs
| _ -> raise (CmdError (status,
(spf "CMD = %s, RESULT = %s"
cmd (String.concat "\n" xs))))
)
else [x]
) +> List.concat
module SMap = Map.Make (String)
type 'a smap = 'a SMap.t