File: execute.py

package info (click to toggle)
fusion-icon 0.2.4-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 300 kB
  • sloc: python: 731; makefile: 22; xml: 14
file content (63 lines) | stat: -rw-r--r-- 2,167 bytes parent folder | download | duplicates (4)
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
59
60
61
62
63
# -*- python -*-
# 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/>.
#
# Original copyright 2007 Christopher Williams <christopherw@verizon.net>
# Author(s): crdlb, nesl247, raveit65

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'

	try:
		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"), universal_newlines=True).communicate()[0]
			else:
				output = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=open(os.devnull, "w"), universal_newlines=True, env=env).communicate()[0]

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

			return output

	except OSError:
		print(' * execution of "' + ' '.join(command) + '" failed')
		return None