Steven Osborn
"I would love to change the world, but they won't give me the source code".

Django manage.py wrapper script for Google App Engine

April 10, 2008 – 11:54 pm

I’m used to running mange.py in my Django apps and I kept forgetting the name of the Google App Engine equivalents. My script only knows how to do two things at the moment, “runserver” and “publish” but now I can manage everything the way I’m accustomed to.

Update: Aprill 11 - Added toggle_debug, automatically switches debug off when publishing and back on for development.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env python
#
# Simple convience script for common publishing and develop tasks
# This script expects dev_appserver.py to be in your $PATH
import os, sys, re
 
def main(argv):
 
    command = ""
 
    try:
        command = argv[1]
    except:
        usage()
        exit(2)       
 
    if command == 'publish':
        publish()
    elif command == 'publish-debug':
        publish(True)
    elif command == 'runserver':
        runserver()
    elif command == "--help" or command == "-h":
        usage()
        exit(0)
 
def usage():
    print """
    Usage: python manage.py [command] <options>
 
    Commands:
        publish - 'updates' Google App Engine with your current version
        publish-debug - 'updates' Goolge App Engine with debug turned on
        runserver - launches the development server on port 8080
 
    Options:
        --help, -h - Prints this screen
 
    For more information on a particular command, run [command] --help
    """
def get_gae_dir():
    """ Locate dev_appserver.py and return the directory is in. """
 
    # There's probably a smarter way to do this...
    script = 'dev_appserver.py'
    stdin, stdout = os.popen2('which %s'%script)
    xpath = stdout.read().strip()
    stdout.close()
    stdin.close()
 
    # If the file is a symlink, find out where the actual file lives.
    try:
        tmpdir = os.readlink(xpath)
    except OSError:
        tmpdir = xpath
 
    return os.path.abspath(os.path.dirname(tmpdir))
 
def toggle_debug(status):
    st = 'False'
    if status:
        st = 'True'
 
    os.rename('settings.py','settings.py.bak')
    orig = open('settings.py.bak').read()
    p = re.compile('DEBUG\s=\s(True|False)')
    n = p.sub('DEBUG = %s'%st,orig)
    nw = open('settings.py','w')
    nw.write(n)
    nw.close()    
 
def gae_parent(script):
 
    DIR_PATH = get_gae_dir()
    DEV_APPSERVER_PATH = os.path.join(DIR_PATH,'google/appengine/tools/%s'%script)
 
    EXTRA_PATHS = [
      DIR_PATH,
      os.path.join(DIR_PATH, 'lib', 'django'),
      os.path.join(DIR_PATH, 'lib', 'webob'),
      os.path.join(DIR_PATH, 'lib', 'yaml', 'lib'),
    ]
 
    sys.path = EXTRA_PATHS + sys.path
    execfile(DEV_APPSERVER_PATH, globals())
 
def publish(debug=False):
 
    if(debug):
        toggle_debug(True)
    else:
        toggle_debug(False)
 
    sys.argv[1] = "update"
    try:
        sys.argv[2] = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
    except IndexError:
        sys.argv.append(os.path.abspath(os.path.dirname(os.path.realpath(__file__))))
 
    gae_parent('appcfg.py')
 
def runserver():
    toggle_debug(True)
    sys.argv[1] = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
    gae_parent('dev_appserver_main.py')
 
if __name__ == '__main__':
    main(sys.argv)
  1. 1 Trackback(s)

  2. Apr 11, 2008: Django Google App Engine Snippets « Hone Watson Bookmarks

You must be logged in to post a comment.