Author: gnichols
Date: 2009-12-22 17:57:33 +0000 (Tue, 22 Dec 2009)
New Revision: 270
Modified:
trunk/v7/command.py
Log:
484657 - hts uses code deprecated in python 2.6
Modified: trunk/v7/command.py
===================================================================
--- trunk/v7/command.py 2009-12-22 17:57:21 UTC (rev 269)
+++ trunk/v7/command.py 2009-12-22 17:57:33 UTC (rev 270)
@@ -19,7 +19,7 @@
# __main__ self test function at the end of this file.
-import os,re, commands, exceptions, popen2, string, sys
+import os,re, commands, subprocess, string, sys
class Command:
@@ -37,18 +37,29 @@
self.errors = None
self.returnValue = 0
self.signal = 0
+ self.pipe = None
def _run(self):
- commandPipe = popen2.Popen3(self.command, capturestderr=True)
- self.output = commandPipe.fromchild.readlines()
- result = commandPipe.wait()
- self.returnValue = (result >> 8) & 0xFF
- self.signal = result & 0xFF
- self.errors = None
-
- if commandPipe.childerr:
- self.errors = commandPipe.childerr.readlines()
- # always print error messages
+ # commandPipe = popen2.Popen3(self.command, capturestderr=True)
+ self.pipe = subprocess.Popen(self.command, shell=True,
+ stdin=subprocess.PIPE,
+ stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE,)
+ (output, errors) = self.pipe.communicate()
+ if output:
+ self.output = output.splitlines()
+ # make the last line has the new line at the end
+ if self.output[-1][-1:] != '\n':
+ self.output[-1] = self.output[-1] + '\n'
+ if errors:
+ self.errors = errors.splitlines()
+ # make the last line has the new line at the end
+ if self.errors[-1][-1:] != '\n':
+ self.errors[-1] = self.output[-1] + '\n'
+ self.returnValue = (self.pipe.returncode >> 8) & 0xFF
+ self.signal = self.pipe.returncode & 0xFF
+
+ if self.errors:
for line in self.errors:
sys.stderr.write( line )
sys.stderr.flush()
@@ -77,26 +88,26 @@
is echo'd on stdout. """
self._run()
self._checkErrors()
- for line in self.output:
- sys.stdout.write( line )
+ self.printOutput()
return 0
def echoIgnoreErrors(self):
""" like echo, except don't raise exception on errors """
self._run()
- for line in self.output:
- sys.stdout.write( line )
+ self.printOutput()
return 0
def printOutput(self):
- for line in self.output:
- sys.stdout.write( line )
- sys.stdout.flush()
+ if self.output:
+ for line in self.output:
+ sys.stdout.write( line )
+ sys.stdout.flush()
def printErrors(self):
- for line in self.errors:
- sys.stderr.write( line )
- sys.stderr.flush()
+ if self.errors:
+ for line in self.errors:
+ sys.stderr.write( line )
+ sys.stderr.flush()
def _getString(self, regex=None, regexGroup=None, singleLine=True, returnList=False, ignoreErrors=False):
@@ -121,27 +132,28 @@
self._run()
if self.singleLine:
- if len(self.output) > 1:
+ if self.output and len(self.output) > 1:
raise V7CommandException(self, "Found %u lines of output, expected 1" % len(self.output))
- line = self.output[0].strip()
- if not self.regex:
- return line
- # otherwise, try the regex
- pattern = re.compile(self.regex)
- match = pattern.match(line)
- if match:
- if self.regexGroup:
- return match.group(self.regexGroup)
- # otherwise, no group, return the whole line
- return line
-
- # no regex match try a grep-style match
- if not self.regexGroup:
- match = pattern.search(line)
- if match:
- return match.group()
+ if self.output:
+ line = self.output[0].strip()
+ if not self.regex:
+ return line
+ # otherwise, try the regex
+ pattern = re.compile(self.regex)
+ match = pattern.match(line)
+ if match:
+ if self.regexGroup:
+ return match.group(self.regexGroup)
+ # otherwise, no group, return the whole line
+ return line
+ # no regex match try a grep-style match
+ if not self.regexGroup:
+ match = pattern.search(line)
+ if match:
+ return match.group()
+
# otherwise
raise V7CommandException(self, "no match for regular expression %s" % self.regex)
@@ -153,25 +165,26 @@
result = None
if returnList:
result = list()
- for line in self.output:
- if self.regexGroup:
- match = pattern.match(line)
- if match:
- if self.regexGroup:
+ if self.output:
+ for line in self.output:
+ if self.regexGroup:
+ match = pattern.match(line)
+ if match:
+ if self.regexGroup:
+ if returnList:
+ result.append(match.group(self.regexGroup))
+ else:
+ return match.group(self.regexGroup)
+ else:
+ # otherwise, return the matching line
+ match = pattern.search(line)
+ if match:
if returnList:
- result.append(match.group(self.regexGroup))
+ result.append(match.group())
else:
- return match.group(self.regexGroup)
- else:
- # otherwise, return the matching line
- match = pattern.search(line)
- if match:
- if returnList:
- result.append(match.group())
- else:
- return match.group()
- if result:
- return result
+ return match.group()
+ if result:
+ return result
# otherwise
raise V7CommandException(self, "no match for regular expression %s" % self.regex)
@@ -199,14 +212,22 @@
value = self.getString(regex, regexGroup, singleLine)
return string.atoi(value)
-class V7CommandException(exceptions.Exception):
+class V7CommandException(Exception):
def __init__(self, command, message):
self.message = message
self.command = command
def __str__(self):
return "\"%s\" %s" % (self.command.command, self.message)
+
+ # BaseException.message has been deprecated since Python 2.6. To prevent
+ # DeprecationWarning from popping up over this pre-existing attribute, use
+ # a new property that takes lookup precedence.
+ def _get_message(self): return self.__message
+ def _set_message(self, value): self.__message = value
+ message = property(_get_message, _set_message)
+
if __name__ == "__main__":