2 * Copyright (C) 2013 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.
24 let (//) = Filename.concat
25 let quote = Filename.quote
27 type goal_result_t = Goal_OK | Goal_failed of string
28 exception Goal_result of goal_result_t
30 let goal_failed msg = raise (Goal_result (Goal_failed msg))
33 if v then raise (Goal_result Goal_OK)
34 let target_all vs = target (List.fold_left (&&) true vs)
35 let target_exists vs = target (List.fold_left (||) false vs)
38 let file_exists = Sys.file_exists
40 let file_newer_than f1 f2 =
44 | Unix_error (ENOENT, _, _) -> None
45 | Unix_error (err, _, _) ->
46 let msg = sprintf "file_newer_than: %s: %s" f (error_message err) in
49 let s1 = stat f1 and s2 = stat f2 in
55 let msg = sprintf "file_newer_than: %s: file does not exist" f2 in
58 s1.st_mtime >= s2.st_mtime
60 let more_recent objs srcs =
61 if not (List.for_all file_exists objs) then false
64 fun obj -> List.for_all (file_newer_than obj) srcs
69 (* http://stackoverflow.com/questions/12199059/how-to-check-if-an-url-exists-with-the-shell-and-probably-curl *)
71 sprintf "curl --output /dev/null --silent --head --fail %s" (quote url) in
72 match Sys.command cmd with
76 let msg = sprintf "curl error testing '%s' (exit code %d)" url r in
79 let file_contains_string filename str =
80 let cmd = sprintf "grep -q %s %s" (quote str) (quote filename) in
81 match Sys.command cmd with
85 let msg = sprintf "grep error testing for '%s' in '%s' (exit code %d)"
89 let url_contains_string url str =
90 let tmp = Filename.temp_file "goaljobsurl" "" in
92 sprintf "curl --output %s --silent --fail %s" (quote tmp) (quote url) in
93 (match Sys.command cmd with
96 let msg = sprintf "curl failed to download URL '%s'" url in
99 let msg = sprintf "curl error testing '%s' (exit code %d)" url r in
102 let r = file_contains_string tmp str in
109 let cmd = "set -e\nset -x\n\n" ^ cmd in
110 let r = Sys.command cmd in
112 let msg = sprintf "external command failed with code %d" r in
119 let cmd = "set -e\nset -x\n\n" ^ cmd in
120 let chan = open_process_in cmd in
121 let lines = ref [] in
123 let line = input_line chan in
124 lines := line :: !lines;
127 (try loop () with End_of_file -> ());
128 let r = close_process_in chan in
130 | WEXITED 0 -> List.rev !lines
132 let msg = sprintf "external command failed with code %d" i in
135 let msg = sprintf "external command was killed by signal %d" i in
138 let msg = sprintf "external command was stopped by signal %d" i in
140 let shlines fs = ksprintf do_shlines fs
143 let lines = do_shlines cmd in
144 String.concat "\n" lines
145 let shout fs = ksprintf do_shout fs
148 val shell : string ref
152 val replace_substring : string -> string -> string -> string
155 let change_file_extension ext filename =
157 try String.rindex filename '.'
158 with Not_found -> String.length filename in
159 String.sub filename 0 i ^ "." ^ ext
162 val filter_file_extension : string -> string list -> string
165 (* XXX The Memory is not actually persistent yet. *)
166 let memory = Hashtbl.create 13
168 let memory_exists = Hashtbl.mem memory
169 let memory_set = Hashtbl.replace memory
170 let memory_get k = try Some (Hashtbl.find memory k) with Not_found -> None
171 let memory_delete = Hashtbl.remove memory
173 let published_goals = ref []
174 let publish name fn = published_goals := (name, fn) :: !published_goals
176 try Some (List.assoc name !published_goals) with Not_found -> None
178 let goal_file_exists filename =
179 if not (file_exists filename) then (
180 let msg = sprintf "file '%s' required but not found" filename in
183 let goal_file_newer_than f1 f2 =
184 if not (file_newer_than f1 f2) then (
185 let msg = sprintf "file %s is required to be newer than %s" f1 f2 in
188 let goal_more_recent objs srcs =
189 if not (more_recent objs srcs) then (
190 let msg = sprintf "object(s) %s are required to be newer than source(s) %s"
191 (String.concat " " objs) (String.concat " " srcs) in
194 let goal_url_exists url =
195 if not (url_exists url) then (
196 let msg = sprintf "url_exists: URL '%s' required but does not exist" url in
199 let goal_file_contains_string filename str =
200 if not (file_contains_string filename str) then (
201 let msg = sprintf "file_contains_string: file '%s' is required to contain string '%s'" filename str in
204 let goal_url_contains_string url str =
205 if not (url_contains_string url str) then (
206 let msg = sprintf "url_contains_string: URL '%s' is required to contain string '%s'" url str in
209 let goal_memory_exists k =
210 if not (memory_exists k) then (
211 let msg = sprintf "memory_exists: key '%s' required but does not exist" k in
215 (* Run the program. *)
217 let prog = Sys.executable_name in
218 let prog = Filename.basename prog in
222 let display_version () =
223 printf "%s %s\n" package_name package_version;
228 let names = !published_goals in
229 let names = List.map fst names in
230 let names = List.sort compare names in
231 List.iter print_endline names
234 let argspec = Arg.align [
235 "--goals", Arg.Unit list_goals, " List all goals";
236 "-l", Arg.Unit list_goals, " List all goals";
237 "-V", Arg.Unit display_version, " Display version number and exit";
238 "--version", Arg.Unit display_version, " Display version number and exit";
240 let anon_fun str = args := str :: !args in
241 let usage_msg = sprintf "\
242 %s: a script generated by goaljobs
244 List all goals: %s -l
245 Run a single goal like this: %s <name-of-goal> [<goal-args ...>]
247 For more information see the goaljobs(1) man page.
252 Arg.parse argspec anon_fun usage_msg;
254 let args = List.rev !args in
256 (* Was a goal named on the command line? *)
259 (match get_goal name with
262 eprintf "error: no goal called '%s' was found.\n" name;
263 eprintf "Use %s -l to list all published goals in this script.\n" name;
267 (* Does a published 'all' goal exist? *)
268 match get_goal "all" with
271 (* No published 'all' goal. This is only a warning, because
272 * other top-level code may exist in the script.
274 eprintf "warning: no 'all' goal found.\n"