xref: /titanic_50/usr/src/tools/onbld/Checks/ProcessCheck.py (revision cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0)
1*cdf0c1d5Smjnelson#
2*cdf0c1d5Smjnelson# CDDL HEADER START
3*cdf0c1d5Smjnelson#
4*cdf0c1d5Smjnelson# The contents of this file are subject to the terms of the
5*cdf0c1d5Smjnelson# Common Development and Distribution License (the "License").
6*cdf0c1d5Smjnelson# You may not use this file except in compliance with the License.
7*cdf0c1d5Smjnelson#
8*cdf0c1d5Smjnelson# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*cdf0c1d5Smjnelson# or http://www.opensolaris.org/os/licensing.
10*cdf0c1d5Smjnelson# See the License for the specific language governing permissions
11*cdf0c1d5Smjnelson# and limitations under the License.
12*cdf0c1d5Smjnelson#
13*cdf0c1d5Smjnelson# When distributing Covered Code, include this CDDL HEADER in each
14*cdf0c1d5Smjnelson# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*cdf0c1d5Smjnelson# If applicable, add the following below this CDDL HEADER, with the
16*cdf0c1d5Smjnelson# fields enclosed by brackets "[]" replaced with your own identifying
17*cdf0c1d5Smjnelson# information: Portions Copyright [yyyy] [name of copyright owner]
18*cdf0c1d5Smjnelson#
19*cdf0c1d5Smjnelson# CDDL HEADER END
20*cdf0c1d5Smjnelson#
21*cdf0c1d5Smjnelson
22*cdf0c1d5Smjnelson#
23*cdf0c1d5Smjnelson# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24*cdf0c1d5Smjnelson# Use is subject to license terms.
25*cdf0c1d5Smjnelson#
26*cdf0c1d5Smjnelson# ident	"%Z%%M%	%I%	%E% SMI"
27*cdf0c1d5Smjnelson#
28*cdf0c1d5Smjnelson
29*cdf0c1d5Smjnelson#
30*cdf0c1d5Smjnelson# Wrap a command-line check tool in a pythonic API
31*cdf0c1d5Smjnelson#
32*cdf0c1d5Smjnelson
33*cdf0c1d5Smjnelsonimport subprocess
34*cdf0c1d5Smjnelsonimport tempfile
35*cdf0c1d5Smjnelson
36*cdf0c1d5Smjnelsondef processcheck(command, args, inpt, output):
37*cdf0c1d5Smjnelson	'''Run a checking command, command, with arguments as args.
38*cdf0c1d5Smjnelson	Input is provided by inpt (an iterable), error output is
39*cdf0c1d5Smjnelson	written to output (a stream-like entity).
40*cdf0c1d5Smjnelson
41*cdf0c1d5Smjnelson	Return a tuple (error, handle), where handle is a file handle
42*cdf0c1d5Smjnelson	(you must close it), containing output from the command.'''
43*cdf0c1d5Smjnelson
44*cdf0c1d5Smjnelson	#
45*cdf0c1d5Smjnelson	# We use a tempfile for output, rather than a pipe, so we
46*cdf0c1d5Smjnelson	# don't deadlock with the child if both pipes fill.
47*cdf0c1d5Smjnelson	#
48*cdf0c1d5Smjnelson	try:
49*cdf0c1d5Smjnelson		tmpfile = tempfile.TemporaryFile(prefix=command)
50*cdf0c1d5Smjnelson	except EnvironmentError, e:
51*cdf0c1d5Smjnelson		output.write("Could not create temporary file: %s\n" % e)
52*cdf0c1d5Smjnelson		return (3, None)
53*cdf0c1d5Smjnelson
54*cdf0c1d5Smjnelson	try:
55*cdf0c1d5Smjnelson		p = subprocess.Popen([command] + args,
56*cdf0c1d5Smjnelson				     stdin=subprocess.PIPE, stdout=tmpfile,
57*cdf0c1d5Smjnelson				     stderr=subprocess.STDOUT, close_fds=False)
58*cdf0c1d5Smjnelson	except OSError, e:
59*cdf0c1d5Smjnelson		output.write("Could not execute %s: %s\n" % (command, e))
60*cdf0c1d5Smjnelson		return (3, None)
61*cdf0c1d5Smjnelson
62*cdf0c1d5Smjnelson	for line in inpt:
63*cdf0c1d5Smjnelson		p.stdin.write(line)
64*cdf0c1d5Smjnelson
65*cdf0c1d5Smjnelson	p.stdin.close()
66*cdf0c1d5Smjnelson
67*cdf0c1d5Smjnelson	ret = p.wait()
68*cdf0c1d5Smjnelson	tmpfile.seek(0)
69*cdf0c1d5Smjnelson
70*cdf0c1d5Smjnelson	return (ret < 0 and 1 or ret, tmpfile)
71