~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to serve-branches

  • Committer: Matt Nordhoff
  • Date: 2008-10-27 02:05:58 UTC
  • mto: This revision was merged to the branch mainline in revision 234.
  • Revision ID: mnordhoff@mattnordhoff.com-20081027020558-1nro7mqn9bz09o0p
Don't forget to set absolutepath.

(I converted a tab to spaces too.)

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
 
32
from loggerhead.apps.error import ErrorHandlerApp
31
33
 
32
34
 
33
35
def command_line_parser():
35
37
    parser.set_defaults(
36
38
        user_dirs=False,
37
39
        show_version=False,
 
40
        log_folder=None,
38
41
        )
39
42
    parser.add_option("--user-dirs", action="store_true", dest="user_dirs",
40
43
                      help="Serve user directories as ~user.")
41
44
    parser.add_option("--trunk-dir", metavar="DIR",
42
45
                      help="The directory that contains the trunk branches.")
 
46
    parser.add_option("--port", dest="user_port",
 
47
                      help=("Port Loggerhead should listen on "
 
48
                            "(defaults to 8080)."))
 
49
    parser.add_option("--host", dest="user_host",
 
50
                      help="Host Loggerhead should listen on.")
 
51
    parser.add_option("--prefix", dest="user_prefix",
 
52
                      help="Specify host prefix.")
 
53
    parser.add_option("--reload", action="store_true", dest="reload",
 
54
                      help="Restarts the application when changing python"
 
55
                           " files. Only used for development purposes.")
 
56
    parser.add_option('--log-folder', dest="log_folder",
 
57
                      type=str, help="The directory to place log files in.")
43
58
    parser.add_option("--version", action="store_true", dest="show_version",
44
59
                      help="Print the software version and exit")
45
60
    return parser
46
61
 
47
62
 
48
63
def main(args):
49
 
    logging.basicConfig()
50
 
    logging.getLogger().setLevel(logging.DEBUG)
51
 
 
52
64
    parser = command_line_parser()
53
65
    (options, args) = parser.parse_args(sys.argv[1:])
54
66
 
72
84
        print "--trunk-dir is only valid with --user-dirs"
73
85
        sys.exit(1)
74
86
 
 
87
    if options.reload:
 
88
        if Reloader.is_installed():
 
89
            Reloader.install()
 
90
        else:
 
91
            return Reloader.restart_with_reloader()
 
92
 
75
93
    if options.user_dirs:
76
94
        if not options.trunk_dir:
77
95
            print "You didn't specify a directory for the trunk directories."
79
97
        app = UserBranchesFromFileSystemRoot(path, options.trunk_dir)
80
98
    else:
81
99
        app = BranchesFromFileSystemRoot(path)
 
100
 
 
101
    # setup_logging()
 
102
    logging.basicConfig()
 
103
    logging.getLogger('').setLevel(logging.DEBUG)
 
104
    logger = getattr(app, 'log', logging.getLogger('loggerhead'))
 
105
    if options.log_folder:
 
106
        logfile_path = os.path.join(options.log_folder, 'serve-branches.log')
 
107
    else:
 
108
        logfile_path = 'serve-branches.log'
 
109
    logfile = logging.FileHandler(logfile_path, 'a')
 
110
    formatter = logging.Formatter('%(asctime)s %(levelname)-8s %(name)s:'
 
111
                                  ' %(message)s')
 
112
    logfile.setFormatter(formatter)
 
113
    logfile.setLevel(logging.DEBUG)
 
114
    logger.addHandler(logfile)
 
115
    # setup_logging() #end
 
116
    app = ErrorHandlerApp(app)
82
117
    app = HTTPExceptionHandler(app)
83
 
    app = TransLogger(app)
 
118
    app = TransLogger(app, logger=logger)
 
119
 
 
120
    if not options.user_prefix:
 
121
        prefix = '/'
 
122
    else:
 
123
        prefix = options.user_prefix
84
124
 
85
125
    try:
86
126
        from paste.deploy.config import PrefixMiddleware
87
127
    except ImportError:
88
128
        pass
89
129
    else:
90
 
        app = PrefixMiddleware(app)
91
 
 
92
 
    httpserver.serve(app, host='0.0.0.0', port='8080')
 
130
        app = PrefixMiddleware(app, prefix=prefix)
 
131
 
 
132
    if not options.user_port:
 
133
        port = '8080'
 
134
    else:
 
135
        port = options.user_port
 
136
 
 
137
    if not options.user_host:
 
138
        host = '0.0.0.0'
 
139
    else:
 
140
        host = options.user_host
 
141
 
 
142
    httpserver.serve(app, host=host, port=port)
93
143
 
94
144
 
95
145
if __name__ == "__main__":