# TODO: Manage smart and configurable logs
import daemon
+import lockfile
import logging
import optparse
import os
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()
sys.exit(1)
def run_server():
- # Launch server
- 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)
+ 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:
# 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()