+ (* trim trailing "/" *)
+ try
+ if String.sub p (String.length p - 1) 1 <> "/" then p
+ else String.sub p 0 (String.length p - 1)
+ with
+ | Invalid_argument _ -> p
+
+let html_of_library uid ft =
+ let i = ref 0 in
+ let newid () = incr i; ("node" ^ string_of_int !i) in
+
+ let basedir = (Helm_registry.get "matita.rt_base_dir") ^ "/users/" ^ uid in
+
+
+ let branch lpath children =
+ let id = newid () in
+ let name = Filename.basename lpath in
+ let name = if name <> "." then name else "cic:/matita" in
+ let flag =
+ try List.assoc lpath ft
+ with Not_found -> MSynchronized in
+ let szflag = string_of_matita_flag flag in
+ "<span class=\"trigger\" onClick=\"showBranch('" ^ id ^ "','" ^ lpath ^ "')\">\n" ^
+ "<img src=\"treeview/closed.gif\" id=\"I" ^ id ^ "\"/>\n" ^
+ name ^ " " ^ szflag ^ "<br/></span>\n" ^
+ "<span class=\"branch\" id=\"" ^ id ^ "\">\n" ^
+ children ^ "\n</span>"
+ in
+ let leaf lpath =
+ let flag =
+ try List.assoc lpath ft
+ with Not_found -> MSynchronized in
+ let szflag = string_of_matita_flag flag in
+ "<img src=\"treeview/doc.gif\"/>\n" ^
+ "<a href=\"javascript:void(0)\" onClick=\"selectFile('" ^ lpath ^ "')\" onDblClick=\"dialogBox.callback('" ^ lpath ^ "')\">" ^
+ (Filename.basename lpath) ^ " " ^ szflag ^ "</a><br/>"
+ in
+
+ let rec aux path =
+ let lpath filename = path ^ "/" ^ filename in
+ let gpath filename = basedir ^ "/" ^ path ^ "/" ^ filename in
+
+ (* hide hidden dirs ... including svn stuff *)
+ let dirlist =
+ List.filter (fun x -> String.sub x 0 1 <> ".")
+ (Array.to_list (Sys.readdir (basedir ^ "/" ^ path))) in
+ let subdirs = List.filter (fun x -> Sys.is_directory (gpath x)) dirlist in
+
+ (* only .ma scripts, hidden files excluded *)
+ let scripts =
+ List.filter (fun x ->
+ try
+ let i = String.rindex x '.' in
+ let len = String.length x - i in
+ not (Sys.is_directory (gpath x)) &&
+ (String.sub x 0 1 <> ".") && (String.sub x i len = ".ma")
+ with Not_found | Invalid_argument _ -> false) dirlist in
+ let subdirtags =
+ String.concat "\n" (List.map (fun x -> aux (normalize_qfn (lpath x ^ "/"))) subdirs) in
+ let scripttags =
+ String.concat "\n"
+ (List.map (fun x -> leaf (normalize_qfn (lpath x))) scripts)
+ in
+ branch path (subdirtags ^ "\n" ^ scripttags)
+ in
+
+ let res = aux "." in
+ prerr_endline "BEGIN TREE";prerr_endline res;prerr_endline "END TREE";
+ res
+;;
+
+let reset_lib () =
+ let to_be_removed = (Helm_registry.get "matita.rt_base_dir") ^ "/users/*" in
+ ignore (Sys.command ("rm -rf " ^ to_be_removed))
+;;
+
+let update_user user =
+ let rt_dir = Helm_registry.get "matita.rt_base_dir" in
+ let _repo = Helm_registry.get "matita.weblib" in
+
+ let errno, outlines, errlines = exec_process
+ ("svn up --non-interactive " ^ rt_dir ^ "/users/" ^ user ^ "/")
+ in
+ let files, anomalies =
+ List.fold_left (fun (facc,eacc) line ->
+ try
+ (let fname,flags = up_classify line user in
+ (fname, flags)::facc), eacc
+ with
+ | SvnAnomaly l -> facc, l::eacc) ([],[]) outlines
+ in
+ let added = count (fun (_,flags) -> List.mem Add flags) files in
+ let conflict = count (fun (_,flags) -> List.mem Conflict flags) files in
+ let del = count (fun (_,flags) -> List.mem Delete flags) files in
+ let upd = count (fun (_,flags) -> List.mem Update flags) files in
+ let merged = count (fun (_,flags) -> List.mem Merge flags) files in
+
+ let files =
+ List.map (fun (fname,flags) -> fname,matita_flag_of_update flags) files
+ in
+
+ if errno = 0 then files, anomalies, (added,conflict,del,upd,merged)
+ else raise (SvnError (string_of_output outlines errlines))
+;;
+
+let add_files user files =
+ if (List.length files > 0) then
+ (let rt_dir = Helm_registry.get "matita.rt_base_dir" in
+ let _repo = Helm_registry.get "matita.weblib" in
+
+ let files = String.concat " "
+ (List.map ((^) (rt_dir ^ "/users/" ^ user ^ "/")) files) in
+
+ let errno, outlines, errlines =
+ if files <> "" then
+ exec_process ("svn add --non-interactive " ^ files)
+ else 0,[],[]
+ in
+ if errno = 0 then
+ "BEGIN ADD - " ^ user ^ ":\n" ^ (string_of_output outlines errlines) ^ "END ADD - " ^ user ^ "\n\n"
+ else raise (SvnError (string_of_output outlines errlines)))
+ else ("ADD - nothing to do for " ^ user ^ "\n")
+;;
+
+(* this function should only be called by the server itself (or
+ * the admin) at a scheduled time, so no concurrent instances and no CS needed
+ * also, svn should already be safe as far as concurrency is concerned *)
+let commit user files =
+ if (List.length files > 0) then
+ (let rt_dir = Helm_registry.get "matita.rt_base_dir" in
+ let _repo = Helm_registry.get "matita.weblib" in
+
+ let files = String.concat " "
+ (List.map ((^) (rt_dir ^ "/users/" ^ user ^ "/")) files) in
+
+ let errno, outlines, errlines = exec_process
+ ("svn ci --non-interactive --message \"commit by user " ^ user ^ "\" " ^ files)
+ in
+ if errno = 0 then
+ "BEGIN COMMIT - " ^ user ^ ":\n" ^ (string_of_output outlines errlines) ^ "END COMMIT - " ^ user ^ "\n\n"
+ else raise (SvnError (string_of_output outlines errlines)))
+ else ("COMMIT nothing to do for " ^ user ^ "\n")
+;;
+