Chromium Code Reviews| 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 socket | |
| 32 import stat | |
| 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 = url | |
| 42 if not self.DoesUrlExist(self._url): | |
| 43 raise httplib.error('%s is not a valid URL.' % self._url) | |
| 44 self._outdir = outdir | |
| 45 self._platform = ((lambda p: p and p or pyauto_utils.GetCurrentPlatform()) | |
| 46 (platform)) | |
| 47 self._InitZipName() | |
| 48 self._InitDownloadUrls() | |
| 34 | 49 |
| 35 class FetchPrebuilt(object): | 50 def _InitZipName(self): |
| 36 """Util class to fetch prebuilt binaries to run PyAuto.""" | 51 """Determines the name of the zip file based on platform.""" |
| 37 | 52 if not self._platform.startswith('linux'): |
| 38 def _ParseArgs(self): | 53 self._chrome_zip_name = 'chrome-%s' % { 'mac' : 'mac', |
| 39 parser = optparse.OptionParser() | 54 'win' : 'win32' |
| 40 parser.add_option( | 55 }[self._platform] |
| 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: | 56 else: |
| 75 linux_32_names = ['linux', 'lucid32bit'] | 57 linux_32_names = ['linux', 'lucid32bit'] |
| 76 linux_64_names = ['linux64', 'lucid64bit'] | 58 linux_64_names = ['linux64', 'lucid64bit'] |
| 77 linux_names = {'linux': linux_32_names + linux_64_names, | 59 linux_names = {'linux': linux_32_names + linux_64_names, |
| 78 'linux32': linux_32_names, | 60 'linux32': linux_32_names, |
| 79 'linux64': linux_64_names | 61 'linux64': linux_64_names |
| 80 }[self._options.platform] | 62 }[self._platform] |
| 81 for name in linux_names: | 63 for name in linux_names: |
| 82 zip_name = 'chrome-' + name | 64 zip_name = 'chrome-' + name |
| 83 if self._DoesURLExist('%s/%s.zip' % (self._url, zip_name)): | 65 if self.DoesUrlExist('%s/%s.zip' % (self._url, zip_name)): |
| 84 self._chrome_zip_name = zip_name | 66 self._chrome_zip_name = zip_name |
| 85 break | 67 break |
| 86 else: | 68 else: |
| 87 raise RuntimeError('Could not find chrome zip at ' + self._url) | 69 raise RuntimeError('Could not find %s at ' % zip_name + self._url) |
| 88 | 70 |
| 89 # Setup urls to download. | 71 def _InitDownloadUrls(self): |
| 72 """Generates URLs for files that are to be downloaded.""" | |
| 90 self._chrome_zip_url = '%s/%s.zip' % (self._url, self._chrome_zip_name) | 73 self._chrome_zip_url = '%s/%s.zip' % (self._url, self._chrome_zip_name) |
| 91 self._remoting_zip_url = self._url + '/' + 'remoting-webapp.zip' | 74 self._remoting_zip_url = self._url + '/' + 'remoting-webapp.zip' |
| 92 chrome_test_url = '%s/%s.test' % (self._url, self._chrome_zip_name) | 75 chrome_test_url = '%s/%s.test' % (self._url, self._chrome_zip_name) |
| 93 self._pyautolib_py_url = '%s/pyautolib.py' % chrome_test_url | 76 self._pyautolib_py_url = '%s/pyautolib.py' % chrome_test_url |
| 94 if self._options.platform == 'win': | 77 if self._platform == 'win': |
| 95 self._pyautolib_so_name = '_pyautolib.pyd' | 78 self._pyautolib_so_name = '_pyautolib.pyd' |
| 96 self._chromedriver_name = 'chromedriver.exe' | 79 self._chromedriver_name = 'chromedriver.exe' |
| 97 else: | 80 else: |
| 98 self._pyautolib_so_name = '_pyautolib.so' | 81 self._pyautolib_so_name = '_pyautolib.so' |
| 99 self._chromedriver_name = 'chromedriver' | 82 self._chromedriver_name = 'chromedriver' |
| 100 self._pyautolib_so_url = chrome_test_url + '/' + self._pyautolib_so_name | 83 self._pyautolib_so_url = chrome_test_url + '/' + self._pyautolib_so_name |
| 101 self._chromedriver_url = chrome_test_url + '/' + self._chromedriver_name | 84 self._chromedriver_url = chrome_test_url + '/' + self._chromedriver_name |
| 102 | 85 |
| 103 def _GetLastestDownloadURL(self, os_platform): | 86 @staticmethod |
| 87 def GetLastestDownloadURL(os_platform): | |
| 104 os_type = {'win': 'Win', | 88 os_type = {'win': 'Win', |
| 105 'mac': 'Mac', | 89 'mac': 'Mac', |
| 106 'linux': 'Linux', | 90 'linux': 'Linux', |
| 107 'linux32': 'Linux', | 91 'linux32': 'Linux', |
| 108 'linux64': 'Linux_x64'}[os_platform] | 92 'linux64': 'Linux_x64'}[os_platform] |
| 109 if os_type == 'Linux' and platform.architecture()[0] == '64bit': | 93 if os_type == 'Linux' and platform.architecture()[0] == '64bit': |
| 110 os_type = 'Linux_x64' | 94 os_type = 'Linux_x64' |
| 111 last_change_url = ('http://commondatastorage.googleapis.com/' | 95 last_change_url = ('http://commondatastorage.googleapis.com/' |
| 112 'chromium-browser-continuous/%s/LAST_CHANGE' % os_type) | 96 'chromium-browser-continuous/%s/LAST_CHANGE' % os_type) |
| 113 response = urllib2.urlopen(last_change_url) | 97 response = urllib2.urlopen(last_change_url) |
| 114 last_change = response.read() | 98 last_change = response.read() |
| 115 if not last_change: | 99 if not last_change: |
| 116 print >>sys.stderr, ('Unable to get latest from %s' % last_change_url) | 100 print >>sys.stderr, ('Unable to get latest from %s' % last_change_url) |
| 117 sys.exit(2) | 101 sys.exit(2) |
| 118 last_change_url = ('http://commondatastorage.googleapis.com/' | 102 last_change_url = ('http://commondatastorage.googleapis.com/' |
| 119 'chromium-browser-continuous/%s/%s' % (os_type, | 103 'chromium-browser-continuous/%s/%s' % (os_type, |
| 120 last_change)) | 104 last_change)) |
| 121 return last_change_url | 105 return last_change_url |
| 122 | 106 |
| 123 def _DoesURLExist(self, url): | 107 def DoesUrlExist(self, url): |
| 124 """Determines whether a resource exists at the given URL.""" | 108 """Determines whether a resource exists at the given URL.""" |
| 125 parsed = urlparse.urlparse(url) | 109 parsed = urlparse.urlparse(url) |
| 126 conn = httplib.HTTPConnection(parsed.netloc) | 110 try: |
| 127 conn.request('HEAD', parsed.path) | 111 conn = httplib.HTTPConnection(parsed.netloc) |
| 128 response = conn.getresponse() | 112 conn.request('HEAD', parsed.path) |
| 129 if response.status == 302: # Redirect; follow it. | 113 response = conn.getresponse() |
| 130 return self._DoesURLExist(response.getheader('location')) | 114 except (socket.gaierror, socket.error), err: |
| 115 print 'FetchPrebuilt.DoesUrlExist: %s' % err | |
| 116 return False | |
| 117 # Follow both permanent (301) and temporary (302) redirects. | |
| 118 if response.status == 302 or response.status == 301: | |
| 119 return self.DoesUrlExist(response.getheader('location')) | |
| 131 return response.status == 200 | 120 return response.status == 200 |
| 132 | 121 |
| 133 def Cleanup(self): | 122 def _Cleanup(self): |
| 134 """Remove old binaries, if any.""" | 123 """Remove old binaries, if any.""" |
| 135 pass | 124 lst_dirs = ['PepperFlash', 'remoting', 'locales', 'resources', 'installer'] |
|
kkania
2012/05/29 18:23:14
rename this var
nkang
2012/05/29 19:29:57
I'm assuming you were talking about 'files', so I
| |
| 125 files = [os.path.join(self._outdir, name) for name in lst_dirs] | |
| 126 for f in files: | |
| 127 if os.path.exists(f): | |
| 128 try: | |
| 129 shutil.rmtree(f, onerror=self._OnError) | |
| 130 except: | |
| 131 raise RuntimeError('Could not delete %s, access violation' % f) | |
| 136 | 132 |
| 137 def Run(self): | 133 def _OnError(self, func, path, exc_info): |
|
kkania
2012/05/29 18:23:14
how about move this func into _Cleanup?
nkang
2012/05/29 19:29:57
Wasn't sure how the style guide felt about nested
| |
| 138 self._ParseArgs() | 134 """Callback for shutil.rmtree.""" |
| 135 if not os.access(path, os.W_OK): | |
| 136 os.chmod(path, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO) | |
| 137 func(path) | |
| 138 else: | |
| 139 raise OSError('%s is not a read-only file, access denied.' % path) | |
| 140 | |
| 141 def Run(self): | |
| 139 if not os.path.isdir(self._outdir): | 142 if not os.path.isdir(self._outdir): |
| 140 os.makedirs(self._outdir) | 143 os.makedirs(self._outdir) |
| 141 get_remoting = self._DoesURLExist(self._remoting_zip_url) | 144 else: |
| 142 | 145 print 'Cleaning up %s' % self._outdir |
| 146 self._Cleanup() | |
| 147 get_remoting = self.DoesUrlExist(self._remoting_zip_url) | |
| 143 # Fetch chrome & pyauto binaries | 148 # Fetch chrome & pyauto binaries |
| 144 print 'Fetching', self._chrome_zip_url | 149 print 'Fetching', self._chrome_zip_url |
| 145 chrome_zip = urllib.urlretrieve(self._chrome_zip_url)[0] | 150 chrome_zip = urllib.urlretrieve(self._chrome_zip_url)[0] |
| 146 | 151 |
| 147 if get_remoting: | 152 if get_remoting: |
| 148 print 'Fetching', self._remoting_zip_url | 153 print 'Fetching', self._remoting_zip_url |
| 149 remoting_zip = urllib.urlretrieve(self._remoting_zip_url)[0] | 154 remoting_zip = urllib.urlretrieve(self._remoting_zip_url)[0] |
| 150 else: | 155 else: |
| 151 print 'Warning: %s does not exist.' % self._remoting_zip_url | 156 print 'Warning: %s does not exist.' % self._remoting_zip_url |
| 152 | 157 |
| 153 print 'Fetching', self._pyautolib_py_url | 158 print 'Fetching', self._pyautolib_py_url |
| 154 pyautolib_py = urllib.urlretrieve(self._pyautolib_py_url)[0] | 159 pyautolib_py = urllib.urlretrieve(self._pyautolib_py_url)[0] |
| 155 | 160 |
| 156 print 'Fetching', self._pyautolib_so_url | 161 print 'Fetching', self._pyautolib_so_url |
| 157 pyautolib_so = urllib.urlretrieve(self._pyautolib_so_url)[0] | 162 pyautolib_so = urllib.urlretrieve(self._pyautolib_so_url)[0] |
| 158 | 163 |
| 159 print 'Fetching', self._chromedriver_url | 164 print 'Fetching', self._chromedriver_url |
| 160 chromedriver = urllib.urlretrieve(self._chromedriver_url)[0] | 165 chromedriver = urllib.urlretrieve(self._chromedriver_url)[0] |
| 161 | 166 |
| 162 chrome_unzip_dir = os.path.join(self._outdir, self._chrome_zip_name) | 167 chrome_unzip_dir = os.path.join(self._outdir, self._chrome_zip_name) |
| 163 if os.path.exists(chrome_unzip_dir): | 168 if os.path.exists(chrome_unzip_dir): |
| 164 print 'Cleaning', chrome_unzip_dir | 169 print 'Cleaning', chrome_unzip_dir |
| 165 pyauto_utils.RemovePath(chrome_unzip_dir) | 170 pyauto_utils.RemovePath(chrome_unzip_dir) |
| 166 print 'Unzipping' | 171 print 'Unzipping' |
| 167 pyauto_utils.UnzipFilenameToDir(chrome_zip, self._outdir) | 172 try: |
| 173 pyauto_utils.UnzipFilenameToDir(chrome_zip, self._outdir) | |
| 174 except: | |
|
kkania
2012/05/29 18:23:14
why did you change this? I think it was better to
nkang
2012/05/29 19:29:57
I changed it because we have to import 'zipfile' j
| |
| 175 raise RuntimeError('Specified zip file does not exist or is corrupted.') | |
| 168 if get_remoting: | 176 if get_remoting: |
| 169 pyauto_utils.UnzipFilenameToDir(remoting_zip, self._outdir) | 177 pyauto_utils.UnzipFilenameToDir(remoting_zip, self._outdir) |
| 170 shutil.move(self._outdir + '/remoting-webapp', | 178 shutil.move(self._outdir + '/remoting-webapp', |
| 171 self._outdir + '/remoting/remoting.webapp') | 179 self._outdir + '/remoting/remoting.webapp') |
| 172 | 180 # Copy over the binaries to destination directory. |
| 173 # Copy over the binaries to outdir | |
| 174 items_to_copy = { | 181 items_to_copy = { |
| 175 pyautolib_py: os.path.join(self._outdir, 'pyautolib.py'), | 182 pyautolib_py: os.path.join(self._outdir, 'pyautolib.py'), |
| 176 pyautolib_so: os.path.join(self._outdir, self._pyautolib_so_name), | 183 pyautolib_so: os.path.join(self._outdir, self._pyautolib_so_name), |
| 177 chromedriver: os.path.join(self._outdir, self._chromedriver_name) | 184 chromedriver: os.path.join(self._outdir, self._chromedriver_name) |
| 178 } | 185 } |
| 179 unzip_dir_contents = glob.glob(os.path.join(chrome_unzip_dir, '*')) | 186 unzip_dir_contents = glob.glob(os.path.join(chrome_unzip_dir, '*')) |
| 180 for item in unzip_dir_contents: | 187 for item in unzip_dir_contents: |
| 181 name = os.path.basename(item) | 188 name = os.path.basename(item) |
| 182 items_to_copy[item] = os.path.join(self._outdir, name) | 189 items_to_copy[item] = os.path.join(self._outdir, name) |
| 183 | 190 |
| 184 for src, dest in items_to_copy.iteritems(): | 191 for src, dest in items_to_copy.iteritems(): |
| 185 pyauto_utils.RemovePath(dest) | 192 pyauto_utils.RemovePath(dest) |
| 186 print '%s ==> %s' % (os.path.basename(src), dest) | 193 print '%s ==> %s' % (os.path.basename(src), dest) |
| 187 shutil.move(src, dest) | 194 shutil.move(src, dest) |
| 188 pyauto_utils.RemovePath(chrome_unzip_dir) | 195 pyauto_utils.RemovePath(chrome_unzip_dir) |
| 189 | 196 |
| 190 # Final setup (if any) | 197 # Final setup (if any) |
| 191 # Set executable bit on chromedriver binary. | 198 # Set executable bit on chromedriver binary. |
| 192 if not self._options.platform == 'win': | 199 if not self._platform == 'win': |
| 193 os.chmod(items_to_copy[chromedriver], 0700) | 200 os.chmod(items_to_copy[chromedriver], 0700) |
| 194 | 201 |
| 195 # Create symlink to .framework on Mac | 202 # Create symlink to .framework on Mac |
| 196 if self._options.platform == 'mac': | 203 if self._platform == 'mac': |
| 197 mac_app_name = os.path.basename([x for x in unzip_dir_contents | 204 mac_app_name = os.path.basename([x for x in unzip_dir_contents |
| 198 if x.endswith('.app')][0]) | 205 if x.endswith('.app')][0]) |
| 199 os.chdir(self._outdir) | 206 os.chdir(self._outdir) |
| 200 framework = glob.glob(os.path.join( | 207 framework = glob.glob(os.path.join( |
| 201 mac_app_name, 'Contents', 'Versions', '*', '*.framework'))[0] | 208 mac_app_name, 'Contents', 'Versions', '*', '*.framework'))[0] |
| 202 print framework | 209 print framework |
| 203 dest = os.path.basename(framework) | 210 dest = os.path.basename(framework) |
| 204 os.path.lexists(dest) and os.remove(dest) | 211 os.path.lexists(dest) and os.remove(dest) |
| 205 print 'Creating symlink "%s"' % dest | 212 print 'Creating symlink "%s"' % dest |
| 206 os.symlink(framework, dest) | 213 os.symlink(framework, dest) |
| 207 | 214 |
| 208 print 'Prepared binaries in "%s"' % self._outdir | 215 print 'Prepared binaries in "%s"' % self._outdir |
| 209 return 0 | 216 return 0 |
| 210 | 217 |
| 211 | 218 |
| 212 if __name__ == '__main__': | 219 class Main: |
| 213 sys.exit(FetchPrebuilt().Run()) | 220 |
| 221 def __init__(self): | |
| 222 self._ParseArgs() | |
| 223 FetchPrebuilt(self._url, self._outdir, self._platform).Run() | |
| 224 | |
| 225 def _ParseArgs(self): | |
| 226 parser = optparse.OptionParser() | |
| 227 parser.add_option( | |
| 228 '-d', '--outdir', type='string', default=None, | |
| 229 help='Directory in which to setup. This is typically the directory ' | |
| 230 'where the binaries would go when compiled from source.') | |
| 231 parser.add_option( | |
| 232 '-p', '--platform', type='string', | |
| 233 default=pyauto_utils.GetCurrentPlatform(), | |
| 234 help='Platform. Valid options: win, mac, linux32, linux64. ' | |
| 235 'Default: current platform (%s)' % pyauto_utils.GetCurrentPlatform()) | |
| 236 parser.add_option( | |
| 237 '-l', '--latest', action='store_true', default=False, | |
| 238 help='Download the latest chromium build from commondatastorage. ' | |
| 239 '[default=False]') | |
| 240 options, args = parser.parse_args() | |
| 241 if not options.outdir: | |
| 242 print >>sys.stderr, 'Need output directory: -d/--outdir' | |
| 243 sys.exit(1) | |
| 244 if options.latest: | |
| 245 self._url = FetchPrebuilt.GetLastestDownloadURL(options.platform) | |
| 246 elif not args: | |
| 247 print >>sys.stderr, 'Need download url' | |
| 248 sys.exit(2) | |
| 249 else: | |
| 250 self._url = args[0] | |
| 251 | |
| 252 self._platform = options.platform | |
| 253 self._outdir = options.outdir | |
| 254 # Chromium continuous build archive has a non-standard format. | |
| 255 if 'index.html?path=' in self._url: | |
| 256 self._url = self._url.replace('index.html?path=', '') | |
| 257 self._url = self._url.rstrip('/') | |
| 258 | |
| 259 | |
| 260 if __name__ == '__main__': | |
| 261 Main() | |
| OLD | NEW |