~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to serve-branches

  • Committer: Guillermo Gonzalez
  • Date: 2008-09-09 22:45:53 UTC
  • mto: This revision was merged to the branch mainline in revision 221.
  • Revision ID: guillo.gonzo@gmail.com-20080909224553-eev34llmsdwwnjdl
 * added --reload option

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
from loggerhead.apps.filesystem import (
30
30
    BranchesFromFileSystemRoot, UserBranchesFromFileSystemRoot)
31
31
 
 
32
_reloader_environ_key = 'PYTHON_RELOADER_SHOULD_RUN'
32
33
 
33
34
def command_line_parser():
34
35
    parser = OptionParser("%prog [options] <path>")
46
47
                      help="Host Loggerhead should listen on.")
47
48
    parser.add_option("--prefix", dest="user_prefix",
48
49
                      help="Specify host prefix.")
 
50
    parser.add_option("--reload", action="store_true", dest="reload",
 
51
                      help="Use a file monitor to restart the application")
49
52
    parser.add_option("--version", action="store_true", dest="show_version",
50
53
                      help="Print the software version and exit")
51
54
    return parser
77
80
    if options.trunk_dir and not options.user_dirs:
78
81
        print "--trunk-dir is only valid with --user-dirs"
79
82
        sys.exit(1)
 
83
        
 
84
    if options.reload:
 
85
        if os.environ.get(_reloader_environ_key):
 
86
            from paste import reloader
 
87
            reloader.install(int(1))
 
88
        else:
 
89
            return restart_with_reloader()
80
90
 
81
91
    if options.user_dirs:
82
92
        if not options.trunk_dir:
111
121
        host = options.user_host
112
122
 
113
123
    httpserver.serve(app, host=host, port=port)
 
124
    
 
125
 
 
126
def restart_with_reloader():
 
127
    print 'Starting subprocess with file monitor'
 
128
    while 1:
 
129
        args = [sys.executable] + sys.argv
 
130
        new_environ = os.environ.copy()
 
131
        new_environ[_reloader_environ_key] = 'true'
 
132
        proc = None
 
133
        try:
 
134
            try:
 
135
                import paste.script.serve
 
136
                import subprocess
 
137
                paste.script.serve._turn_sigterm_into_systemexit()
 
138
                proc = subprocess.Popen(args, env=new_environ)
 
139
                exit_code = proc.wait()
 
140
                proc = None
 
141
            except KeyboardInterrupt:
 
142
                print '^C caught in monitor process'
 
143
                return 1
 
144
        finally:
 
145
            if (proc is not None
 
146
                and hasattr(os, 'kill')):
 
147
                import signal
 
148
                try:
 
149
                    os.kill(proc.pid, signal.SIGTERM)
 
150
                except (OSError, IOError):
 
151
                    pass
 
152
            
 
153
        # Reloader always exits with code 3; but if we are
 
154
        # a monitor, any exit code will restart
 
155
        if exit_code != 3:
 
156
            return exit_code
 
157
        print '-'*20, 'Restarting', '-'*20
114
158
 
115
159
 
116
160
if __name__ == "__main__":