8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / tools / onbld / Checks / ProcessCheck.py
blob52ebabd002b63eb5de570c3d95e2f892e6dde044
2 # CDDL HEADER START
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]
19 # CDDL HEADER END
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
33 import subprocess
34 import tempfile
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.
48 try:
49 tmpfile = tempfile.TemporaryFile(prefix=command)
50 except EnvironmentError, e:
51 output.write("Could not create temporary file: %s\n" % e)
52 return (3, None)
54 try:
55 p = subprocess.Popen([command] + args,
56 stdin=subprocess.PIPE, stdout=tmpfile,
57 stderr=subprocess.STDOUT, close_fds=False)
58 except OSError, e:
59 output.write("Could not execute %s: %s\n" % (command, e))
60 return (3, None)
62 for line in inpt:
63 p.stdin.write(line)
65 p.stdin.close()
67 ret = p.wait()
68 tmpfile.seek(0)
70 return (ret < 0 and 1 or ret, tmpfile)