Bug 758325 - Add --userdata argument to runemu, a=test-only, DONTBUILD because NPOTB
authorJonathan Griffin <jgriffin@mozilla.com>
Thu, 24 May 2012 12:12:52 -0700
changeset 94807 63be356973d3c388d82fffe800292fc63ecdfcb7
parent 94806 26d651834b0eb13daeb92f8ba556c98b765a6576
child 94808 5742ef22a91dc843b30572bbf8d98eb7312c572f
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewerstest-only, DONTBUILD
bugs758325
milestone15.0a1
Bug 758325 - Add --userdata argument to runemu, a=test-only, DONTBUILD because NPOTB
testing/marionette/client/marionette/emulator.py
testing/marionette/client/marionette/scripts/runemu.py
--- a/testing/marionette/client/marionette/emulator.py
+++ b/testing/marionette/client/marionette/emulator.py
@@ -32,33 +32,36 @@ class LogcatProc(ProcessHandlerMixin):
         f.write(line + "\n")
         f.flush()
 
 
 class Emulator(object):
 
     deviceRe = re.compile(r"^emulator-(\d+)(\s*)(.*)$")
 
-    def __init__(self, homedir=None, noWindow=False, logcat_dir=None, arch="x86", emulatorBinary=None):
+    def __init__(self, homedir=None, noWindow=False, logcat_dir=None, arch="x86",
+                 emulatorBinary=None, userdata=None):
         self.port = None
         self._emulator_launched = False
         self.proc = None
         self.marionette_port = None
         self.telnet = None
         self._tmp_userdata = None
         self._adb_started = False
         self.logcat_dir = logcat_dir
         self.logcat_proc = None
         self.arch = arch
         self.binary = emulatorBinary
         self.battery = EmulatorBattery(self)
         self.homedir = homedir
         self.noWindow = noWindow
         if self.homedir is not None:
             self.homedir = os.path.expanduser(homedir)
+        self.dataImg = userdata
+        self.copy_userdata = self.dataImg is None
 
     def _check_for_b2g(self):
         if self.homedir is None:
             self.homedir = os.getenv('B2G_HOME')
         if self.homedir is None:
             raise Exception('Must define B2G_HOME or pass the homedir parameter')
         self._check_file(self.homedir)
 
@@ -97,17 +100,18 @@ class Emulator(object):
         self._check_file(self.binary)
 
         self.kernelImg = os.path.join(self.homedir, kernel)
         self._check_file(self.kernelImg)
 
         self.sysDir = os.path.join(self.homedir, sysdir)
         self._check_file(self.sysDir)
 
-        self.dataImg = os.path.join(self.sysDir, 'userdata.img')
+        if not self.dataImg:
+            self.dataImg = os.path.join(self.sysDir, 'userdata.img')
         self._check_file(self.dataImg)
 
     def __del__(self):
         if self.telnet:
             self.telnet.write('exit\n')
             self.telnet.read_all()
 
     def _check_file(self, filePath):
@@ -241,22 +245,23 @@ class Emulator(object):
                 raise Exception('timed out waiting for emulator to be available')
             online, offline = self._get_adb_devices()
         self.port = int(list(online)[0])
 
     def start(self):
         self._check_for_b2g()
         self.start_adb()
 
-        # Make a copy of the userdata.img for this instance of the emulator
-        # to use.
-        self._tmp_userdata = tempfile.mktemp(prefix='marionette')
-        shutil.copyfile(self.dataImg, self._tmp_userdata)
         qemu_args = self.args[:]
-        qemu_args[qemu_args.index('-data') + 1] = self._tmp_userdata
+        if self.copy_userdata:
+            # Make a copy of the userdata.img for this instance of the emulator
+            # to use.
+            self._tmp_userdata = tempfile.mktemp(prefix='marionette')
+            shutil.copyfile(self.dataImg, self._tmp_userdata)
+            qemu_args[qemu_args.index('-data') + 1] = self._tmp_userdata
 
         original_online, original_offline = self._get_adb_devices()
 
         self.proc = subprocess.Popen(qemu_args,
                                      stdout=subprocess.PIPE,
                                      stderr=subprocess.PIPE)
 
         online, offline = self._get_adb_devices()
--- a/testing/marionette/client/marionette/scripts/runemu.py
+++ b/testing/marionette/client/marionette/scripts/runemu.py
@@ -1,14 +1,16 @@
 from marionette import Marionette, Emulator
 from optparse import OptionParser
 
 
-def runemulator(homedir=None, url=None, pidfile=None, arch='x86', noWindow=False):
-    qemu = Emulator(homedir=homedir, arch=arch, noWindow=noWindow)
+def runemulator(homedir=None, url=None, pidfile=None, arch='x86', noWindow=False,
+                userdata=None):
+    qemu = Emulator(homedir=homedir, arch=arch, noWindow=noWindow,
+                    userdata=userdata)
     qemu.start()
     port = qemu.setup_port_forwarding(2828)
     assert(qemu.wait_for_port())
     if pidfile:
         f = open(pidfile, 'w')
         f.write("%d" % qemu.proc.pid)
         f.close()
     print 'emulator launched, pid:', qemu.proc.pid
@@ -27,19 +29,22 @@ if __name__ == '__main__':
                       default='x86',
                       help='the emulator cpu architecture (x86 or arm)')
     parser.add_option('--url', dest='url', action='store',
                       help='url to navigate to after launching emulator')
     parser.add_option('--pidfile', dest='pidfile', action='store',
                       help='file in which to store emulator pid')
     parser.add_option('--no-window', dest='noWindow', action='store_true',
                       help='pass -no-window to the emulator')
+    parser.add_option('--userdata', dest='userdata', action='store',
+                      help='path to userdata.img file to use')
 
     options, args = parser.parse_args()
     if not options.repo_path:
         raise Exception ("must specify the --repo /path/to/B2G/repo argument")
 
     runemulator(homedir=options.repo_path,
                 url=options.url,
                 pidfile=options.pidfile,
                 arch=options.arch,
-                noWindow=options.noWindow)
+                noWindow=options.noWindow,
+                userdata=options.userdata)