1 (* HCoop
Domtool (http
://hcoop
.sourceforge
.net
/)
2 * Copyright (c
) 2006, Adam Chlipala
4 * This program is free software
; you can redistribute it
and/or
5 * modify it under the terms
of the GNU General Public License
6 * as published by the Free Software Foundation
; either version
2
7 * of the License
, or (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
15 * along
with this program
; if not
, write to the Free Software
16 * Foundation
, Inc
., 51 Franklin Street
, Fifth Floor
, Boston
, MA
02110-1301, USA
.
19 (* Code for receiving
and executing configuration files
*)
21 structure Slave
:> SLAVE
= struct
23 datatype file_action
=
28 fun isDelete (Delete _
) = true
31 type file_status
= {action
: file_action
,
36 val fileHandler
= ref (fn _
: file_status
=> ())
37 val preHandler
= ref (fn () => ())
38 val postHandler
= ref (fn () => ())
40 fun registerFileHandler handler
=
42 val old
= !fileHandler
44 fileHandler
:= (fn x
=> (handler x
; old x
))
47 fun registerPreHandler handler
=
51 preHandler
:= (fn () => (handler (); old ()))
54 fun registerPostHandler handler
=
56 val old
= !postHandler
58 postHandler
:= (fn () => (handler (); old ()))
61 fun handleChanges fs
= (!preHandler ();
62 app (fn recd
as {action
, file
, ...} =>
66 if b
andalso Posix
.FileSys
.access (file
, []) then
67 OS
.FileSys
.remove file
73 fun shell ss
= OS
.Process
.isSuccess (OS
.Process
.system (String.concat ss
))
75 fun shellF (ss
, msg
) =
77 val s
= String.concat ss
79 if OS
.Process
.isSuccess (OS
.Process
.system s
) then
82 ErrorMsg
.error
NONE (msg s
)
85 fun run (program
, argv
) =
87 val proc
= Unix
.execute (program
, argv
)
90 case TextIO.inputLine inf
of
92 | SOME line
=> loop inf
93 (* Programs that output will fail unless we eat their output
*)
94 val () = loop (Unix
.textInstreamOf proc
)
96 OS
.Process
.isSuccess (Unix
.reap proc
)
100 fun runOutput (program
, argv
) =
102 val proc
= Unix
.execute (program
, argv
)
103 val inf
= Unix
.textInstreamOf proc
106 case TextIO.inputLine inf
of
107 NONE
=> if (List.length out
) > 0 then
108 SOME (String.concat (rev out
))
111 | SOME line
=> loop (line
:: out
)
116 SOME lines
=> print lines
119 (OS
.Process
.isSuccess (Unix
.reap proc
), lines
)
124 val proc
= Unix
.execute ("/bin/bash", ["-c", String.concat ss ^
" 2>&1"])
125 val inf
= Unix
.textInstreamOf proc
128 case TextIO.inputLine inf
of
129 NONE
=> String.concat (rev out
)
130 | SOME line
=> loop (line
:: out
)
135 if OS
.Process
.isSuccess (Unix
.reap proc
) then
143 val inf
= TextIO.openIn
"/etc/hostname"
145 case TextIO.inputLine inf
of
146 NONE
=> (TextIO.closeIn inf
; raise Fail
"No line in /etc/hostname")
147 | SOME line
=> (TextIO.closeIn inf
; String.substring (line
, 0, size line
- 1))
150 fun concatTo p fname
=
154 val dir
= Posix
.FileSys
.opendir dname
157 case Posix
.FileSys
.readdir dir
of
158 NONE
=> Posix
.FileSys
.closedir dir
161 val path
= OS
.Path
.joinDirFile
{dir
= dname
, file
= fname
'}
163 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat path
) then
165 else if p fname
' then
166 shellF ([Config
.cat
, " ", path
, " >>", fname
],
167 fn cl
=> "Error concatenating: " ^ cl
)
176 TextIO.closeOut (TextIO.openOut fname
);
177 visitDir (OS
.Path
.joinDirFile
{dir
= Config
.resultRoot
, file
= hostname ()})
180 fun enumerateTo p sep fname
=
182 val outf
= TextIO.openOut fname
185 val baseLen
= length (String.fields (fn ch
=> ch
= #
"/") Config
.resultRoot
) + 1
189 val dir
= Posix
.FileSys
.opendir dname
192 case Posix
.FileSys
.readdir dir
of
193 NONE
=> Posix
.FileSys
.closedir dir
196 val path
= OS
.Path
.joinDirFile
{dir
= dname
, file
= fname
'}
198 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat path
) then
200 else if p fname
' then
202 val toks
= String.fields (fn ch
=> ch
= #
"/") dname
203 val toks
= List.drop (toks
, baseLen
)
204 val dom
= String.concatWith
"." (rev toks
)
209 TextIO.output (outf
, sep
);
210 TextIO.output (outf
, dom
)
220 visitDir (OS
.Path
.joinDirFile
{dir
= Config
.resultRoot
, file
= hostname ()});
226 val inf
= TextIO.openIn fname
229 case TextIO.inputLine inf
of
231 | SOME line
=> loop (String.substring (line
, 0, size line
- 1) :: acc
)
234 before TextIO.closeIn inf
237 fun writeList (fname
, ls
) =
239 val outf
= TextIO.openOut fname
241 app (fn s
=> (TextIO.output (outf
, s
);
242 TextIO.output1 (outf
, #
"\n"))) ls
;
246 fun lineInFile fname line
=
248 val inf
= TextIO.openIn fname
249 val line
' = line ^
"\n"
252 case TextIO.inputLine inf
of
254 | SOME line
=> line
= line
' orelse loop ()
257 before TextIO.closeIn inf
258 end handle IO
.Io _
=> false
260 fun inGroup
{user
, group
} =
261 List.exists (fn x
=> x
= user
)
262 (Posix
.SysDB
.Group
.members (Posix
.SysDB
.getgrnam group
))
263 handle OS
.SysErr _
=> false
265 fun mkDirAll dir
= ignore (OS
.Process
.system ("mkdir -p " ^ dir
))
267 fun remove (ls
, x
) = List.filter (fn y
=> y
<> x
) ls
268 fun removeDups ls
= List.foldr (fn (x
, ls
) =>
269 if List.exists (fn y
=> y
= x
) ls
then
274 fun moveDirCreate
{from
, to
} =
276 if Posix
.FileSys
.access (from
, []) then
277 (ignore (OS
.Process
.system ("rm -rf " ^ to
));
278 ignore (OS
.Process
.system ("cp -r " ^ from ^
" " ^ to
));
279 ignore (OS
.Process
.system ("rm -rf " ^ from
)))