OLD | NEW |
---|---|
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 | 2 |
3 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 3 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
6 | 6 |
7 """Fetch prebuilt binaries to run PyAuto. | 7 """Fetch prebuilt binaries to run PyAuto. |
8 | 8 |
9 Sets up Chrome and PyAuto binaries using prebuilt binaries from the | 9 Sets up Chrome and PyAuto binaries using prebuilt binaries from the |
10 continuous build archives. Works on mac, win, linux (32 & 64 bit). | 10 continuous build archives. Works on mac, win, linux (32 & 64 bit). |
11 | 11 |
12 Examples: | 12 Examples: |
13 On Mac: | 13 On Mac: |
14 $ python fetch_prebuilt_pyauto.py -d xcodebuild/Release | 14 $ python fetch_prebuilt_pyauto.py -d xcodebuild/Release |
15 http://build.chromium.org/f/chromium/continuous/mac/LATEST | 15 http://build.chromium.org/f/chromium/continuous/mac/LATEST |
16 | 16 |
17 On Win: | 17 On Win: |
18 $ python fetch_prebuilt_pyauto.py -d chrome\Release | 18 $ python fetch_prebuilt_pyauto.py -d chrome\Release |
19 http://build.chromium.org/f/chromium/continuous/win/LATEST | 19 http://build.chromium.org/f/chromium/continuous/win/LATEST |
20 """ | 20 """ |
21 | 21 |
22 import glob | 22 import glob |
23 import httplib | |
23 import optparse | 24 import optparse |
24 import os | 25 import os |
25 import shutil | 26 import shutil |
26 import sys | 27 import sys |
27 import urllib | 28 import urllib |
29 import urlparse | |
28 | 30 |
29 import pyauto_utils | 31 import pyauto_utils |
30 | 32 |
31 | 33 |
32 class FetchPrebuilt(object): | 34 class FetchPrebuilt(object): |
33 """Util class to fetch prebuilt binaries to run PyAuto.""" | 35 """Util class to fetch prebuilt binaries to run PyAuto.""" |
34 | 36 |
35 def _ParseArgs(self): | 37 def _ParseArgs(self): |
36 parser = optparse.OptionParser() | 38 parser = optparse.OptionParser() |
37 parser.add_option( | 39 parser.add_option( |
38 '-d', '--outdir', type='string', default=None, | 40 '-d', '--outdir', type='string', default=None, |
39 help='Directory in which to setup. This is typically the directory ' | 41 help='Directory in which to setup. This is typically the directory ' |
40 'where the binaries would go when compiled from source.') | 42 'where the binaries would go when compiled from source.') |
41 parser.add_option( | 43 parser.add_option( |
42 '-p', '--platform', type='string', | 44 '-p', '--platform', type='string', |
43 default=pyauto_utils.GetCurrentPlatform(), | 45 default=pyauto_utils.GetCurrentPlatform(), |
44 help='Platform. Valid options: win, mac, linux32, linux64. ' | 46 help='Platform. Valid options: win, mac, linux32, linux64. ' |
45 'Default: current platform (%s)' % | 47 'Default: current platform (%s)' % |
46 pyauto_utils.GetCurrentPlatform()) | 48 pyauto_utils.GetCurrentPlatform()) |
47 self._options, self._args = parser.parse_args() | 49 self._options, self._args = parser.parse_args() |
48 if not self._options.outdir: | 50 if not self._options.outdir: |
49 print >>sys.stderr, 'Need output directory: -d/--outdir' | 51 print >>sys.stderr, 'Need output directory: -d/--outdir' |
50 sys.exit(1) | 52 sys.exit(1) |
51 if not self._args: | 53 if not self._args: |
52 print >>sys.stderr, 'Need download url' | 54 print >>sys.stderr, 'Need download url' |
53 sys.exit(2) | 55 sys.exit(2) |
54 self._outdir = self._options.outdir | 56 self._outdir = self._options.outdir |
55 self._url = self._args[0] | 57 self._url = self._args[0] |
56 | 58 |
57 # Setup urls to download | 59 # Determine name of zip. |
58 self._chrome_zip_name = 'chrome-%s' % { 'linux64': 'linux64bit', | 60 if not self._options.platform.startswith('linux'): |
59 'linux32': 'linux32bit', | 61 self._chrome_zip_name = 'chrome-%s' % { 'mac': 'mac', |
60 'mac': 'mac', | 62 'win': 'win32' |
61 'win': 'win32' | 63 }[self._options.platform] |
62 }[self._options.platform] | 64 else: |
65 linux_32_names = ['linux', 'lucid32bit'] | |
66 linux_64_names = ['linux64', 'lucid64bit'] | |
67 linux_names = { 'linux': linux_32_names + linux_64_names, | |
68 'linux32': linux_32_names, | |
69 'linux64': linux_64_names | |
70 }[self._options.platform] | |
71 for name in linux_names: | |
72 zip_name = 'chrome-' + name | |
73 if self._DoesURLExist('%s/%s.zip' % (self._url, zip_name)): | |
74 self._chrome_zip_name = zip_name | |
75 break | |
76 else: | |
77 assert False, 'Could not find chrome zip at ' + self._url | |
Nirnimesh
2011/08/26 23:55:27
raise RuntimeError('Could not ...')
kkania
2011/08/30 13:30:54
Done. What's the guideline for whether to use ass
Nirnimesh
2011/08/30 16:38:34
You assert on a conditional. Here it's False, so w
| |
78 | |
79 # Setup urls to download. | |
63 self._chrome_zip_url = '%s/%s.zip' % (self._url, self._chrome_zip_name) | 80 self._chrome_zip_url = '%s/%s.zip' % (self._url, self._chrome_zip_name) |
64 chrome_test_url = '%s/%s.test' % (self._url, self._chrome_zip_name) | 81 chrome_test_url = '%s/%s.test' % (self._url, self._chrome_zip_name) |
65 self._pyautolib_py_url = '%s/pyautolib.py' % chrome_test_url | 82 self._pyautolib_py_url = '%s/pyautolib.py' % chrome_test_url |
66 self._pyautolib_so_url = '%s/%s' % (chrome_test_url, | 83 if self._options.platform == 'win': |
67 { 'linux64': '_pyautolib.so', | 84 self._pyautolib_so_name = '_pyautolib.pyd' |
68 'linux32': '_pyautolib.so', | 85 self._chromedriver_name = 'chromedriver.exe' |
69 'mac': '_pyautolib.so', | 86 else: |
70 'win': '_pyautolib.pyd', | 87 self._pyautolib_so_name = '_pyautolib.so' |
71 }[self._options.platform]) | 88 self._chromedriver_name = 'chromedriver' |
89 self._pyautolib_so_url = chrome_test_url + '/' + self._pyautolib_so_name | |
90 self._chromedriver_url = chrome_test_url + '/' + self._chromedriver_name | |
91 | |
92 def _DoesURLExist(self, url): | |
93 """Returns whether a resource exists at the given URL.""" | |
Nirnimesh
2011/08/26 23:55:27
s/Returns/Determines/
kkania
2011/08/30 13:30:54
Done.
| |
94 parsed = urlparse.urlparse(url) | |
95 conn = httplib.HTTPConnection(parsed.netloc) | |
96 conn.request('HEAD', parsed.path) | |
97 return conn.getresponse().status == 200 | |
72 | 98 |
73 def Cleanup(self): | 99 def Cleanup(self): |
74 """Remove old binaries, if any.""" | 100 """Remove old binaries, if any.""" |
75 pass | 101 pass |
76 | 102 |
77 def Run(self): | 103 def Run(self): |
78 self._ParseArgs() | 104 self._ParseArgs() |
79 if not os.path.isdir(self._outdir): | 105 if not os.path.isdir(self._outdir): |
80 os.makedirs(self._outdir) | 106 os.makedirs(self._outdir) |
81 | 107 |
82 # Fetch chrome & pyauto binaries | 108 # Fetch chrome & pyauto binaries |
83 print 'Fetching' | 109 print 'Fetching' |
84 print self._chrome_zip_url | 110 print self._chrome_zip_url |
85 print self._pyautolib_py_url | 111 print self._pyautolib_py_url |
86 print self._pyautolib_so_url | 112 print self._pyautolib_so_url |
113 print self._chromedriver_url | |
87 chrome_zip = urllib.urlretrieve(self._chrome_zip_url)[0] | 114 chrome_zip = urllib.urlretrieve(self._chrome_zip_url)[0] |
88 pyautolib_py = urllib.urlretrieve(self._pyautolib_py_url)[0] | 115 pyautolib_py = urllib.urlretrieve(self._pyautolib_py_url)[0] |
89 pyautolib_so = urllib.urlretrieve(self._pyautolib_so_url)[0] | 116 pyautolib_so = urllib.urlretrieve(self._pyautolib_so_url)[0] |
117 chromedriver = urllib.urlretrieve(self._chromedriver_url)[0] | |
90 chrome_unzip_dir = os.path.join(self._outdir, self._chrome_zip_name) | 118 chrome_unzip_dir = os.path.join(self._outdir, self._chrome_zip_name) |
91 if os.path.exists(chrome_unzip_dir): | 119 if os.path.exists(chrome_unzip_dir): |
92 print 'Cleaning', chrome_unzip_dir | 120 print 'Cleaning', chrome_unzip_dir |
93 pyauto_utils.RemovePath(chrome_unzip_dir) | 121 pyauto_utils.RemovePath(chrome_unzip_dir) |
94 pyauto_utils.UnzipFilenameToDir(chrome_zip, self._outdir) | 122 pyauto_utils.UnzipFilenameToDir(chrome_zip, self._outdir) |
95 | 123 |
96 # Copy over the binaries to outdir | 124 # Copy over the binaries to outdir |
97 items_to_copy = { | 125 items_to_copy = { |
98 pyautolib_py: os.path.join(self._outdir, 'pyautolib.py'), | 126 pyautolib_py: os.path.join(self._outdir, 'pyautolib.py'), |
99 pyautolib_so: os.path.join(self._outdir, | 127 pyautolib_so: os.path.join(self._outdir, self._pyautolib_so_name), |
100 { 'linux64': '_pyautolib.so', | 128 chromedriver: os.path.join(self._outdir, self._chromedriver_name) |
101 'linux32': '_pyautolib.so', | |
102 'mac': '_pyautolib.so', | |
103 'win': '_pyautolib.pyd' | |
104 }[self._options.platform]) | |
105 } | 129 } |
106 unzip_dir_contents = glob.glob(os.path.join(chrome_unzip_dir, '*')) | 130 unzip_dir_contents = glob.glob(os.path.join(chrome_unzip_dir, '*')) |
107 for item in unzip_dir_contents: | 131 for item in unzip_dir_contents: |
108 name = os.path.basename(item) | 132 name = os.path.basename(item) |
109 items_to_copy[item] = os.path.join(self._outdir, name) | 133 items_to_copy[item] = os.path.join(self._outdir, name) |
110 | 134 |
111 for src, dest in items_to_copy.iteritems(): | 135 for src, dest in items_to_copy.iteritems(): |
112 pyauto_utils.RemovePath(dest) | 136 pyauto_utils.RemovePath(dest) |
113 print '%s ==> %s' % (os.path.basename(src), dest) | 137 print '%s ==> %s' % (os.path.basename(src), dest) |
114 shutil.move(src, dest) | 138 shutil.move(src, dest) |
115 pyauto_utils.RemovePath(chrome_unzip_dir) | 139 pyauto_utils.RemovePath(chrome_unzip_dir) |
116 | 140 |
117 # Final setup (if any) | 141 # Final setup (if any) |
118 # Create symlink to .framework on Mac | 142 # Create symlink to .framework on Mac |
119 if self._options.platform == 'mac': | 143 if self._options.platform == 'mac': |
120 mac_app_name = os.path.basename([x for x in unzip_dir_contents | 144 mac_app_name = os.path.basename([x for x in unzip_dir_contents |
121 if x.endswith('.app')][0]) | 145 if x.endswith('.app')][0]) |
122 os.chdir(self._outdir) | 146 os.chdir(self._outdir) |
123 framework = glob.glob(os.path.join( | 147 framework = glob.glob(os.path.join( |
124 mac_app_name, 'Contents', 'Versions', '*', '*.framework'))[0] | 148 mac_app_name, 'Contents', 'Versions', '*', '*.framework'))[0] |
125 print framework | 149 print framework |
126 dest = os.path.basename(framework) | 150 dest = os.path.basename(framework) |
127 os.path.lexists(dest) and os.remove(dest) | 151 os.path.lexists(dest) and os.remove(dest) |
128 print 'Creating symlink "%s"' % dest | 152 print 'Creating symlink "%s"' % dest |
129 os.symlink(framework, dest) | 153 os.symlink(framework, dest) |
130 | 154 |
155 # Set executable bit on chromedriver binary. | |
156 if not self._options.platform == 'win': | |
Nirnimesh
2011/08/26 23:55:27
You should do this only if it's not set already
kkania
2011/08/30 13:30:54
Done. What's the reason why we should check first?
Nirnimesh
2011/08/30 16:38:34
Sorry, I got mixed up with the chat we had about f
| |
157 os.chmod(items_to_copy[chromedriver], 0700) | |
158 | |
131 print 'Prepared binaries in "%s"' % self._outdir | 159 print 'Prepared binaries in "%s"' % self._outdir |
132 | 160 |
133 | 161 |
134 if __name__ == '__main__': | 162 if __name__ == '__main__': |
135 FetchPrebuilt().Run() | 163 FetchPrebuilt().Run() |
136 | |
OLD | NEW |