summaryrefslogtreecommitdiffstats
path: root/FusionIcon/execute.py
blob: 5615cd107b5a8129a7c876cdd5bfbea22a783514 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# This file is part of Fusion-icon.

# Fusion-icon is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Fusion-icon is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Author(s): crdlb, nesl247
#
# Copyright 2007 Christopher Williams <christopherw@verizon.net> 

import subprocess, signal, os

# avoid zombies
signal.signal(signal.SIGCHLD, signal.SIG_IGN)

def run(command, mode='spawn', quiet=False, env=None):
	'Simple wrapper for the subprocess module. Supported modes: spawn, call, and output'

	if mode == 'spawn':
		if not quiet:
			popen_object = subprocess.Popen(command)
		else:
			popen_object = subprocess.Popen(command, stdout=open(os.devnull, 'w'))
		
		return popen_object

	elif mode == 'call':
		# restore normal child handling
		signal.signal(signal.SIGCHLD, signal.SIG_DFL)
		if not quiet:
			exitcode = subprocess.call(command, stderr=subprocess.PIPE)
		else:
			exitcode = subprocess.call(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

		# turn zombie protection back on
		signal.signal(signal.SIGCHLD, signal.SIG_IGN)

		return exitcode

	elif mode == 'output':
		signal.signal(signal.SIGCHLD, signal.SIG_DFL)
		if not env:
			output = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=open(os.devnull, 'w')).communicate()[0]
		else:
			output = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=open(os.devnull, 'w'), env=env).communicate()[0]

		signal.signal(signal.SIGCHLD, signal.SIG_IGN)

		return output