Source

trac-ticketlinks / scripts / tracd

Full commit
#!/usr/bin/env python
# -*- coding: iso8859-1 -*-
#
# Copyright (C) 2003, 2004, 2005 Edgewall Software
# Copyright (C) 2003, 2004, 2005 Jonas Borgström <jonas@edgewall.com>
#
# Trac 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.
#
# Trac 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., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# Author: Jonas Borgström <jonas@edgewall.com>

from trac.web.standalone import DigestAuth, TracHTTPServer

import getopt
import locale
import sys
import os

def usage():
    print 'usage: %s [options] <projenv> [projenv] ...' % sys.argv[0]
    print 'usage: %s [options] --env-parent-dir=[path]' % sys.argv[0]
    print '\nOptions:\n'
    print '-a --auth [project],[htdigest_file],[realm]'
    print '-p --port [port]\t\tPort number to use (default: 80)'
    print '-b --hostname [hostname]\tIP to bind to (default: \'\')'
    print '-d --daemonize\tRun tracd in the background as a daemon'
    print '-e --env-parent-dir <path>\tParent directory of the project environments'
    print
    sys.exit(1)

def main():
    locale.setlocale(locale.LC_ALL, '')
    port = 80
    hostname = ''
    auths = {}
    daemonize = 0
    env_parent_dir = None
    try:
        opts, args = getopt.getopt(sys.argv[1:], "a:p:b:de:",
                                   ["auth=", "port=", "hostname=","daemonize",
                                    "env-parent-dir="])
    except getopt.GetoptError, e:
        print e
        usage()

    for o, a in opts:
        if o in ("-a", "--auth"):
            info = a.split(',', 3)
            if len(info) != 3:
                usage()
            p, h, r = info
            auths[p] = DigestAuth(h, r)
        if o in ("-p", "--port"):
            port = int(a)
        elif o in ("-b", "--hostname"):
            hostname = a
        if o in ("-d","--daemonize"):
            daemonize = 1
        if o in ("-e", "--env-parent-dir"):
            env_parent_dir = a

    if not env_parent_dir and not args:
        usage()

    server_address = (hostname, port)
    httpd = TracHTTPServer(server_address, env_parent_dir, args, auths)

    try:
        if daemonize:
            pid=os.fork()
            if pid == 0:
                os.chdir(os.sep)
                os.setpgrp()
                # Redirect standard file descriptors
                sys.stdin = open('/dev/null', 'r')
                sys.stdout = open('/dev/null', 'w')
                sys.stderr = open('/dev/null', 'w')
            else:
                print 'tracd daemon running with pid: %d' % pid
                sys.exit(0)
        httpd.serve_forever()
    except OSError:
        sys.exit(1)

if __name__ == '__main__':
    main()