~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to serve-branches

  • Committer: Guillermo Gonzalez
  • Date: 2008-09-10 00:02:06 UTC
  • mto: This revision was merged to the branch mainline in revision 221.
  • Revision ID: guillo.gonzo@gmail.com-20080910000206-kt35kgrzu8y4472v
 * update NEWS and serve-branches man page 
 * refactored all reload logic into loggerhead.util.Reloader class

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
from loggerhead import __version__
29
29
from loggerhead.apps.filesystem import (
30
30
    BranchesFromFileSystemRoot, UserBranchesFromFileSystemRoot)
 
31
from loggerhead.util import Reloader
31
32
 
32
 
_reloader_environ_key = 'PYTHON_RELOADER_SHOULD_RUN'
33
33
 
34
34
def command_line_parser():
35
35
    parser = OptionParser("%prog [options] <path>")
48
48
    parser.add_option("--prefix", dest="user_prefix",
49
49
                      help="Specify host prefix.")
50
50
    parser.add_option("--reload", action="store_true", dest="reload",
51
 
                      help="Use a file monitor to restart the application")
 
51
                      help="Restarts the application when changing python"
 
52
                           " files. Only used for development purposes.")
52
53
    parser.add_option("--version", action="store_true", dest="show_version",
53
54
                      help="Print the software version and exit")
54
55
    return parser
82
83
        sys.exit(1)
83
84
        
84
85
    if options.reload:
85
 
        if os.environ.get(_reloader_environ_key):
86
 
            from paste import reloader
87
 
            reloader.install(int(1))
 
86
        if Reloader.is_installed():
 
87
            Reloader.install()
88
88
        else:
89
 
            return restart_with_reloader()
 
89
            return Reloader.restart_with_reloader()
90
90
 
91
91
    if options.user_dirs:
92
92
        if not options.trunk_dir:
121
121
        host = options.user_host
122
122
 
123
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
158
124
 
159
125
 
160
126
if __name__ == "__main__":