Bug 961108 - make dumpScreen always write to a file in MOZ_UPLOAD_DIR. r=ted,jmaher
authorMihai Tabara <tabara.mihai@gmail.com>
Fri, 17 Jan 2014 12:04:02 -0500
changeset 180741 e2c507997d550bf62f558526fcfc8a2f235a3c93
parent 180740 db641fd01a485e554259272992cfc8a658d7dc0a
child 180742 4faf6493c9ea5abcc5e6bad66eb98388e89b3282
push id3343
push userffxbld
push dateMon, 17 Mar 2014 21:55:32 +0000
treeherdermozilla-beta@2f7d3415f79f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersted, jmaher
bugs961108
milestone29.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 961108 - make dumpScreen always write to a file in MOZ_UPLOAD_DIR. r=ted,jmaher
build/automation.py.in
build/automationutils.py
--- a/build/automation.py.in
+++ b/build/automation.py.in
@@ -629,60 +629,18 @@ class Automation(object):
         raise
 
   def dumpScreen(self, utilityPath):
     if self.haveDumpedScreen:
       self.log.info("Not taking screenshot here: see the one that was previously logged")
       return
 
     self.haveDumpedScreen = True;
-
-    # Need to figure out what tool and whether it write to a file or stdout
-    if self.UNIXISH:
-      utility = [os.path.join(utilityPath, "screentopng")]
-      imgoutput = 'stdout'
-    elif self.IS_MAC:
-      utility = ['/usr/sbin/screencapture', '-C', '-x', '-t', 'png']
-      imgoutput = 'file'
-    elif self.IS_WIN32:
-      utility = [os.path.join(utilityPath, "screenshot.exe")]
-      imgoutput = 'file'
+    automationutils.dumpScreen(utilityPath)
 
-    # Run the capture correctly for the type of capture
-    try:
-      if imgoutput == 'file':
-        tmpfd, imgfilename = tempfile.mkstemp(prefix='mozilla-test-fail_')
-        os.close(tmpfd)
-        dumper = self.Process(utility + [imgfilename])
-      elif imgoutput == 'stdout':
-        dumper = self.Process(utility, bufsize=-1,
-                              stdout=subprocess.PIPE, close_fds=True)
-    except OSError, err:
-      self.log.info("Failed to start %s for screenshot: %s",
-                    utility[0], err.strerror)
-      return
-
-    # Check whether the capture utility ran successfully
-    dumper_out, dumper_err = dumper.communicate()
-    if dumper.returncode != 0:
-      self.log.info("%s exited with code %d", utility, dumper.returncode)
-      return
-
-    try:
-      if imgoutput == 'stdout':
-        image = dumper_out
-      elif imgoutput == 'file':
-        with open(imgfilename, 'rb') as imgfile:
-          image = imgfile.read()
-    except IOError, err:
-        self.log.info("Failed to read image from %s", imgoutput)
-
-    import base64
-    encoded = base64.b64encode(image)
-    self.log.info("SCREENSHOT: data:image/png;base64,%s", encoded)
 
   def killAndGetStack(self, processPID, utilityPath, debuggerInfo):
     """Kill the process, preferrably in a way that gets us a stack trace.
        Also attempts to obtain a screenshot before killing the process."""
     if not debuggerInfo:
       self.dumpScreen(utilityPath)
     self.killAndGetStackNoScreenshot(processPID, utilityPath, debuggerInfo)
 
--- a/build/automationutils.py
+++ b/build/automationutils.py
@@ -479,68 +479,50 @@ def environment(xrePath, env=None, crash
       log.info("Failed determine available memory, disabling ASan low-memory configuration: %s", err.strerror)
     except:
       log.info("Failed determine available memory, disabling ASan low-memory configuration")
     else:
       log.info(message)
 
   return env
 
+def dumpScreen(utilityPath):
+  """dumps a screenshot of the entire screen to a directory specified by
+  the MOZ_UPLOAD_DIR environment variable"""
+  import mozfile
 
-def dumpScreen(utilityPath):
-  """dumps the screen to the log file as a data URI"""
-
-  # Need to figure out what tool and whether it write to a file or stdout
+  # Need to figure out which OS-dependent tool to use
   if mozinfo.isUnix:
     utility = [os.path.join(utilityPath, "screentopng")]
-    imgoutput = 'stdout'
   elif mozinfo.isMac:
     utility = ['/usr/sbin/screencapture', '-C', '-x', '-t', 'png']
-    imgoutput = 'file'
   elif mozinfo.isWin:
     utility = [os.path.join(utilityPath, "screenshot.exe")]
-    imgoutput = 'file'
-  else:
-    log.warn("Unable to dump screen on platform '%s'", sys.platform)
+
+  # Get dir where to write the screenshot file
+  parent_dir = os.environ.get('MOZ_UPLOAD_DIR', None)
+  if not parent_dir:
+    log.info('Failed to retrieve MOZ_UPLOAD_DIR env var')
+    return
 
-  # Run the capture correctly for the type of capture
-  kwargs = {'stdout': subprocess.PIPE}
-  if imgoutput == 'file':
-    tmpfd, imgfilename = tempfile.mkstemp(prefix='mozilla-test-fail_')
-    os.close(tmpfd)
-    utility.append(imgfilename)
-  elif imgoutput == 'stdout':
-    kwargs.update(dict(bufsize=-1, close_fds=True))
+  # Run the capture
   try:
-    dumper = subprocess.Popen(utility, **kwargs)
+    with mozfile.NamedTemporaryFile(suffix='.png',
+                                    prefix='mozilla-test-fail-screenshot_',
+                                    dir=parent_dir,
+                                    delete=False) as f:
+      returncode = subprocess.call(utility + [f.name])
   except OSError, err:
     log.info("Failed to start %s for screenshot: %s",
              utility[0], err.strerror)
     return
 
   # Check whether the capture utility ran successfully
-  stdout, _ = dumper.communicate()
-  returncode = dumper.poll()
-  if returncode:
+  if returncode != 0:
     log.info("%s exited with code %d", utility, returncode)
-    return
-
-  try:
-    if imgoutput == 'stdout':
-      image = stdout
-    elif imgoutput == 'file':
-      with open(imgfilename, 'rb') as imgfile:
-        image = imgfile.read()
-  except IOError, err:
-    log.info("Failed to read image from %s", imgoutput)
-
-  encoded = base64.b64encode(image)
-  uri = "data:image/png;base64,%s" %  encoded
-  log.info("SCREENSHOT: %s", uri)
-  return uri
 
 class ShutdownLeaks(object):
   """
   Parses the mochitest run log when running a debug build, assigns all leaked
   DOM windows (that are still around after test suite shutdown, despite running
   the GC) to the tests that created them and prints leak statistics.
   """