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

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, 7 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 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)
kkania 2012/05/22 16:36:42 i would assume that url is not None here; just che
42 self._outdir = outdir
43 self._platform = ((lambda p: p and p or pyauto_utils.GetCurrentPlatform())
44 (platform))
45 if not self._url:
kkania 2012/05/22 16:36:42 i would move this to parse args
46 print 'The specified URL does not exist, aborting...'
47 sys.exit(-1)
34 48
35 class FetchPrebuilt(object): 49 def _GetZipName(self):
kkania 2012/05/22 16:36:42 I don't like this name. I expect it to return the
36 """Util class to fetch prebuilt binaries to run PyAuto.""" 50 """Determines the name of the zip file based on platform."""
37 51 if not self._platform.startswith('linux'):
38 def _ParseArgs(self): 52 self._chrome_zip_name = 'chrome-%s' % { 'mac' : 'mac',
39 parser = optparse.OptionParser() 53 'win' : 'win32'
40 parser.add_option( 54 }[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: 55 else:
75 linux_32_names = ['linux', 'lucid32bit'] 56 linux_32_names = ['linux', 'lucid32bit']
76 linux_64_names = ['linux64', 'lucid64bit'] 57 linux_64_names = ['linux64', 'lucid64bit']
77 linux_names = {'linux': linux_32_names + linux_64_names, 58 linux_names = {'linux': linux_32_names + linux_64_names,
78 'linux32': linux_32_names, 59 'linux32': linux_32_names,
79 'linux64': linux_64_names 60 'linux64': linux_64_names
80 }[self._options.platform] 61 }[self._platform]
81 for name in linux_names: 62 for name in linux_names:
82 zip_name = 'chrome-' + name 63 zip_name = 'chrome-' + name
83 if self._DoesURLExist('%s/%s.zip' % (self._url, zip_name)): 64 if self.DoesUrlExist('%s/%s.zip' % (self._url, zip_name)):
84 self._chrome_zip_name = zip_name 65 self._chrome_zip_name = zip_name
85 break 66 break
86 else: 67 else:
87 raise RuntimeError('Could not find chrome zip at ' + self._url) 68 raise RuntimeError('Could not find chrome zip at ' + self._url)
88 69
89 # Setup urls to download. 70 def _GetDownloadUrls(self):
kkania 2012/05/22 16:36:42 Same as _GetZipName
71 """Generates URLs for files that are to be downloaded."""
90 self._chrome_zip_url = '%s/%s.zip' % (self._url, self._chrome_zip_name) 72 self._chrome_zip_url = '%s/%s.zip' % (self._url, self._chrome_zip_name)
91 self._remoting_zip_url = self._url + '/' + 'remoting-webapp.zip' 73 self._remoting_zip_url = self._url + '/' + 'remoting-webapp.zip'
92 chrome_test_url = '%s/%s.test' % (self._url, self._chrome_zip_name) 74 chrome_test_url = '%s/%s.test' % (self._url, self._chrome_zip_name)
93 self._pyautolib_py_url = '%s/pyautolib.py' % chrome_test_url 75 self._pyautolib_py_url = '%s/pyautolib.py' % chrome_test_url
94 if self._options.platform == 'win': 76 if self._platform == 'win':
95 self._pyautolib_so_name = '_pyautolib.pyd' 77 self._pyautolib_so_name = '_pyautolib.pyd'
96 self._chromedriver_name = 'chromedriver.exe' 78 self._chromedriver_name = 'chromedriver.exe'
97 else: 79 else:
98 self._pyautolib_so_name = '_pyautolib.so' 80 self._pyautolib_so_name = '_pyautolib.so'
99 self._chromedriver_name = 'chromedriver' 81 self._chromedriver_name = 'chromedriver'
100 self._pyautolib_so_url = chrome_test_url + '/' + self._pyautolib_so_name 82 self._pyautolib_so_url = chrome_test_url + '/' + self._pyautolib_so_name
101 self._chromedriver_url = chrome_test_url + '/' + self._chromedriver_name 83 self._chromedriver_url = chrome_test_url + '/' + self._chromedriver_name
102 84
103 def _GetLastestDownloadURL(self, os_platform): 85 @staticmethod
86 def GetLastestDownloadURL(os_platform):
104 os_type = {'win': 'Win', 87 os_type = {'win': 'Win',
105 'mac': 'Mac', 88 'mac': 'Mac',
106 'linux': 'Linux', 89 'linux': 'Linux',
107 'linux32': 'Linux', 90 'linux32': 'Linux',
108 'linux64': 'Linux_x64'}[os_platform] 91 'linux64': 'Linux_x64'}[os_platform]
109 if os_type == 'Linux' and platform.architecture()[0] == '64bit': 92 if os_type == 'Linux' and platform.architecture()[0] == '64bit':
110 os_type = 'Linux_x64' 93 os_type = 'Linux_x64'
111 last_change_url = ('http://commondatastorage.googleapis.com/' 94 last_change_url = ('http://commondatastorage.googleapis.com/'
112 'chromium-browser-continuous/%s/LAST_CHANGE' % os_type) 95 'chromium-browser-continuous/%s/LAST_CHANGE' % os_type)
113 response = urllib2.urlopen(last_change_url) 96 response = urllib2.urlopen(last_change_url)
114 last_change = response.read() 97 last_change = response.read()
115 if not last_change: 98 if not last_change:
116 print >>sys.stderr, ('Unable to get latest from %s' % last_change_url) 99 print >>sys.stderr, ('Unable to get latest from %s' % last_change_url)
117 sys.exit(2) 100 sys.exit(2)
118 last_change_url = ('http://commondatastorage.googleapis.com/' 101 last_change_url = ('http://commondatastorage.googleapis.com/'
119 'chromium-browser-continuous/%s/%s' % (os_type, 102 'chromium-browser-continuous/%s/%s' % (os_type,
120 last_change)) 103 last_change))
121 return last_change_url 104 return last_change_url
122 105
123 def _DoesURLExist(self, url): 106 def DoesUrlExist(self, url):
124 """Determines whether a resource exists at the given URL.""" 107 """Determines whether a resource exists at the given URL."""
125 parsed = urlparse.urlparse(url) 108 parsed = urlparse.urlparse(url)
126 conn = httplib.HTTPConnection(parsed.netloc) 109 try:
127 conn.request('HEAD', parsed.path) 110 conn = httplib.HTTPConnection(parsed.netloc)
128 response = conn.getresponse() 111 conn.request('HEAD', parsed.path)
129 if response.status == 302: # Redirect; follow it. 112 response = conn.getresponse()
130 return self._DoesURLExist(response.getheader('location')) 113 except(socket.gaierror, socket.error), err:
kkania 2012/05/22 16:36:42 space between except and '('? check the style guid
114 print 'FetchPrebuilt.DoesUrlExist: %s' %(err)
kkania 2012/05/22 16:36:42 % err
115 return False
116 # Redirect, follow it (301:Permanent/302:temporary, both should be caught)
kkania 2012/05/22 16:36:42 '.' at end; use an english sentence, like: # Follo
117 if response.status == 302 or response.status == 301:
118 return self.DoesUrlExist(response.getheader('location'))
131 return response.status == 200 119 return response.status == 200
132 120
133 def Cleanup(self): 121 def Cleanup(self):
134 """Remove old binaries, if any.""" 122 """Remove old binaries, if any."""
135 pass 123 pass
136 124
137 def Run(self): 125 def Run(self):
138 self._ParseArgs()
139 if not os.path.isdir(self._outdir): 126 if not os.path.isdir(self._outdir):
140 os.makedirs(self._outdir) 127 os.makedirs(self._outdir)
141 get_remoting = self._DoesURLExist(self._remoting_zip_url) 128 # Generate name of the zip file that will be downloaded.
142 129 self._GetZipName()
130 # Generate URLs for files that are to be downloaded.
131 self._GetDownloadUrls()
132 get_remoting = self.DoesUrlExist(self._remoting_zip_url)
133
143 # Fetch chrome & pyauto binaries 134 # Fetch chrome & pyauto binaries
144 print 'Fetching', self._chrome_zip_url 135 print 'Fetching', self._chrome_zip_url
145 chrome_zip = urllib.urlretrieve(self._chrome_zip_url)[0] 136 chrome_zip = urllib.urlretrieve(self._chrome_zip_url)[0]
146 137
147 if get_remoting: 138 if get_remoting:
148 print 'Fetching', self._remoting_zip_url 139 print 'Fetching', self._remoting_zip_url
149 remoting_zip = urllib.urlretrieve(self._remoting_zip_url)[0] 140 remoting_zip = urllib.urlretrieve(self._remoting_zip_url)[0]
150 else: 141 else:
151 print 'Warning: %s does not exist.' % self._remoting_zip_url 142 print 'Warning: %s does not exist.' % self._remoting_zip_url
152 143
153 print 'Fetching', self._pyautolib_py_url 144 print 'Fetching', self._pyautolib_py_url
154 pyautolib_py = urllib.urlretrieve(self._pyautolib_py_url)[0] 145 pyautolib_py = urllib.urlretrieve(self._pyautolib_py_url)[0]
155 146
156 print 'Fetching', self._pyautolib_so_url 147 print 'Fetching', self._pyautolib_so_url
157 pyautolib_so = urllib.urlretrieve(self._pyautolib_so_url)[0] 148 pyautolib_so = urllib.urlretrieve(self._pyautolib_so_url)[0]
158 149
159 print 'Fetching', self._chromedriver_url 150 print 'Fetching', self._chromedriver_url
160 chromedriver = urllib.urlretrieve(self._chromedriver_url)[0] 151 chromedriver = urllib.urlretrieve(self._chromedriver_url)[0]
161 152
162 chrome_unzip_dir = os.path.join(self._outdir, self._chrome_zip_name) 153 chrome_unzip_dir = os.path.join(self._outdir, self._chrome_zip_name)
163 if os.path.exists(chrome_unzip_dir): 154 if os.path.exists(chrome_unzip_dir):
164 print 'Cleaning', chrome_unzip_dir 155 print 'Cleaning', chrome_unzip_dir
165 pyauto_utils.RemovePath(chrome_unzip_dir) 156 pyauto_utils.RemovePath(chrome_unzip_dir)
166 print 'Unzipping' 157 print 'Unzipping'
167 pyauto_utils.UnzipFilenameToDir(chrome_zip, self._outdir) 158 try:
159 pyauto_utils.UnzipFilenameToDir(chrome_zip, self._outdir)
160 except(zipfile.BadZipfile, OSError), err:
kkania 2012/05/22 16:36:42 why do you want to catch this exception? What do y
nkang 2012/05/24 17:00:13 I added this exception handler and the one below,
161 print 'FetchPrebuilt.Run: %s' % (err)
162 return -1
168 if get_remoting: 163 if get_remoting:
169 pyauto_utils.UnzipFilenameToDir(remoting_zip, self._outdir) 164 pyauto_utils.UnzipFilenameToDir(remoting_zip, self._outdir)
170 shutil.move(self._outdir + '/remoting-webapp', 165 # Added this try/catch block to prevent a run-time error, which occurs
kkania 2012/05/22 16:36:42 How does this class work in general if the outdir
nkang 2012/05/24 17:00:13 See above response.
171 self._outdir + '/remoting/remoting.webapp') 166 # if remoting-webapp directory already exists at the destination.
167 try:
168 shutil.move(self._outdir + '/remoting-webapp',
169 self._outdir + '/remoting/remoting.webapp')
170 except shutil.Error, err:
171 print 'FetchPrebuilt.Run: %s' % (err)
172 return -1
172 173
173 # Copy over the binaries to outdir 174 # Copy over the binaries to destination directory.
174 items_to_copy = { 175 items_to_copy = {
175 pyautolib_py: os.path.join(self._outdir, 'pyautolib.py'), 176 pyautolib_py: os.path.join(self._outdir, 'pyautolib.py'),
176 pyautolib_so: os.path.join(self._outdir, self._pyautolib_so_name), 177 pyautolib_so: os.path.join(self._outdir, self._pyautolib_so_name),
177 chromedriver: os.path.join(self._outdir, self._chromedriver_name) 178 chromedriver: os.path.join(self._outdir, self._chromedriver_name)
178 } 179 }
179 unzip_dir_contents = glob.glob(os.path.join(chrome_unzip_dir, '*')) 180 unzip_dir_contents = glob.glob(os.path.join(chrome_unzip_dir, '*'))
180 for item in unzip_dir_contents: 181 for item in unzip_dir_contents:
181 name = os.path.basename(item) 182 name = os.path.basename(item)
182 items_to_copy[item] = os.path.join(self._outdir, name) 183 items_to_copy[item] = os.path.join(self._outdir, name)
183 184
184 for src, dest in items_to_copy.iteritems(): 185 for src, dest in items_to_copy.iteritems():
185 pyauto_utils.RemovePath(dest) 186 pyauto_utils.RemovePath(dest)
186 print '%s ==> %s' % (os.path.basename(src), dest) 187 print '%s ==> %s' % (os.path.basename(src), dest)
187 shutil.move(src, dest) 188 shutil.move(src, dest)
188 pyauto_utils.RemovePath(chrome_unzip_dir) 189 pyauto_utils.RemovePath(chrome_unzip_dir)
189 190
190 # Final setup (if any) 191 # Final setup (if any)
191 # Set executable bit on chromedriver binary. 192 # Set executable bit on chromedriver binary.
192 if not self._options.platform == 'win': 193 if not self._platform == 'win':
193 os.chmod(items_to_copy[chromedriver], 0700) 194 os.chmod(items_to_copy[chromedriver], 0700)
194 195
195 # Create symlink to .framework on Mac 196 # Create symlink to .framework on Mac
196 if self._options.platform == 'mac': 197 if self._platform == 'mac':
197 mac_app_name = os.path.basename([x for x in unzip_dir_contents 198 mac_app_name = os.path.basename([x for x in unzip_dir_contents
198 if x.endswith('.app')][0]) 199 if x.endswith('.app')][0])
199 os.chdir(self._outdir) 200 os.chdir(self._outdir)
200 framework = glob.glob(os.path.join( 201 framework = glob.glob(os.path.join(
201 mac_app_name, 'Contents', 'Versions', '*', '*.framework'))[0] 202 mac_app_name, 'Contents', 'Versions', '*', '*.framework'))[0]
202 print framework 203 print framework
203 dest = os.path.basename(framework) 204 dest = os.path.basename(framework)
204 os.path.lexists(dest) and os.remove(dest) 205 os.path.lexists(dest) and os.remove(dest)
205 print 'Creating symlink "%s"' % dest 206 print 'Creating symlink "%s"' % dest
206 os.symlink(framework, dest) 207 os.symlink(framework, dest)
207 208
208 print 'Prepared binaries in "%s"' % self._outdir 209 print 'Prepared binaries in "%s"' % self._outdir
209 return 0 210 return 0
210 211
211 212
212 if __name__ == '__main__': 213 class Main:
213 sys.exit(FetchPrebuilt().Run()) 214
215 def __init__(self):
216 self._ParseArgs()
217 FetchPrebuilt(self._url, self._outdir, self._platform).Run()
218
219 def _ParseArgs(self):
220 parser = optparse.OptionParser()
221 parser.add_option(
222 '-d', '--outdir', type='string', default=None,
223 help='Directory in which to setup. This is typically the directory '
224 'where the binaries would go when compiled from source.')
225 parser.add_option(
226 '-p', '--platform', type='string',
227 default=pyauto_utils.GetCurrentPlatform(),
228 help='Platform. Valid options: win, mac, linux32, linux64. '
229 'Default: current platform (%s)' % pyauto_utils.GetCurrentPlatform())
230 parser.add_option(
231 '-l', '--latest', action='store_true', default=False,
232 help='Download the latest chromium build from commondatastorage. '
233 '[default=False]')
234 self._options, self._args = parser.parse_args()
kkania 2012/05/22 16:36:42 change these from member vars to local vars
235 if not self._options.outdir:
236 print >>sys.stderr, 'Need output directory: -d/--outdir'
237 sys.exit(1)
238 if self._options.latest:
239 self._url = FetchPrebuilt.GetLastestDownloadURL(self._options.platform)
240 elif not self._args:
241 print >>sys.stderr, 'Need download url'
242 sys.exit(2)
243 else:
244 self._url = self._args[0]
245
246 self._platform = self._options.platform
247 self._outdir = self._options.outdir
248 # Chromium continuous build archive has a non-standard format.
249 if 'index.html?path=' in self._url:
250 self._url = self._url.replace('index.html?path=', '')
251 self._url = self._url.rstrip('/')
252
253
254 if __name__ == '__main__':
255 Main()
OLDNEW
« no previous file with comments | « pyautolib/chrome_installer.py ('k') | pyautolib/install_test.py » ('j') | pyautolib/pyauto.py » ('J')

Powered by Google App Engine
This is Rietveld 408576698