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