App Engine Python SDK version 1.8.9
[gae.git] / python / google / appengine / tools / remote_api_shell.py
blobf6ea7f8f34b69dc9fb765664daa35d60d233cc8b
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.
21 """An interactive python shell that uses remote_api.
23 Usage:
24 %prog [-s HOSTNAME] [-p PATH] [APPID]
26 If the -s HOSTNAME flag is not specified, the APPID must be specified.
27 """
32 from google.appengine.tools import os_compat
34 import atexit
35 import code
36 import getpass
37 import optparse
38 import os
39 import sys
41 try:
42 import readline
43 except ImportError:
44 readline = None
46 from google.appengine.ext.remote_api import remote_api_stub
47 from google.appengine.tools import appengine_rpc
51 from google.appengine.api import memcache
52 from google.appengine.api import urlfetch
53 from google.appengine.api import users
54 from google.appengine.ext import db
55 from google.appengine.ext import ndb
58 HISTORY_PATH = os.path.expanduser('~/.remote_api_shell_history')
59 DEFAULT_PATH = '/_ah/remote_api'
60 BANNER = """App Engine remote_api shell
61 Python %s
62 The db, ndb, users, urlfetch, and memcache modules are imported.\
63 """ % sys.version
66 def auth_func():
67 return (raw_input('Email: '), getpass.getpass('Password: '))
70 def remote_api_shell(servername, appid, path, secure, rpc_server_factory):
71 """Actually run the remote_api_shell."""
74 remote_api_stub.ConfigureRemoteApi(appid, path, auth_func,
75 servername=servername,
76 save_cookies=True, secure=secure,
77 rpc_server_factory=rpc_server_factory)
78 remote_api_stub.MaybeInvokeAuthentication()
81 os.environ['SERVER_SOFTWARE'] = 'Development (remote_api_shell)/1.0'
83 if not appid:
85 appid = os.environ['APPLICATION_ID']
86 sys.ps1 = '%s> ' % appid
87 if readline is not None:
89 readline.parse_and_bind('tab: complete')
90 atexit.register(lambda: readline.write_history_file(HISTORY_PATH))
91 if os.path.exists(HISTORY_PATH):
92 readline.read_history_file(HISTORY_PATH)
95 if '' not in sys.path:
96 sys.path.insert(0, '')
98 preimported_locals = {
99 'memcache': memcache,
100 'urlfetch': urlfetch,
101 'users': users,
102 'db': db,
103 'ndb': ndb,
107 code.interact(banner=BANNER, local=preimported_locals)
110 def main(argv):
111 """Parse arguments and run shell."""
112 parser = optparse.OptionParser(usage=__doc__)
113 parser.add_option('-s', '--server', dest='server',
114 help='The hostname your app is deployed on. '
115 'Defaults to <app_id>.appspot.com.')
116 parser.add_option('-p', '--path', dest='path',
117 help='The path on the server to the remote_api handler. '
118 'Defaults to %s.' % DEFAULT_PATH)
119 parser.add_option('--secure', dest='secure', action="store_true",
120 default=False, help='Use HTTPS when communicating '
121 'with the server.')
122 (options, args) = parser.parse_args()
125 if ((not options.server and not args) or len(args) > 2
126 or (options.path and len(args) > 1)):
127 parser.print_usage(sys.stderr)
128 if len(args) > 2:
129 print >> sys.stderr, 'Unexpected arguments: %s' % args[2:]
130 elif options.path and len(args) > 1:
131 print >> sys.stderr, 'Path specified twice.'
132 sys.exit(1)
135 servername = options.server
136 appid = None
137 path = options.path or DEFAULT_PATH
138 if args:
139 if servername:
141 appid = args[0]
142 else:
144 servername = '%s.appspot.com' % args[0]
145 if len(args) == 2:
147 path = args[1]
148 remote_api_shell(servername, appid, path, options.secure,
149 appengine_rpc.HttpRpcServer)
152 if __name__ == '__main__':
153 main(sys.argv)