Skip to content
This repository has been archived by the owner on Aug 20, 2018. It is now read-only.

Commit

Permalink
Merge pull request #31 from malini/master
Browse files Browse the repository at this point in the history
Add timeouts for shell/checkCmd
  • Loading branch information
malini committed Aug 16, 2012
2 parents 87647d5 + 56a9c1d commit 6a089f1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
2 changes: 1 addition & 1 deletion mozdevice/mozdevice/devicemanager.py
Expand Up @@ -39,7 +39,7 @@ def not_implemented(*args, **kwargs):
class DeviceManager:

@abstractmethod
def shell(self, cmd, outputfile, env=None, cwd=None):
def shell(self, cmd, outputfile, env=None, cwd=None, timeout=None):
"""
executes shell command on device
returns:
Expand Down
29 changes: 25 additions & 4 deletions mozdevice/mozdevice/devicemanagerADB.py
Expand Up @@ -8,6 +8,7 @@
import os
import sys
import tempfile
import time

class DeviceManagerADB(DeviceManager):

Expand Down Expand Up @@ -94,7 +95,7 @@ def __del__(self):
# returns:
# success: <return code>
# failure: None
def shell(self, cmd, outputfile, env=None, cwd=None):
def shell(self, cmd, outputfile, env=None, cwd=None, timeout=None):
# FIXME: this function buffers all output of the command into memory,
# always. :(

Expand All @@ -117,6 +118,15 @@ def shell(self, cmd, outputfile, env=None, cwd=None):
args.extend(["shell", cmdline])
proc = subprocess.Popen(args,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if timeout:
start_time = time.time()
ret_code = proc.poll()
while ((time.time() - start_time) <= timeout) and ret_code == None:
time.sleep(1)
ret_code = proc.poll()
if ret_code == None:
proc.kill()
raise DMError("Timeout exceeded for shell call")
(stdout, stderr) = proc.communicate()
outputfile.write(stdout.rstrip('\n'))

Expand Down Expand Up @@ -710,7 +720,7 @@ def runCmdAs(self, args):
args.insert(2, self.packageName)
return self.runCmd(args)

def checkCmd(self, args):
def checkCmd(self, args, timeout=None):
# If we are not root but have run-as, and we're trying to execute
# a shell command then using run-as is the best we can do
finalArgs = [self.adbPath]
Expand All @@ -720,13 +730,24 @@ def checkCmd(self, args):
args.insert(1, "run-as")
args.insert(2, self.packageName)
finalArgs.extend(args)
if timeout:
proc = subprocess.Popen(finalArgs)
start_time = time.time()
ret_code = proc.poll()
while ((time.time() - start_time) <= timeout) and ret_code == None:
time.sleep(1)
ret_code = proc.poll()
if ret_code == None:
proc.kill()
raise DMError("Timeout exceeded for checkCmd call")
return ret_code
return subprocess.check_call(finalArgs)

def checkCmdAs(self, args):
def checkCmdAs(self, args, timeout=None):
if (self.useRunAs):
args.insert(1, "run-as")
args.insert(2, self.packageName)
return self.checkCmd(args)
return self.checkCmd(args, timeout)

# external function
# returns:
Expand Down
6 changes: 3 additions & 3 deletions mozdevice/mozdevice/devicemanagerSUT.py
Expand Up @@ -265,16 +265,16 @@ def _doCmds(self, cmdlist, outputfile, timeout):
# returns:
# success: <return code>
# failure: None
def shell(self, cmd, outputfile, env=None, cwd=None):
def shell(self, cmd, outputfile, env=None, cwd=None, timeout=None):
cmdline = self._escapedCommandLine(cmd)
if env:
cmdline = '%s %s' % (self.formatEnvString(env), cmdline)

try:
if cwd:
self.sendCmds([{ 'cmd': 'execcwd %s %s' % (cwd, cmdline) }], outputfile)
self.sendCmds([{ 'cmd': 'execcwd %s %s' % (cwd, cmdline) }], outputfile, timeout)
else:
self.sendCmds([{ 'cmd': 'exec su -c "%s"' % cmdline }], outputfile)
self.sendCmds([{ 'cmd': 'exec su -c "%s"' % cmdline }], outputfile, timeout)
except AgentError:
return None

Expand Down

0 comments on commit 6a089f1

Please sign in to comment.