Allow job names to be arbitrary OCaml expressions.
authorRichard W.M. Jones <rjones@redhat.com>
Thu, 23 Feb 2012 15:06:17 +0000 (15:06 +0000)
committerRichard W.M. Jones <rjones@redhat.com>
Thu, 23 Feb 2012 15:13:34 +0000 (15:13 +0000)
lib/pa_when.ml
tests/parsing/Makefile.am
tests/parsing/t040_ocaml_jobnames.ml [new file with mode: 0644]
tools/whenjobs.pod

index 764cabc..6e0592d 100644 (file)
@@ -84,8 +84,10 @@ let lift_expr = M.Expr.meta_expr
 
 (* Handle a top level statement. *)
 let rec call_stmt name (_loc, stmt, sh) =
 
 (* Handle a top level statement. *)
 let rec call_stmt name (_loc, stmt, sh) =
-  let name = if name <> "" then name else unique_job_name () in
-  let name = <:expr< $str:name$ >> in
+  let name =
+    match name with
+    | None -> let name = unique_job_name () in <:expr< $str:name$ >>
+    | Some name -> name in
   match stmt with
   | `When e -> when_stmt _loc name e sh
   | `Every p -> every_stmt _loc name p sh
   match stmt with
   | `When e -> when_stmt _loc name e sh
   | `Every p -> every_stmt _loc name p sh
@@ -211,8 +213,8 @@ EXTEND Gram
 
   (* "str_item" is a top level statement in an OCaml program. *)
   str_item: LEVEL "top" [
 
   (* "str_item" is a top level statement in an OCaml program. *)
   str_item: LEVEL "top" [
-    [ s = statement -> call_stmt "" s ]
-  | [ "job"; name = STRING; s = statement -> call_stmt name s ]
+    [ s = statement -> call_stmt None s ]
+  | [ "job"; name = expr; s = statement -> call_stmt (Some name) s ]
   ];
 
 END
   ];
 
 END
index fe224da..cfc85cb 100644 (file)
@@ -17,7 +17,7 @@
 
 EXTRA_DIST = $(SOURCES) test_load.ml
 
 
 EXTRA_DIST = $(SOURCES) test_load.ml
 
-SOURCES = t010_load.ml t020_simple.ml t030_jobnames.ml
+SOURCES = t010_load.ml t020_simple.ml t030_jobnames.ml t040_ocaml_jobnames.ml
 
 tests = $(SOURCES:.ml=.cmo)
 
 
 tests = $(SOURCES:.ml=.cmo)
 
diff --git a/tests/parsing/t040_ocaml_jobnames.ml b/tests/parsing/t040_ocaml_jobnames.ml
new file mode 100644 (file)
index 0000000..6990a93
--- /dev/null
@@ -0,0 +1,39 @@
+(* whenjobs
+ * Copyright (C) 2012 Red Hat Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *)
+
+(* Test OCaml job names. *)
+
+let prefix = "foo_"
+
+job (prefix ^ "check")
+when foo = "value" :
+<<
+  # nothing
+>>
+
+job (prefix ^ "poll")
+every minute :
+<<
+  # nothing
+>>
+
+(* no job name *)
+when bar = "value" :
+<<
+  # nothing
+>>
index 2acec9f..43fe847 100644 (file)
@@ -513,6 +513,37 @@ with C<job "name">:
 The job name is passed to the shell script in the C<$JOBNAME>
 environment variable.
 
 The job name is passed to the shell script in the C<$JOBNAME>
 environment variable.
 
+=head2 OCAML EXPRESSIONS
+
+As well as simple "every" and "when" expressions, advanced users may
+want to use arbitrary OCaml expressions, functions, etc in the jobs
+script.  These are useful for factoring common code or strings, for
+setting the initial values of variables, or for defining cleanup
+functions.
+
+A simple example of an OCaml expression is:
+
+ let prefix = "daily_"
+ job (prefix ^ "virus_scan")
+ every day :
+ <<
+   # ...
+ >>
+ job (prefix ^ "disk_check")
+ every day :
+ <<
+   # ...
+ >>
+
+which creates two jobs called C<"daily_virus_scan"> and
+C<"daily_disk_check"> (C<^> is the OCaml string concatenation
+operator).
+
+The OCaml expressions run once, when the jobs file is being loaded or
+reloaded.
+