#!/usr/bin/env python
"""Downloads a given version of the appengine SDK and unzips to /opt/.

A version number must be passed to the script at runtime.  Derived from Peter
Hudec's gae_installer package: https://github.com/peterhudec/gae_installer
"""
# future imports
from __future__ import absolute_import
from __future__ import print_function

# stdlib imports
import os
import sys
import tempfile
import urllib2
import zipfile


def _get_download_url(version, deprecated=False):
    """Return a URL for a given SDK version.
    """
    base_url = 'https://storage.googleapis.com/appengine-sdks/{0}/google_appengine_{1}.zip'
    if deprecated:
        return base_url.format('deprecated/{0}'.format(version.replace('.', '')), version)
    else:
        return base_url.format('featured', version)


def _print_deprecation_warning(version):
    """Print a deprecation warning to the user's terminal.
    """
    print()
    print('########## WARNING!! ##########')
    print()
    print('SDK version {0} is deprecated!'.format(version))
    print('Check for the latest release and update your Dockerfile')
    print()
    print('    https://cloud.google.com/appengine/downloads ')
    print()


def download_sdk(version):
    """Downloads the GAE SDK.
    """

    try:
        response = urllib2.urlopen(_get_download_url(version))
    except urllib2.HTTPError, e:
        if e.code == 404:
            _print_deprecation_warning(version)
            response = urllib2.urlopen(_get_download_url(version, deprecated=True))
        else:
            raise

    zip_path = os.path.join(tempfile.gettempdir(), 'google_appengine_{0}.zip'.format(version))
    with open(zip_path, 'w') as f:
        f.write(response.read())

    zipfile.ZipFile(zip_path).extractall('/opt/')


if __name__ == '__main__':

    # get version number from the command line (required)
    try:
        version_number = sys.argv[1]
    except IndexError:
        print('A version number is required as the first argument to the script.')
        sys.exit(1)

    download_sdk(version_number)

@@ -1185,24 +1185,1348 @@
   print()%0A%0A%0A
+def ensure_directory(path):%0A    %22%22%22Ensure that the parent directory of %60path%60 exists%22%22%22%0A    dirname = os.path.dirname(path)%0A    if not os.path.isdir(dirname):%0A        os.makedirs(dirname)%0A%0A%0Adef unpack_zipfile(filename, extract_dir, progress_filter=lambda x, y: y):%0A    %22%22%22Unpack zip %60filename%60 to %60extract_dir%60%0A    %22%22%22%0A%0A    z = zipfile.ZipFile(filename)%0A    try:%0A        for info in z.infolist():%0A            name = info.filename%0A%0A            # don't extract absolute paths or ones with .. in them%0A            if name.startswith('/') or '..' in name:%0A                continue%0A%0A            target = os.path.join(extract_dir, *name.split('/'))%0A            target = progress_filter(name, target)%0A            if not target:%0A                continue%0A            if name.endswith('/'):%0A                # directory%0A                ensure_directory(target)%0A            else:%0A                # file%0A                ensure_directory(target)%0A                data = z.read(info.filename)%0A                f = open(target, 'wb')%0A                try:%0A                    f.write(data)%0A                finally:%0A                    f.close()%0A                    del data%0A            unix_attributes = info.external_attr %3E%3E 16%0A            if unix_attributes:%0A                os.chmod(target, unix_attributes)%0A    finally:%0A        z.close()%0A%0A%0A
 def download
@@ -2536,24 +2536,24 @@
 k(version):%0A
-
     %22%22%22Downl
@@ -3047,45 +3047,33 @@
     
-zipfile.ZipFile(zip_path).extractall(
+unpack_zipfile(zip_path, 
 '/op

use the unzip func from distribute to preserve file perms