2 * Copyright (C) 2012 Red Hat Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
34 | Expr_int of Big_int.big_int
37 | Expr_and of whenexpr * whenexpr
38 | Expr_or of whenexpr * whenexpr
39 | Expr_lt of whenexpr * whenexpr
40 | Expr_le of whenexpr * whenexpr
41 | Expr_eq of whenexpr * whenexpr
42 | Expr_ge of whenexpr * whenexpr
43 | Expr_gt of whenexpr * whenexpr
44 | Expr_not of whenexpr
45 | Expr_add of whenexpr * whenexpr
46 | Expr_sub of whenexpr * whenexpr
47 | Expr_mul of whenexpr * whenexpr
48 | Expr_div of whenexpr * whenexpr
49 | Expr_mod of whenexpr * whenexpr
50 | Expr_len of whenexpr
51 | Expr_changes of string
52 | Expr_increases of string
53 | Expr_decreases of string
57 (* This internal type is used during conversion of the OCaml AST
58 * to the whenexpr type.
64 | IExpr_int of Big_int.big_int
65 | IExpr_float of float
67 | IExpr_app of string * whenexpr_int list
69 (* Note that days are not necessarily expressible in seconds (because
70 * of leap seconds), months are not expressible in days (because months
71 * have different lengths), and years are not expressible in days
72 * (because of leap days) although we could save a case here by
73 * expressing years in months.
76 | Every_seconds of int
87 res_job_name : string;
93 type cleanup = result -> unit
102 let variable_of_rpc = function
104 | `bool_t b -> T_bool b
105 | `string_t s -> T_string s
106 | `int_t i -> T_int (big_int_of_string i)
107 | `float_t f -> T_float f
109 let rpc_of_variable = function
111 | T_bool b -> `bool_t b
112 | T_string s -> `string_t s
113 | T_int i -> `int_t (string_of_big_int i)
114 | T_float f -> `float_t f
116 type variables = variable StringMap.t
119 | When_job of whenexpr
120 | Every_job of periodexpr
125 job_cleanup : cleanup option;
127 job_script : shell_script;
130 let rec expr_of_ast _loc ast =
131 expr_of_iexpr _loc (iexpr_of_ast _loc ast)
133 and iexpr_of_ast _loc = function
134 | ExId (_, IdUid (_, "()")) -> IExpr_unit
135 | ExId (_, IdUid (_, "True")) -> IExpr_bool true
136 | ExId (_, IdUid (_, "False")) -> IExpr_bool false
137 | ExStr (_, str) -> IExpr_str str
138 | ExInt (_, i) -> IExpr_int (big_int_of_string i) (* XXX too large? *)
139 | ExFlo (_, f) -> IExpr_float (float_of_string f)
140 | ExId (_, IdLid (_, id)) -> IExpr_var id
142 (* In the OCaml AST, functions are curried right to left, so we
143 * must uncurry to get the list of arguments.
145 | ExApp (_, left_tree, right_arg) ->
146 let f, left_args = uncurry_app_tree _loc left_tree in
147 IExpr_app (f, List.rev_map (iexpr_of_ast _loc) (right_arg :: left_args))
150 (* https://groups.google.com/group/fa.caml/browse_thread/thread/f35452d085654bd6 *)
151 eprintf "expr_of_ast: invalid expression: %!";
152 let e = Ast.StExp (_loc, e) in
153 Printers.OCaml.print_implem ~output_file:"/dev/stderr" e;
155 invalid_arg (sprintf "%s: invalid expression" (Loc.to_string _loc))
157 and uncurry_app_tree _loc = function
158 | ExId (_, IdLid (_, f)) -> f, []
159 | ExApp (_, left_tree, right_arg) ->
160 let f, left_args = uncurry_app_tree _loc left_tree in
161 f, (right_arg :: left_args)
163 eprintf "uncurry_app_tree: invalid expression: %!";
164 let e = Ast.StExp (_loc, e) in
165 Printers.OCaml.print_implem ~output_file:"/dev/stderr" e;
167 invalid_arg (sprintf "%s: invalid expression" (Loc.to_string _loc))
169 and expr_of_iexpr _loc = function
170 | IExpr_unit -> Expr_unit
171 | IExpr_bool b -> Expr_bool b
172 | IExpr_str s -> Expr_str s
173 | IExpr_int i -> Expr_int i
174 | IExpr_float f -> Expr_float f
175 | IExpr_var v -> Expr_var v
177 | IExpr_app ("&&", exprs) ->
178 two_params _loc "&&" exprs (fun e1 e2 -> Expr_and (e1, e2))
180 | IExpr_app ("||", exprs) ->
181 two_params _loc "||" exprs (fun e1 e2 -> Expr_or (e1, e2))
183 | IExpr_app ("<", exprs) ->
184 two_params _loc "<" exprs (fun e1 e2 -> Expr_lt (e1, e2))
186 | IExpr_app ("<=", exprs) ->
187 two_params _loc "<=" exprs (fun e1 e2 -> Expr_le (e1, e2))
189 | IExpr_app (("="|"=="), exprs) ->
190 two_params _loc "=" exprs (fun e1 e2 -> Expr_eq (e1, e2))
192 | IExpr_app (">=", exprs) ->
193 two_params _loc ">=" exprs (fun e1 e2 -> Expr_ge (e1, e2))
195 | IExpr_app (">", exprs) ->
196 two_params _loc ">" exprs (fun e1 e2 -> Expr_gt (e1, e2))
198 | IExpr_app ("!", exprs) ->
199 one_param _loc "!" exprs (fun e1 -> Expr_not e1)
201 | IExpr_app ("+", exprs) ->
202 two_params _loc "+" exprs (fun e1 e2 -> Expr_add (e1, e2))
204 | IExpr_app ("-", exprs) ->
205 two_params _loc "+" exprs (fun e1 e2 -> Expr_sub (e1, e2))
207 | IExpr_app ("*", exprs) ->
208 two_params _loc "+" exprs (fun e1 e2 -> Expr_mul (e1, e2))
210 | IExpr_app ("/", exprs) ->
211 two_params _loc "+" exprs (fun e1 e2 -> Expr_div (e1, e2))
213 | IExpr_app ("mod", exprs) ->
214 two_params _loc "+" exprs (fun e1 e2 -> Expr_mod (e1, e2))
216 | IExpr_app (("len"|"length"|"size"), exprs) ->
217 one_param _loc "len" exprs (fun e1 -> Expr_len e1)
219 | IExpr_app (("change"|"changes"|"changed"), [IExpr_var v]) ->
222 | IExpr_app (("inc"|"increase"|"increases"|"increased"), [IExpr_var v]) ->
225 | IExpr_app (("dec"|"decrease"|"decreases"|"decreased"), [IExpr_var v]) ->
228 | IExpr_app (("prev"|"previous"), [IExpr_var v]) ->
231 | IExpr_app (("change"|"changes"|"changed"|"inc"|"increase"|"increases"|"increased"|"dec"|"decrease"|"decreases"|"decreased"|"prev"|"previous") as op, _) ->
232 invalid_arg (sprintf "%s: '%s' operator must be followed by a variable name"
233 (Loc.to_string _loc) op)
235 | IExpr_app ("reloaded", [IExpr_unit]) ->
238 | IExpr_app ("reloaded", _) ->
239 invalid_arg (sprintf "%s: you must use 'reloaded ()'" (Loc.to_string _loc))
241 | IExpr_app (op, _) ->
242 invalid_arg (sprintf "%s: unknown operator in expression: %s"
243 (Loc.to_string _loc) op)
245 and two_params _loc op exprs f =
247 | [e1; e2] -> f (expr_of_iexpr _loc e1) (expr_of_iexpr _loc e2)
249 invalid_arg (sprintf "%s: %s operator must be applied to two parameters"
250 op (Loc.to_string _loc))
252 and one_param _loc op exprs f =
254 | [e1] -> f (expr_of_iexpr _loc e1)
256 invalid_arg (sprintf "%s: %s operator must be applied to one parameter"
257 op (Loc.to_string _loc))
259 let rec string_of_whenexpr = function
261 | Expr_bool b -> sprintf "%b" b
262 | Expr_str s -> sprintf "%S" s
263 | Expr_int i -> sprintf "%s" (string_of_big_int i)
264 | Expr_float f -> sprintf "%f" f
265 | Expr_var v -> sprintf "%s" v
266 | Expr_and (e1, e2) ->
267 sprintf "%s && %s" (string_of_whenexpr e1) (string_of_whenexpr e2)
268 | Expr_or (e1, e2) ->
269 sprintf "%s || %s" (string_of_whenexpr e1) (string_of_whenexpr e2)
270 | Expr_lt (e1, e2) ->
271 sprintf "%s < %s" (string_of_whenexpr e1) (string_of_whenexpr e2)
272 | Expr_le (e1, e2) ->
273 sprintf "%s <= %s" (string_of_whenexpr e1) (string_of_whenexpr e2)
274 | Expr_eq (e1, e2) ->
275 sprintf "%s == %s" (string_of_whenexpr e1) (string_of_whenexpr e2)
276 | Expr_ge (e1, e2) ->
277 sprintf "%s >= %s" (string_of_whenexpr e1) (string_of_whenexpr e2)
278 | Expr_gt (e1, e2) ->
279 sprintf "%s > %s" (string_of_whenexpr e1) (string_of_whenexpr e2)
280 | Expr_not e -> sprintf "! %s" (string_of_whenexpr e)
281 | Expr_add (e1, e2) ->
282 sprintf "%s + %s" (string_of_whenexpr e1) (string_of_whenexpr e2)
283 | Expr_sub (e1, e2) ->
284 sprintf "%s - %s" (string_of_whenexpr e1) (string_of_whenexpr e2)
285 | Expr_mul (e1, e2) ->
286 sprintf "%s * %s" (string_of_whenexpr e1) (string_of_whenexpr e2)
287 | Expr_div (e1, e2) ->
288 sprintf "%s / %s" (string_of_whenexpr e1) (string_of_whenexpr e2)
289 | Expr_mod (e1, e2) ->
290 sprintf "%s mod %s" (string_of_whenexpr e1) (string_of_whenexpr e2)
291 | Expr_len e -> sprintf "len %s" (string_of_whenexpr e)
292 | Expr_changes v -> sprintf "changes %s" v
293 | Expr_increases v -> sprintf "increases %s" v
294 | Expr_decreases v -> sprintf "decreases %s" v
295 | Expr_prev v -> sprintf "prev %s" v
296 | Expr_reloaded -> "reloaded ()"
298 let string_of_periodexpr = function
299 | Every_seconds 1 -> "1 second"
300 | Every_seconds i -> sprintf "%d seconds" i
301 | Every_days 1 -> "1 day"
302 | Every_days i -> sprintf "%d days" i
303 | Every_months 1 -> "1 month"
304 | Every_months i -> sprintf "%d months" i
305 | Every_years 1 -> "1 year"
306 | Every_years i -> sprintf "%d years" i
308 let rec dependencies_of_whenexpr = function
326 | Expr_mod (e1, e2) ->
327 dependencies_of_whenexpr e1 @ dependencies_of_whenexpr e2
330 dependencies_of_whenexpr e
335 | Expr_reloaded -> []
337 let dependencies_of_job = function
338 | { job_cond = When_job whenexpr } -> dependencies_of_whenexpr whenexpr
339 | { job_cond = Every_job _ } -> []
341 let rec eval_whenexpr variables prev_variables onload = function
342 | Expr_unit -> T_unit
343 | Expr_bool b -> T_bool b
344 | Expr_str s -> T_string s
345 | Expr_int i -> T_int i
346 | Expr_float f -> T_float f
349 get_variable variables v
351 | Expr_and (e1, e2) ->
352 if eval_whenexpr_as_bool variables prev_variables onload e1 &&
353 eval_whenexpr_as_bool variables prev_variables onload e2 then
358 | Expr_or (e1, e2) ->
359 if eval_whenexpr_as_bool variables prev_variables onload e1 ||
360 eval_whenexpr_as_bool variables prev_variables onload e2 then
365 | Expr_lt (e1, e2) ->
366 let e1 = eval_whenexpr variables prev_variables onload e1
367 and e2 = eval_whenexpr variables prev_variables onload e2 in
368 if compare_values e1 e2 < 0 then
373 | Expr_le (e1, e2) ->
374 let e1 = eval_whenexpr variables prev_variables onload e1
375 and e2 = eval_whenexpr variables prev_variables onload e2 in
376 if compare_values e1 e2 <= 0 then
381 | Expr_eq (e1, e2) ->
382 let e1 = eval_whenexpr variables prev_variables onload e1
383 and e2 = eval_whenexpr variables prev_variables onload e2 in
384 if compare_values e1 e2 = 0 then
389 | Expr_ge (e1, e2) ->
390 let e1 = eval_whenexpr variables prev_variables onload e1
391 and e2 = eval_whenexpr variables prev_variables onload e2 in
392 if compare_values e1 e2 >= 0 then
397 | Expr_gt (e1, e2) ->
398 let e1 = eval_whenexpr variables prev_variables onload e1
399 and e2 = eval_whenexpr variables prev_variables onload e2 in
400 if compare_values e1 e2 > 0 then
406 if not (eval_whenexpr_as_bool variables prev_variables onload e) then
411 | Expr_add (e1, e2) ->
412 let e1 = eval_whenexpr variables prev_variables onload e1
413 and e2 = eval_whenexpr variables prev_variables onload e2 in
416 | Expr_sub (e1, e2) ->
417 let e1 = eval_whenexpr variables prev_variables onload e1
418 and e2 = eval_whenexpr variables prev_variables onload e2 in
421 | Expr_mul (e1, e2) ->
422 let e1 = eval_whenexpr variables prev_variables onload e1
423 and e2 = eval_whenexpr variables prev_variables onload e2 in
426 | Expr_div (e1, e2) ->
427 let e1 = eval_whenexpr variables prev_variables onload e1
428 and e2 = eval_whenexpr variables prev_variables onload e2 in
431 | Expr_mod (e1, e2) ->
432 let e1 = eval_whenexpr variables prev_variables onload e1
433 and e2 = eval_whenexpr variables prev_variables onload e2 in
437 let e = eval_whenexpr variables prev_variables onload e in
438 let e = string_of_variable e in
439 T_int (big_int_of_int (String.length e))
442 let prev_value, curr_value = get_prev_curr_value variables prev_variables v in
443 if compare_values prev_value curr_value <> 0 then
448 | Expr_increases v ->
449 let prev_value, curr_value = get_prev_curr_value variables prev_variables v in
450 if compare_values prev_value curr_value < 0 then
455 | Expr_decreases v ->
456 let prev_value, curr_value = get_prev_curr_value variables prev_variables v in
457 if compare_values prev_value curr_value > 0 then
463 get_prev_variable prev_variables v
468 and get_prev_curr_value variables prev_variables v =
469 let prev_value = get_prev_variable prev_variables v in
470 let curr_value = get_variable variables v in
471 prev_value, curr_value
473 and get_variable variables v =
474 try StringMap.find v variables with Not_found -> T_string ""
476 and get_prev_variable prev_variables v =
477 match prev_variables with
479 (* Job has never run. XXX Should do better here. *)
481 | Some prev_variables ->
482 get_variable prev_variables v
484 (* Call {!eval_whenexpr} and cast the result to a boolean. *)
485 and eval_whenexpr_as_bool variables prev_variables onload expr =
486 match eval_whenexpr variables prev_variables onload expr with
489 | T_string s -> s <> ""
490 | T_int i -> sign_big_int i <> 0
491 | T_float f -> f <> 0.
493 (* Do a comparison on two typed values and return -1/0/+1. If the
494 * types are different then we compare the values as strings. The user
495 * can avoid this by specifying types.
497 and compare_values value1 value2 =
498 match value1, value2 with
499 | T_bool b1, T_bool b2 -> compare b1 b2
500 | T_string s1, T_string s2 -> compare s1 s2
501 | T_int i1, T_int i2 -> compare_big_int i1 i2
502 | T_float f1, T_float f2 -> compare f1 f2
503 (* XXX BUG: int should be promoted to float in mixed numeric comparison *)
505 let value1 = string_of_variable value1
506 and value2 = string_of_variable value2 in
507 compare value1 value2
509 (* + operator is addition or string concatenation. *)
510 and add_values value1 value2 =
511 match value1, value2 with
512 | T_int i1, T_int i2 -> T_int (add_big_int i1 i2)
513 | T_float i1, T_float i2 -> T_float (i1 +. i2)
514 | T_int i1, T_float i2 -> T_float (float_of_big_int i1 +. i2)
515 | T_float i1, T_int i2 -> T_float (i1 +. float_of_big_int i2)
516 | T_string i1, T_string i2 -> T_string (i1 ^ i2)
519 (sprintf "incompatible types in addition: %s + %s"
520 (printable_string_of_variable value1)
521 (printable_string_of_variable value2))
523 and sub_values value1 value2 =
524 match value1, value2 with
525 | T_int i1, T_int i2 -> T_int (sub_big_int i1 i2)
526 | T_float i1, T_float i2 -> T_float (i1 -. i2)
527 | T_int i1, T_float i2 -> T_float (float_of_big_int i1 -. i2)
528 | T_float i1, T_int i2 -> T_float (i1 -. float_of_big_int i2)
531 (sprintf "incompatible types in subtraction: %s - %s"
532 (printable_string_of_variable value1)
533 (printable_string_of_variable value2))
535 and mul_values value1 value2 =
536 match value1, value2 with
537 | T_int i1, T_int i2 -> T_int (mult_big_int i1 i2)
538 | T_float i1, T_float i2 -> T_float (i1 *. i2)
539 | T_int i1, T_float i2 -> T_float (float_of_big_int i1 *. i2)
540 | T_float i1, T_int i2 -> T_float (i1 *. float_of_big_int i2)
543 (sprintf "incompatible types in multiplication: %s * %s"
544 (printable_string_of_variable value1)
545 (printable_string_of_variable value2))
547 and div_values value1 value2 =
548 match value1, value2 with
549 | T_int i1, T_int i2 -> T_int (div_big_int i1 i2)
550 | T_float i1, T_float i2 -> T_float (i1 /. i2)
551 | T_int i1, T_float i2 -> T_float (float_of_big_int i1 /. i2)
552 | T_float i1, T_int i2 -> T_float (i1 /. float_of_big_int i2)
555 (sprintf "incompatible types in division: %s / %s"
556 (printable_string_of_variable value1)
557 (printable_string_of_variable value2))
559 and mod_values value1 value2 =
560 match value1, value2 with
561 | T_int i1, T_int i2 -> T_int (mod_big_int i1 i2)
562 | T_float i1, T_float i2 -> T_float (mod_float i1 i2)
563 | T_int i1, T_float i2 -> T_float (mod_float (float_of_big_int i1) i2)
564 | T_float i1, T_int i2 -> T_float (mod_float i1 (float_of_big_int i2))
567 (sprintf "incompatible types in modulo: %s mod %s"
568 (printable_string_of_variable value1)
569 (printable_string_of_variable value2))
571 and string_of_variable = function
572 | T_unit -> "" (* for string_of_variable, we don't want () here *)
573 | T_bool b -> string_of_bool b
575 | T_int i -> string_of_big_int i
576 | T_float f -> string_of_float f
578 and printable_string_of_variable = function
580 | T_bool b -> string_of_bool b
581 | T_string s -> sprintf "%S" s
582 | T_int i -> string_of_big_int i
583 | T_float f -> string_of_float f
585 let next_periodexpr =
586 (* Round up 'a' to the next multiple of 'i'. *)
587 let round_up_float a i =
588 let r = mod_float a i in
589 if r = 0. then a +. i else a +. (i -. r)
592 if r = 0 then a + i else a + (i - r)
597 let i = float_of_int i in
603 (* Round 'tm' up to the first day of the next year. *)
604 let year = round_up tm.tm_year i in
605 let tm = { tm with tm_sec = 0; tm_min = 0; tm_hour = 0;
606 tm_mday = 1; tm_mon = 0; tm_year = year } in
610 let t = Date.from_unixfloat t in
611 let t0 = Date.make 1970 1 1 in
613 (* Number of whole days since Unix Epoch. *)
614 let nb_days = Date.Period.safe_nb_days (Date.sub t t0) in
616 let nb_days = round_up nb_days i in
617 let t' = Date.add t0 (Date.Period.day nb_days) in
621 (* Calculate number of whole months since Unix Epoch. *)
623 let months = 12 * (tm.tm_year - 70) + tm.tm_mon in
625 let months = round_up months i in
626 let t0 = Date.make 1970 1 1 in
627 let t' = Date.add t0 (Date.Period.month months) in
630 let check_valid_variable_name name =
631 (* Don't permit certain names. *)
632 if name = "JOBSERIAL" then
633 failwith "JOBSERIAL variable cannot be set";
635 let len = String.length name in
637 failwith "variable name is an empty string";
638 if name.[0] <> '_' && not (isalpha name.[0]) then
639 failwith "variable name must start with alphabetic character or underscore";
643 else if name.[i] <> '_' && not (isalnum name.[i]) then
644 failwith "variable name contains non-alphanumeric non-underscore character"