1.9.30 sync.
[gae.git] / python / google / appengine / tools / remote_api_shell.py
blob18158728aaa1562cbe8a89a29f5951d2ca3222ce
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.
17 """An interactive python shell that uses remote_api.
19 Usage:
20 %prog [-s HOSTNAME] [-p PATH] [--secure] [APPID]
22 If the -s HOSTNAME flag is not specified, the APPID must be specified.
23 """
29 from google.appengine.tools import os_compat
31 import atexit
32 import code
33 import getpass
34 import optparse
35 import os
36 import sys
38 try:
39 import readline
40 except ImportError:
41 readline = None
43 from google.appengine.ext.remote_api import remote_api_stub
44 from google.appengine.tools import appengine_rpc
48 from google.appengine.api import memcache
49 from google.appengine.api import urlfetch
50 from google.appengine.api import users
51 from google.appengine.ext import db
52 from google.appengine.ext import ndb
55 HISTORY_PATH = os.path.expanduser('~/.remote_api_shell_history')
56 DEFAULT_PATH = '/_ah/remote_api'
57 BANNER = """App Engine remote_api shell
58 Python %s
59 The db, ndb, users, urlfetch, and memcache modules are imported.\
60 """ % sys.version
63 def auth_func():
64 return (raw_input('Email: '), getpass.getpass('Password: '))
67 def remote_api_shell(servername, appid, path, secure, rpc_server_factory):
68 """Actually run the remote_api_shell."""
71 remote_api_stub.ConfigureRemoteApi(appid, path, auth_func,
72 servername=servername,
73 save_cookies=True, secure=secure,
74 rpc_server_factory=rpc_server_factory)
75 remote_api_stub.MaybeInvokeAuthentication()
78 os.environ['SERVER_SOFTWARE'] = 'Development (remote_api_shell)/1.0'
80 if not appid:
82 appid = os.environ['APPLICATION_ID']
83 sys.ps1 = '%s> ' % appid
84 if readline is not None:
86 readline.parse_and_bind('tab: complete')
87 atexit.register(lambda: readline.write_history_file(HISTORY_PATH))
88 if os.path.exists(HISTORY_PATH):
89 readline.read_history_file(HISTORY_PATH)
92 if '' not in sys.path:
93 sys.path.insert(0, '')
95 preimported_locals = {
96 'memcache': memcache,
97 'urlfetch': urlfetch,
98 'users': users,
99 'db': db,
100 'ndb': ndb,
104 code.interact(banner=BANNER, local=preimported_locals)
107 def main(argv):
108 """Parse arguments and run shell."""
109 parser = optparse.OptionParser(usage=__doc__)
110 parser.add_option('-s', '--server', dest='server',
111 help='The hostname your app is deployed on. '
112 'Defaults to <app_id>.appspot.com.')
113 parser.add_option('-p', '--path', dest='path',
114 help='The path on the server to the remote_api handler. '
115 'Defaults to %s.' % DEFAULT_PATH)
116 parser.add_option('--secure', dest='secure', action="store_true",
117 default=False, help='Use HTTPS when communicating '
118 'with the server.')
119 (options, args) = parser.parse_args()
122 if ((not options.server and not args) or len(args) > 2
123 or (options.path and len(args) > 1)):
124 parser.print_usage(sys.stderr)
125 if len(args) > 2:
126 print >> sys.stderr, 'Unexpected arguments: %s' % args[2:]
127 elif options.path and len(args) > 1:
128 print >> sys.stderr, 'Path specified twice.'
129 sys.exit(1)
132 servername = options.server
133 appid = None
134 path = options.path or DEFAULT_PATH
135 if args:
136 if servername:
138 appid = args[0]
139 else:
141 servername = '%s.appspot.com' % args[0]
142 if len(args) == 2:
144 path = args[1]
145 remote_api_shell(servername, appid, path, options.secure,
146 appengine_rpc.HttpRpcServer)
149 if __name__ == '__main__':
150 main(sys.argv)