forgot this file
This commit is contained in:
parent
13c8d5694b
commit
6fa39070d6
1 changed files with 27 additions and 0 deletions
27
poopen.py
Normal file
27
poopen.py
Normal file
|
@ -0,0 +1,27 @@
|
|||
import subprocess as sp
|
||||
|
||||
# subprocess doesn't come with very useful wrappers,
|
||||
# so let's write our own!
|
||||
|
||||
class PoopenError(sp.CalledProcessError):
|
||||
def __init__(self, returncode, cmd, output=None, error=None):
|
||||
self.returncode = returncode
|
||||
self.cmd = cmd
|
||||
self.output = output
|
||||
self.error = error
|
||||
def __str__(self):
|
||||
s = "Command failed with exit status {}:\n{}".format(self.returncode, self.cmd)
|
||||
if self.output:
|
||||
output = str(self.output, 'utf-8', 'ignore')
|
||||
s += "\nstdout:\n{}\n".format(output)
|
||||
if self.error:
|
||||
error = str(self.error, 'utf-8', 'ignore')
|
||||
s += "\nstderr:\n{}\n".format(error)
|
||||
return s
|
||||
|
||||
def poopen(args, env=None):
|
||||
p = sp.Popen(args, stdout=sp.PIPE, stderr=sp.PIPE, env=env)
|
||||
out, err = p.communicate()
|
||||
if p.returncode != 0:
|
||||
raise PoopenError(returncode=p.returncode, cmd=args, output=out, error=err)
|
||||
return p.returncode, out, err
|
Loading…
Reference in a new issue