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)
69 lsb_release = '/etc/lsb-release'
70 if sys.platform.startswith('linux') and os.path.isfile(lsb_release):
71 with open(lsb_release) as fp:
73 return 'CHROMEOS_RELEASE_NAME=' in contents
76 # Ensure this is the right python version (2.6 for chrome, 2.7 for chromeOS).
78 if sys.version_info[0:2] != (2, 7):
79 cmd = ['python2.7'] + sys.argv
80 print 'Running: ', ' '.join(cmd)
81 proc = subprocess.Popen(cmd)
84 if sys.version_info[0:2] != (2, 6):
87 # Check this is the right bitness on mac.
88 # platform.architecture() will not help us on mac, since multiple binaries
89 # are stuffed inside the universal python binary.
90 if sys.platform.startswith('darwin') and sys.maxint > 2**32:
91 # User is running 64-bit python, but we should use 32-bit.
95 # Do not attempt to figure out python versions if
96 # DO_NOT_RESTART_PYTHON_FOR_PYAUTO is set.
97 if os.getenv('DO_NOT_RESTART_PYTHON_FOR_PYAUTO') is None:
98 RunWithCorrectPythonIfNecessary()
100 print 'Will not try to restart with the correct version of python '\
101 'as DO_NOT_RESTART_PYTHON_FOR_PYAUTO is set.'
107 print >>sys.stderr, 'Cannot import pyauto from %s' % sys.path
111 class Main(pyauto.Main):
112 """Main program for running PyAuto functional tests."""
115 # Make scripts in this dir importable
116 sys.path.append(os.path.dirname(__file__))
117 pyauto.Main.__init__(self)
120 return os.path.dirname(__file__)
123 if __name__ == '__main__':