]> matita.cs.unibo.it Git - helm.git/blob - helm/interface/getter.ml
This commit was manufactured by cvs2svn to create tag 'V6-2'.
[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 exception PerlGetterNotResponding;;
60
61 let update () =
62  let module C = Configuration in
63   let fd = open_in C.servers_file in
64   let servers = ref [] in
65    try
66     while true do
67      servers := (input_line fd) :: !servers
68     done
69    with
70     End_of_file ->
71      let urls_of_uris = mk_urls_of_uris (List.rev !servers) in
72       (try Sys.remove (C.uris_dbm ^ ".db") with _ -> ()) ;
73       let dbm =
74        Dbm.opendbm C.uris_dbm [Dbm.Dbm_wronly ; Dbm.Dbm_create] 0o660
75       in
76        MapOfStrings.iter (fun uri url -> Dbm.add dbm uri url) urls_of_uris ;
77        Dbm.close dbm ;
78        (* Inform also the Perl-getter *)
79        if Sys.command ("wget -O /dev/null http://localhost:8081/update") <> 0
80        then
81         raise PerlGetterNotResponding ;
82 ;;
83
84 (* url_of_uri : uri -> url *)
85 let url_of_uri uri =
86  let dbm = Dbm.opendbm Configuration.uris_dbm [Dbm.Dbm_rdonly] 0o660 in
87   let url = Dbm.find dbm (UriManager.string_of_uri uri) in
88    Dbm.close dbm ;
89    url
90 ;;
91
92 let filedir_of_uri uri =
93  let fn = UriManager.buri_of_uri uri in
94   let fn' = Str.replace_first (Str.regexp ".*:") Configuration.dest fn in
95    fn'
96 ;;
97
98 let name_and_ext_of_uri uri =
99  let str = UriManager.string_of_uri uri in
100   Str.replace_first (Str.regexp ".*/") "" str
101 ;;
102
103 (* get_file : uri -> filename *)
104 let get_file uri =
105  let dir = filedir_of_uri uri in
106   let fn = dir ^ "/" ^ name_and_ext_of_uri uri ^ ".xml" in
107    if not (Sys.file_exists fn) then
108     begin
109      let url = url_of_uri uri in
110       (*CSC: use -q for quiet mode *)
111       if Sys.command ("wget -c -P " ^ dir ^ " \"" ^ url ^"\"") <> 0
112       then
113        raise (ErrorGetting url) ;
114     end ;
115    fn
116 ;;
117
118 (* get : uri -> filename *)
119 (* If uri is the URI of an annotation, the annotated object is processed *)
120 let get uri =
121  let module U = UriManager in
122   get_file
123    (U.uri_of_string
124     (Str.replace_first (Str.regexp "\.types$") ""
125      (Str.replace_first (Str.regexp "\.ann$") "" (U.string_of_uri uri))))
126 ;;
127
128 (* get_ann : uri -> filename *)
129 (* If uri is the URI of an annotation, the annotation file is processed *)
130 let get_ann = get_file;;
131
132 (* get_ann_file_name_and_uri : uri -> filename * annuri *)
133 (* If given an URI, it returns the name of the corresponding *)
134 (* annotation file and the annotation uri                    *)
135 let get_ann_file_name_and_uri uri = 
136  let module U = UriManager in
137   let uri = U.string_of_uri uri in
138    let annuri =
139     U.uri_of_string (
140      if Str.string_match (Str.regexp ".*\.ann$") uri 0 then
141       uri
142      else
143       uri ^ ".ann"
144     )
145    in
146     let dir = filedir_of_uri annuri in
147      let fn = dir ^ "/" ^ name_and_ext_of_uri annuri ^ ".xml" in
148       (fn, annuri)
149 ;;