OLD | NEW |
| (Empty) |
1 #!/usr/bin/env python | |
2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
3 # Use of this source code is governed by a BSD-style license that can be | |
4 # found in the LICENSE file. | |
5 | |
6 """This script is copied from | |
7 https://chromium.googlesource.com/infra/infra.git/+/master/bootstrap | |
8 """ | |
9 | |
10 import datetime | |
11 import logging | |
12 import optparse | |
13 import os | |
14 import re | |
15 import shutil | |
16 import sys | |
17 import time | |
18 import tempfile | |
19 import urllib2 | |
20 import zipfile | |
21 | |
22 BASE_DIR = os.path.dirname(os.path.abspath(__file__)) | |
23 | |
24 | |
25 def get_gae_sdk_version(gae_path): | |
26 """Returns the installed GAE SDK version or None.""" | |
27 version_path = os.path.join(gae_path, 'VERSION') | |
28 if os.path.isfile(version_path): | |
29 values = dict( | |
30 map(lambda x: x.strip(), l.split(':')) | |
31 for l in open(version_path) if ':' in l) | |
32 if 'release' in values: | |
33 return values['release'].strip('"') | |
34 | |
35 | |
36 def get_latest_gae_sdk_url(name): | |
37 """Returns the url to get the latest GAE SDK and its version.""" | |
38 url = 'https://cloud.google.com/appengine/downloads.html' | |
39 logging.debug('%s', url) | |
40 content = urllib2.urlopen(url).read() | |
41 regexp = ( | |
42 r'(https\:\/\/storage.googleapis.com\/appengine-sdks\/featured\/' | |
43 + re.escape(name) + r'[0-9\.]+?\.zip)') | |
44 m = re.search(regexp, content) | |
45 url = m.group(1) | |
46 # Calculate the version from the url. | |
47 new_version = re.search(re.escape(name) + r'(.+?).zip', url).group(1) | |
48 # Upgrade to https | |
49 return url.replace('http://', 'https://'), new_version | |
50 | |
51 | |
52 def extract_zip(z, root_path): | |
53 """Extracts files in a zipfile but keep the executable bits.""" | |
54 count = 0 | |
55 for f in z.infolist(): | |
56 perm = (f.external_attr >> 16L) & 0777 | |
57 mtime = time.mktime(datetime.datetime(*f.date_time).timetuple()) | |
58 filepath = os.path.join(root_path, f.filename) | |
59 logging.debug('Extracting %s', f.filename) | |
60 if f.filename.endswith('/'): | |
61 os.mkdir(filepath, perm) | |
62 else: | |
63 z.extract(f, root_path) | |
64 os.chmod(filepath, perm) | |
65 count += 1 | |
66 os.utime(filepath, (mtime, mtime)) | |
67 print('Extracted %d files' % count) | |
68 | |
69 | |
70 def install_latest_gae_sdk(root_path, fetch_go, dry_run): | |
71 if fetch_go: | |
72 rootdir = 'go_appengine' | |
73 if sys.platform == 'darwin': | |
74 name = 'go_appengine_sdk_darwin_amd64-' | |
75 else: | |
76 # Add other platforms as needed. | |
77 name = 'go_appengine_sdk_linux_amd64-' | |
78 else: | |
79 rootdir = 'google_appengine' | |
80 name = 'google_appengine_' | |
81 | |
82 # The zip file already contains 'google_appengine' (for python) or | |
83 # 'go_appengine' (for go) in its path so it's a bit | |
84 # awkward to unzip otherwise. Hard code the path in for now. | |
85 gae_path = os.path.join(root_path, rootdir) | |
86 print('Looking up path %s' % gae_path) | |
87 version = get_gae_sdk_version(gae_path) | |
88 if version: | |
89 print('Found installed version %s' % version) | |
90 else: | |
91 print('Didn\'t find an SDK') | |
92 | |
93 url, new_version = get_latest_gae_sdk_url(name) | |
94 print('New version is %s' % new_version) | |
95 if version == new_version: | |
96 return 0 | |
97 | |
98 if os.path.isdir(gae_path): | |
99 print('Removing previous version') | |
100 if not dry_run: | |
101 shutil.rmtree(gae_path) | |
102 | |
103 print('Fetching %s' % url) | |
104 if not dry_run: | |
105 u = urllib2.urlopen(url) | |
106 with tempfile.NamedTemporaryFile() as f: | |
107 while True: | |
108 chunk = u.read(2 ** 20) | |
109 if not chunk: | |
110 break | |
111 f.write(chunk) | |
112 # Assuming we're extracting there. In fact, we have no idea. | |
113 print('Extracting into %s' % gae_path) | |
114 z = zipfile.ZipFile(f, 'r') | |
115 try: | |
116 extract_zip(z, root_path) | |
117 finally: | |
118 z.close() | |
119 return 0 | |
120 | |
121 | |
122 def main(): | |
123 parser = optparse.OptionParser(prog='python -m %s' % __package__) | |
124 parser.add_option('-v', '--verbose', action='store_true') | |
125 parser.add_option( | |
126 '-g', '--go', action='store_true', help='Defaults to python SDK') | |
127 parser.add_option( | |
128 '-d', '--dest', default=os.path.dirname(BASE_DIR), help='Output') | |
129 parser.add_option('--dry-run', action='store_true', help='Do not download') | |
130 options, args = parser.parse_args() | |
131 if args: | |
132 parser.error('Unsupported args: %s' % ' '.join(args)) | |
133 logging.basicConfig(level=logging.DEBUG if options.verbose else logging.ERROR) | |
134 return install_latest_gae_sdk( | |
135 os.path.abspath(options.dest), options.go, options.dry_run) | |
136 | |
137 | |
138 if __name__ == '__main__': | |
139 sys.exit(main()) | |
OLD | NEW |