Make --import update existing entries where present
[jelmer/calypso.git] / calypso.py
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 #
4 # This file is part of Calypso Server - Calendar 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 os
40 import sys
41 import optparse
42
43 import calypso
44 import calypso.ical as ical
45
46 # Get command-line options
47 parser = optparse.OptionParser()
48 parser.add_option(
49     "-v", "--version", action="store_true",
50     default=False,
51     help="show version and exit")
52 parser.add_option(
53     "-d", "--daemon", action="store_true",
54     default=calypso.config.getboolean("server", "daemon"),
55     help="launch as daemon")
56 parser.add_option(
57     "-f", "--foreground", action="store_false", dest="daemon",
58     help="launch in foreground (opposite of --daemon)")
59 parser.add_option(
60     "-H", "--host",
61     default=calypso.config.get("server", "host"),
62     help="set server hostname")
63 parser.add_option(
64     "-p", "--port", type="int",
65     default=calypso.config.getint("server", "port"),
66     help="set server port")
67 parser.add_option(
68     "-s", "--ssl", action="store_true",
69     default=calypso.config.getboolean("server", "ssl"),
70     help="use SSL connection")
71 parser.add_option(
72     "-S", "--no-ssl", action="store_false", dest="ssl",
73     help="do not use SSL connection (opposite of --ssl)")
74 parser.add_option(
75     "-k", "--key",
76     default=calypso.config.get("server", "key"),
77     help="private key file ")
78 parser.add_option(
79     "-c", "--certificate",
80     default=calypso.config.get("server", "certificate"),
81     help="certificate file ")
82 parser.add_option(
83     "-i", "--import", dest="import_dest")
84     
85 (options, args) = parser.parse_args()
86
87 # Update Calypso configuration according to options
88 for option in parser.option_list:
89     key = option.dest
90     if key:
91         value = getattr(options, key)
92         calypso.config.set("server", key, value)
93
94 # Print version and exit if the option is given
95 if options.version:
96     print(calypso.VERSION)
97     sys.exit()
98
99 # Run import if requested
100 if options.import_dest:
101     try:
102         calendar = ical.Calendar(options.import_dest)
103     except Exception:
104         print "Cannot open calendar %s" % options.import_dest
105         sys.exit(1)
106     success = True
107     for arg in args:
108         if not calendar.import_file(arg):
109             success = False
110     if success:
111         sys.exit(0)
112     else:
113         sys.exit(1)
114
115 # Fork if Calypso is launched as daemon
116 if options.daemon:
117     if os.fork():
118         sys.exit()
119     sys.stdout = sys.stderr = open(os.devnull, "w")
120
121 # Launch calendar server
122 server_class = calypso.HTTPSServer if options.ssl else calypso.HTTPServer
123 server = server_class(
124     (options.host, options.port), calypso.CalendarHTTPHandler)
125 server.serve_forever(poll_interval=10)