]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matitacleanLib.ml
matitamake stuff:
[helm.git] / helm / matita / matitacleanLib.ml
1 (* Copyright (C) 2005, HELM Team.
2  * 
3  * This file is part of HELM, an Hypertextual, Electronic
4  * Library of Mathematics, developed at the Computer Science
5  * Department, University of Bologna, Italy.
6  * 
7  * HELM is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  * 
12  * HELM is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with HELM; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://helm.cs.unibo.it/
24  *)
25
26 let debug = false
27 let debug_prerr = if debug then prerr_endline else ignore
28
29 module HGT = Http_getter_types;;
30 module HG = Http_getter;;
31 module HGM = Http_getter_misc;;
32 module UM = UriManager;;
33 module TA = TacticAst;;
34
35 let baseuri_of_baseuri_decl st =
36   let module TA = TacticAst in
37   match st with
38   | TA.Executable (_, TA.Command (_, TA.Set (_, "baseuri", buri))) ->
39       Some buri
40   | _ -> None
41
42 let cache_of_processed_baseuri = Hashtbl.create 1024
43
44 let one_step_depend suri =
45   let buri =
46     try
47       UM.buri_of_uri (UM.uri_of_string suri)
48     with UM.IllFormedUri _ -> suri
49   in
50   if Hashtbl.mem cache_of_processed_baseuri buri then 
51     []
52   else
53     begin
54       Hashtbl.add cache_of_processed_baseuri buri true;
55       let query = 
56         let buri = buri ^ "/" in 
57         let buri = Mysql.escape buri in
58         let obj_tbl = MetadataTypes.obj_tbl () in
59         Printf.sprintf 
60           "SELECT source, h_occurrence FROM %s WHERE h_occurrence LIKE '%s%%'" obj_tbl buri
61       in
62       try 
63         let rc = Mysql.exec (MatitaDb.instance ()) query in
64         let l = ref [] in
65         Mysql.iter rc (
66           fun row -> 
67             match row.(0), row.(1) with 
68             | Some uri, Some occ when Filename.dirname occ = buri -> 
69                 l := uri :: !l
70             | _ -> ());
71         let l = List.sort Pervasives.compare !l in
72         MatitaMisc.list_uniq l
73       with
74         exn -> raise exn (* no errors should be accepted *)
75     end
76
77     
78 let safe_buri_of_suri suri =
79   try
80     UM.buri_of_uri (UM.uri_of_string suri)
81   with
82     UM.IllFormedUri _ -> suri
83
84 let close_uri_list uri_to_remove =
85   (* to remove an uri you have to remove the whole script *)
86   let buri_to_remove = 
87     MatitaMisc.list_uniq 
88       (List.fast_sort Pervasives.compare 
89         (List.map safe_buri_of_suri uri_to_remove))
90   in
91   (* cleand the already visided baseuris *)
92   let buri_to_remove = 
93     List.filter 
94       (fun buri -> 
95         if Hashtbl.mem cache_of_processed_baseuri buri then false
96         else true)
97       buri_to_remove
98   in
99   (* now calculate the list of objects that belong to these baseuris *)
100   let uri_to_remove = 
101     try
102       List.fold_left 
103         (fun acc buri ->
104           let inhabitants = HG.ls (buri ^ "/") in
105           let inhabitants = List.filter 
106               (function HGT.Ls_object _ -> true | _ -> false) 
107             inhabitants
108           in
109           let inhabitants = List.map 
110               (function 
111                | HGT.Ls_object e -> buri ^ "/" ^ e.HGT.uri 
112                | _ -> assert false)
113             inhabitants
114           in
115           inhabitants @ acc)
116       [] buri_to_remove 
117     with HGT.Invalid_URI u -> 
118       MatitaLog.error ("We were listing an invalid buri: " ^ u);
119       exit 1
120   in
121   (* now we want the list of all uri that depend on them *) 
122   let depend = 
123     List.fold_left
124     (fun acc u -> one_step_depend u @ acc) [] uri_to_remove
125   in
126   let depend = 
127     MatitaMisc.list_uniq 
128       (List.fast_sort Pervasives.compare depend) 
129   in
130   uri_to_remove, depend
131
132 let baseuri_of_file file = 
133   let ic = open_in file in
134   let stms = CicTextualParser2.parse_statements (Stream.of_channel ic) in
135   close_in ic;
136   let uri = ref "" in
137   List.iter 
138     (fun stm ->
139       match baseuri_of_baseuri_decl stm with
140       | Some buri -> 
141           let u = MatitaMisc.strip_trailing_slash buri in
142           if String.length u < 5 || String.sub u 0 5 <> "cic:/" then
143             MatitaLog.error (file ^ " sets an incorrect baseuri: " ^ buri);
144           (try 
145             ignore(HG.resolve u)
146           with
147           | HGT.Unresolvable_URI _ -> 
148               MatitaLog.error (file ^ " sets an unresolvable baseuri: "^buri)
149           | HGT.Key_not_found _ -> ());
150           uri := u
151       | None -> ())
152     stms;
153   !uri
154
155 let rec fix uris next =
156   match next with
157   | [] -> uris
158   | l -> let uris, next = close_uri_list l in fix uris next @ uris
159   
160 let clean_baseuris ?(verbose=true) buris =
161   Hashtbl.clear cache_of_processed_baseuri;
162   let buris = List.map HGM.strip_trailing_slash buris in
163   debug_prerr "clean_baseuris called on:";
164   if debug then
165     List.iter debug_prerr buris; 
166   let l = fix [] buris in
167   let l = MatitaMisc.list_uniq (List.fast_sort Pervasives.compare l) in
168   let l = List.map UriManager.uri_of_string l in
169   debug_prerr "clean_baseuri will remove:";
170   if debug then
171     List.iter (fun u -> debug_prerr (UriManager.string_of_uri u)) l; 
172   List.iter (MatitaSync.remove ~verbose) l
173   
174 let is_empty buri = HG.ls (HGM.strip_trailing_slash buri ^ "/") = []
175