scripts/interactive.py
author Sverre Rabbelier <srabbelier@gmail.com>
Sat, 28 Mar 2009 02:21:48 +0000
changeset 2024 43e5b515b71d
child 2039 07f46319d24f
permissions -rwxr-xr-x
Add a script to start an interactive shell with remote api Patch by: Sverre Rabbelier
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2024
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     2
#
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     3
# Copyright 2009 the Melange authors.
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     4
#
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     8
#
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    10
#
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    15
# limitations under the License.
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    16
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    17
"""This module supplies an interactive shell with remote api configured.
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    18
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    19
Usage is simple:
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    20
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    21
App Engine interactive console for srabbelier-melange
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    22
>>> from soc.models.user import User
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    23
>>> gen = lambda: User.all()
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    24
>>> it = deepFetch(gen)
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    25
>>> result = [i for i in it]
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    26
"""
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    27
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    28
__authors__ = [
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    29
  '"Sverre Rabbelier" <sverre@rabbelier.nl>',
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    30
]
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    31
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    32
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    33
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    34
import code
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    35
import getpass
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    36
import os
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    37
import sys
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    38
import sys
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    39
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    40
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    41
def auth_func():
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    42
  """Returns a tuple with username and password.
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    43
  """
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    44
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    45
  return raw_input('Username:'), getpass.getpass('Password:')
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    46
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    47
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    48
def deepFetch(queryGen,key=None,batchSize = 100):
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    49
  """Iterator that yields an entity in batches.
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    50
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    51
  Args:
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    52
    queryGen: should return a Query object
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    53
    key: used to .filter() for __key__
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    54
    batchSize: how many entities to retrieve in one datastore call
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    55
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    56
  Retrieved from http://tinyurl.com/d887ll (AppEngine cookbook).
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    57
  """
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    58
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    59
   # AppEngine will not fetch more than 1000 results
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    60
  batchSize = min(batchSize,1000)
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    61
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    62
  query = None
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    63
  done = False
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    64
  count = 0
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    65
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    66
  if key:
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    67
    key = db.Key(key)
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    68
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    69
  while not done:
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    70
    query = queryGen()
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    71
    if key:
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    72
      query.filter("__key__ > ",key)
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    73
    results = query.fetch(batchSize)
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    74
    for result in results:
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    75
      count += 1
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    76
      yield result
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    77
    if batchSize > len(results):
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    78
      done = True
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    79
    else:
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    80
      key = results[-1].key()
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    81
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    82
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    83
def remote(args):
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    84
  """Starts a shell with the datastore as remote_api_stub.
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    85
  """
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    86
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    87
  from google.appengine.ext import db
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    88
  from google.appengine.ext.remote_api import remote_api_stub
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    89
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    90
  app_id = args[0]
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    91
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    92
  if len(args) > 1:
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    93
    host = args[1]
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    94
  else:
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    95
    host = '%s.appspot.com' % app_id
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    96
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    97
  remote_api_stub.ConfigureRemoteDatastore(app_id, '/remote_api', auth_func, host)
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    98
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    99
  context = {
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   100
      'deepFetch': deepFetch,
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   101
  }
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   102
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   103
  code.interact('App Engine interactive console for %s' % (app_id,), None, context)
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   104
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   105
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   106
def main(args):
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   107
  """Sets up the sys.path and environment for development.
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   108
  """
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   109
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   110
  here = os.path.abspath(__file__)
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   111
  here = os.path.join(os.path.dirname(here), '..')
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   112
  here = os.path.normpath(here)
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   113
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   114
  appengine_location = os.path.join(here, 'thirdparty', 'google_appengine')
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   115
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   116
  extra_paths = [here,
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   117
                 os.path.join(appengine_location, 'lib', 'django'),
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   118
                 os.path.join(appengine_location, 'lib', 'webob'),
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   119
                 os.path.join(appengine_location, 'lib', 'yaml', 'lib'),
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   120
                 appengine_location,
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   121
                 os.path.join(here, 'app'),
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   122
                ]
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   123
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   124
  sys.path = extra_paths + sys.path
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   125
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   126
  os.environ['SERVER_SOFTWARE'] = 'Development Interactive Shell'
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   127
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   128
  import main as app_main
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   129
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   130
  remote(args)
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   131
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   132
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   133
if __name__ == '__main__':
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   134
  if len(sys.argv) < 2:
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   135
    print "Usage: %s app_id [host]" % (sys.argv[0],)
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   136
    sys.exit(1)
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   137
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   138
  main(sys.argv[1:])
43e5b515b71d Add a script to start an interactive shell with remote api
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   139