Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(404)

Side by Side Diff: pyautolib/fetch_prebuilt_pyauto.py

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

Powered by Google App Engine
This is Rietveld 408576698