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