]> matita.cs.unibo.it Git - helm.git/blob - components/library/librarian.ml
get rid of gragrep, matitamake(Lib) and development windows,
[helm.git] / components / library / librarian.ml
1 exception NoRootFor of string
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   try HExtlib.find_in paths "root"
11   with Failure "find_in" -> raise (NoRootFor path)
12 ;;
13   
14 let ensure_trailing_slash s = 
15   if s = "" then "/" else
16   if s.[String.length s-1] <> '/' then s^"/" else s
17 ;;
18
19 let remove_trailing_slash s = 
20   if s = "" then "" else
21   let len = String.length s in
22   if s.[len-1] = '/' then String.sub s 0 (len-1) else s
23 ;;
24
25 let parse_root rootpath =
26   let data = HExtlib.input_file rootpath in
27   let lines = Str.split (Str.regexp "\n") data in
28   List.map 
29     (fun l -> 
30       match Str.split (Str.regexp "=") l with
31       | [k;v] -> Pcre.replace ~pat:" " k, Pcre.replace ~pat:" " v
32       | _ -> raise (Failure ("Malformed root file: " ^ rootpath)))
33     lines
34 ;;
35
36
37 let find_root_for ~include_paths file = 
38  let include_paths = "" :: Sys.getcwd () :: include_paths in
39  let path = HExtlib.find_in include_paths file in
40  (* HLog.debug ("file "^file^" resolved as "^path); *)
41  let rootpath, root, buri = 
42    try
43      let mburi = Helm_registry.get "matita.baseuri" in
44      match Str.split (Str.regexp " ") mburi with
45      | [root; buri] when HExtlib.is_prefix_of root path -> 
46          ":registry:", root, buri
47      | _ -> raise (Helm_registry.Key_not_found "matita.baseuri")
48    with Helm_registry.Key_not_found "matita.baseuri" -> 
49      let rootpath = find_root path in
50      let buri = List.assoc "baseuri" (parse_root rootpath) in
51      rootpath, Filename.dirname rootpath, buri
52  in
53  (* HLog.debug ("file "^file^" rooted by "^rootpath^""); *)
54  let uri = Http_getter_misc.strip_trailing_slash buri in
55  if String.length uri < 5 || String.sub uri 0 5 <> "cic:/" then
56    HLog.error (rootpath ^ " sets an incorrect baseuri: " ^ buri);
57  ensure_trailing_slash root, remove_trailing_slash uri, path
58 ;;
59   
60 let baseuri_of_script ?(include_paths=[]) file = 
61   let root, buri, path = find_root_for ~include_paths file in
62   let path = HExtlib.normalize_path path in
63   let root = HExtlib.normalize_path root in
64   let lpath = Str.split (Str.regexp "/") path in
65   let lroot = Str.split (Str.regexp "/") root in
66   let rec substract l1 l2 =
67     match l1, l2 with
68     | h1::tl1,h2::tl2 when h1 = h2 -> substract tl1 tl2
69     | l,[] -> l
70     | _ -> raise (NoRootFor file)
71   in
72   let extra_buri = substract lpath lroot in
73   let chop name = 
74     assert(Filename.check_suffix name ".ma");
75     try Filename.chop_extension name
76     with Invalid_argument "Filename.chop_extension" -> name
77   in
78    root,
79    remove_trailing_slash (HExtlib.normalize_path 
80     (buri ^ "/" ^ chop (String.concat "/" extra_buri))),
81    path
82 ;;
83
84 let find_roots_in_dir dir =
85   HExtlib.find ~test:(fun f ->
86     Filename.basename f = "root" &&
87     try (Unix.stat f).Unix.st_kind = Unix.S_REG 
88     with Unix.Unix_error _ -> false)
89     dir
90 ;;
91