]> matita.cs.unibo.it Git - helm.git/blob - components/library/librarian.ml
missing files added
[helm.git] / components / library / librarian.ml
1 exception UnableToQualify
2
3 let find_root path =
4   let paths = List.rev (Str.split (Str.regexp "/") path) in
5   let rec build = function
6     | he::tl as l -> ("/" ^ String.concat "/" (List.rev l) ^ "/") :: build tl
7     | [] -> ["/"]
8   in
9   let paths = List.map HExtlib.normalize_path (build paths) in
10   HExtlib.find_in paths "root"
11 ;;
12   
13 let is_prefix_of d1 d2 =
14     let len1 = String.length d1 in
15     let len2 = String.length d2 in
16     if len2 < len1 then 
17       false
18     else
19       let pref = String.sub d2 0 len1 in
20       pref = d1 && (len1 = len2 || d2.[len1] = '/')
21 ;;
22
23 let ensure_trailing_slash s = 
24   if s = "" then "/" else
25   if s.[String.length s-1] <> '/' then s^"/" else s
26 ;;
27
28 let remove_trailing_slash s = 
29   if s = "" then "" else
30   let len = String.length s in
31   if s.[len-1] = '/' then String.sub s 0 (len-1) else s
32 ;;
33
34 let find_root_for ~include_paths file = 
35  let include_paths = "" :: Sys.getcwd () :: include_paths in
36  let path = HExtlib.find_in include_paths file in
37  (* HLog.debug ("file "^file^" resolved as "^path); *)
38  let rootpath, root, buri = 
39    try
40      let mburi = Helm_registry.get "matita.baseuri" in
41      match Str.split (Str.regexp " ") mburi with
42      | [root; buri] when is_prefix_of root path -> ":registry:", root, buri
43      | _ -> raise (Helm_registry.Key_not_found "matita.baseuri")
44    with Helm_registry.Key_not_found "matita.baseuri" -> 
45      let rootpath = find_root path in
46      let data = HExtlib.input_file rootpath in
47      let buri = 
48        let lines = Str.split (Str.regexp "\n") data in
49        let lines = 
50          List.map (fun l -> 
51            match Str.split (Str.regexp "=") l with
52            | [k;v] -> Pcre.replace ~pat:" " k, Pcre.replace ~pat:" " v
53            | _ -> raise (Failure ("Malformed root file: " ^ rootpath)))
54            lines
55        in
56        List.assoc "baseuri" lines
57      in
58      rootpath, Filename.dirname rootpath, buri
59  in
60  (* HLog.debug ("file "^file^" rooted by "^rootpath^""); *)
61  let uri = Http_getter_misc.strip_trailing_slash buri in
62  if String.length uri < 5 || String.sub uri 0 5 <> "cic:/" then
63    HLog.error (rootpath ^ " sets an incorrect baseuri: " ^ buri);
64  ensure_trailing_slash root, remove_trailing_slash uri, path
65 ;;
66
67 let filename () =
68   try
69     Helm_registry.get "matita.filename"
70   with Helm_registry.Key_not_found "matita.filename" ->
71     try
72       match Helm_registry.get_list Helm_registry.string "matita.args" with
73       | [x] -> 
74          HLog.warn ("Using matita.args as filename: "^x);
75          Helm_registry.set "matita.filename" x;
76          x
77       | _ -> raise (Failure "no (or more than one) filename to compile")
78     with Helm_registry.Key_not_found "matita.filename" -> 
79       raise (Failure "Unable to get current file name")
80 ;;  
81   
82 let baseuri_of_script ?(include_paths=[]) file = 
83   let root, buri, path = find_root_for ~include_paths file in
84   let path = HExtlib.normalize_path path in
85   let root = HExtlib.normalize_path root in
86   let lpath = Str.split (Str.regexp "/") path in
87   let lroot = Str.split (Str.regexp "/") root in
88   let rec substract l1 l2 =
89     match l1, l2 with
90     | h1::tl1,h2::tl2 when h1 = h2 -> substract tl1 tl2
91     | l,[] -> l
92     | _ -> raise UnableToQualify
93   in
94   let extra_buri = substract lpath lroot in
95   let chop name = 
96     assert(Filename.check_suffix name ".ma");
97     try Filename.chop_extension name
98     with Invalid_argument "Filename.chop_extension" -> name
99   in
100    root,
101    remove_trailing_slash (HExtlib.normalize_path 
102     (buri ^ "/" ^ chop (String.concat "/" extra_buri))),
103    path
104 ;;
105
106 let find_roots_in_dir dir =
107   HExtlib.find ~test:(fun f ->
108     Filename.basename f = "root" &&
109     try (Unix.stat f).Unix.st_kind = Unix.S_REG 
110     with Unix.Unix_error _ -> false)
111     dir
112 ;;
113