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
8 from operator
import itemgetter
23 log
= logging
.getLogger()
26 log
.removeHandler(log
.handlers
[0])
27 handler
= logging
.StreamHandler(sys
.stdout
)
28 log
.setLevel(logging
.INFO
)
29 log
.addHandler(handler
)
32 def setAutomationLog(alt_logger
):
36 # Python does not provide strsignal() even in the very latest 3.x.
37 # This is a reasonable fake.
39 # Signal numbers run 0 through NSIG-1; an array with NSIG members
40 # has exactly that many slots
41 _sigtbl
= [None]*signal
.NSIG
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
)
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
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
)))
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
79 utility
= [os
.path
.join(utilityPath
, "screentopng")]
80 utilityname
= "screentopng"
82 utility
= ['/usr/sbin/screencapture', '-C', '-x', '-t', 'png']
83 utilityname
= "screencapture"
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)
91 log
.info('Failed to retrieve MOZ_UPLOAD_DIR env var')
96 tmpfd
, imgfilename
= tempfile
.mkstemp(prefix
='mozilla-test-fail-screenshot_', suffix
='.png', dir=parent_dir
)
98 returncode
= subprocess
.call(utility
+ [imgfilename
])
99 printstatus(returncode
, utilityname
)
101 log
.info("Failed to start %s for screenshot: %s"
102 % (utility
[0], err
.strerror
))