From 438813bcf327729f9dafc1a56c6cede550435e2e Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Thu, 23 Feb 2012 15:06:17 +0000 Subject: [PATCH] Allow job names to be arbitrary OCaml expressions. --- lib/pa_when.ml | 10 +++++---- tests/parsing/Makefile.am | 2 +- tests/parsing/t040_ocaml_jobnames.ml | 39 ++++++++++++++++++++++++++++++++++++ tools/whenjobs.pod | 31 ++++++++++++++++++++++++++++ 4 files changed, 77 insertions(+), 5 deletions(-) create mode 100644 tests/parsing/t040_ocaml_jobnames.ml diff --git a/lib/pa_when.ml b/lib/pa_when.ml index 764cabc..6e0592d 100644 --- a/lib/pa_when.ml +++ b/lib/pa_when.ml @@ -84,8 +84,10 @@ let lift_expr = M.Expr.meta_expr (* 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 @@ -211,8 +213,8 @@ EXTEND Gram (* "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 diff --git a/tests/parsing/Makefile.am b/tests/parsing/Makefile.am index fe224da..cfc85cb 100644 --- a/tests/parsing/Makefile.am +++ b/tests/parsing/Makefile.am @@ -17,7 +17,7 @@ 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) diff --git a/tests/parsing/t040_ocaml_jobnames.ml b/tests/parsing/t040_ocaml_jobnames.ml new file mode 100644 index 0000000..6990a93 --- /dev/null +++ b/tests/parsing/t040_ocaml_jobnames.ml @@ -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 +>> diff --git a/tools/whenjobs.pod b/tools/whenjobs.pod index 2acec9f..43fe847 100644 --- a/tools/whenjobs.pod +++ b/tools/whenjobs.pod @@ -513,6 +513,37 @@ with C: 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. + -- 1.8.3.1