Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 138 lines (125 sloc) 5.723 kb
5bfa25b @choller Initial code commit
choller authored
1 #!/usr/bin/env python
2 #
3 # This Source Code Form is subject to the terms of the Mozilla Public
4 # License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 # You can obtain one at http://mozilla.org/MPL/2.0/.
6
7 import os
8 import platform
9 import subprocess
10 import time
11
12 verbose = False
13
14 ########################
15 # Platform Detection #
16 ########################
17
18 def macType():
19 '''
20 If system is a Mac, return the mac type.
21 '''
22 assert platform.system() in ('Windows', 'Linux', 'Darwin')
23 isMac = isSL = amiLion = False
24 if platform.system() == 'Darwin':
25 isMac = True
26 # Script has only been tested on Snow Leopard and Lion.
27 assert 6 <= int(platform.mac_ver()[0].split('.')[1]) <= 7
28 isSL = isMac and platform.mac_ver()[0].split('.')[1] == '6' \
29 and platform.mac_ver()[0].split('.') >= ['10', '6']
30 amiLion = isMac and platform.mac_ver()[0].split('.')[1] == '7' \
31 and platform.mac_ver()[0].split('.') >= ['10', '7']
32 return (isMac, isSL, amiLion)
33
34 def isVM():
35 '''
36 Returns the OS of the system, if system is a VM.
37 '''
38 vm = False
39 # In VMware, shared folders are in z:, and we copy from the shared folders to avoid having
40 # another copy of the repository in the VM.
41 if (platform.uname()[2] == 'XP' \
42 and os.path.exists(os.path.join('z:', os.sep, 'fuzzing'))) or \
43 platform.uname()[0] == 'Linux' \
44 and os.path.exists(os.path.join('/', 'mnt', 'hgfs', 'fuzzing')):
45 assert not os.path.exists(normExpUserPath(os.path.join('~', 'fuzzing')))
46 assert not os.path.exists(normExpUserPath(os.path.join('~', 'trees')))
47 vm = True
48 return (platform.system(), vm)
49
50 #####################
51 # Shell Functions #
52 #####################
53
54 def captureStdout(cmd, ignoreStderr=False, combineStderr=False, ignoreExitCode=False,
55 currWorkingDir=os.getcwdu(), env=os.environ, verbosity=False):
56 '''
57 Captures standard output, returns the output as a string, along with the return value.
58 '''
59 vdump(' '.join(cmd))
60 p = subprocess.Popen(cmd,
61 stdin = subprocess.PIPE,
62 stdout = subprocess.PIPE,
63 stderr = subprocess.STDOUT if combineStderr else subprocess.PIPE,
64 cwd=currWorkingDir, env=env)
65 (stdout, stderr) = p.communicate()
66 if not ignoreExitCode and p.returncode != 0:
67 # Potential problem area: Note that having a non-zero exit code does not mean that the
68 # operation did not succeed, for example when compiling a shell. A non-zero exit code can
69 # appear even though a shell compiled successfully. This issue has been bypassed in the
70 # makeShell function in autoBisect.
71 # Pymake in builds earlier than revision 232553f741a0 did not support the '-s' option.
72 if 'no such option: -s' not in stdout:
73 print 'Nonzero exit code from ' + repr(cmd)
74 print stdout
75 if stderr is not None:
76 print stderr
77 # Pymake in builds earlier than revision 232553f741a0 did not support the '-s' option.
78 if 'hg pull: option --rebase not recognized' not in stdout and \
79 'no such option: -s' not in stdout:
80 raise Exception('Nonzero exit code')
81 if not combineStderr and not ignoreStderr and len(stderr) > 0:
82 if not ((platform.system() == 'Windows' and \
83 # Ignore hg color mode throwing an error in console on Windows platforms.
84 'warning: failed to set color mode to win32' in stderr) or \
85 (isVM() == ('Linux', True) and \
86 # Ignore stderr warning when running a Linux VM on a Mac host:
87 # Not trusting file /mnt/hgfs/trees/mozilla-central/.hg/hgrc from untrusted user 501...
88 'hgrc from untrusted user 501' in stderr)):
89 print 'Unexpected output on stderr from ' + repr(cmd)
90 print stdout, stderr
91 raise Exception('Unexpected output on stderr')
92 if stderr and ignoreStderr and len(stderr) > 0 and p.returncode != 0:
93 # During configure, there will always be stderr. Sometimes this stderr causes configure to
94 # stop the entire script, especially on Windows.
95 print 'Return code not zero, and unexpected output on stderr from ' + repr(cmd)
96 print stdout, stderr
97 raise Exception('Return code not zero, and unexpected output on stderr')
98 if verbose or verbosity:
99 print stdout
100 if stderr is not None:
101 print stderr
102 return stdout.rstrip(), p.returncode
103
104 def dateStr():
105 '''
106 Equivalent of: assert subprocess.check_output(['Date'])[:-1] == currDateTime
107 '''
108 currTz = time.tzname[0] if time.daylight == 1 else time.tzname[1]
109 currAscDateTime = time.asctime( time.localtime(time.time()) )
110 currDateTime = currAscDateTime[:-4] + currTz + ' ' + currAscDateTime[-4:]
111 return currDateTime
112
113 def normExpUserPath(p):
114 return os.path.normpath(os.path.expanduser(p))
115
116 def timeSubprocess(command, cwd=os.getcwdu(), vb=False):
117 '''
118 Calculates how long a captureStdout command takes and prints it. Returns the stdout and return
119 value that captureStdout passes on.
120 '''
121 print 'Running `%s` now..' % ' '.join(command)
122 startTime = time.time()
123 stdOutput, retVal = captureStdout(
124 command, ignoreStderr=True, combineStderr=True, currWorkingDir=cwd, verbosity=vb)
125 endTime = time.time()
126 print '`' + ' '.join(command) + '` took %.3f seconds.\n' % (endTime - startTime)
127 return stdOutput, retVal
128
129 def vdump(inp):
130 '''
131 This function appends the word 'DEBUG' to any verbose output.
132 '''
133 if verbose:
134 print 'DEBUG -', inp
135
136 if __name__ == '__main__':
137 pass
Something went wrong with that request. Please try again.