def setup_remote(self):
     if not self.remote_api_path:
         from ..utils import appconfig
         for handler in appconfig.handlers:
             if handler.script == REMOTE_API_SCRIPT:
                 self.remote_api_path = handler.url
                 break
     self.remote = True
     remote_url = 'https://%s.appspot.com%s' % (self.remote_app_id,
                                                self.remote_api_path)
     logging.info('Setting up remote_api for "%s" at %s' %
                  (self.remote_app_id, remote_url))
     if not have_appserver:
         print 'Connecting to remote_api handler'
     from google.appengine.ext.remote_api import remote_api_stub
     remote_api_stub.ConfigureRemoteApi(self.remote_app_id,
         self.remote_api_path, auth_func, secure=self.secure_remote_api,
         rpc_server_factory=rpc_server_factory)
     retry_delay = 1
     while retry_delay <= 16:
         try:
             remote_api_stub.MaybeInvokeAuthentication()
         except HTTPError, e:
             if not have_appserver:
                 print 'Retrying in %d seconds...' % retry_delay
             time.sleep(retry_delay)
             retry_delay *= 2
         else:
             break
Exemple #2
0
 def setup_remote(self):
     if not self.remote_api_path:
         from ..utils import appconfig
         for handler in appconfig.handlers:
             if handler.script == REMOTE_API_SCRIPT:
                 self.remote_api_path = handler.url.split('(', 1)[0]
                 break
     self.remote = True
     remote_url = 'https://%s.appspot.com%s' % (self.remote_app_id,
                                                self.remote_api_path)
     logging.info('Setting up remote_api for "%s" at %s' %
                  (self.remote_app_id, remote_url))
     if not have_appserver:
         print('Connecting to remote_api handler.\n\n'
               'IMPORTANT: Check your login method settings in the '
               'App Engine Dashboard if you have problems logging in. '
               'Login is only supported for Google Accounts.\n')
     from google.appengine.ext.remote_api import remote_api_stub
     remote_api_stub.ConfigureRemoteApi(self.remote_app_id,
         self.remote_api_path, auth_func, secure=self.secure_remote_api,
         rpc_server_factory=rpc_server_factory)
     retry_delay = 1
     while retry_delay <= 16:
         try:
             remote_api_stub.MaybeInvokeAuthentication()
         except HTTPError, e:
             if not have_appserver:
                 print 'Retrying in %d seconds...' % retry_delay
             time.sleep(retry_delay)
             retry_delay *= 2
         else:
             break
def remote_api_shell(servername, appid, path, secure, rpc_server_factory):
  """Actually run the remote_api_shell."""


  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)


  code.interact(banner=BANNER, local=globals())
Exemple #4
0
def download_appstats(servername, appid, path, secure, rpc_server_factory,
                      filename, appdir, merge, java_application):
    """Invoke remote_api to download appstats data."""

    if os.path.isdir(appdir):
        sys.path.insert(0, appdir)
        try:
            logging.info('Importing appengine_config from %s', appdir)
            import appengine_config
        except ImportError as err:
            logging.warn('Failed to load appengine_config: %s', err)

    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']
    download_data(filename, merge, java_application)
Exemple #5
0
    def _env_setUp(self):
        self.kind_method_swapped = False
        self.original_kind_method = None

        if self.may_cleanup_used_kind():
            global kind_names_for_test
            kind_names_for_test = {}

        set_environments(self)

        if self.use_swapped_kind_name():
            self.swap_model_kind()

        if is_in_production() and self.use_production_environment():
            apiproxy_stub_map.apiproxy = get_original_apiproxy_behind_gaeunit()
        elif is_in_production():
            pass  # use apiproxy prepared by GAEUnit (datastore_stub is datastore_file_stub)
        elif not is_in_gaeunit() and self.use_remote_api():
            remote_api_stub.ConfigureRemoteApi(APP_ID,
                                               REMOTE_API_ENTRY_POINT,
                                               auth_func,
                                               secure=True,
                                               save_cookies=True)
            if not self.authenticated_for_remote_api:
                remote_api_stub.MaybeInvokeAuthentication()
                self.authenticated_for_remote_api = True
        else:
            apiproxy_stub_map.apiproxy = get_dev_apiproxy()
Exemple #6
0
 def setup_remote_stubs(self, connection):
     if self.active_stubs == 'remote':
         return
     if not connection.remote_api_path:
         from ..utils import appconfig
         for handler in appconfig.handlers:
             if handler.script in REMOTE_API_SCRIPTS:
                 connection.remote_api_path = handler.url.split('(', 1)[0]
                 break
     server = '%s.%s' % (connection.remote_app_id, connection.domain)
     remote_url = 'https://%s%s' % (server, connection.remote_api_path)
     logging.info("Setting up remote_api for '%s' at %s." %
                  (connection.remote_app_id, remote_url))
     if not have_appserver:
         logging.info(
             "Connecting to remote_api handler.\n\n"
             "IMPORTANT: Check your login method settings in the "
             "App Engine Dashboard if you have problems logging in. "
             "Login is only supported for Google Accounts.")
     from google.appengine.ext.remote_api import remote_api_stub
     remote_api_stub.ConfigureRemoteApi(None,
         connection.remote_api_path, auth_func, servername=server,
         secure=connection.secure_remote_api,
         rpc_server_factory=rpc_server_factory)
     retry_delay = 1
     while retry_delay <= 16:
         try:
             remote_api_stub.MaybeInvokeAuthentication()
         except HTTPError, e:
             if not have_appserver:
                 logging.info("Retrying in %d seconds..." % retry_delay)
             time.sleep(retry_delay)
             retry_delay *= 2
         else:
             break
Exemple #7
0
def clear_datastore(appid=('a', ''),
                    host=('h', ''),
                    path=('p', ''),
                    kinds=('k', ''),
                    clear_memcache=('c', False),
                    secure=True):
    """Clear all the data on GAE environment using remote_api.
  """
    if not appid:
        appid = get_appid()
    if not host:
        host = "%s.appspot.com" % appid
    if not path:
        path = '/remote_api'
    if not kinds:
        models = None
    else:
        models_dict = get_all_models_as_dict()
        models = []
        for kind in kinds.split(','):
            models.append(db.class_for_kind(kind))

    remote_api_stub.ConfigureRemoteApi(appid,
                                       path,
                                       auth_func,
                                       host,
                                       secure=secure,
                                       save_cookies=True)
    remote_api_stub.MaybeInvokeAuthentication()
    delete_all_entities(models)
    if clear_memcache:
        from google.appengine.api import memcache
        memcache.flush_all()
        sys.stderr.write("Flushed memcache.\n")
Exemple #8
0
def create_user(user_name=('u', ''), password=('P', ''), is_admin=('A', False),
                appid=('a', ''), host=('h', ''), path=('p', ''), secure=True):
  """ Create new user using remote_api.
  """
  from kay.auth import (
    create_new_user, DuplicateKeyError,
  )
  if not user_name:
    print_status('user_name required')
    sys.exit(1)
  if not password:
    password = getpass.getpass('Please input a password for new user:'******'/remote_api'
  
  remote_api_stub.ConfigureRemoteApi(None, path, auth_func,
                                     host, secure=secure, save_cookies=True)
  remote_api_stub.MaybeInvokeAuthentication()
  try:
    create_new_user(user_name, password, is_admin=is_admin)
    print_status('A new user: %s successfully created.' % user_name)
    sys.exit(0)
  except DuplicateKeyError, e:
    print_status(e)
    sys.exit(1)
Exemple #9
0
    def inner(appid=('a', ''),
              host=('h', ''),
              path=('p', ''),
              secure=True,
              clean=('c', False)):
        if not appid:
            appid = get_appid()
        if not host:
            host = "%s.appspot.com" % appid
        if not path:
            path = '/remote_api'

        if 'localhost' in host:
            auth_func = dummy_auth
        else:
            auth_func = auth

        remote_api_stub.ConfigureRemoteApi(appid,
                                           path,
                                           auth_func,
                                           host,
                                           secure=secure,
                                           save_cookies=True)
        remote_api_stub.MaybeInvokeAuthentication()

        if clean and callable(clean_func):
            clean_func()
        if callable(main_func):
            main_func()
Exemple #10
0
def attach(appid):
    def auth_func():
        return (raw_input('Email:'), getpass.getpass('Password:'******'SERVER_SOFTWARE'] = 'Development (remote_api)/1.0'
Exemple #11
0
def prep_remote_shell(path='/remote_api'):
    """Prepares a remote shell using remote_api located at the given path on
    the given host, if given.  By default, will use the default version of the
    current App Engine application."""
    auth_func = lambda: (raw_input('Email: '), getpass.getpass('Password: '******'SERVER_SOFTWARE'] = 'Development (remote_api_shell)/1.0'
Exemple #12
0
def attach(host=None):
    def auth_func():
        if host and host.startswith('localhost'):
            return ('foo', 'bar')
        else:
            return (EMAIL, getpass.getpass())

    remote_api_stub.ConfigureRemoteApi(APPID, REMOTE_API_PATH, auth_func, host)
    remote_api_stub.MaybeInvokeAuthentication()
    os.environ['SERVER_SOFTWARE'] = 'Development (remote_api)/1.0'
Exemple #13
0
def rshell(appid=('a', ''),
           host=('h', ''),
           path=('p', ''),
           useful_imports=True,
           secure=True,
           use_ipython=True):
    """Start a new interactive python session with RemoteDatastore stub."""
    banner = ("Interactive Kay Shell with RemoteDatastore. \n"
              "-----------------WARNING--------------------\n"
              "\n"
              "Please be careful in this console session.\n"
              "\n"
              "-----------------WARNING--------------------\n")
    if useful_imports:
        namespace = create_useful_locals_for_rshell()
    else:
        namespace = {}
    if not appid:
        appid = get_appid()
    if not host:
        host = "%s.appspot.com" % appid
    if not path:
        path = '/remote_api'

    remote_api_stub.ConfigureRemoteApi(appid,
                                       path,
                                       auth_func,
                                       host,
                                       secure=secure,
                                       save_cookies=True)
    remote_api_stub.MaybeInvokeAuthentication()
    if use_ipython:
        try:
            import IPython
        except ImportError:
            pass
        else:
            sh = IPython.Shell.IPShellEmbed(
                argv=['-pi1',
                      '%s[\#]: ' % appid, '-po',
                      '%s[\#]: ' % appid],
                banner=banner)
            sh(global_ns={}, local_ns=namespace)
            return
    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)
    from code import interact
    interact(banner, local=namespace)
def configure_remote_api():
    def auth_func():
        return ('*****@*****.**', 'pyluojtwiazmklmq')

    remote_api_stub.ConfigureRemoteApi(
        'edufocal-app',
        '/_ah/remote_api',
        auth_func,
        servername='edufocal-app.appspot.com',
        save_cookies=True,
        secure=False,
        rpc_server_factory=appengine_rpc.HttpRpcServer)
    remote_api_stub.MaybeInvokeAuthentication()
Exemple #15
0
def setupRemote(app_id, host=None):
    """Sets up execution for the specified remote.
  """
    from google.appengine.api import apiproxy_stub_map
    from google.appengine.ext import db
    from google.appengine.ext.remote_api import remote_api_stub

    if not host:
        host = '%s.appspot.com' % app_id.split("~")[-1]

    remote_api_stub.ConfigureRemoteDatastore(app_id, '/_ah/remote_api',
                                             auth_func, host)
    remote_api_stub.MaybeInvokeAuthentication()
 def establish(self):
     """Establishes the environment for RPC execution."""
     try:
         remote_api_stub.ConfigureRemoteApi(
             self._get_internal_application_id(),
             self._path,
             self._get_auth_func(),
             servername=self._server,
             save_cookies=True,
             secure=self._get_secure(),
             rpc_server_factory=appengine_rpc.HttpRpcServer)
         remote_api_stub.MaybeInvokeAuthentication()
     except AttributeError:
         raise EnvironmentAuthenticationError
Exemple #17
0
def main(argv):
  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.')
  (options, args) = parser.parse_args()

  if ((not options.server and not args) or len(args) > 2
      or (options.path and len(args) > 1)):
    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.'
    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_stub.ConfigureRemoteApi(appid, path, auth_func,
                                     servername=servername,
                                     save_cookies=True, secure=options.secure)
  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)

  code.interact(banner=BANNER, local=globals())
def remote_api_shell(servername,
                     appid,
                     path,
                     secure,
                     rpc_server_factory,
                     oauth2=False):
    """Actually run the remote_api_shell."""

    if oauth2:
        remote_api_stub.ConfigureRemoteApiForOAuth(servername,
                                                   path,
                                                   secure=secure,
                                                   app_id=appid)
    else:
        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, '')

    preimported_locals = {
        'memcache': memcache,
        'urlfetch': urlfetch,
        'users': users,
        'db': db,
        'ndb': ndb,
    }

    code.interact(banner=BANNER, local=preimported_locals)
Exemple #19
0
def remote_api_shell(servername,
                     appid,
                     path,
                     secure,
                     rpc_server_factory,
                     email=None,
                     password=None,
                     ipython=True):
    """Actually run the remote_api_shell."""
    auth = partial(auth_func, email=email, password=password)
    remote_api_stub.ConfigureRemoteApi(appid,
                                       path,
                                       auth,
                                       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, '')

    preimported_locals = {
        'memcache': memcache,
        'urlfetch': urlfetch,
        'users': users,
        'db': db,
        'ndb': ndb,
    }

    if ipython and TerminalInteractiveShell:
        ishell = TerminalInteractiveShell(banner1=BANNER,
                                          user_ns=preimported_locals)
        ishell.mainloop()
    else:
        code.interact(banner=BANNER, local=preimported_locals)
Exemple #20
0
def setupRemoteDatastore(app_id, email, password):
    """Enables remote API mode for all datastore operations.

    Args:
        app_id: Valid GAE app id.
        email: User email.
        password: User password.
    """

    from google.appengine.ext.remote_api import remote_api_stub
    remote_api_stub.ConfigureRemoteApi(app_id,
                                       '/remote_api',
                                       lambda: (email, password),
                                       secure=True,
                                       servername=app_id + '.appspot.com',
                                       services=['datastore_v3'])
    remote_api_stub.MaybeInvokeAuthentication()
def main(argv):
    parser = optparse.OptionParser()
    parser.add_option('-s',
                      '--server',
                      dest='server',
                      help='The hostname your app is deployed on. '
                      'Defaults to <app_id>.appspot.com.')
    parser.add_option('--secure',
                      dest='secure',
                      action="store_true",
                      default=False,
                      help='Use HTTPS when communicating '
                      'with the server.')
    (options, args) = parser.parse_args()

    if not args or len(args) > 2:
        print >> sys.stderr, __doc__
        if len(args) > 2:
            print >> sys.stderr, 'Unexpected arguments: %s' % args[2:]
        sys.exit(1)

    appid = args[0]
    if len(args) == 2:
        path = args[1]
    else:
        path = DEFAULT_PATH

    remote_api_stub.ConfigureRemoteApi(appid,
                                       path,
                                       auth_func,
                                       servername=options.server,
                                       save_cookies=True,
                                       secure=options.secure)
    remote_api_stub.MaybeInvokeAuthentication()

    os.environ['SERVER_SOFTWARE'] = 'Development (remote_api_shell)/1.0'

    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)

    code.interact(banner=BANNER, local=globals())
Exemple #22
0
def attach(host=None):
    def auth_func():
        print "(Hint: Google Password)"
        if host and host.startswith('localhost'):
            return ('foo', 'bar')
        else:
            return (EMAIL, getpass.getpass())

    remote_api_stub.ConfigureRemoteApi(
        APPID,
        REMOTE_API_PATH,
        auth_func,
        save_cookies=True,
        secure=False,
        rpc_server_factory=appengine_rpc.HttpRpcServer,
        servername='marketplacr.appspot.com')
    remote_api_stub.MaybeInvokeAuthentication()
    os.environ['SERVER_SOFTWARE'] = 'Development (remote_api)/1.0'
Exemple #23
0
def remote_api_shell(servername, appid, path, secure, rpc_server_factory):
    """Actually run the remote_api_shell.
    """

    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'

    # ensure our app is on the python path
    sys.path.insert(0, '/src-server')
    # run an embedded IPython shell in the current context
    IPython.embed(header=BANNER)
Exemple #24
0
def main():
    parser = optparse.OptionParser()
    parser.add_option('-s',
                      '--server',
                      dest='server',
                      help='The hostname your app is deployed on. '
                      'Defaults to <app_id>.appspot.com.')
    parser.add_option('--secure',
                      dest='secure',
                      action="store_true",
                      default=False,
                      help='Use HTTPS when communicating '
                      'with the server.')
    (options, args) = parser.parse_args()

    if not args or len(args) > 3:
        print >> sys.stderr, __doc__
        if len(args) > 3:
            print >> sys.stderr, 'Unexpected arguments: %s' % args[2:]
        sys.exit(1)

    script_path = args.pop()
    if not os.path.isfile(script_path):
        print >> sys.stderr, "Script was not found: %s" % script_path
        sys.exit(1)

    appid = args[0]
    if len(args) == 2:
        path = args[1]
    else:
        path = DEFAULT_PATH

    remote_api_stub.ConfigureRemoteApi(appid,
                                       path,
                                       auth_func,
                                       servername=options.server,
                                       save_cookies=True,
                                       secure=options.secure)
    remote_api_stub.MaybeInvokeAuthentication()

    execfile(script_path, globals())
Exemple #25
0
def configure(host=None):
    secure = True
    if not host or host == "localhost:8080" or host == "127.0.0.1:8080":
        host = "localhost:8080"
        secure = False
    import google.appengine.ext.remote_api.remote_api_stub as remote_api_stub

    def auth_func():
        # Probably localhost, so let's skip and give some fake admin user
        if host.split(":")[0] in ("localhost", "127.0.0.1"):
            return ("a@b", "b")

        import getpass
        return (raw_input('email: '), getpass.getpass('password: '))

    remote_api_stub.ConfigureRemoteApi(None,
                                       "/_ah/remote_api",
                                       auth_func,
                                       host,
                                       save_cookies=True,
                                       secure=secure)
    remote_api_stub.MaybeInvokeAuthentication()
Exemple #26
0
            self.remote_api_path, auth_func, secure=self.secure_remote_api,
            rpc_server_factory=rpc_server_factory)
        retry_delay = 1
        while retry_delay <= 16:
            try:
                remote_api_stub.MaybeInvokeAuthentication()
            except HTTPError, e:
                if not have_appserver:
                    print 'Retrying in %d seconds...' % retry_delay
                time.sleep(retry_delay)
                retry_delay *= 2
            else:
                break
        else:
            try:
                remote_api_stub.MaybeInvokeAuthentication()
            except HTTPError, e:
                raise URLError("%s\n"
                               "Couldn't reach remote_api handler at %s.\n"
                               "Make sure you've deployed your project and "
                               "installed a remote_api handler in app.yaml."
                               % (e, remote_url))
        logging.info('Now using the remote datastore for "%s" at %s' %
                     (self.remote_app_id, remote_url))

    def flush(self):
        """Helper function to remove the current datastore and re-open the stubs"""
        if self.remote:
            import random, string
            code = ''.join([random.choice(string.ascii_letters) for x in range(4)])
            print '\n\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
Exemple #27
0
def CMDshell(parser, args):
    """Opens interactive remote shell with app's GAE environment.

  Connects to a specific version of a specific module (an active version of
  'default' module by default). The app must have 'remote_api: on' builtin
  enabled in app.yaml.

  Always uses password based authentication.
  """
    parser.allow_positional_args = True
    parser.add_option('-H',
                      '--host',
                      help='Only necessary if not hosted on .appspot.com')
    parser.add_option('--local',
                      action='store_true',
                      help='Operates locally on an empty dev instance')
    app, options, args = parser.parse_args(args)

    module = 'default'
    version = None
    if len(args) == 2:
        module, version = args
    elif len(args) == 1:
        module = args[0]
    elif args:
        parser.error('Unknown args: %s' % args)

    if module not in app.modules:
        parser.error('No such module: %s' % module)

    if not options.host and not options.local:
        prefixes = filter(None, (version, module, app.app_id))
        options.host = '%s.appspot.com' % '-dot-'.join(prefixes)

    # Ensure remote_api is initialized and GAE sys.path is set.
    gae_sdk_utils.setup_env(app.app_dir,
                            app.app_id,
                            version,
                            module,
                            remote_api=True)

    if options.host:
        # Open the connection.
        from google.appengine.ext.remote_api import remote_api_stub
        try:
            print('Connecting...')
            remote_api_stub.ConfigureRemoteApi(
                None,
                '/_ah/remote_api',
                gae_sdk_utils.get_authentication_function(),
                options.host,
                save_cookies=True,
                secure=True)
        except urllib2.URLError:
            print >> sys.stderr, 'Failed to access %s' % options.host
            return 1
        remote_api_stub.MaybeInvokeAuthentication()

    def register_sys_path(*path):
        abs_path = os.path.abspath(os.path.join(*path))
        if os.path.isdir(abs_path) and not abs_path in sys.path:
            sys.path.insert(0, abs_path)

    # Simplify imports of app modules (with dependencies). This code is optimized
    # for layout of apps that use 'components'.
    register_sys_path(app.app_dir)
    register_sys_path(app.app_dir, 'third_party')
    register_sys_path(app.app_dir, 'components', 'third_party')

    # Import some common modules into interactive console namespace.
    def setup_context():
        # pylint: disable=unused-variable
        from google.appengine.api import app_identity
        from google.appengine.api import memcache
        from google.appengine.api import urlfetch
        from google.appengine.ext import ndb
        return locals().copy()

    context = setup_context()

    # Fancy readline support.
    if readline is not None:
        readline.parse_and_bind('tab: complete')
        history_file = os.path.expanduser('~/.config/gae_tool/remote_api_%s' %
                                          app.app_id)
        if not os.path.exists(os.path.dirname(history_file)):
            os.makedirs(os.path.dirname(history_file))
        atexit.register(lambda: readline.write_history_file(history_file))
        if os.path.exists(history_file):
            readline.read_history_file(history_file)

    prompt = [
        'App Engine interactive console for "%s".' % app.app_id,
        'Available symbols:',
    ]
    prompt.extend(sorted('  %s' % symbol for symbol in context))
    code.interact('\n'.join(prompt), None, context)
    return 0
Exemple #28
0
def main():
    getlibdir()
    from google.appengine.ext import db
    from google.appengine.ext.remote_api import remote_api_stub
    from google.appengine.tools import appengine_rpc

    class SplashData(db.Model):
        version = db.IntegerProperty()
        content = db.TextProperty()
        width = db.IntegerProperty()
        height = db.IntegerProperty()

    filename = None
    host = 'localhost'
    getversion = False
    bumpversion = False
    setversion = None
    width = None
    height = None
    try:
        opts = getopt(sys.argv[1:], 'sbgf:w:h:H:', [
            'set', 'bump', 'getversion', 'file=', 'width=', 'height=', 'host='
        ])
    except GetoptError:
        sys.stderr.write(
            'Usage: splashmanager.py [-f splashfile -w width -h height] [-H host] \n'
        )
        sys.exit(1)
    for opt in opts[0]:
        if opt == []:
            continue
        if len(opt) < 2:
            sys.stderr.write(
                'Usage: splashmanager.py [-f splashfile -w width -h height] [-H host] \n'
            )
            sys.exit(1)
        if opt[0] in ('--file', '-f'):
            filename = opt[1]
            whitelistname = opt[1]
        elif opt[0] in ('-H', '--host'):
            host = opt[1]
        elif opt[0] in ('-g', '--getversion'):
            getversion = True
        elif opt[0] in ('--bump', '-b'):
            bumpversion = True
        elif opt[0] in ('-w', '--width'):
            width = opt[1]
        elif opt[0] in ('-h', '--host'):
            height = opt[1]
        elif opt[0] in ('-s', '--set'):
            setversion = opt[1]

    if setversion and bumpversion:
        sys.stderr.write(
            'Error: -s and -b are incompatible, use one or the other.\n')
        sys.stderr.write(
            'Usage: splashmanager.py [-f splashfile -w width -h height] [-H host] \n'
        )
        sys.exit(1)

    print 'Connecting to %s' % host
    if host == 'localhost':
        host = host + ':8888'
        secure = False
    else:
        secure = True

    remote_api_stub.ConfigureRemoteApi(
        None,
        '/remote_api',
        auth_func,
        servername=host,
        save_cookies=True,
        secure=secure,
        rpc_server_factory=appengine_rpc.HttpRpcServer)
    remote_api_stub.MaybeInvokeAuthentication()

    update = False
    sd = SplashData.all().fetch(1)[0]
    if getversion:
        print 'Splash Version: %d' % sd.version
    if setversion:
        sd.version = setversion
        update = True
    if filename:
        if width == None or height == None:
            sys.stderr.write(
                'Must specify width and height while you provide a file.\n')
            sys.exit(1)
        data = open(filename).read()
        sd.content = data
        sd.width = int(width)
        sd.height = int(height)
        update = True
    if bumpversion:
        sd.version = sd.version + 1
        update = True
    if update:
        sd.put()
def initialize_remote_api(app_id=APP_ID, path=REMOTE_API_PATH):
    remote_api_stub.ConfigureRemoteApi(app_id, path, auth_func)
    remote_api_stub.MaybeInvokeAuthentication()
Exemple #30
0
def main():
    getlibdir()
    from google.appengine.ext import db
    from google.appengine.ext.remote_api import remote_api_stub
    from google.appengine.tools import appengine_rpc

    class WhiteListData(db.Model):
        emailLower = db.StringProperty()

    whitelistname = 'whitelist'
    host = 'localhost'
    getonly = False
    try:
        opts = getopt(sys.argv[1:], 'n:h:d', ['name=', 'getonly'])
    except GetoptError:
        sys.stderr.write(
            'Usage: whitelist.py [-n whitelistfile] [-h host] [-d] [--getonly]\n'
        )
        sys.exit(1)
    for opt in opts[0]:
        if opt == []:
            continue
        if len(opt) < 2:
            sys.stderr.write(
                'Usage: whitelist.py [-n whitelistfile] [-h host] [-d] [--getonly]\n'
            )
            sys.exit(1)
        if opt[0] in ('-n', '--name'):
            whitelistname = opt[1]
        elif opt[0] == '-h':
            host = opt[1]
        elif opt[0] == '-d':
            host = 'localhost'
        elif opt[0] == '--getonly':
            getonly = True

    print 'Using %s for input' % whitelistname
    print 'Connecting to %s' % host
    if host == 'localhost':
        host = host + ':8888'
        secure = False
    else:
        secure = True

    remote_api_stub.ConfigureRemoteApi(
        None,
        '/remote_api',
        auth_func,
        servername=host,
        save_cookies=True,
        secure=secure,
        rpc_server_factory=appengine_rpc.HttpRpcServer)
    remote_api_stub.MaybeInvokeAuthentication()

    input_people = open(whitelistname).readlines()
    input_people = [x.strip() for x in input_people]

    installed_people = []
    c = None
    while True:
        q = WhiteListData.gql("")
        if c:
            q = q.with_cursor(c)
        z = q.fetch(500)
        c = q.cursor()
        installed_people += z
        if len(z) < 500:
            break

    if getonly:
        print 'Getonly set, returning existing whitelist with no changes'
        for person in installed_people:
            print person.emailLower
        return

    WHITE = {}
    for email in input_people:
        WHITE[unicode(email)] = [0, None]
    for person in installed_people:
        email = person.emailLower
        if WHITE.has_key(email):
            WHITE[email] = [2, person]
        else:
            WHITE[email] = [1, person]

    # Now we go through the dictionary. Remove people in state 1
    # and add people in state 0

    for (email, z) in WHITE.items():
        state, person = z
        if state == 0:
            v = WhiteListData()
            v.emailLower = email
            v.put()
            print 'Added %s' % email
        elif state == 1:
            person.delete()
            print 'Removed %s' % email