Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 107 lines (82 sloc) 3.59 KB
#!/usr/bin/env python
# Adopted from
# https://github.com/potatolondon/djangae-scaffold/blob/master/install_deps
import os
import subprocess
import shutil
import stat
import platform
from StringIO import StringIO
from zipfile import ZipFile
from urllib import urlopen
REQUIREMENTS = ["pip", "git"]
FILES_IGNORED_FOR_DELETION = ["google_appengine"]
PROJECT_DIR = os.path.abspath(os.path.dirname(__file__))
TARGET_DIR = os.path.join(PROJECT_DIR, "lib")
REQUIREMENTS_FILE = os.path.join(PROJECT_DIR, "requirements.txt")
# we don't need to upload the SDK so it lives in the lib directory
APPENGINE_TARGET_DIR = os.path.join(TARGET_DIR, "google_appengine")
APPENGINE_SDK_VERSION = "1.9.40"
APPENGINE_SDK_FILENAME = "google_appengine_{}.zip".format(APPENGINE_SDK_VERSION)
# Google move versions from 'featured' to 'deprecated' when they bring
# out new releases
FEATURED_SDK_REPO = "https://storage.googleapis.com/appengine-sdks/featured/"
DEPRECATED_SDK_REPO = "https://storage.googleapis.com/appengine-sdks/deprecated/{}/".format(
APPENGINE_SDK_VERSION.replace('.', '')
)
def install_app_engine_sdk():
"""Download and install the Google App Engine SDK."""
if not os.path.exists(APPENGINE_TARGET_DIR):
print('Downloading the AppEngine SDK...')
# First try and get it from the 'featured' folder
sdk_file = urlopen(FEATURED_SDK_REPO + APPENGINE_SDK_FILENAME)
if sdk_file.getcode() == 404:
# Failing that, 'deprecated'
sdk_file = urlopen(DEPRECATED_SDK_REPO + APPENGINE_SDK_FILENAME)
# Handle other errors
if sdk_file.getcode() >= 299:
raise Exception(
'App Engine SDK could not be found. {} returned code {}.'.format(
sdk_file.geturl(), sdk_file.getcode())
)
zipfile = ZipFile(StringIO(sdk_file.read()))
zipfile.extractall(TARGET_DIR)
# Make sure the dev_appserver and appcfg are executable
for module in ("dev_appserver.py", "appcfg.py"):
app = os.path.join(APPENGINE_TARGET_DIR, module)
st = os.stat(app)
os.chmod(app, st.st_mode | stat.S_IEXEC)
else:
print(
'Not updating SDK as it exists. Remove {} and re-run to get the '
'latest SDK'.format(APPENGINE_TARGET_DIR)
)
def install_requirements():
"""Install requirements into their respective directories."""
pip_msg = "Running pip to install {filename} dependencies into {target}..."
pip_targets = [
(REQUIREMENTS_FILE, TARGET_DIR),
]
for requirements, target in pip_targets:
if os.path.exists(target):
# remove all folders and files, leaving symlinks which point to submodules
filenames = (
filename for filename in os.listdir(target) if
filename not in FILES_IGNORED_FOR_DELETION
)
for filename in filenames:
path = os.path.join(target, filename)
if os.path.isdir(path) and not os.path.islink(path):
shutil.rmtree(path)
elif os.path.isfile(path):
os.remove(path)
# install all the required third party libs into the target directory
print(pip_msg.format(filename=os.path.basename(requirements), target=target))
args = ["pip", "install", "-r", requirements, "-t", target, "-I"]
p = subprocess.Popen(args)
p.wait()
if __name__ == "__main__":
# install the app engine SDK if needed
install_app_engine_sdk()
# install all the python requirements into sitepackages
install_requirements()