thirdparty/google_appengine/google/appengine/tools/remote_api_shell.py
changeset 2864 2e0b0af889be
equal deleted inserted replaced
2862:27971a13089f 2864:2e0b0af889be
       
     1 #!/usr/bin/env python
       
     2 #
       
     3 # Copyright 2007 Google Inc.
       
     4 #
       
     5 # Licensed under the Apache License, Version 2.0 (the "License");
       
     6 # you may not use this file except in compliance with the License.
       
     7 # You may obtain a copy of the License at
       
     8 #
       
     9 #     http://www.apache.org/licenses/LICENSE-2.0
       
    10 #
       
    11 # Unless required by applicable law or agreed to in writing, software
       
    12 # distributed under the License is distributed on an "AS IS" BASIS,
       
    13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    14 # See the License for the specific language governing permissions and
       
    15 # limitations under the License.
       
    16 #
       
    17 
       
    18 """An interactive python shell that uses remote_api.
       
    19 
       
    20 Usage:
       
    21   remote_api_shell.py [-s HOSTNAME] APPID [PATH]
       
    22 """
       
    23 
       
    24 
       
    25 import atexit
       
    26 import code
       
    27 import getpass
       
    28 import optparse
       
    29 import os
       
    30 import sys
       
    31 
       
    32 try:
       
    33   import readline
       
    34 except ImportError:
       
    35   readline = None
       
    36 
       
    37 from google.appengine.ext.remote_api import remote_api_stub
       
    38 
       
    39 from google.appengine.api import datastore
       
    40 from google.appengine.api import memcache
       
    41 from google.appengine.api import urlfetch
       
    42 from google.appengine.api import users
       
    43 from google.appengine.ext import db
       
    44 from google.appengine.ext import search
       
    45 
       
    46 
       
    47 HISTORY_PATH = os.path.expanduser('~/.remote_api_shell_history')
       
    48 DEFAULT_PATH = '/remote_api'
       
    49 BANNER = """App Engine remote_api shell
       
    50 Python %s
       
    51 The db, users, urlfetch, and memcache modules are imported.""" % sys.version
       
    52 
       
    53 
       
    54 def auth_func():
       
    55   return (raw_input('Email: '), getpass.getpass('Password: '))
       
    56 
       
    57 
       
    58 def main(argv):
       
    59   parser = optparse.OptionParser()
       
    60   parser.add_option('-s', '--server', dest='server',
       
    61                     help='The hostname your app is deployed on. '
       
    62                          'Defaults to <app_id>.appspot.com.')
       
    63   (options, args) = parser.parse_args()
       
    64 
       
    65   if not args or len(args) > 2:
       
    66     print >> sys.stderr, __doc__
       
    67     if len(args) > 2:
       
    68       print >> sys.stderr, 'Unexpected arguments: %s' % args[2:]
       
    69     sys.exit(1)
       
    70 
       
    71   appid = args[0]
       
    72   if len(args) == 2:
       
    73     path = args[1]
       
    74   else:
       
    75     path = DEFAULT_PATH
       
    76 
       
    77   remote_api_stub.ConfigureRemoteApi(appid, path, auth_func,
       
    78                                      servername=options.server)
       
    79   remote_api_stub.MaybeInvokeAuthentication()
       
    80 
       
    81   os.environ['SERVER_SOFTWARE'] = 'Development (remote_api_shell)/1.0'
       
    82 
       
    83   sys.ps1 = '%s> ' % appid
       
    84   if readline is not None:
       
    85     readline.parse_and_bind('tab: complete')
       
    86     atexit.register(lambda: readline.write_history_file(HISTORY_PATH))
       
    87     if os.path.exists(HISTORY_PATH):
       
    88       readline.read_history_file(HISTORY_PATH)
       
    89 
       
    90   code.interact(banner=BANNER, local=globals())
       
    91 
       
    92 
       
    93 if __name__ == '__main__':
       
    94   main(sys.argv)