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

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
« no previous file with comments | « install_test/py_unittest_util.py ('k') | pyautolib/pyauto.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 = 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 # Generate name of the zip file that will be downloaded.
kkania 2012/05/25 00:50:20 remove this comment and the comment below.
48 self._InitZipName()
49 # Generate URLs for files that are to be downloaded.
50 self._InitDownloadUrls()
34 51
35 class FetchPrebuilt(object): 52 def _InitZipName(self):
36 """Util class to fetch prebuilt binaries to run PyAuto.""" 53 """Determines the name of the zip file based on platform."""
37 54 if not self._platform.startswith('linux'):
38 def _ParseArgs(self): 55 self._chrome_zip_name = 'chrome-%s' % { 'mac' : 'mac',
39 parser = optparse.OptionParser() 56 'win' : 'win32'
40 parser.add_option( 57 }[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: 58 else:
75 linux_32_names = ['linux', 'lucid32bit'] 59 linux_32_names = ['linux', 'lucid32bit']
76 linux_64_names = ['linux64', 'lucid64bit'] 60 linux_64_names = ['linux64', 'lucid64bit']
77 linux_names = {'linux': linux_32_names + linux_64_names, 61 linux_names = {'linux': linux_32_names + linux_64_names,
78 'linux32': linux_32_names, 62 'linux32': linux_32_names,
79 'linux64': linux_64_names 63 'linux64': linux_64_names
80 }[self._options.platform] 64 }[self._platform]
81 for name in linux_names: 65 for name in linux_names:
82 zip_name = 'chrome-' + name 66 zip_name = 'chrome-' + name
83 if self._DoesURLExist('%s/%s.zip' % (self._url, zip_name)): 67 if self.DoesUrlExist('%s/%s.zip' % (self._url, zip_name)):
84 self._chrome_zip_name = zip_name 68 self._chrome_zip_name = zip_name
85 break 69 break
86 else: 70 else:
87 raise RuntimeError('Could not find chrome zip at ' + self._url) 71 raise RuntimeError('Could not find chrome zip at ' + self._url)
88 72
89 # Setup urls to download. 73 def _InitDownloadUrls(self):
74 """Generates URLs for files that are to be downloaded."""
90 self._chrome_zip_url = '%s/%s.zip' % (self._url, self._chrome_zip_name) 75 self._chrome_zip_url = '%s/%s.zip' % (self._url, self._chrome_zip_name)
91 self._remoting_zip_url = self._url + '/' + 'remoting-webapp.zip' 76 self._remoting_zip_url = self._url + '/' + 'remoting-webapp.zip'
92 chrome_test_url = '%s/%s.test' % (self._url, self._chrome_zip_name) 77 chrome_test_url = '%s/%s.test' % (self._url, self._chrome_zip_name)
93 self._pyautolib_py_url = '%s/pyautolib.py' % chrome_test_url 78 self._pyautolib_py_url = '%s/pyautolib.py' % chrome_test_url
94 if self._options.platform == 'win': 79 if self._platform == 'win':
95 self._pyautolib_so_name = '_pyautolib.pyd' 80 self._pyautolib_so_name = '_pyautolib.pyd'
96 self._chromedriver_name = 'chromedriver.exe' 81 self._chromedriver_name = 'chromedriver.exe'
97 else: 82 else:
98 self._pyautolib_so_name = '_pyautolib.so' 83 self._pyautolib_so_name = '_pyautolib.so'
99 self._chromedriver_name = 'chromedriver' 84 self._chromedriver_name = 'chromedriver'
100 self._pyautolib_so_url = chrome_test_url + '/' + self._pyautolib_so_name 85 self._pyautolib_so_url = chrome_test_url + '/' + self._pyautolib_so_name
101 self._chromedriver_url = chrome_test_url + '/' + self._chromedriver_name 86 self._chromedriver_url = chrome_test_url + '/' + self._chromedriver_name
102 87
103 def _GetLastestDownloadURL(self, os_platform): 88 @staticmethod
89 def GetLastestDownloadURL(os_platform):
104 os_type = {'win': 'Win', 90 os_type = {'win': 'Win',
105 'mac': 'Mac', 91 'mac': 'Mac',
106 'linux': 'Linux', 92 'linux': 'Linux',
107 'linux32': 'Linux', 93 'linux32': 'Linux',
108 'linux64': 'Linux_x64'}[os_platform] 94 'linux64': 'Linux_x64'}[os_platform]
109 if os_type == 'Linux' and platform.architecture()[0] == '64bit': 95 if os_type == 'Linux' and platform.architecture()[0] == '64bit':
110 os_type = 'Linux_x64' 96 os_type = 'Linux_x64'
111 last_change_url = ('http://commondatastorage.googleapis.com/' 97 last_change_url = ('http://commondatastorage.googleapis.com/'
112 'chromium-browser-continuous/%s/LAST_CHANGE' % os_type) 98 'chromium-browser-continuous/%s/LAST_CHANGE' % os_type)
113 response = urllib2.urlopen(last_change_url) 99 response = urllib2.urlopen(last_change_url)
114 last_change = response.read() 100 last_change = response.read()
115 if not last_change: 101 if not last_change:
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 # Follow both permanent (301) and temporary (302) redirects.
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 get_remoting = self.DoesUrlExist(self._remoting_zip_url)
142
143 # Fetch chrome & pyauto binaries 132 # Fetch chrome & pyauto binaries
144 print 'Fetching', self._chrome_zip_url 133 print 'Fetching', self._chrome_zip_url
145 chrome_zip = urllib.urlretrieve(self._chrome_zip_url)[0] 134 chrome_zip = urllib.urlretrieve(self._chrome_zip_url)[0]
146 135
147 if get_remoting: 136 if get_remoting:
148 print 'Fetching', self._remoting_zip_url 137 print 'Fetching', self._remoting_zip_url
149 remoting_zip = urllib.urlretrieve(self._remoting_zip_url)[0] 138 remoting_zip = urllib.urlretrieve(self._remoting_zip_url)[0]
150 else: 139 else:
151 print 'Warning: %s does not exist.' % self._remoting_zip_url 140 print 'Warning: %s does not exist.' % self._remoting_zip_url
152 141
153 print 'Fetching', self._pyautolib_py_url 142 print 'Fetching', self._pyautolib_py_url
154 pyautolib_py = urllib.urlretrieve(self._pyautolib_py_url)[0] 143 pyautolib_py = urllib.urlretrieve(self._pyautolib_py_url)[0]
155 144
156 print 'Fetching', self._pyautolib_so_url 145 print 'Fetching', self._pyautolib_so_url
157 pyautolib_so = urllib.urlretrieve(self._pyautolib_so_url)[0] 146 pyautolib_so = urllib.urlretrieve(self._pyautolib_so_url)[0]
158 147
159 print 'Fetching', self._chromedriver_url 148 print 'Fetching', self._chromedriver_url
160 chromedriver = urllib.urlretrieve(self._chromedriver_url)[0] 149 chromedriver = urllib.urlretrieve(self._chromedriver_url)[0]
161 150
162 chrome_unzip_dir = os.path.join(self._outdir, self._chrome_zip_name) 151 chrome_unzip_dir = os.path.join(self._outdir, self._chrome_zip_name)
163 if os.path.exists(chrome_unzip_dir): 152 if os.path.exists(chrome_unzip_dir):
164 print 'Cleaning', chrome_unzip_dir 153 print 'Cleaning', chrome_unzip_dir
165 pyauto_utils.RemovePath(chrome_unzip_dir) 154 pyauto_utils.RemovePath(chrome_unzip_dir)
166 print 'Unzipping' 155 print 'Unzipping'
167 pyauto_utils.UnzipFilenameToDir(chrome_zip, self._outdir) 156 try:
157 pyauto_utils.UnzipFilenameToDir(chrome_zip, self._outdir)
158 except (zipfile.BadZipfile, OSError), err:
159 print 'FetchPrebuilt.Run: %s' % err
kkania 2012/05/25 00:50:20 instead of printing and returing -1, just throw Ru
160 return -1
168 if get_remoting: 161 if get_remoting:
169 pyauto_utils.UnzipFilenameToDir(remoting_zip, self._outdir) 162 pyauto_utils.UnzipFilenameToDir(remoting_zip, self._outdir)
170 shutil.move(self._outdir + '/remoting-webapp', 163 # Added this try/catch block to prevent a run-time error, which occurs
171 self._outdir + '/remoting/remoting.webapp') 164 # if remoting-webapp directory already exists at the destination.
165 try:
166 shutil.move(self._outdir + '/remoting-webapp',
167 self._outdir + '/remoting/remoting.webapp')
168 except shutil.Error, err:
169 print 'FetchPrebuilt.Run: %s' % err
kkania 2012/05/25 00:50:20 How about just delete the file if it already exist
170 return -1
172 171
173 # Copy over the binaries to outdir 172 # Copy over the binaries to destination directory.
174 items_to_copy = { 173 items_to_copy = {
175 pyautolib_py: os.path.join(self._outdir, 'pyautolib.py'), 174 pyautolib_py: os.path.join(self._outdir, 'pyautolib.py'),
176 pyautolib_so: os.path.join(self._outdir, self._pyautolib_so_name), 175 pyautolib_so: os.path.join(self._outdir, self._pyautolib_so_name),
177 chromedriver: os.path.join(self._outdir, self._chromedriver_name) 176 chromedriver: os.path.join(self._outdir, self._chromedriver_name)
178 } 177 }
179 unzip_dir_contents = glob.glob(os.path.join(chrome_unzip_dir, '*')) 178 unzip_dir_contents = glob.glob(os.path.join(chrome_unzip_dir, '*'))
180 for item in unzip_dir_contents: 179 for item in unzip_dir_contents:
181 name = os.path.basename(item) 180 name = os.path.basename(item)
182 items_to_copy[item] = os.path.join(self._outdir, name) 181 items_to_copy[item] = os.path.join(self._outdir, name)
183 182
184 for src, dest in items_to_copy.iteritems(): 183 for src, dest in items_to_copy.iteritems():
185 pyauto_utils.RemovePath(dest) 184 pyauto_utils.RemovePath(dest)
186 print '%s ==> %s' % (os.path.basename(src), dest) 185 print '%s ==> %s' % (os.path.basename(src), dest)
187 shutil.move(src, dest) 186 shutil.move(src, dest)
188 pyauto_utils.RemovePath(chrome_unzip_dir) 187 pyauto_utils.RemovePath(chrome_unzip_dir)
189 188
190 # Final setup (if any) 189 # Final setup (if any)
191 # Set executable bit on chromedriver binary. 190 # Set executable bit on chromedriver binary.
192 if not self._options.platform == 'win': 191 if not self._platform == 'win':
193 os.chmod(items_to_copy[chromedriver], 0700) 192 os.chmod(items_to_copy[chromedriver], 0700)
194 193
195 # Create symlink to .framework on Mac 194 # Create symlink to .framework on Mac
196 if self._options.platform == 'mac': 195 if self._platform == 'mac':
197 mac_app_name = os.path.basename([x for x in unzip_dir_contents 196 mac_app_name = os.path.basename([x for x in unzip_dir_contents
198 if x.endswith('.app')][0]) 197 if x.endswith('.app')][0])
199 os.chdir(self._outdir) 198 os.chdir(self._outdir)
200 framework = glob.glob(os.path.join( 199 framework = glob.glob(os.path.join(
201 mac_app_name, 'Contents', 'Versions', '*', '*.framework'))[0] 200 mac_app_name, 'Contents', 'Versions', '*', '*.framework'))[0]
202 print framework 201 print framework
203 dest = os.path.basename(framework) 202 dest = os.path.basename(framework)
204 os.path.lexists(dest) and os.remove(dest) 203 os.path.lexists(dest) and os.remove(dest)
205 print 'Creating symlink "%s"' % dest 204 print 'Creating symlink "%s"' % dest
206 os.symlink(framework, dest) 205 os.symlink(framework, dest)
207 206
208 print 'Prepared binaries in "%s"' % self._outdir 207 print 'Prepared binaries in "%s"' % self._outdir
209 return 0 208 return 0
210 209
211 210
212 if __name__ == '__main__': 211 class Main:
213 sys.exit(FetchPrebuilt().Run()) 212
213 def __init__(self):
214 self._ParseArgs()
215 FetchPrebuilt(self._url, self._outdir, self._platform).Run()
216
217 def _ParseArgs(self):
218 parser = optparse.OptionParser()
219 parser.add_option(
220 '-d', '--outdir', type='string', default=None,
221 help='Directory in which to setup. This is typically the directory '
222 'where the binaries would go when compiled from source.')
223 parser.add_option(
224 '-p', '--platform', type='string',
225 default=pyauto_utils.GetCurrentPlatform(),
226 help='Platform. Valid options: win, mac, linux32, linux64. '
227 'Default: current platform (%s)' % pyauto_utils.GetCurrentPlatform())
228 parser.add_option(
229 '-l', '--latest', action='store_true', default=False,
230 help='Download the latest chromium build from commondatastorage. '
231 '[default=False]')
232 options, args = parser.parse_args()
233 if not options.outdir:
234 print >>sys.stderr, 'Need output directory: -d/--outdir'
235 sys.exit(1)
236 if options.latest:
237 self._url = FetchPrebuilt.GetLastestDownloadURL(options.platform)
238 elif not args:
239 print >>sys.stderr, 'Need download url'
240 sys.exit(2)
241 else:
242 self._url = args[0]
243
244 self._platform = options.platform
245 self._outdir = options.outdir
246 # Chromium continuous build archive has a non-standard format.
247 if 'index.html?path=' in self._url:
248 self._url = self._url.replace('index.html?path=', '')
249 self._url = self._url.rstrip('/')
250
251
252 if __name__ == '__main__':
253 Main()
OLDNEW
« no previous file with comments | « install_test/py_unittest_util.py ('k') | pyautolib/pyauto.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698