~loggerhead-team/loggerhead/trunk-rich

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
#!/usr/bin/env python
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

"""Search for branches underneath a directory and serve them all."""

import logging
import os
import sys

from optparse import OptionParser

from paste import httpserver
from paste.httpexceptions import HTTPExceptionHandler
from paste.translogger import TransLogger

from loggerhead import __version__
from loggerhead.apps.filesystem import (
    BranchesFromFileSystemRoot, UserBranchesFromFileSystemRoot)


def command_line_parser():
    parser = OptionParser("%prog [options] <path>")
    parser.set_defaults(
        user_dirs=False,
        show_version=False,
        )
    parser.add_option("--user-dirs", action="store_true", dest="user_dirs",
                      help="Serve user directories as ~user.")
    parser.add_option("--trunk-dir", metavar="DIR",
                      help="The directory that contains the trunk branches.")
    parser.add_option("--version", action="store_true", dest="show_version",
                      help="Print the software version and exit")
    return parser


def main(args):
    logging.basicConfig()
    logging.getLogger().setLevel(logging.DEBUG)

    parser = command_line_parser()
    (options, args) = parser.parse_args(sys.argv[1:])

    if options.show_version:
        print "loggerhead %s" % __version__
        sys.exit(0)

    if len(args) > 1:
        parser.print_help()
        sys.exit(1)
    elif len(args) == 1:
        [path] = args
    else:
        path = '.'

    if not os.path.isdir(path):
        print "%s is not a directory" % path
        sys.exit(1)

    if options.trunk_dir and not options.user_dirs:
        print "--trunk-dir is only valid with --user-dirs"
        sys.exit(1)

    if options.user_dirs:
        if not options.trunk_dir:
            print "You didn't specify a directory for the trunk directories."
            sys.exit(1)
        app = UserBranchesFromFileSystemRoot(path, options.trunk_dir)
    else:
        app = BranchesFromFileSystemRoot(path)
    app = HTTPExceptionHandler(app)
    app = TransLogger(app)

    try:
        from paste.deploy.config import PrefixMiddleware
    except ImportError:
        pass
    else:
        app = PrefixMiddleware(app)

    httpserver.serve(app, host='0.0.0.0', port='8080')


if __name__ == "__main__":
    main(sys.argv)