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
88 pi_serial : Big_int.big_int;
92 res_job_name : string;
93 res_serial : Big_int.big_int;
97 res_start_time : float;
100 type pre = preinfo -> bool
101 type post = result -> unit
110 let variable_of_rpc = function
112 | `bool_t b -> T_bool b
113 | `string_t s -> T_string s
114 | `int_t i -> T_int (big_int_of_string i)
115 | `float_t f -> T_float f
117 let rpc_of_variable = function
119 | T_bool b -> `bool_t b
120 | T_string s -> `string_t s
121 | T_int i -> `int_t (string_of_big_int i)
122 | T_float f -> `float_t f
124 type variables = variable StringMap.t
127 | When_job of whenexpr
128 | Every_job of periodexpr
133 job_pre : pre option;
134 job_post : post option;
136 job_script : shell_script;
139 let rec expr_of_ast _loc ast =
140 expr_of_iexpr _loc (iexpr_of_ast _loc ast)
142 and iexpr_of_ast _loc = function
143 | ExId (_, IdUid (_, "()")) -> IExpr_unit
144 | ExId (_, IdUid (_, "True")) -> IExpr_bool true
145 | ExId (_, IdUid (_, "False")) -> IExpr_bool false
146 | ExStr (_, str) -> IExpr_str str
147 | ExInt (_, i) -> IExpr_int (big_int_of_string i) (* XXX too large? *)
148 | ExFlo (_, f) -> IExpr_float (float_of_string f)
149 | ExId (_, IdLid (_, id)) -> IExpr_var id
151 (* In the OCaml AST, functions are curried right to left, so we
152 * must uncurry to get the list of arguments.
154 | ExApp (_, left_tree, right_arg) ->
155 let f, left_args = uncurry_app_tree _loc left_tree in
156 IExpr_app (f, List.rev_map (iexpr_of_ast _loc) (right_arg :: left_args))
159 (* https://groups.google.com/group/fa.caml/browse_thread/thread/f35452d085654bd6 *)
160 eprintf "expr_of_ast: invalid expression: %!";
161 let e = Ast.StExp (_loc, e) in
162 Printers.OCaml.print_implem ~output_file:"/dev/stderr" e;
164 invalid_arg (sprintf "%s: invalid expression" (Loc.to_string _loc))
166 and uncurry_app_tree _loc = function
167 | ExId (_, IdLid (_, f)) -> f, []
168 | ExApp (_, left_tree, right_arg) ->
169 let f, left_args = uncurry_app_tree _loc left_tree in
170 f, (right_arg :: left_args)
172 eprintf "uncurry_app_tree: invalid expression: %!";
173 let e = Ast.StExp (_loc, e) in
174 Printers.OCaml.print_implem ~output_file:"/dev/stderr" e;
176 invalid_arg (sprintf "%s: invalid expression" (Loc.to_string _loc))
178 and expr_of_iexpr _loc = function
179 | IExpr_unit -> Expr_unit
180 | IExpr_bool b -> Expr_bool b
181 | IExpr_str s -> Expr_str s
182 | IExpr_int i -> Expr_int i
183 | IExpr_float f -> Expr_float f
184 | IExpr_var v -> Expr_var v
186 | IExpr_app ("&&", exprs) ->
187 two_params _loc "&&" exprs (fun e1 e2 -> Expr_and (e1, e2))
189 | IExpr_app ("||", exprs) ->
190 two_params _loc "||" exprs (fun e1 e2 -> Expr_or (e1, e2))
192 | IExpr_app ("<", exprs) ->
193 two_params _loc "<" exprs (fun e1 e2 -> Expr_lt (e1, e2))
195 | IExpr_app ("<=", exprs) ->
196 two_params _loc "<=" exprs (fun e1 e2 -> Expr_le (e1, e2))
198 | IExpr_app (("="|"=="), exprs) ->
199 two_params _loc "=" exprs (fun e1 e2 -> Expr_eq (e1, e2))
201 | IExpr_app (">=", exprs) ->
202 two_params _loc ">=" exprs (fun e1 e2 -> Expr_ge (e1, e2))
204 | IExpr_app (">", exprs) ->
205 two_params _loc ">" exprs (fun e1 e2 -> Expr_gt (e1, e2))
207 | IExpr_app ("!", exprs) ->
208 one_param _loc "!" exprs (fun e1 -> Expr_not e1)
210 | IExpr_app ("+", exprs) ->
211 two_params _loc "+" exprs (fun e1 e2 -> Expr_add (e1, e2))
213 | IExpr_app ("-", exprs) ->
214 two_params _loc "+" exprs (fun e1 e2 -> Expr_sub (e1, e2))
216 | IExpr_app ("*", exprs) ->
217 two_params _loc "+" exprs (fun e1 e2 -> Expr_mul (e1, e2))
219 | IExpr_app ("/", exprs) ->
220 two_params _loc "+" exprs (fun e1 e2 -> Expr_div (e1, e2))
222 | IExpr_app ("mod", exprs) ->
223 two_params _loc "+" exprs (fun e1 e2 -> Expr_mod (e1, e2))
225 | IExpr_app (("len"|"length"|"size"), exprs) ->
226 one_param _loc "len" exprs (fun e1 -> Expr_len e1)
228 | IExpr_app (("change"|"changes"|"changed"), [IExpr_var v]) ->
231 | IExpr_app (("inc"|"increase"|"increases"|"increased"), [IExpr_var v]) ->
234 | IExpr_app (("dec"|"decrease"|"decreases"|"decreased"), [IExpr_var v]) ->
237 | IExpr_app (("prev"|"previous"), [IExpr_var v]) ->
240 | IExpr_app (("change"|"changes"|"changed"|"inc"|"increase"|"increases"|"increased"|"dec"|"decrease"|"decreases"|"decreased"|"prev"|"previous") as op, _) ->
241 invalid_arg (sprintf "%s: '%s' operator must be followed by a variable name"
242 (Loc.to_string _loc) op)
244 | IExpr_app ("reloaded", [IExpr_unit]) ->
247 | IExpr_app ("reloaded", _) ->
248 invalid_arg (sprintf "%s: you must use 'reloaded ()'" (Loc.to_string _loc))
250 | IExpr_app (op, _) ->
251 invalid_arg (sprintf "%s: unknown operator in expression: %s"
252 (Loc.to_string _loc) op)
254 and two_params _loc op exprs f =
256 | [e1; e2] -> f (expr_of_iexpr _loc e1) (expr_of_iexpr _loc e2)
258 invalid_arg (sprintf "%s: %s operator must be applied to two parameters"
259 op (Loc.to_string _loc))
261 and one_param _loc op exprs f =
263 | [e1] -> f (expr_of_iexpr _loc e1)
265 invalid_arg (sprintf "%s: %s operator must be applied to one parameter"
266 op (Loc.to_string _loc))
268 let rec string_of_whenexpr = function
270 | Expr_bool b -> sprintf "%b" b
271 | Expr_str s -> sprintf "%S" s
272 | Expr_int i -> sprintf "%s" (string_of_big_int i)
273 | Expr_float f -> sprintf "%f" f
274 | Expr_var v -> sprintf "%s" v
275 | Expr_and (e1, e2) ->
276 sprintf "%s && %s" (string_of_whenexpr e1) (string_of_whenexpr e2)
277 | Expr_or (e1, e2) ->
278 sprintf "%s || %s" (string_of_whenexpr e1) (string_of_whenexpr e2)
279 | Expr_lt (e1, e2) ->
280 sprintf "%s < %s" (string_of_whenexpr e1) (string_of_whenexpr e2)
281 | Expr_le (e1, e2) ->
282 sprintf "%s <= %s" (string_of_whenexpr e1) (string_of_whenexpr e2)
283 | Expr_eq (e1, e2) ->
284 sprintf "%s == %s" (string_of_whenexpr e1) (string_of_whenexpr e2)
285 | Expr_ge (e1, e2) ->
286 sprintf "%s >= %s" (string_of_whenexpr e1) (string_of_whenexpr e2)
287 | Expr_gt (e1, e2) ->
288 sprintf "%s > %s" (string_of_whenexpr e1) (string_of_whenexpr e2)
289 | Expr_not e -> sprintf "! %s" (string_of_whenexpr e)
290 | Expr_add (e1, e2) ->
291 sprintf "%s + %s" (string_of_whenexpr e1) (string_of_whenexpr e2)
292 | Expr_sub (e1, e2) ->
293 sprintf "%s - %s" (string_of_whenexpr e1) (string_of_whenexpr e2)
294 | Expr_mul (e1, e2) ->
295 sprintf "%s * %s" (string_of_whenexpr e1) (string_of_whenexpr e2)
296 | Expr_div (e1, e2) ->
297 sprintf "%s / %s" (string_of_whenexpr e1) (string_of_whenexpr e2)
298 | Expr_mod (e1, e2) ->
299 sprintf "%s mod %s" (string_of_whenexpr e1) (string_of_whenexpr e2)
300 | Expr_len e -> sprintf "len %s" (string_of_whenexpr e)
301 | Expr_changes v -> sprintf "changes %s" v
302 | Expr_increases v -> sprintf "increases %s" v
303 | Expr_decreases v -> sprintf "decreases %s" v
304 | Expr_prev v -> sprintf "prev %s" v
305 | Expr_reloaded -> "reloaded ()"
307 let string_of_periodexpr = function
308 | Every_seconds 1 -> "1 second"
309 | Every_seconds i -> sprintf "%d seconds" i
310 | Every_days 1 -> "1 day"
311 | Every_days i -> sprintf "%d days" i
312 | Every_months 1 -> "1 month"
313 | Every_months i -> sprintf "%d months" i
314 | Every_years 1 -> "1 year"
315 | Every_years i -> sprintf "%d years" i
317 let rec dependencies_of_whenexpr = function
335 | Expr_mod (e1, e2) ->
336 dependencies_of_whenexpr e1 @ dependencies_of_whenexpr e2
339 dependencies_of_whenexpr e
344 | Expr_reloaded -> []
346 let dependencies_of_job = function
347 | { job_cond = When_job whenexpr } -> dependencies_of_whenexpr whenexpr
348 | { job_cond = Every_job _ } -> []
350 let rec eval_whenexpr variables prev_variables onload = function
351 | Expr_unit -> T_unit
352 | Expr_bool b -> T_bool b
353 | Expr_str s -> T_string s
354 | Expr_int i -> T_int i
355 | Expr_float f -> T_float f
358 get_variable variables v
360 | Expr_and (e1, e2) ->
361 if eval_whenexpr_as_bool variables prev_variables onload e1 &&
362 eval_whenexpr_as_bool variables prev_variables onload e2 then
367 | Expr_or (e1, e2) ->
368 if eval_whenexpr_as_bool variables prev_variables onload e1 ||
369 eval_whenexpr_as_bool variables prev_variables onload e2 then
374 | Expr_lt (e1, e2) ->
375 let e1 = eval_whenexpr variables prev_variables onload e1
376 and e2 = eval_whenexpr variables prev_variables onload e2 in
377 if compare_values e1 e2 < 0 then
382 | Expr_le (e1, e2) ->
383 let e1 = eval_whenexpr variables prev_variables onload e1
384 and e2 = eval_whenexpr variables prev_variables onload e2 in
385 if compare_values e1 e2 <= 0 then
390 | Expr_eq (e1, e2) ->
391 let e1 = eval_whenexpr variables prev_variables onload e1
392 and e2 = eval_whenexpr variables prev_variables onload e2 in
393 if compare_values e1 e2 = 0 then
398 | Expr_ge (e1, e2) ->
399 let e1 = eval_whenexpr variables prev_variables onload e1
400 and e2 = eval_whenexpr variables prev_variables onload e2 in
401 if compare_values e1 e2 >= 0 then
406 | Expr_gt (e1, e2) ->
407 let e1 = eval_whenexpr variables prev_variables onload e1
408 and e2 = eval_whenexpr variables prev_variables onload e2 in
409 if compare_values e1 e2 > 0 then
415 if not (eval_whenexpr_as_bool variables prev_variables onload e) then
420 | Expr_add (e1, e2) ->
421 let e1 = eval_whenexpr variables prev_variables onload e1
422 and e2 = eval_whenexpr variables prev_variables onload e2 in
425 | Expr_sub (e1, e2) ->
426 let e1 = eval_whenexpr variables prev_variables onload e1
427 and e2 = eval_whenexpr variables prev_variables onload e2 in
430 | Expr_mul (e1, e2) ->
431 let e1 = eval_whenexpr variables prev_variables onload e1
432 and e2 = eval_whenexpr variables prev_variables onload e2 in
435 | Expr_div (e1, e2) ->
436 let e1 = eval_whenexpr variables prev_variables onload e1
437 and e2 = eval_whenexpr variables prev_variables onload e2 in
440 | Expr_mod (e1, e2) ->
441 let e1 = eval_whenexpr variables prev_variables onload e1
442 and e2 = eval_whenexpr variables prev_variables onload e2 in
446 let e = eval_whenexpr variables prev_variables onload e in
447 let e = string_of_variable e in
448 T_int (big_int_of_int (String.length e))
451 let prev_value, curr_value = get_prev_curr_value variables prev_variables v in
452 if compare_values prev_value curr_value <> 0 then
457 | Expr_increases v ->
458 let prev_value, curr_value = get_prev_curr_value variables prev_variables v in
459 if compare_values prev_value curr_value < 0 then
464 | Expr_decreases v ->
465 let prev_value, curr_value = get_prev_curr_value variables prev_variables v in
466 if compare_values prev_value curr_value > 0 then
472 get_prev_variable prev_variables v
477 and get_prev_curr_value variables prev_variables v =
478 let prev_value = get_prev_variable prev_variables v in
479 let curr_value = get_variable variables v in
480 prev_value, curr_value
482 and get_variable variables v =
483 try StringMap.find v variables with Not_found -> T_string ""
485 and get_prev_variable prev_variables v =
486 match prev_variables with
488 (* Job has never run. XXX Should do better here. *)
490 | Some prev_variables ->
491 get_variable prev_variables v
493 (* Call {!eval_whenexpr} and cast the result to a boolean. *)
494 and eval_whenexpr_as_bool variables prev_variables onload expr =
495 match eval_whenexpr variables prev_variables onload expr with
498 | T_string s -> s <> ""
499 | T_int i -> sign_big_int i <> 0
500 | T_float f -> f <> 0.
502 (* Do a comparison on two typed values and return -1/0/+1. If the
503 * types are different then we compare the values as strings. The user
504 * can avoid this by specifying types.
506 and compare_values value1 value2 =
507 match value1, value2 with
508 | T_bool b1, T_bool b2 -> compare b1 b2
509 | T_string s1, T_string s2 -> compare s1 s2
510 | T_int i1, T_int i2 -> compare_big_int i1 i2
511 | T_float f1, T_float f2 -> compare f1 f2
512 (* XXX BUG: int should be promoted to float in mixed numeric comparison *)
514 let value1 = string_of_variable value1
515 and value2 = string_of_variable value2 in
516 compare value1 value2
518 (* + operator is addition or string concatenation. *)
519 and add_values value1 value2 =
520 match value1, value2 with
521 | T_int i1, T_int i2 -> T_int (add_big_int i1 i2)
522 | T_float i1, T_float i2 -> T_float (i1 +. i2)
523 | T_int i1, T_float i2 -> T_float (float_of_big_int i1 +. i2)
524 | T_float i1, T_int i2 -> T_float (i1 +. float_of_big_int i2)
525 | T_string i1, T_string i2 -> T_string (i1 ^ i2)
528 (sprintf "incompatible types in addition: %s + %s"
529 (printable_string_of_variable value1)
530 (printable_string_of_variable value2))
532 and sub_values value1 value2 =
533 match value1, value2 with
534 | T_int i1, T_int i2 -> T_int (sub_big_int i1 i2)
535 | T_float i1, T_float i2 -> T_float (i1 -. i2)
536 | T_int i1, T_float i2 -> T_float (float_of_big_int i1 -. i2)
537 | T_float i1, T_int i2 -> T_float (i1 -. float_of_big_int i2)
540 (sprintf "incompatible types in subtraction: %s - %s"
541 (printable_string_of_variable value1)
542 (printable_string_of_variable value2))
544 and mul_values value1 value2 =
545 match value1, value2 with
546 | T_int i1, T_int i2 -> T_int (mult_big_int i1 i2)
547 | T_float i1, T_float i2 -> T_float (i1 *. i2)
548 | T_int i1, T_float i2 -> T_float (float_of_big_int i1 *. i2)
549 | T_float i1, T_int i2 -> T_float (i1 *. float_of_big_int i2)
552 (sprintf "incompatible types in multiplication: %s * %s"
553 (printable_string_of_variable value1)
554 (printable_string_of_variable value2))
556 and div_values value1 value2 =
557 match value1, value2 with
558 | T_int i1, T_int i2 -> T_int (div_big_int i1 i2)
559 | T_float i1, T_float i2 -> T_float (i1 /. i2)
560 | T_int i1, T_float i2 -> T_float (float_of_big_int i1 /. i2)
561 | T_float i1, T_int i2 -> T_float (i1 /. float_of_big_int i2)
564 (sprintf "incompatible types in division: %s / %s"
565 (printable_string_of_variable value1)
566 (printable_string_of_variable value2))
568 and mod_values value1 value2 =
569 match value1, value2 with
570 | T_int i1, T_int i2 -> T_int (mod_big_int i1 i2)
571 | T_float i1, T_float i2 -> T_float (mod_float i1 i2)
572 | T_int i1, T_float i2 -> T_float (mod_float (float_of_big_int i1) i2)
573 | T_float i1, T_int i2 -> T_float (mod_float i1 (float_of_big_int i2))
576 (sprintf "incompatible types in modulo: %s mod %s"
577 (printable_string_of_variable value1)
578 (printable_string_of_variable value2))
580 and string_of_variable = function
581 | T_unit -> "" (* for string_of_variable, we don't want () here *)
582 | T_bool b -> string_of_bool b
584 | T_int i -> string_of_big_int i
585 | T_float f -> string_of_float f
587 and printable_string_of_variable = function
589 | T_bool b -> string_of_bool b
590 | T_string s -> sprintf "%S" s
591 | T_int i -> string_of_big_int i
592 | T_float f -> string_of_float f
594 let next_periodexpr =
595 (* Round up 'a' to the next multiple of 'i'. *)
596 let round_up_float a i =
597 let r = mod_float a i in
598 if r = 0. then a +. i else a +. (i -. r)
601 if r = 0 then a + i else a + (i - r)
606 let i = float_of_int i in
612 (* Round 'tm' up to the first day of the next year. *)
613 let year = round_up tm.tm_year i in
614 let tm = { tm with tm_sec = 0; tm_min = 0; tm_hour = 0;
615 tm_mday = 1; tm_mon = 0; tm_year = year } in
619 let t = Date.from_unixfloat t in
620 let t0 = Date.make 1970 1 1 in
622 (* Number of whole days since Unix Epoch. *)
623 let nb_days = Date.Period.safe_nb_days (Date.sub t t0) in
625 let nb_days = round_up nb_days i in
626 let t' = Date.add t0 (Date.Period.day nb_days) in
630 (* Calculate number of whole months since Unix Epoch. *)
632 let months = 12 * (tm.tm_year - 70) + tm.tm_mon in
634 let months = round_up months i in
635 let t0 = Date.make 1970 1 1 in
636 let t' = Date.add t0 (Date.Period.month months) in
639 let check_valid_variable_name name =
640 (* Don't permit certain names. *)
641 if name = "JOBSERIAL" then
642 failwith "JOBSERIAL variable cannot be set";
644 let len = String.length name in
646 failwith "variable name is an empty string";
647 if name.[0] <> '_' && not (isalpha name.[0]) then
648 failwith "variable name must start with alphabetic character or underscore";
652 else if name.[i] <> '_' && not (isalnum name.[i]) then
653 failwith "variable name contains non-alphanumeric non-underscore character"