39 lines
1.1 KiB
Python
Executable file
39 lines
1.1 KiB
Python
Executable file
"a system command launcher"
|
|
|
|
import os, sys
|
|
import subprocess
|
|
from subprocess import Popen, PIPE
|
|
|
|
def cmdexec(cmd):
|
|
""" return output of executing 'cmd' in a separate process.
|
|
|
|
raise ExecutionFailed exception if the command failed.
|
|
the exception will provide an 'err' attribute containing
|
|
the error-output from the command.
|
|
"""
|
|
process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
out, err = process.communicate()
|
|
status = process.poll()
|
|
if status:
|
|
raise ExecutionFailed(status, status, cmd, out, err)
|
|
return out
|
|
|
|
class ExecutionFailed(Exception):
|
|
def __init__(self, status, systemstatus, cmd, out, err):
|
|
Exception.__init__(self)
|
|
self.status = status
|
|
self.systemstatus = systemstatus
|
|
self.cmd = cmd
|
|
self.err = err
|
|
self.out = out
|
|
|
|
def __str__(self):
|
|
return "ExecutionFailed: %d %s\n%s" %(self.status, self.cmd, self.err)
|
|
|
|
# export the exception under the name 'Error'
|
|
Error = ExecutionFailed
|
|
try:
|
|
ExecutionFailed.__module__ = 'cmdexec'
|
|
ExecutionFailed.__name__ = 'Error'
|
|
except (AttributeError, TypeError):
|
|
pass
|