goaljobs 0.3
[goaljobs.git] / goaljobs.ml
index f3d27b7..208882c 100644 (file)
@@ -33,11 +33,31 @@ exception Goal_result of goal_result_t
 
 let goal_failed msg = raise (Goal_result (Goal_failed msg))
 
+let depth = ref 0
+let indent fs =
+  let do_indent str =
+    prerr_string "| ";
+    for i = 0 to !depth-1 do prerr_string "  " done;
+    prerr_string str;
+    Pervasives.flush Pervasives.stderr
+  in
+  ksprintf do_indent fs
+
 let target v =
   if v then raise (Goal_result Goal_OK)
 let target_all vs = target (List.fold_left (&&) true vs)
 let target_exists vs = target (List.fold_left (||) false vs)
-let require f = f ()
+let require name f =
+  indent "require: %s\n" name;
+  incr depth;
+  let r = (try Either (f ()) with exn -> Or exn) in
+  decr depth;
+  match r with
+  | Either x -> x
+  | Or exn -> raise exn
+
+let _enter_goal name = indent "enter goal: %s\n" name
+let _leave_goal name = indent "leave goal: %s\n" name
 
 type period_t = Seconds | Days | Months | Years
 let seconds = (1, Seconds)
@@ -196,7 +216,7 @@ let url_exists url =
     goal_failed msg
 
 let file_contains_string filename str =
-  let cmd = sprintf "grep -q %s %s" (quote str) (quote filename) in
+  let cmd = sprintf "grep -q -F %s %s" (quote str) (quote filename) in
   match Sys.command cmd with
   | 0 -> true
   | 1 -> false
@@ -340,6 +360,7 @@ let with_memory_locked ?(write = false) f =
   let filename = getenv "HOME" // ".goaljobs-memory" in
   let fd = openfile filename [O_RDWR; O_CREAT] 0o644 in
   lockf fd (if write then F_LOCK else F_RLOCK) 0;
+
   (* If the file is newly created with zero size, write an
    * empty hash table.
    *)
@@ -354,6 +375,7 @@ let with_memory_locked ?(write = false) f =
   (* Run the function. *)
   let r = try Either (f fd) with exn -> Or exn in
   lockf fd F_ULOCK 0;
+  close fd;
   match r with
   | Either x -> x
   | Or exn -> raise exn
@@ -398,6 +420,14 @@ let memory_delete key =
       Pervasives.flush chan;
   )
 
+let memory_list () =
+  with_memory_locked (
+    fun fd ->
+      let chan = in_channel_of_descr fd in
+      let memory : (string, string) Hashtbl.t = input_value chan in
+      Hashtbl.fold (fun key value xs -> (key, value) :: xs) memory []
+  )
+
 let published_goals = ref []
 let publish name fn = published_goals := (name, fn) :: !published_goals
 let get_goal name =