Пример #1
0
def loaddata(path):
    """Load the specified JSON fixtures.  If preceded by a deployment target,
the fixture data will be loaded onto that target.  Otherwise they will be
loaded into the local datastore.

Arguments:

    :path -- The path to the fixture data to load

Usage:

    # Load data locally
    fab loaddata:groups/fixtures/test_groups.json

    # Load data onto staging
    fab staging loaddata:groups/fixtures/test_groups.json
"""
    import fixtures

    # Are we loading the fixtures remotely or locally?
    if hasattr(env, 'gae'):
        utils.prep_remote_shell()
    else:
        utils.prep_local_shell()

    # Actually load the fixtures (tweak the logging so their info shows up)
    logging.getLogger().setLevel(logging.INFO)
    fixtures.load_fixtures(path)
Пример #2
0
def dumpjson(kinds):
    """Dumps data from the local or remote datastore in JSON format.

Arguments:

    :kinds -- A comma-separated list of kinds to dump, specified as
              `path.to.module.ModelName `
    """
    import fixtures
    if hasattr(env, 'gae'):
        utils.prep_remote_shell()
    else:
        utils.prep_local_shell()
    for kind in kinds.split(','):
        print fixtures.serialize_entities(kind)
Пример #3
0
def shell(cmd=None, path='/remote_api'):
    """Launches an interactive shell for this app. If preceded by a deployment
target (e.g. production or staging), a remote_api shell on the given target is
started. Otherwise, a local shell is started.  Uses enhanced ipython or
bpython shells, if available, falling back on the normal Python shell.

Optional arguments:

    :cmd -- A string of valid Python code to be executed on the shell. The
    shell will exit after the code is executed.

    :path -- The path to the remote_api handler on the deployment
    target. Defaults to '/remote_api'.

Usage:

    # A local shell
    fab shell

    # A remote_api shell on production
    fab production shell

    # Run a command directly on production
    fab production shell:cmd="memcache.flush_all()"
"""

    # Import the modules we want to make available by default
    from google.appengine.api import urlfetch
    from google.appengine.api import memcache
    from google.appengine.ext import deferred
    from google.appengine.ext import db

    # Build a dict usable as locals() from the modules we want to use
    modname = lambda m: m.__name__.rpartition('.')[-1]
    mods = [db, deferred, memcache, sys, urlfetch]
    mods = dict((modname(m), m) for m in mods)

    # The banner for either kind of shell
    banner = 'Python %s\n\nImported modules: %s\n' % (
        sys.version, ', '.join(sorted(mods)))

    # Are we running a remote shell?
    if hasattr(env, 'gae'):
        # Add more info to the banner
        loc = '%s%s' % (env.gae.host, path)
        banner = '\nApp Engine remote_api shell\n%s\n\n%s' % (loc, banner)
        # Actually prepare the remote shell
        utils.prep_remote_shell(path=path)

    # Otherwise, we're starting a local shell
    else:
        utils.prep_local_shell()

    # Define the kinds of shells we're going to try to run
    def ipython_shell():
        import IPython
        shell = IPython.Shell.IPShell(argv=[], user_ns=mods)
        shell.mainloop(banner=banner)

    def bpython_shell():
        from bpython import cli
        cli.main(args=[], banner=banner, locals_=mods)

    def plain_shell():
        sys.ps1 = '>>> '
        sys.ps2 = '... '
        code.interact(banner=banner, locals=mods)

    # If we have a command to run, run it.
    if cmd:
        print 'Running remote command: %s' % cmd
        exec cmd in mods

    # Otherwise, start an interactive shell
    else:
        try:
            ipython_shell()
        except ImportError:
            try:
                bpython_shell()
            except ImportError:
                plain_shell()