/usr/share/pyshared/cream/util/subprocess.py is in python-cream 0.5.3-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | # Copyright: 2007-2013, Sebastian Billaudelle <sbillaudelle@googlemail.com>
# 2010-2013, Kristoffer Kleine <kris.kleine@yahoo.de>
# This library is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
# This library 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 Lesser General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
import os
import sys
from gi.repository import GObject as gobject
class Subprocess(gobject.GObject):
"""
GObject API for handling child processes.
:param command: The command to be run as a subprocess.
:param fork: If `True` this process will be detached from its parent and
run independent. This means that no excited-signal will be emited.
:type command: `list`
:type fork: `bool`
"""
__gtype_name__ = 'Subprocess'
__gsignals__ = {
'exited': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_INT, gobject.TYPE_INT))
}
def __init__(self, command, name=None, fork=False):
gobject.GObject.__init__(self)
self.process = None
self.pid = None
if not fork:
self.stdout = True
self.stderr = True
else:
self.stdout = False
self.stderr = False
self.command = command
self.name = name
self.forked = fork
if fork:
self.fork()
def run(self):
""" Run the process. """
process_data = gobject.spawn_async(self.command,
flags=gobject.SPAWN_SEARCH_PATH|gobject.SPAWN_DO_NOT_REAP_CHILD,
standard_output=self.stdout,
standard_error=self.stderr
)
self.pid = process_data[0]
self.stdout = os.fdopen(process_data[2])
self.stderr = os.fdopen(process_data[3])
print self.stderr
self.watch = gobject.child_watch_add(self.pid, self.exited_cb)
return self.pid
def exited_cb(self, pid, condition):
if not self.forked:
self.emit('exited', pid, condition)
def fork(self):
try:
# first fork
pid = os.fork()
if pid > 0:
sys.exit(0)
except OSError, e:
sys.exit(1)
os.chdir("/")
os.setsid()
os.umask(0)
try:
# second fork
pid = os.fork()
if pid > 0:
sys.exit(0)
except OSError, e:
sys.exit(1)
|