public
Created

An google-appengine loading module to aid writing scripts for GAE

  • Download Gist
ae.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
######################################################
# allow scripts to use appengine apis
#
# assumes you store your data in a "data" directory
# in your app directory. See connect_local_datastore()
# and assumes that you have a HRD app id with "~"
######################################################
#
# locate app-engine SDK:
AE_PATH = "/your/path/to/sdk/google_appengine/"
#
# Your username:
AE_USER = 'your@email.com'
######################################################
 
import getpass
import sys
import os
 
# fix timezone
import time
os.environ['TZ'] = time.tzname[1]
import datetime
 
# load the AE paths (as stolen from dev_appserver.py)
APP_PATH = os.path.abspath( os.path.join(os.path.dirname(__file__), '..') )
EXTRA_PATHS = [
APP_PATH,
AE_PATH,
os.path.join(AE_PATH, 'lib', 'antlr3'),
os.path.join(AE_PATH, 'lib', 'django'),
os.path.join(AE_PATH, 'lib', 'django_1_2'),
os.path.join(AE_PATH, 'lib', 'ipaddr'),
os.path.join(AE_PATH, 'lib', 'protorpc'),
os.path.join(AE_PATH, 'lib', 'webob'),
os.path.join(AE_PATH, 'lib', 'yaml', 'lib'),
os.path.join(AE_PATH, 'lib', 'fancy_urllib'), #http://code.google.com/p/google-app-engine-django/issues/detail?id=178
os.path.join(AE_PATH, 'lib', 'graphy')
]
sys.path = EXTRA_PATHS + sys.path
 
# shhhhhhh
import warnings
warnings.simplefilter('ignore', DeprecationWarning)
 
# choose django
from google.appengine.dist import use_library
use_library('django', '1.2')
 
# work out what our app-id is
cmd = "(cd %s && grep 'application:' app.yaml | cut -c 13-)" % APP_PATH
APP_ID = os.popen(cmd).read().strip()
if not APP_ID:
raise Exception("Could not work out app id")
os.environ['APPLICATION_ID'] = APP_ID
 
import logging
 
def connect_remote_datastore():
"""Run this from scripts to get access to the PRODUCTION devserver"""
import getpass
import os
import threading
from google.appengine.ext.remote_api import remote_api_stub
from google.appengine.ext import db
from google.appengine.ext import blobstore
from google.appengine.ext.remote_api import throttle
passw = None
def auth_func():
passw = os.environ.get('GAE_PASS')
if not passw:
passw = getpass.getpass("password:")
os.environ['GAE_PASS'] = passw
return AE_USER,passw
host = '%s.appspot.com' % APP_ID
ns = os.environ.get('GAE_NAMESPACE','')
remote_api_stub.ConfigureRemoteDatastore(
"s~%s" % APP_ID,
'/remote_api',
auth_func,
host,
save_cookies=True,
secure=True)
# full_throttle = throttle.DefaultThrottle(multiplier=10.0)
# throttle.ThrottleRemoteDatastore(full_throttle)
# full_throttle.Register(threading.currentThread())
logging.info("connected to %s datastore using %s namespace" % (host,ns))
# set ns
from google.appengine.api import namespace_manager
namespace_manager.set_namespace( ns )
def connect_local_datastore():
"""Run this from scripts to get access to the local devserver"""
from google.appengine.tools import dev_appserver
from google.appengine.tools.dev_appserver_main import ParseArguments
argv = [
sys.argv[0],
'--datastore_path', os.path.join(APP_PATH,'data','datastore.db'),
'--rdbms_sqlite_path', os.path.join(APP_PATH,'data','datastore.sqlite'),
'--blobstore_path', os.path.join(APP_PATH,'data','blobstore'),
'--history_path', os.path.join(APP_PATH,'data','history'),
'--use_sqlite',
'--require_indexes',
'--skip_sdk_update_check'
]
args,opts = ParseArguments(argv) # TODO allow opts
dev_appserver.SetupStubs(os.environ['APPLICATION_ID'], **opts)
# set ns
from google.appengine.api import namespace_manager
ns = os.environ.get('GAE_NAMESPACE','')
logging.info("connected to local datastore using %s namespace" % ns)
namespace_manager.set_namespace( ns )

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.