4 # The contents of this file are subject to the terms of the
5 # Common Development and Distribution License (the "License").
6 # You may not use this file except in compliance with the License.
8 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 # or http://www.opensolaris.org/os/licensing.
10 # See the License for the specific language governing permissions
11 # and limitations under the License.
13 # When distributing Covered Code, include this CDDL HEADER in each
14 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 # If applicable, add the following below this CDDL HEADER, with the
16 # fields enclosed by brackets "[]" replaced with your own identifying
17 # information: Portions Copyright [yyyy] [name of copyright owner]
23 # Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 # Use is subject to license terms.
26 # ident "%Z%%M% %I% %E% SMI"
30 # Wrap a command-line check tool in a pythonic API
36 def processcheck(command
, args
, inpt
, output
):
37 '''Run a checking command, command, with arguments as args.
38 Input is provided by inpt (an iterable), error output is
39 written to output (a stream-like entity).
41 Return a tuple (error, handle), where handle is a file handle
42 (you must close it), containing output from the command.'''
45 # We use a tempfile for output, rather than a pipe, so we
46 # don't deadlock with the child if both pipes fill.
49 tmpfile
= tempfile
.TemporaryFile(prefix
=command
)
50 except EnvironmentError, e
:
51 output
.write("Could not create temporary file: %s\n" % e
)
55 p
= subprocess
.Popen([command
] + args
,
56 stdin
=subprocess
.PIPE
, stdout
=tmpfile
,
57 stderr
=subprocess
.STDOUT
, close_fds
=False)
59 output
.write("Could not execute %s: %s\n" % (command
, e
))
70 return (ret
< 0 and 1 or ret
, tmpfile
)