allow collections deep inside git
[jelmer/calypso.git] / calypso.py
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 #
4 # This file is part of Calypso - CalDAV/CardDAV/WebDAV Server
5 # Copyright © 2011 Keith Packard
6 # Copyright © 2008-2011 Guillaume Ayoub
7 # Copyright © 2008 Nicolas Kandel
8 # Copyright © 2008 Pascal Halter
9 #
10 # This library is free software: you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation, either version 3 of the License, or
13 # (at your option) any later version.
14 #
15 # This library is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with Calypso.  If not, see <http://www.gnu.org/licenses/>.
22
23 # This file is just a script, allow [a-z0-9]* variable names
24 # pylint: disable-msg=C0103
25
26 # ``import calypso`` refers to the ``calypso`` module, not ``calypso.py`` 
27 # pylint: disable-msg=W0406
28
29 """
30 Calypso Server entry point.
31
32 Launch the Calypso Server according to configuration and command-line
33 arguments.
34
35 """
36
37 # TODO: Manage smart and configurable logs
38
39 import daemon
40 from daemon import pidlockfile
41 import logging
42 import optparse
43 import os
44 import sys
45
46 import calypso
47 import calypso.webdav as webdav
48
49 # Get command-line options
50 parser = optparse.OptionParser()
51 parser.add_option(
52     "-v", "--version", action="store_true",
53     default=False,
54     help="show version and exit")
55 parser.add_option(
56     "-d", "--daemon", action="store_true",
57     default=calypso.config.getboolean("server", "daemon"),
58     help="launch as daemon")
59 parser.add_option(
60     "-f", "--foreground", action="store_false", dest="daemon",
61     help="launch in foreground (opposite of --daemon)")
62 parser.add_option(
63     "-H", "--host",
64     default=calypso.config.get("server", "host"),
65     help="set server hostname")
66 parser.add_option(
67     "-p", "--port", type="int",
68     default=calypso.config.getint("server", "port"),
69     help="set server port")
70 parser.add_option(
71     "-s", "--ssl", action="store_true",
72     default=calypso.config.getboolean("server", "ssl"),
73     help="use SSL connection")
74 parser.add_option(
75     "-S", "--no-ssl", action="store_false", dest="ssl",
76     help="do not use SSL connection (opposite of --ssl)")
77 parser.add_option(
78     "-k", "--key",
79     default=calypso.config.get("server", "key"),
80     help="private key file ")
81 parser.add_option(
82     "-c", "--certificate",
83     default=calypso.config.get("server", "certificate"),
84     help="certificate file ")
85 parser.add_option(
86     "-i", "--import", dest="import_dest")
87 parser.add_option(
88     "-g", "--debug", action="store_true",
89     default=False,
90     help="enable debug logging")
91 parser.add_option(
92     "-P", "--pid-file", dest="pidfile",
93     default=calypso.config.get("server", "pidfile"),
94     help="set location of process-id file")
95     
96 (options, args) = parser.parse_args()
97
98 # Update Calypso configuration according to options
99 for option in parser.option_list:
100     key = option.dest
101     if key:
102         value = getattr(options, key)
103         calypso.config.set("server", key, value)
104
105 # Print version and exit if the option is given
106 if options.version:
107     print(calypso.VERSION)
108     sys.exit()
109
110 log = logging.getLogger()
111 ch = logging.StreamHandler()
112
113 # Handle debugging option and log levels
114 if options.debug:
115     log.setLevel(logging.DEBUG)
116     ch.setLevel(logging.DEBUG)
117     logging.basicConfig(level=logging.DEBUG)
118     log.debug("enable debugging")
119 else:
120     log.setLevel(logging.WARN)
121     ch.setLevel(logging.WARN)
122     logging.basicConfig(level=logging.WARN)
123     
124
125 # Run import if requested
126 if options.import_dest:
127     try:
128         collection = webdav.Collection(options.import_dest)
129     except Exception:
130         print "Cannot open collection %s" % options.import_dest
131         sys.exit(1)
132     success = True
133     for arg in args:
134         if not collection.import_file(arg):
135             success = False
136     if success:
137         sys.exit(0)
138     else:
139         sys.exit(1)
140
141 def run_server():
142     try:
143         # Launch server
144         server_class = calypso.HTTPSServer if options.ssl else calypso.HTTPServer
145         server = server_class(
146             (options.host, options.port), calypso.CollectionHTTPHandler)
147         server.serve_forever(poll_interval=10)
148     except KeyboardInterrupt:
149         server.socket.close()
150
151 # If foreground execution is requested, just run the server
152 if not options.daemon:
153     run_server()
154     sys.exit(0)
155
156 # Otherwise, daemonize Calypso
157 context = daemon.DaemonContext()
158 context.umask = 0o002
159 if options.pidfile:
160     # Generate a pidfile where requested
161     context.pidfile = pidlockfile.PIDLockFile(options.pidfile)
162 with context:
163     run_server()
164
165 # vim: set ts=4 sw=4 et si :