Coccinelle release 0.2.5-rc4
[bpt/coccinelle.git] / tools / distributed / cleanup.ml
CommitLineData
34e49164
C
1let read_to_diff i =
2 let lines = ref [] in
3 let rec loop _ =
4 let l = input_line i in
5 if Str.string_match (Str.regexp "diff -u -p ") l 0
6 then (List.rev !lines,Some l)
7 else (lines := l::!lines; loop ()) in
8 try loop() with End_of_file -> (List.rev !lines,None)
9
10let get_file l = (* l is a diff line *)
11 match Str.split (Str.regexp " ") l with
12 [_diff;_u;_p;old;_new] -> old
13 | _ -> failwith "bad diff line"
14
15let get_files prefix =
16 let files = Array.to_list(Sys.readdir(Sys.getcwd())) in
34e49164 17 let relevant name =
0708f913
C
18 let rel_re = Str.regexp "\\(.*\\)\\.[0-9]+\\.out" in
19 if Str.string_match rel_re name 0 then
20 let pref = Str.matched_group 1 name in
21 pref = prefix
22 else
23 false
24 in
34e49164
C
25 List.filter relevant files
26
27let process_file fl =
28 let i = open_in fl in
29 let elements = ref [] in
30 (match read_to_diff i with
31 (_,Some first_line) ->
32 let rec loop diff =
33 let (cur,more) = read_to_diff i in
34 elements := (get_file diff,diff::cur) :: !elements;
35 match more with
36 Some next_line -> loop next_line
37 | None -> () in
38 loop first_line
39 | _ -> ()); (* file is empty *)
40 close_in i;
41 !elements
42
43let process_all_files files out =
44 let elements =
45 List.sort compare (List.concat (List.map process_file files)) in
46 match elements with
47 [] -> (* only python output *)
48 let fl = String.concat " " (List.sort compare files) in
49 let _ = Sys.command (Printf.sprintf "cat %s > %s" fl out) in
50 ()
51 | _ ->
52 let o = open_out out in
53 List.iter
54 (function (_,code) ->
55 List.iter
56 (function x -> Printf.fprintf o "%s\n" x)
57 code)
58 elements
59
60let _ =
61 let arg = List.hd(List.tl(Array.to_list Sys.argv)) in
62 Printf.printf "arg %s\n" arg;
63 let arg = Filename.chop_extension arg in
64 let files = get_files arg in
0708f913 65 process_all_files files (arg^".out");
34e49164
C
66 let tmp_files =
67 String.concat " "
68 (List.map (function x -> "tmp."^x) (List.sort compare files)) in
0708f913 69 let _ = Sys.command (Printf.sprintf "cat %s > %s.tmp" tmp_files arg) in
34e49164
C
70 List.iter
71 (function file ->
72 let _ = Sys.command (Printf.sprintf "/bin/rm %s" file) in
73 let _ = Sys.command (Printf.sprintf "/bin/rm tmp.%s" file) in ())
74 files