Bug 1188336 - Enable Mulet TaskCluster Gbu. r=garndt
[gecko.git] / build / automationutils.py
blob527131ea0d90c8f4e5c920f68f435d4fd01f1c3c
2 # This Source Code Form is subject to the terms of the Mozilla Public
3 # License, v. 2.0. If a copy of the MPL was not distributed with this
4 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 from __future__ import with_statement
7 import logging
8 from operator import itemgetter
9 import os
10 import platform
11 import re
12 import signal
13 import subprocess
14 import sys
15 import tempfile
16 import mozinfo
18 __all__ = [
19 'dumpScreen',
20 "setAutomationLog",
23 log = logging.getLogger()
24 def resetGlobalLog():
25 while log.handlers:
26 log.removeHandler(log.handlers[0])
27 handler = logging.StreamHandler(sys.stdout)
28 log.setLevel(logging.INFO)
29 log.addHandler(handler)
30 resetGlobalLog()
32 def setAutomationLog(alt_logger):
33 global log
34 log = alt_logger
36 # Python does not provide strsignal() even in the very latest 3.x.
37 # This is a reasonable fake.
38 def strsig(n):
39 # Signal numbers run 0 through NSIG-1; an array with NSIG members
40 # has exactly that many slots
41 _sigtbl = [None]*signal.NSIG
42 for k in dir(signal):
43 if k.startswith("SIG") and not k.startswith("SIG_") and k != "SIGCLD" and k != "SIGPOLL":
44 _sigtbl[getattr(signal, k)] = k
45 # Realtime signals mostly have no names
46 if hasattr(signal, "SIGRTMIN") and hasattr(signal, "SIGRTMAX"):
47 for r in range(signal.SIGRTMIN+1, signal.SIGRTMAX+1):
48 _sigtbl[r] = "SIGRTMIN+" + str(r - signal.SIGRTMIN)
49 # Fill in any remaining gaps
50 for i in range(signal.NSIG):
51 if _sigtbl[i] is None:
52 _sigtbl[i] = "unrecognized signal, number " + str(i)
53 if n < 0 or n >= signal.NSIG:
54 return "out-of-range signal, number "+str(n)
55 return _sigtbl[n]
57 def printstatus(status, name = ""):
58 # 'status' is the exit status
59 if os.name != 'posix':
60 # Windows error codes are easier to look up if printed in hexadecimal
61 if status < 0:
62 status += 2**32
63 print "TEST-INFO | %s: exit status %x\n" % (name, status)
64 elif os.WIFEXITED(status):
65 print "TEST-INFO | %s: exit %d\n" % (name, os.WEXITSTATUS(status))
66 elif os.WIFSIGNALED(status):
67 # The python stdlib doesn't appear to have strsignal(), alas
68 print "TEST-INFO | {}: killed by {}".format(name,strsig(os.WTERMSIG(status)))
69 else:
70 # This is probably a can't-happen condition on Unix, but let's be defensive
71 print "TEST-INFO | %s: undecodable exit status %04x\n" % (name, status)
73 def dumpScreen(utilityPath):
74 """dumps a screenshot of the entire screen to a directory specified by
75 the MOZ_UPLOAD_DIR environment variable"""
77 # Need to figure out which OS-dependent tool to use
78 if mozinfo.isUnix:
79 utility = [os.path.join(utilityPath, "screentopng")]
80 utilityname = "screentopng"
81 elif mozinfo.isMac:
82 utility = ['/usr/sbin/screencapture', '-C', '-x', '-t', 'png']
83 utilityname = "screencapture"
84 elif mozinfo.isWin:
85 utility = [os.path.join(utilityPath, "screenshot.exe")]
86 utilityname = "screenshot"
88 # Get dir where to write the screenshot file
89 parent_dir = os.environ.get('MOZ_UPLOAD_DIR', None)
90 if not parent_dir:
91 log.info('Failed to retrieve MOZ_UPLOAD_DIR env var')
92 return
94 # Run the capture
95 try:
96 tmpfd, imgfilename = tempfile.mkstemp(prefix='mozilla-test-fail-screenshot_', suffix='.png', dir=parent_dir)
97 os.close(tmpfd)
98 returncode = subprocess.call(utility + [imgfilename])
99 printstatus(returncode, utilityname)
100 except OSError, err:
101 log.info("Failed to start %s for screenshot: %s"
102 % (utility[0], err.strerror))
103 return