1# Copyright 2012 the V8 project authors. All rights reserved.
2# Redistribution and use in source and binary forms, with or without
3# modification, are permitted provided that the following conditions are
4# met:
5#
6#     * Redistributions of source code must retain the above copyright
7#       notice, this list of conditions and the following disclaimer.
8#     * Redistributions in binary form must reproduce the above
9#       copyright notice, this list of conditions and the following
10#       disclaimer in the documentation and/or other materials provided
11#       with the distribution.
12#     * Neither the name of Google Inc. nor the names of its
13#       contributors may be used to endorse or promote products derived
14#       from this software without specific prior written permission.
15#
16# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28
29from os.path import exists
30from os.path import isdir
31from os.path import join
32import os
33import platform
34import re
35import subprocess
36import urllib2
37
38
39### Exit codes and their meaning.
40# Normal execution.
41EXIT_CODE_PASS = 0
42# Execution with test failures.
43EXIT_CODE_FAILURES = 1
44# Execution with no tests executed.
45EXIT_CODE_NO_TESTS = 2
46# Execution aborted with SIGINT (Ctrl-C).
47EXIT_CODE_INTERRUPTED = 3
48# Execution aborted with SIGTERM.
49EXIT_CODE_TERMINATED = 4
50# Internal error.
51EXIT_CODE_INTERNAL_ERROR = 5
52
53
54def GetSuitePaths(test_root):
55  return [ f for f in os.listdir(test_root) if isdir(join(test_root, f)) ]
56
57
58# Reads a file into an array of strings
59def ReadLinesFrom(name):
60  lines = []
61  with open(name) as f:
62    for line in f:
63      if line.startswith('#'): continue
64      if '#' in line:
65        line = line[:line.find('#')]
66      line = line.strip()
67      if not line: continue
68      lines.append(line)
69  return lines
70
71
72def GuessOS():
73  system = platform.system()
74  if system == 'Linux':
75    return 'linux'
76  elif system == 'Darwin':
77    return 'macos'
78  elif system.find('CYGWIN') >= 0:
79    return 'cygwin'
80  elif system == 'Windows' or system == 'Microsoft':
81    # On Windows Vista platform.system() can return 'Microsoft' with some
82    # versions of Python, see http://bugs.python.org/issue1082
83    return 'windows'
84  elif system == 'FreeBSD':
85    return 'freebsd'
86  elif system == 'OpenBSD':
87    return 'openbsd'
88  elif system == 'SunOS':
89    return 'solaris'
90  elif system == 'NetBSD':
91    return 'netbsd'
92  elif system == 'AIX':
93    return 'aix'
94  else:
95    return None
96
97
98def UseSimulator(arch):
99  machine = platform.machine()
100  return (machine and
101      (arch == "mipsel" or arch == "arm" or arch == "arm64") and
102      not arch.startswith(machine))
103
104
105# This will default to building the 32 bit VM even on machines that are
106# capable of running the 64 bit VM.
107def DefaultArch():
108  machine = platform.machine()
109  machine = machine.lower()  # Windows 7 capitalizes 'AMD64'.
110  if machine.startswith('arm'):
111    return 'arm'
112  elif (not machine) or (not re.match('(x|i[3-6])86$', machine) is None):
113    return 'ia32'
114  elif machine == 'i86pc':
115    return 'ia32'
116  elif machine == 'x86_64':
117    return 'ia32'
118  elif machine == 'amd64':
119    return 'ia32'
120  elif machine == 's390x':
121    return 's390'
122  elif machine == 'ppc64':
123    return 'ppc'
124  else:
125    return None
126
127
128def GuessWordsize():
129  if '64' in platform.machine():
130    return '64'
131  else:
132    return '32'
133
134
135def IsWindows():
136  return GuessOS() == 'windows'
137
138
139def URLRetrieve(source, destination):
140  """urllib is broken for SSL connections via a proxy therefore we
141  can't use urllib.urlretrieve()."""
142  if IsWindows():
143    try:
144      # In python 2.7.6 on windows, urlopen has a problem with redirects.
145      # Try using curl instead. Note, this is fixed in 2.7.8.
146      subprocess.check_call(["curl", source, '-k', '-L', '-o', destination])
147      return
148    except:
149      # If there's no curl, fall back to urlopen.
150      print "Curl is currently not installed. Falling back to python."
151      pass
152  with open(destination, 'w') as f:
153    f.write(urllib2.urlopen(source).read())
154
155
156class FrozenDict(dict):
157  def __setitem__(self, *args, **kwargs):
158    raise Exception('Tried to mutate a frozen dict')
159
160  def update(self, *args, **kwargs):
161    raise Exception('Tried to mutate a frozen dict')
162
163
164def Freeze(obj):
165  if isinstance(obj, dict):
166    return FrozenDict((k, Freeze(v)) for k, v in obj.iteritems())
167  elif isinstance(obj, set):
168    return frozenset(obj)
169  elif isinstance(obj, list):
170    return tuple(Freeze(item) for item in obj)
171  else:
172    # Make sure object is hashable.
173    hash(obj)
174    return obj
175