]> matita.cs.unibo.it Git - helm.git/blob - helm/interface/getter.ml
http_getter reimplemented from scratch
[helm.git] / helm / interface / getter.ml
1 (******************************************************************************)
2 (*                                                                            *)
3 (*                               PROJECT HELM                                 *)
4 (*                                                                            *)
5 (*                Claudio Sacerdoti Coen <sacerdot@cs.unibo.it>               *)
6 (*                                 24/01/2000                                 *)
7 (*                                                                            *)
8 (******************************************************************************)
9
10 exception ErrorGetting of string;;
11
12 module OrderedStrings =
13  struct
14   type t = string
15   let compare (s1 : t) (s2 : t) = compare s1 s2
16  end
17 ;;
18
19 module MapOfStrings = Map.Make(OrderedStrings);;
20
21 let read_index url =
22  let module C = Configuration in
23   if Sys.command ("wget -c -P " ^ C.tmpdir ^ " " ^ url ^ "/\"" ^
24    C.indexname ^ "\"") <> 0
25   then
26    raise (ErrorGetting url) ;
27   let tmpfilename = C.tmpdir ^ "/" ^ C.indexname in
28    let fd = open_in tmpfilename in
29    let uris = ref [] in
30     try
31      while true do
32       uris := (input_line fd) :: !uris
33      done ;
34      [] (* only to make the compiler happy *)
35     with
36      End_of_file ->
37       Sys.remove tmpfilename ;
38       !uris
39 ;;
40
41 (* mk_urls_of_uris list_of_servers_base_urls *)
42 let rec mk_urls_of_uris =
43  function
44     [] -> MapOfStrings.empty
45   | he::tl ->
46      let map = mk_urls_of_uris tl in
47       let uris = read_index he in
48        let url_of_uri uri =
49         let url = uri  ^ ".xml" in
50          let url' = Str.replace_first (Str.regexp "cic:") he url in
51          let url'' = Str.replace_first (Str.regexp "theory:") he url' in
52           url''
53        in
54         List.fold_right
55          (fun uri m -> MapOfStrings.add uri (url_of_uri uri) m)
56          uris map
57 ;;
58
59 let update () =
60  let module C = Configuration in
61   let fd = open_in C.servers_file in
62   let servers = ref [] in
63    try
64     while true do
65      servers := (input_line fd) :: !servers
66     done
67    with
68     End_of_file ->
69      let urls_of_uris = mk_urls_of_uris (List.rev !servers) in
70       (try Sys.remove (C.uris_dbm ^ ".db") with _ -> ()) ;
71       let dbm =
72        Dbm.opendbm C.uris_dbm [Dbm.Dbm_wronly ; Dbm.Dbm_create] 0o660
73       in
74        MapOfStrings.iter (fun uri url -> Dbm.add dbm uri url) urls_of_uris ;
75        Dbm.close dbm
76 ;;
77
78 (* url_of_uri : uri -> url *)
79 let url_of_uri uri =
80  let dbm = Dbm.opendbm Configuration.uris_dbm [Dbm.Dbm_rdonly] 0o660 in
81   let url = Dbm.find dbm (UriManager.string_of_uri uri) in
82    Dbm.close dbm ;
83    url
84 ;;
85
86 let filedir_of_uri uri =
87  let fn = UriManager.buri_of_uri uri in
88   let fn' = Str.replace_first (Str.regexp ".*:") Configuration.dest fn in
89    fn'
90 ;;
91
92 let name_and_ext_of_uri uri =
93  let str = UriManager.string_of_uri uri in
94   Str.replace_first (Str.regexp ".*/") "" str
95 ;;
96
97 (* get_file : uri -> filename *)
98 let get_file uri =
99  let dir = filedir_of_uri uri in
100   let fn = dir ^ "/" ^ name_and_ext_of_uri uri ^ ".xml" in
101    if not (Sys.file_exists fn) then
102     begin
103      let url = url_of_uri uri in
104       (*CSC: use -q for quiet mode *)
105       if Sys.command ("wget -c -P " ^ dir ^ " \"" ^ url ^"\"") <> 0
106       then
107        raise (ErrorGetting url) ;
108     end ;
109    fn
110 ;;
111
112 (* get : uri -> filename *)
113 (* If uri is the URI of an annotation, the annotated object is processed *)
114 let get uri =
115  let module U = UriManager in
116   get_file
117    (U.uri_of_string
118     (Str.replace_first (Str.regexp "\.types$") ""
119      (Str.replace_first (Str.regexp "\.ann$") "" (U.string_of_uri uri))))
120 ;;
121
122 (* get_ann : uri -> filename *)
123 (* If uri is the URI of an annotation, the annotation file is processed *)
124 let get_ann = get_file;;
125
126 (* get_ann_file_name_and_uri : uri -> filename * annuri *)
127 (* If given an URI, it returns the name of the corresponding *)
128 (* annotation file and the annotation uri                    *)
129 let get_ann_file_name_and_uri uri = 
130  let module U = UriManager in
131   let uri = U.string_of_uri uri in
132    let annuri =
133     U.uri_of_string (
134      if Str.string_match (Str.regexp ".*\.ann$") uri 0 then
135       uri
136      else
137       uri ^ ".ann"
138     )
139    in
140     let dir = filedir_of_uri annuri in
141      let fn = dir ^ "/" ^ name_and_ext_of_uri annuri ^ ".xml" in
142       (fn, annuri)
143 ;;