1 #!/usr/bin/env python 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. 5 6 """Setup for PyAuto functional tests. 7 8 Use the following in your scripts to run them standalone: 9 10 # This should be at the top 11 import pyauto_functional 12 13 if __name__ == '__main__': 14 pyauto_functional.Main() 15 16 This script can be used as an executable to fire off other scripts, similar 17 to unittest.py 18 python pyauto_functional.py test_script 19 """ 20 21 import os 22 import subprocess 23 import sys 24 25 26 def _LocatePyAutoDir(): 27 sys.path.append(os.path.join(os.path.dirname(__file__), 28 os.pardir, 'pyautolib')) 29 30 31 _LocatePyAutoDir() 32 import pyauto_paths 33 34 35 def RunWithCorrectPythonIfNecessary(): 36 """Runs this script with the correct version of python if necessary. 37 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 41 version of python. 42 43 Note, this function will either return after doing nothing, or will exit with 44 the subprocess's return code. 45 """ 46 def RunAgain(): 47 """Run the script again with the correct version of python. 48 49 Note, this function does not return, but exits with the return code of the 50 child. 51 """ 52 if sys.platform == 'cygwin' or sys.platform.startswith('win'): 53 cmd = [os.path.join(pyauto_paths.GetThirdPartyDir(), 'python_26', 54 'python_slave.exe')] 55 elif sys.platform.startswith('darwin'): 56 # Arch runs the specified architecture of a universal binary. Run 57 # the 32 bit one. 58 cmd = ['arch', '-i386', 'python2.6'] 59 elif sys.platform.startswith('linux'): 60 cmd = ['python2.6'] 61 62 cmd.extend(sys.argv) 63 print 'Running:', ' '.join(cmd) 64 proc = subprocess.Popen(cmd) 65 proc.wait() 66 sys.exit(proc.returncode) 67 68 def IsChromeOS(): 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: 72 contents = fp.read() 73 return 'CHROMEOS_RELEASE_NAME=' in contents 74 return False 75 76 # Ensure this is the right python version (2.6 for chrome, 2.7 for chromeOS). 77 if IsChromeOS(): 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) 82 proc.wait() 83 else: 84 if sys.version_info[0:2] != (2, 6): 85 RunAgain() 86 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. 92 RunAgain() 93 94 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() 99 else: 100 print 'Will not try to restart with the correct version of python '\ 101 'as DO_NOT_RESTART_PYTHON_FOR_PYAUTO is set.' 102 103 104 try: 105 import pyauto 106 except ImportError: 107 print >>sys.stderr, 'Cannot import pyauto from %s' % sys.path 108 raise 109 110 111 class Main(pyauto.Main): 112 """Main program for running PyAuto functional tests.""" 113 114 def __init__(self): 115 # Make scripts in this dir importable 116 sys.path.append(os.path.dirname(__file__)) 117 pyauto.Main.__init__(self) 118 119 def TestsDir(self): 120 return os.path.dirname(__file__) 121 122 123 if __name__ == '__main__': 124 Main() 125