#!/usr/bin/python
# -*- coding: utf-8 -*-
#
-# This file is part of Calypso Server - Calendar Server
+# This file is part of Calypso - CalDAV/CardDAV/WebDAV Server
# Copyright © 2011 Keith Packard
# Copyright © 2008-2011 Guillaume Ayoub
# Copyright © 2008 Nicolas Kandel
# TODO: Manage smart and configurable logs
+import daemon
+import lockfile
import logging
+import optparse
import os
import sys
-import optparse
import calypso
-import calypso.ical as ical
+import calypso.webdav as webdav
# Get command-line options
-parser = optparse.OptionParser()
-parser.add_option(
- "-v", "--version", action="store_true",
- default=False,
- help="show version and exit")
+parser = optparse.OptionParser(version=calypso.VERSION)
parser.add_option(
"-d", "--daemon", action="store_true",
default=calypso.config.getboolean("server", "daemon"),
"-g", "--debug", action="store_true",
default=False,
help="enable debug logging")
+parser.add_option(
+ "-P", "--pid-file", dest="pidfile",
+ default=calypso.config.get("server", "pidfile"),
+ help="set location of process-id file")
(options, args) = parser.parse_args()
value = getattr(options, key)
calypso.config.set("server", key, value)
-# Print version and exit if the option is given
-if options.version:
- print(calypso.VERSION)
- sys.exit()
+log = logging.getLogger()
+ch = logging.StreamHandler()
-# Print version and exit if the option is given
+# Handle debugging option and log levels
if options.debug:
+ log.setLevel(logging.DEBUG)
+ ch.setLevel(logging.DEBUG)
logging.basicConfig(level=logging.DEBUG)
+ log.debug("enable debugging")
+else:
+ log.setLevel(logging.WARN)
+ ch.setLevel(logging.WARN)
+ logging.basicConfig(level=logging.WARN)
+
# Run import if requested
if options.import_dest:
try:
- calendar = ical.Calendar(options.import_dest)
+ collection = webdav.Collection(options.import_dest)
except Exception:
- print "Cannot open calendar %s" % options.import_dest
+ print "Cannot open collection %s" % options.import_dest
sys.exit(1)
success = True
for arg in args:
- if not calendar.import_file(arg):
+ if not collection.import_file(arg):
success = False
if success:
sys.exit(0)
else:
sys.exit(1)
-# Fork if Calypso is launched as daemon
-if options.daemon:
- if os.fork():
- sys.exit()
- sys.stdout = sys.stderr = open(os.devnull, "w")
-
-# Launch calendar server
-server_class = calypso.HTTPSServer if options.ssl else calypso.HTTPServer
-server = server_class(
- (options.host, options.port), calypso.CalendarHTTPHandler)
-server.serve_forever(poll_interval=10)
+def run_server():
+ try:
+ # Launch server
+ log.debug("Starting HTTP%s server on %s:%d" % ("S" if options.ssl else "",
+ options.host if options.host else "*",
+ options.port))
+ server_class = calypso.HTTPSServer if options.ssl else calypso.HTTPServer
+ server = server_class(
+ (options.host, options.port), calypso.CollectionHTTPHandler)
+ server.serve_forever(poll_interval=10)
+ except KeyboardInterrupt:
+ server.socket.close()
+
+# If foreground execution is requested, just run the server
+if not options.daemon:
+ run_server()
+ sys.exit(0)
+
+# Otherwise, daemonize Calypso
+context = daemon.DaemonContext()
+context.umask = 0o002
+if options.pidfile:
+ from lockfile import pidlockfile
+ # Generate a pidfile where requested
+ context.pidfile = pidlockfile.PIDLockFile(options.pidfile)
+with context:
+ run_server()
+
+# vim: set ts=4 sw=4 et si :