Add google appengine to repo
[frozenviper.git] / google_appengine / google / appengine / tools / remote_api_shell.py
blob0984d16350f295fb925aae17b4da325422882da9
1 #!/usr/bin/env python
3 # Copyright 2007 Google Inc.
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
18 """An interactive python shell that uses remote_api.
20 Usage:
21 remote_api_shell.py [-s HOSTNAME] APPID [PATH]
22 """
25 from google.appengine.tools import os_compat
27 import atexit
28 import code
29 import getpass
30 import optparse
31 import os
32 import sys
34 try:
35 import readline
36 except ImportError:
37 readline = None
39 from google.appengine.ext.remote_api import remote_api_stub
41 from google.appengine.api import datastore
42 from google.appengine.api import memcache
43 from google.appengine.api import urlfetch
44 from google.appengine.api import users
45 from google.appengine.ext import db
46 from google.appengine.ext import search
49 HISTORY_PATH = os.path.expanduser('~/.remote_api_shell_history')
50 DEFAULT_PATH = '/remote_api'
51 BANNER = """App Engine remote_api shell
52 Python %s
53 The db, users, urlfetch, and memcache modules are imported.""" % sys.version
56 def auth_func():
57 return (raw_input('Email: '), getpass.getpass('Password: '))
60 def main(argv):
61 parser = optparse.OptionParser()
62 parser.add_option('-s', '--server', dest='server',
63 help='The hostname your app is deployed on. '
64 'Defaults to <app_id>.appspot.com.')
65 parser.add_option('--secure', dest='secure', action="store_true",
66 default=False, help='Use HTTPS when communicating '
67 'with the server.')
68 (options, args) = parser.parse_args()
70 if not args or len(args) > 2:
71 print >> sys.stderr, __doc__
72 if len(args) > 2:
73 print >> sys.stderr, 'Unexpected arguments: %s' % args[2:]
74 sys.exit(1)
76 appid = args[0]
77 if len(args) == 2:
78 path = args[1]
79 else:
80 path = DEFAULT_PATH
82 remote_api_stub.ConfigureRemoteApi(appid, path, auth_func,
83 servername=options.server,
84 save_cookies=True, secure=options.secure)
85 remote_api_stub.MaybeInvokeAuthentication()
87 os.environ['SERVER_SOFTWARE'] = 'Development (remote_api_shell)/1.0'
89 sys.ps1 = '%s> ' % appid
90 if readline is not None:
91 readline.parse_and_bind('tab: complete')
92 atexit.register(lambda: readline.write_history_file(HISTORY_PATH))
93 if os.path.exists(HISTORY_PATH):
94 readline.read_history_file(HISTORY_PATH)
96 code.interact(banner=BANNER, local=globals())
99 if __name__ == '__main__':
100 main(sys.argv)