summaryrefslogtreecommitdiff
path: root/compat.py
diff options
context:
space:
mode:
authorSylvain Th?nault <sylvain.thenault@logilab.fr>2009-06-29 16:53:42 +0200
committerSylvain Th?nault <sylvain.thenault@logilab.fr>2009-06-29 16:53:42 +0200
commit411fb58d6fc0c9fa8d6ba4717dedac63319fd9dd (patch)
tree8d193e2ce51301a95dc102c8e4cf23e53b4ad2e0 /compat.py
parentb85ce269cf16e68fcc5ae407b2582fd6b1b526ce (diff)
downloadlogilab-common-411fb58d6fc0c9fa8d6ba4717dedac63319fd9dd.tar.gz
subprocess doesn't exists w/ python < 2.4 or in gae environment
Diffstat (limited to 'compat.py')
-rw-r--r--compat.py94
1 files changed, 49 insertions, 45 deletions
diff --git a/compat.py b/compat.py
index 86b4dad..928d76a 100644
--- a/compat.py
+++ b/compat.py
@@ -242,48 +242,52 @@ except NameError:
# Python2.5 subprocess added functions and exceptions
-from subprocess import Popen
-class CalledProcessError(Exception):
- """This exception is raised when a process run by check_call() returns
- a non-zero exit status. The exit status will be stored in the
- returncode attribute."""
- def __init__(self, returncode, cmd):
- self.returncode = returncode
- self.cmd = cmd
- def __str__(self):
- return "Command '%s' returned non-zero exit status %d" % (self.cmd,
-self.returncode)
-
-def call(*popenargs, **kwargs):
- """Run command with arguments. Wait for command to complete, then
- return the returncode attribute.
-
- The arguments are the same as for the Popen constructor. Example:
-
- retcode = call(["ls", "-l"])
- """
- # workaround: subprocess.Popen(cmd, stdout=sys.stdout) fails
- # see http://bugs.python.org/issue1531862
- if "stdout" in kwargs:
- fileno = kwargs.get("stdout").fileno()
- del kwargs['stdout']
- return Popen(stdout=os.dup(fileno), *popenargs, **kwargs).wait()
- return Popen(*popenargs, **kwargs).wait()
-
-def check_call(*popenargs, **kwargs):
- """Run command with arguments. Wait for command to complete. If
- the exit code was zero then return, otherwise raise
- CalledProcessError. The CalledProcessError object will have the
- return code in the returncode attribute.
-
- The arguments are the same as for the Popen constructor. Example:
-
- check_call(["ls", "-l"])
- """
- retcode = call(*popenargs, **kwargs)
- cmd = kwargs.get("args")
- if cmd is None:
- cmd = popenargs[0]
- if retcode:
- raise CalledProcessError(retcode, cmd)
- return retcode
+try:
+ from subprocess import Popen
+except ImportError:
+ # gae or python < 2.3
+
+ class CalledProcessError(Exception):
+ """This exception is raised when a process run by check_call() returns
+ a non-zero exit status. The exit status will be stored in the
+ returncode attribute."""
+ def __init__(self, returncode, cmd):
+ self.returncode = returncode
+ self.cmd = cmd
+ def __str__(self):
+ return "Command '%s' returned non-zero exit status %d" % (self.cmd,
+ self.returncode)
+
+ def call(*popenargs, **kwargs):
+ """Run command with arguments. Wait for command to complete, then
+ return the returncode attribute.
+
+ The arguments are the same as for the Popen constructor. Example:
+
+ retcode = call(["ls", "-l"])
+ """
+ # workaround: subprocess.Popen(cmd, stdout=sys.stdout) fails
+ # see http://bugs.python.org/issue1531862
+ if "stdout" in kwargs:
+ fileno = kwargs.get("stdout").fileno()
+ del kwargs['stdout']
+ return Popen(stdout=os.dup(fileno), *popenargs, **kwargs).wait()
+ return Popen(*popenargs, **kwargs).wait()
+
+ def check_call(*popenargs, **kwargs):
+ """Run command with arguments. Wait for command to complete. If
+ the exit code was zero then return, otherwise raise
+ CalledProcessError. The CalledProcessError object will have the
+ return code in the returncode attribute.
+
+ The arguments are the same as for the Popen constructor. Example:
+
+ check_call(["ls", "-l"])
+ """
+ retcode = call(*popenargs, **kwargs)
+ cmd = kwargs.get("args")
+ if cmd is None:
+ cmd = popenargs[0]
+ if retcode:
+ raise CalledProcessError(retcode, cmd)
+ return retcode