| 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 urllib |  | 
| 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 = urllib.urlopen(url).read() |  | 
| 35   regexp = ( |  | 
| 36       r'(http\:\/\/googleappengine\.googlecode.com\/files\/' |  | 
| 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     with tempfile.NamedTemporaryFile() as f: |  | 
| 95       urllib.urlretrieve(url, f.name) |  | 
| 96       # Assuming we're extracting there. In fact, we have no idea. |  | 
| 97       print('Extracting into %s' % gae_path) |  | 
| 98       z = zipfile.ZipFile(f.name, 'r') |  | 
| 99       try: |  | 
| 100         extract_zip(z, root_path) |  | 
| 101       finally: |  | 
| 102         z.close() |  | 
| 103   return 0 |  | 
| 104 |  | 
| 105 |  | 
| 106 def main(): |  | 
| 107   parser = optparse.OptionParser() |  | 
| 108   parser.add_option('-v', '--verbose', action='store_true') |  | 
| 109   parser.add_option( |  | 
| 110       '-g', '--go', action='store_true', help='Defaults to python SDK') |  | 
| 111   parser.add_option( |  | 
| 112       '-d', '--dest', default=os.path.dirname(BASE_DIR), help='Output') |  | 
| 113   parser.add_option('--dry-run', action='store_true', help='Do not download') |  | 
| 114   options, args = parser.parse_args() |  | 
| 115   if args: |  | 
| 116     parser.error('Unsupported args: %s' % ' '.join(args)) |  | 
| 117   logging.basicConfig(level=logging.DEBUG if options.verbose else logging.ERROR) |  | 
| 118   return install_latest_gae_sdk( |  | 
| 119       os.path.abspath(options.dest), options.go, options.dry_run) |  | 
| 120 |  | 
| 121 |  | 
| 122 if __name__ == '__main__': |  | 
| 123   sys.exit(main()) |  | 
| OLD | NEW | 
|---|