2 # -*- coding: utf-8 -*-
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
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.
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.
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/>.
23 # This file is just a script, allow [a-z0-9]* variable names
24 # pylint: disable-msg=C0103
26 # ``import calypso`` refers to the ``calypso`` module, not ``calypso.py``
27 # pylint: disable-msg=W0406
30 Calypso Server entry point.
32 Launch the Calypso Server according to configuration and command-line
37 # TODO: Manage smart and configurable logs
40 from daemon import pidlockfile
47 import calypso.webdav as webdav
49 # Get command-line options
50 parser = optparse.OptionParser()
52 "-v", "--version", action="store_true",
54 help="show version and exit")
56 "-d", "--daemon", action="store_true",
57 default=calypso.config.getboolean("server", "daemon"),
58 help="launch as daemon")
60 "-f", "--foreground", action="store_false", dest="daemon",
61 help="launch in foreground (opposite of --daemon)")
64 default=calypso.config.get("server", "host"),
65 help="set server hostname")
67 "-p", "--port", type="int",
68 default=calypso.config.getint("server", "port"),
69 help="set server port")
71 "-s", "--ssl", action="store_true",
72 default=calypso.config.getboolean("server", "ssl"),
73 help="use SSL connection")
75 "-S", "--no-ssl", action="store_false", dest="ssl",
76 help="do not use SSL connection (opposite of --ssl)")
79 default=calypso.config.get("server", "key"),
80 help="private key file ")
82 "-c", "--certificate",
83 default=calypso.config.get("server", "certificate"),
84 help="certificate file ")
86 "-i", "--import", dest="import_dest")
88 "-g", "--debug", action="store_true",
90 help="enable debug logging")
92 "-P", "--pid-file", dest="pidfile",
93 default=calypso.config.get("server", "pidfile"),
94 help="set location of process-id file")
96 (options, args) = parser.parse_args()
98 # Update Calypso configuration according to options
99 for option in parser.option_list:
102 value = getattr(options, key)
103 calypso.config.set("server", key, value)
105 # Print version and exit if the option is given
107 print(calypso.VERSION)
110 log = logging.getLogger()
111 ch = logging.StreamHandler()
113 # Handle debugging option and log levels
115 log.setLevel(logging.DEBUG)
116 ch.setLevel(logging.DEBUG)
117 logging.basicConfig(level=logging.DEBUG)
118 log.debug("enable debugging")
120 log.setLevel(logging.WARN)
121 ch.setLevel(logging.WARN)
122 logging.basicConfig(level=logging.WARN)
125 # Run import if requested
126 if options.import_dest:
128 collection = webdav.Collection(options.import_dest)
130 print "Cannot open collection %s" % options.import_dest
134 if not collection.import_file(arg):
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()
151 # If foreground execution is requested, just run the server
152 if not options.daemon:
156 # Otherwise, daemonize Calypso
157 context = daemon.DaemonContext()
158 context.umask = 0o002
160 # Generate a pidfile where requested
161 context.pidfile = pidlockfile.PIDLockFile(options.pidfile)
165 # vim: set ts=4 sw=4 et si :