| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 """Fetch prebuilt binaries to run PyAuto. | 6 """Fetch prebuilt binaries to run PyAuto. |
| 7 | 7 |
| 8 Sets up Chrome and PyAuto binaries using prebuilt binaries from the | 8 Sets up Chrome and PyAuto binaries using prebuilt binaries from the |
| 9 continuous build archives. Works on mac, win, linux (32 & 64 bit). | 9 continuous build archives. Works on mac, win, linux (32 & 64 bit). |
| 10 | 10 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 import glob | 21 import glob |
| 22 import httplib | 22 import httplib |
| 23 import optparse | 23 import optparse |
| 24 import os | 24 import os |
| 25 import platform | 25 import platform |
| 26 import shutil | 26 import shutil |
| 27 import sys | 27 import sys |
| 28 import urllib | 28 import urllib |
| 29 import urllib2 | 29 import urllib2 |
| 30 import urlparse | 30 import urlparse |
| 31 import zipfile |
| 32 import socket |
| 31 | 33 |
| 32 import pyauto_utils | 34 import pyauto_utils |
| 33 | 35 |
| 36 |
| 37 class FetchPrebuilt(object): |
| 38 """Util class to fetch prebuilt binaries to run PyAuto.""" |
| 39 |
| 40 def __init__(self, url, outdir, platform): |
| 41 self._url = (lambda u: self.DoesUrlExist(u) and u or '')(url) |
| 42 self._outdir = outdir |
| 43 self._platform = ((lambda p: p and p or pyauto_utils.GetCurrentPlatform()) |
| 44 (platform)) |
| 45 if not self._url: |
| 46 print 'The specified URL does not exist, aborting...' |
| 47 sys.exit(-1) |
| 34 | 48 |
| 35 class FetchPrebuilt(object): | 49 <<<<<<< .mine |
| 36 """Util class to fetch prebuilt binaries to run PyAuto.""" | 50 def _GetZipName(self): |
| 51 """Determines the name of the zip file based on platform.""" |
| 52 if not self._platform.startswith('linux'): |
| 53 self._chrome_zip_name = 'chrome-%s' % { 'mac' : 'mac', |
| 54 'win' : 'win32' |
| 55 }[self._platform] |
| 56 ======= |
| 37 | 57 |
| 38 def _ParseArgs(self): | 58 >>>>>>> .r136319 |
| 39 parser = optparse.OptionParser() | |
| 40 parser.add_option( | |
| 41 '-d', '--outdir', type='string', default=None, | |
| 42 help='Directory in which to setup. This is typically the directory ' | |
| 43 'where the binaries would go when compiled from source.') | |
| 44 parser.add_option( | |
| 45 '-p', '--platform', type='string', | |
| 46 default=pyauto_utils.GetCurrentPlatform(), | |
| 47 help='Platform. Valid options: win, mac, linux32, linux64. ' | |
| 48 'Default: current platform (%s)' % pyauto_utils.GetCurrentPlatform()) | |
| 49 parser.add_option( | |
| 50 '-l', '--latest', action='store_true', default=False, | |
| 51 help='Download the latest chromium build from commondatastorage. ' | |
| 52 '[default=False]') | |
| 53 self._options, self._args = parser.parse_args() | |
| 54 if self._options.latest: | |
| 55 self._url = self._GetLastestDownloadURL(self._options.platform) | |
| 56 elif not self._args: | |
| 57 print >>sys.stderr, 'Need download url' | |
| 58 sys.exit(2) | |
| 59 else: | |
| 60 self._url = self._args[0] | |
| 61 if not self._options.outdir: | |
| 62 print >>sys.stderr, 'Need output directory: -d/--outdir' | |
| 63 sys.exit(1) | |
| 64 self._outdir = self._options.outdir | |
| 65 # Chromium continuous build archive has a non-standard format. | |
| 66 if 'index.html?path=' in self._url: | |
| 67 self._url = self._url.replace('index.html?path=', '') | |
| 68 self._url = self._url.rstrip('/') | |
| 69 # Determine name of zip. | |
| 70 if not self._options.platform.startswith('linux'): | |
| 71 self._chrome_zip_name = 'chrome-%s' % {'mac': 'mac', | |
| 72 'win': 'win32' | |
| 73 }[self._options.platform] | |
| 74 else: | 59 else: |
| 75 linux_32_names = ['linux', 'lucid32bit'] | 60 linux_32_names = ['linux', 'lucid32bit'] |
| 76 linux_64_names = ['linux64', 'lucid64bit'] | 61 linux_64_names = ['linux64', 'lucid64bit'] |
| 77 linux_names = {'linux': linux_32_names + linux_64_names, | 62 linux_names = {'linux': linux_32_names + linux_64_names, |
| 78 'linux32': linux_32_names, | 63 'linux32': linux_32_names, |
| 79 'linux64': linux_64_names | 64 'linux64': linux_64_names |
| 80 }[self._options.platform] | 65 }[self._platform] |
| 81 for name in linux_names: | 66 for name in linux_names: |
| 82 zip_name = 'chrome-' + name | 67 zip_name = 'chrome-' + name |
| 83 if self._DoesURLExist('%s/%s.zip' % (self._url, zip_name)): | 68 if self.DoesUrlExist('%s/%s.zip' % (self._url, zip_name)): |
| 84 self._chrome_zip_name = zip_name | 69 self._chrome_zip_name = zip_name |
| 85 break | 70 break |
| 86 else: | 71 else: |
| 87 raise RuntimeError('Could not find chrome zip at ' + self._url) | 72 raise RuntimeError('Could not find chrome zip at ' + self._url) |
| 88 | 73 |
| 89 # Setup urls to download. | 74 def _GetDownloadUrls(self): |
| 75 """Generates URLs for files that are to be downloaded.""" |
| 90 self._chrome_zip_url = '%s/%s.zip' % (self._url, self._chrome_zip_name) | 76 self._chrome_zip_url = '%s/%s.zip' % (self._url, self._chrome_zip_name) |
| 91 self._remoting_zip_url = self._url + '/' + 'remoting-webapp.zip' | 77 self._remoting_zip_url = self._url + '/' + 'remoting-webapp.zip' |
| 92 chrome_test_url = '%s/%s.test' % (self._url, self._chrome_zip_name) | 78 chrome_test_url = '%s/%s.test' % (self._url, self._chrome_zip_name) |
| 93 self._pyautolib_py_url = '%s/pyautolib.py' % chrome_test_url | 79 self._pyautolib_py_url = '%s/pyautolib.py' % chrome_test_url |
| 94 if self._options.platform == 'win': | 80 if self._platform == 'win': |
| 95 self._pyautolib_so_name = '_pyautolib.pyd' | 81 self._pyautolib_so_name = '_pyautolib.pyd' |
| 96 self._chromedriver_name = 'chromedriver.exe' | 82 self._chromedriver_name = 'chromedriver.exe' |
| 97 else: | 83 else: |
| 98 self._pyautolib_so_name = '_pyautolib.so' | 84 self._pyautolib_so_name = '_pyautolib.so' |
| 99 self._chromedriver_name = 'chromedriver' | 85 self._chromedriver_name = 'chromedriver' |
| 100 self._pyautolib_so_url = chrome_test_url + '/' + self._pyautolib_so_name | 86 self._pyautolib_so_url = chrome_test_url + '/' + self._pyautolib_so_name |
| 101 self._chromedriver_url = chrome_test_url + '/' + self._chromedriver_name | 87 self._chromedriver_url = chrome_test_url + '/' + self._chromedriver_name |
| 102 | 88 |
| 103 def _GetLastestDownloadURL(self, os_platform): | 89 def _GetLastestDownloadURL(self, os_platform): |
| 104 os_type = {'win': 'Win', | 90 os_type = {'win': 'Win', |
| (...skipping 11 matching lines...) Expand all Loading... |
| 116 print >>sys.stderr, ('Unable to get latest from %s' % last_change_url) | 102 print >>sys.stderr, ('Unable to get latest from %s' % last_change_url) |
| 117 sys.exit(2) | 103 sys.exit(2) |
| 118 last_change_url = ('http://commondatastorage.googleapis.com/' | 104 last_change_url = ('http://commondatastorage.googleapis.com/' |
| 119 'chromium-browser-continuous/%s/%s' % (os_type, | 105 'chromium-browser-continuous/%s/%s' % (os_type, |
| 120 last_change)) | 106 last_change)) |
| 121 return last_change_url | 107 return last_change_url |
| 122 | 108 |
| 123 def _DoesURLExist(self, url): | 109 def _DoesURLExist(self, url): |
| 124 """Determines whether a resource exists at the given URL.""" | 110 """Determines whether a resource exists at the given URL.""" |
| 125 parsed = urlparse.urlparse(url) | 111 parsed = urlparse.urlparse(url) |
| 126 conn = httplib.HTTPConnection(parsed.netloc) | 112 try: |
| 127 conn.request('HEAD', parsed.path) | 113 conn = httplib.HTTPConnection(parsed.netloc) |
| 128 response = conn.getresponse() | 114 conn.request('HEAD', parsed.path) |
| 129 if response.status == 302: # Redirect; follow it. | 115 response = conn.getresponse() |
| 130 return self._DoesURLExist(response.getheader('location')) | 116 except(socket.gaierror, socket.error), err: |
| 117 print 'FetchPrebuilt.DoesUrlExist: %s' %(err) |
| 118 return False |
| 119 # Redirect, follow it (301:Permanent/302:temporary, both should be caught) |
| 120 if response.status == 302 or response.status == 301: |
| 121 return self.DoesUrlExist(response.getheader('location')) |
| 131 return response.status == 200 | 122 return response.status == 200 |
| 132 | 123 |
| 133 def Cleanup(self): | 124 def Cleanup(self): |
| 134 """Remove old binaries, if any.""" | 125 """Remove old binaries, if any.""" |
| 135 pass | 126 pass |
| 136 | 127 |
| 137 def Run(self): | 128 def Run(self): |
| 138 self._ParseArgs() | |
| 139 if not os.path.isdir(self._outdir): | 129 if not os.path.isdir(self._outdir): |
| 140 os.makedirs(self._outdir) | 130 os.makedirs(self._outdir) |
| 141 get_remoting = self._DoesURLExist(self._remoting_zip_url) | 131 # Generate name of the zip file that will be downloaded. |
| 142 | 132 self._GetZipName() |
| 133 # Generate URLs for files that are to be downloaded. |
| 134 self._GetDownloadUrls() |
| 135 get_remoting = self.DoesUrlExist(self._remoting_zip_url) |
| 136 |
| 143 # Fetch chrome & pyauto binaries | 137 # Fetch chrome & pyauto binaries |
| 144 print 'Fetching', self._chrome_zip_url | 138 print 'Fetching', self._chrome_zip_url |
| 145 chrome_zip = urllib.urlretrieve(self._chrome_zip_url)[0] | 139 chrome_zip = urllib.urlretrieve(self._chrome_zip_url)[0] |
| 146 | 140 |
| 147 if get_remoting: | 141 if get_remoting: |
| 148 print 'Fetching', self._remoting_zip_url | 142 print 'Fetching', self._remoting_zip_url |
| 149 remoting_zip = urllib.urlretrieve(self._remoting_zip_url)[0] | 143 remoting_zip = urllib.urlretrieve(self._remoting_zip_url)[0] |
| 150 else: | 144 else: |
| 151 print 'Warning: %s does not exist.' % self._remoting_zip_url | 145 print 'Warning: %s does not exist.' % self._remoting_zip_url |
| 152 | 146 |
| 153 print 'Fetching', self._pyautolib_py_url | 147 print 'Fetching', self._pyautolib_py_url |
| 154 pyautolib_py = urllib.urlretrieve(self._pyautolib_py_url)[0] | 148 pyautolib_py = urllib.urlretrieve(self._pyautolib_py_url)[0] |
| 155 | 149 |
| 156 print 'Fetching', self._pyautolib_so_url | 150 print 'Fetching', self._pyautolib_so_url |
| 157 pyautolib_so = urllib.urlretrieve(self._pyautolib_so_url)[0] | 151 pyautolib_so = urllib.urlretrieve(self._pyautolib_so_url)[0] |
| 158 | 152 |
| 159 print 'Fetching', self._chromedriver_url | 153 print 'Fetching', self._chromedriver_url |
| 160 chromedriver = urllib.urlretrieve(self._chromedriver_url)[0] | 154 chromedriver = urllib.urlretrieve(self._chromedriver_url)[0] |
| 161 | 155 |
| 162 chrome_unzip_dir = os.path.join(self._outdir, self._chrome_zip_name) | 156 chrome_unzip_dir = os.path.join(self._outdir, self._chrome_zip_name) |
| 163 if os.path.exists(chrome_unzip_dir): | 157 if os.path.exists(chrome_unzip_dir): |
| 164 print 'Cleaning', chrome_unzip_dir | 158 print 'Cleaning', chrome_unzip_dir |
| 165 pyauto_utils.RemovePath(chrome_unzip_dir) | 159 pyauto_utils.RemovePath(chrome_unzip_dir) |
| 166 print 'Unzipping' | 160 print 'Unzipping' |
| 167 pyauto_utils.UnzipFilenameToDir(chrome_zip, self._outdir) | 161 try: |
| 162 pyauto_utils.UnzipFilenameToDir(chrome_zip, self._outdir) |
| 163 except(zipfile.BadZipfile, OSError), err: |
| 164 print 'FetchPrebuilt.Run: %s' % (err) |
| 165 return -1 |
| 168 if get_remoting: | 166 if get_remoting: |
| 169 pyauto_utils.UnzipFilenameToDir(remoting_zip, self._outdir) | 167 pyauto_utils.UnzipFilenameToDir(remoting_zip, self._outdir) |
| 170 shutil.move(self._outdir + '/remoting-webapp', | 168 # Added this try/catch block to prevent a run-time error, which occurs |
| 171 self._outdir + '/remoting/remoting.webapp') | 169 # if remoting-webapp directory already exists at the destination. |
| 170 try: |
| 171 shutil.move(self._outdir + '/remoting-webapp', |
| 172 self._outdir + '/remoting/remoting.webapp') |
| 173 except shutil.Error, err: |
| 174 print 'FetchPrebuilt.Run: %s' % (err) |
| 175 return -1 |
| 172 | 176 |
| 173 # Copy over the binaries to outdir | 177 # Copy over the binaries to destination directory. |
| 174 items_to_copy = { | 178 items_to_copy = { |
| 175 pyautolib_py: os.path.join(self._outdir, 'pyautolib.py'), | 179 pyautolib_py: os.path.join(self._outdir, 'pyautolib.py'), |
| 176 pyautolib_so: os.path.join(self._outdir, self._pyautolib_so_name), | 180 pyautolib_so: os.path.join(self._outdir, self._pyautolib_so_name), |
| 177 chromedriver: os.path.join(self._outdir, self._chromedriver_name) | 181 chromedriver: os.path.join(self._outdir, self._chromedriver_name) |
| 178 } | 182 } |
| 179 unzip_dir_contents = glob.glob(os.path.join(chrome_unzip_dir, '*')) | 183 unzip_dir_contents = glob.glob(os.path.join(chrome_unzip_dir, '*')) |
| 180 for item in unzip_dir_contents: | 184 for item in unzip_dir_contents: |
| 181 name = os.path.basename(item) | 185 name = os.path.basename(item) |
| 182 items_to_copy[item] = os.path.join(self._outdir, name) | 186 items_to_copy[item] = os.path.join(self._outdir, name) |
| 183 | 187 |
| 184 for src, dest in items_to_copy.iteritems(): | 188 for src, dest in items_to_copy.iteritems(): |
| 185 pyauto_utils.RemovePath(dest) | 189 pyauto_utils.RemovePath(dest) |
| 186 print '%s ==> %s' % (os.path.basename(src), dest) | 190 print '%s ==> %s' % (os.path.basename(src), dest) |
| 187 shutil.move(src, dest) | 191 shutil.move(src, dest) |
| 188 pyauto_utils.RemovePath(chrome_unzip_dir) | 192 pyauto_utils.RemovePath(chrome_unzip_dir) |
| 189 | 193 |
| 190 # Final setup (if any) | 194 # Final setup (if any) |
| 191 # Set executable bit on chromedriver binary. | 195 # Set executable bit on chromedriver binary. |
| 192 if not self._options.platform == 'win': | 196 if not self._platform == 'win': |
| 193 os.chmod(items_to_copy[chromedriver], 0700) | 197 os.chmod(items_to_copy[chromedriver], 0700) |
| 194 | 198 |
| 195 # Create symlink to .framework on Mac | 199 # Create symlink to .framework on Mac |
| 196 if self._options.platform == 'mac': | 200 if self._platform == 'mac': |
| 197 mac_app_name = os.path.basename([x for x in unzip_dir_contents | 201 mac_app_name = os.path.basename([x for x in unzip_dir_contents |
| 198 if x.endswith('.app')][0]) | 202 if x.endswith('.app')][0]) |
| 199 os.chdir(self._outdir) | 203 os.chdir(self._outdir) |
| 200 framework = glob.glob(os.path.join( | 204 framework = glob.glob(os.path.join( |
| 201 mac_app_name, 'Contents', 'Versions', '*', '*.framework'))[0] | 205 mac_app_name, 'Contents', 'Versions', '*', '*.framework'))[0] |
| 202 print framework | 206 print framework |
| 203 dest = os.path.basename(framework) | 207 dest = os.path.basename(framework) |
| 204 os.path.lexists(dest) and os.remove(dest) | 208 os.path.lexists(dest) and os.remove(dest) |
| 205 print 'Creating symlink "%s"' % dest | 209 print 'Creating symlink "%s"' % dest |
| 206 os.symlink(framework, dest) | 210 os.symlink(framework, dest) |
| 207 | 211 |
| 208 print 'Prepared binaries in "%s"' % self._outdir | 212 print 'Prepared binaries in "%s"' % self._outdir |
| 209 return 0 | 213 return 0 |
| 210 | 214 |
| 211 | 215 |
| 212 if __name__ == '__main__': | 216 class Main: |
| 213 sys.exit(FetchPrebuilt().Run()) | 217 |
| 218 def __init__(self): |
| 219 self._ParseArgs() |
| 220 FetchPrebuilt(self._url, self._outdir, self._platform).Run() |
| 221 |
| 222 def _ParseArgs(self): |
| 223 parser = optparse.OptionParser() |
| 224 parser.add_option( |
| 225 '-d', '--outdir', type='string', default=None, |
| 226 help='Directory in which to setup. This is typically the directory ' |
| 227 'where the binaries would go when compiled from source.') |
| 228 parser.add_option( |
| 229 '-p', '--platform', type='string', |
| 230 default=pyauto_utils.GetCurrentPlatform(), |
| 231 help='Platform. Valid options: win, man, linux32, linux64. ' |
| 232 'Default: current platform (%s)' % |
| 233 pyauto_utils.GetCurrentPlatform()) |
| 234 self._opts, self._args = parser.parse_args() |
| 235 if not self._opts.outdir: |
| 236 print >>sys.stderr, 'Need output directory: -d/--outdir' |
| 237 sys.exit(1) |
| 238 if not self._args: |
| 239 print >>sys.stderr, 'Need download url' |
| 240 sys.exit(2) |
| 241 |
| 242 self._platform = self._opts.platform |
| 243 self._outdir = self._opts.outdir |
| 244 self._url = self._args[0] |
| 245 # Chromium continuous build archive has a non-standard format. |
| 246 if 'index.html?path=' in self._url: |
| 247 self._url = self._url.replace('index.html?path=', '') |
| 248 self._url = self._url.rstrip('/') |
| 249 |
| 250 def _ParseArgs(self): |
| 251 parser = optparse.OptionParser() |
| 252 parser.add_option( |
| 253 '-d', '--outdir', type='string', default=None, |
| 254 help='Directory in which to setup. This is typically the directory ' |
| 255 'where the binaries would go when compiled from source.') |
| 256 parser.add_option( |
| 257 '-p', '--platform', type='string', |
| 258 default=pyauto_utils.GetCurrentPlatform(), |
| 259 help='Platform. Valid options: win, mac, linux32, linux64. ' |
| 260 'Default: current platform (%s)' % pyauto_utils.GetCurrentPlatform()) |
| 261 parser.add_option( |
| 262 '-l', '--latest', action='store_true', default=False, |
| 263 help='Download the latest chromium build from commondatastorage. ' |
| 264 '[default=False]') |
| 265 self._options, self._args = parser.parse_args() |
| 266 if self._options.latest: |
| 267 self._url = self._GetLastestDownloadURL(self._options.platform) |
| 268 elif not self._args: |
| 269 print >>sys.stderr, 'Need download url' |
| 270 sys.exit(2) |
| 271 else: |
| 272 self._url = self._args[0] |
| 273 if not self._options.outdir: |
| 274 print >>sys.stderr, 'Need output directory: -d/--outdir' |
| 275 sys.exit(1) |
| 276 self._outdir = self._options.outdir |
| 277 # Chromium continuous build archive has a non-standard format. |
| 278 if 'index.html?path=' in self._url: |
| 279 self._url = self._url.replace('index.html?path=', '') |
| 280 self._url = self._url.rstrip('/') |
| 281 # Determine name of zip. |
| 282 if not self._options.platform.startswith('linux'): |
| 283 self._chrome_zip_name = 'chrome-%s' % {'mac': 'mac', |
| 284 'win': 'win32' |
| 285 }[self._options.platform] |
| 286 |
| 287 |
| 288 if __name__ == '__main__': |
| 289 Main() |
| OLD | NEW |