-
Notifications
You must be signed in to change notification settings - Fork 0
/
calypso.py
executable file
·145 lines (127 loc) · 4.17 KB
/
calypso.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# This file is part of Calypso - CalDAV/CardDAV/WebDAV Server
# Copyright © 2011 Keith Packard
# Copyright © 2008-2011 Guillaume Ayoub
# Copyright © 2008 Nicolas Kandel
# Copyright © 2008 Pascal Halter
#
# This library is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Calypso. If not, see <http://www.gnu.org/licenses/>.
# This file is just a script, allow [a-z0-9]* variable names
# pylint: disable-msg=C0103
# ``import calypso`` refers to the ``calypso`` module, not ``calypso.py``
# pylint: disable-msg=W0406
"""
Calypso Server entry point.
Launch the Calypso Server according to configuration and command-line
arguments.
"""
# TODO: Manage smart and configurable logs
import logging
import os
import sys
import optparse
import calypso
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.add_option(
"-d", "--daemon", action="store_true",
default=calypso.config.getboolean("server", "daemon"),
help="launch as daemon")
parser.add_option(
"-f", "--foreground", action="store_false", dest="daemon",
help="launch in foreground (opposite of --daemon)")
parser.add_option(
"-H", "--host",
default=calypso.config.get("server", "host"),
help="set server hostname")
parser.add_option(
"-p", "--port", type="int",
default=calypso.config.getint("server", "port"),
help="set server port")
parser.add_option(
"-s", "--ssl", action="store_true",
default=calypso.config.getboolean("server", "ssl"),
help="use SSL connection")
parser.add_option(
"-S", "--no-ssl", action="store_false", dest="ssl",
help="do not use SSL connection (opposite of --ssl)")
parser.add_option(
"-k", "--key",
default=calypso.config.get("server", "key"),
help="private key file ")
parser.add_option(
"-c", "--certificate",
default=calypso.config.get("server", "certificate"),
help="certificate file ")
parser.add_option(
"-i", "--import", dest="import_dest")
parser.add_option(
"-g", "--debug", action="store_true",
default=False,
help="enable debug logging")
(options, args) = parser.parse_args()
# Update Calypso configuration according to options
for option in parser.option_list:
key = option.dest
if key:
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
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:
collection = webdav.Collection(options.import_dest)
except Exception:
print "Cannot open collection %s" % options.import_dest
sys.exit(1)
success = True
for arg in args:
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 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)