Skip to content

Instantly share code, notes, and snippets.

@faisalraja
Created August 1, 2013 22:57
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save faisalraja/6136087 to your computer and use it in GitHub Desktop.
Save faisalraja/6136087 to your computer and use it in GitHub Desktop.
Modified version of remote_api_shell.py that accepts a -r or --run your_python_file.py and --email --password to quickly run your app engine python scripts without interactivity.
#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""An interactive python shell that uses remote_api.
Usage:
%prog [-s HOSTNAME] [-p PATH] [--email EMAIL] [--password PASSWORD] -r RUNSCRIPT.py [APPID]
If the -s HOSTNAME flag is not specified, the APPID must be specified.
"""
# Change this to where your appengine sdk lives
# '/usr/local/google_appengine'
GOOGLE_APP_ENGINE_SDK = None
import sys
import os
if GOOGLE_APP_ENGINE_SDK and os.path.exists(GOOGLE_APP_ENGINE_SDK):
sys.path.append(GOOGLE_APP_ENGINE_SDK)
appengine_lib = os.path.join(GOOGLE_APP_ENGINE_SDK, 'lib')
for lib in os.listdir(appengine_lib):
sys.path.append(os.path.join(appengine_lib, lib))
from google.appengine.tools import os_compat
import atexit
import getpass
import optparse
import imp
try:
import readline
except ImportError:
readline = None
from google.appengine.ext.remote_api import remote_api_stub
from google.appengine.tools import appengine_rpc
HISTORY_PATH = os.path.expanduser('~/.remote_api_shell_history')
DEFAULT_PATH = '/_ah/remote_api'
BANNER = """App Engine remote_api script
Python %s\
""" % sys.version
def remote_api_shell(servername, appid, path, secure, options, rpc_server_factory):
"""Actually run the remote_api_shell."""
def auth_func():
return (options.email if options.email else raw_input('Email: '),
options.password if options.password else getpass.getpass('Password: '))
remote_api_stub.ConfigureRemoteApi(appid, path, auth_func,
servername=servername,
save_cookies=True, secure=secure,
rpc_server_factory=rpc_server_factory)
remote_api_stub.MaybeInvokeAuthentication()
os.environ['SERVER_SOFTWARE'] = 'Development (remote_api_shell)/1.0'
if not appid:
appid = os.environ['APPLICATION_ID']
sys.ps1 = '%s> ' % appid
if readline is not None:
readline.parse_and_bind('tab: complete')
atexit.register(lambda: readline.write_history_file(HISTORY_PATH))
if os.path.exists(HISTORY_PATH):
readline.read_history_file(HISTORY_PATH)
if '' not in sys.path:
sys.path.insert(0, '')
name, ext = os.path.splitext(os.path.basename(options.run))
imp.load_source(name, options.run)
def main(argv):
"""Parse arguments and run shell."""
parser = optparse.OptionParser(usage=__doc__)
parser.add_option('-s', '--server', dest='server',
help='The hostname your app is deployed on. '
'Defaults to <app_id>.appspot.com.')
parser.add_option('-p', '--path', dest='path',
help='The path on the server to the remote_api handler. '
'Defaults to %s.' % DEFAULT_PATH)
parser.add_option('--secure', dest='secure', action="store_true",
default=False, help='Use HTTPS when communicating '
'with the server.')
parser.add_option('-r', '--run', dest='run',
help='The local python path to execute ')
parser.add_option('--email', dest='email',
help='Login email ')
parser.add_option('--password', dest='password',
help='Login password ')
(options, args) = parser.parse_args()
if ((not options.server and not args) or len(args) > 2
or (options.path and len(args) > 1)) or not options.run:
parser.print_usage(sys.stderr)
if len(args) > 2:
print >> sys.stderr, 'Unexpected arguments: %s' % args[2:]
elif options.path and len(args) > 1:
print >> sys.stderr, 'Path specified twice.'
elif not options.run:
print >> sys.stderr, 'No run specified.'
sys.exit(1)
if not os.path.exists(options.run):
print >> sys.stderr, 'Run script not found.'
sys.exit(1)
servername = options.server
appid = None
path = options.path or DEFAULT_PATH
if args:
if servername:
appid = args[0]
else:
servername = '%s.appspot.com' % args[0]
if len(args) == 2:
path = args[1]
remote_api_shell(servername, appid, path, options.secure, options,
appengine_rpc.HttpRpcServer)
if __name__ == '__main__':
main(sys.argv)
@yousef2o13o
Copy link

ii

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment