2 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 """Setup for PyAuto functional tests.
8 Use the following in your scripts to run them standalone:
10 # This should be at the top
11 import pyauto_functional
13 if __name__ == '__main__':
14 pyauto_functional.Main()
16 This script can be used as an executable to fire off other scripts, similar
18 python pyauto_functional.py test_script
26 def _LocatePyAutoDir():
27 sys
.path
.append(os
.path
.join(os
.path
.dirname(__file__
),
28 os
.pardir
, 'pyautolib'))
35 def RunWithCorrectPythonIfNecessary():
36 """Runs this script with the correct version of python if necessary.
38 Different platforms and versions of pyautolib use different python versions.
39 Instead of requiring testers and infrastructure to handle choosing the right
40 version (and architecture), this will rerun the script with the correct
43 Note, this function will either return after doing nothing, or will exit with
44 the subprocess's return code.
47 """Run the script again with the correct version of python.
49 Note, this function does not return, but exits with the return code of the
52 if sys
.platform
== 'cygwin' or sys
.platform
.startswith('win'):
53 cmd
= [os
.path
.join(pyauto_paths
.GetThirdPartyDir(), 'python_26',
55 elif sys
.platform
.startswith('darwin'):
56 # Arch runs the specified architecture of a universal binary. Run
58 cmd
= ['arch', '-i386', 'python2.6']
59 elif sys
.platform
.startswith('linux'):
63 print 'Running:', ' '.join(cmd
)
64 proc
= subprocess
.Popen(cmd
)
66 sys
.exit(proc
.returncode
)
68 # Check this is the right python version.
69 if sys
.version_info
[0:2] != (2, 6):
72 # Check this is the right bitness on mac.
73 # platform.architecture() will not help us on mac, since multiple binaries
74 # are stuffed inside the universal python binary.
75 if sys
.platform
.startswith('darwin') and sys
.maxint
> 2**32:
76 # User is running 64-bit python, but we should use 32-bit.
80 # Do not attempt to figure out python versions if
81 # DO_NOT_RESTART_PYTHON_FOR_PYAUTO is set.
82 if os
.getenv('DO_NOT_RESTART_PYTHON_FOR_PYAUTO') is None:
83 RunWithCorrectPythonIfNecessary()
85 print 'Will not try to restart with the correct version of python '\
86 'as DO_NOT_RESTART_PYTHON_FOR_PYAUTO is set.'
92 print >>sys
.stderr
, 'Cannot import pyauto from %s' % sys
.path
96 class Main(pyauto
.Main
):
97 """Main program for running PyAuto functional tests."""
100 # Make scripts in this dir importable
101 sys
.path
.append(os
.path
.dirname(__file__
))
102 pyauto
.Main
.__init
__(self
)
105 return os
.path
.dirname(__file__
)
108 if __name__
== '__main__':