radicale.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # This file is part of Radicale Server - Calendar Server
  5. # Copyright © 2008-2011 Guillaume Ayoub
  6. # Copyright © 2008 Nicolas Kandel
  7. # Copyright © 2008 Pascal Halter
  8. #
  9. # This library is free software: you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation, either version 3 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This library is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  21. # This file is just a script, allow [a-z0-9]* variable names
  22. # pylint: disable-msg=C0103
  23. # ``import radicale`` refers to the ``radicale`` module, not ``radicale.py``
  24. # pylint: disable-msg=W0406
  25. """
  26. Radicale Server entry point.
  27. Launch the Radicale Server according to configuration and command-line
  28. arguments.
  29. """
  30. # TODO: Manage smart and configurable logs
  31. import os
  32. import sys
  33. import optparse
  34. import radicale
  35. # Get command-line options
  36. parser = optparse.OptionParser()
  37. parser.add_option(
  38. "-v", "--version", action="store_true",
  39. default=False,
  40. help="show version and exit")
  41. parser.add_option(
  42. "-d", "--daemon", action="store_true",
  43. default=radicale.config.getboolean("server", "daemon"),
  44. help="launch as daemon")
  45. parser.add_option(
  46. "-f", "--foreground", action="store_false", dest="daemon",
  47. help="launch in foreground (opposite of --daemon)")
  48. parser.add_option(
  49. "-H", "--host",
  50. default=radicale.config.get("server", "host"),
  51. help="set server hostname")
  52. parser.add_option(
  53. "-p", "--port", type="int",
  54. default=radicale.config.getint("server", "port"),
  55. help="set server port")
  56. parser.add_option(
  57. "-s", "--ssl", action="store_true",
  58. default=radicale.config.getboolean("server", "ssl"),
  59. help="use SSL connection")
  60. parser.add_option(
  61. "-S", "--no-ssl", action="store_false", dest="ssl",
  62. help="do not use SSL connection (opposite of --ssl)")
  63. parser.add_option(
  64. "-k", "--key",
  65. default=radicale.config.get("server", "key"),
  66. help="private key file ")
  67. parser.add_option(
  68. "-c", "--certificate",
  69. default=radicale.config.get("server", "certificate"),
  70. help="certificate file ")
  71. options = parser.parse_args()[0]
  72. # Update Radicale configuration according to options
  73. for option in parser.option_list:
  74. key = option.dest
  75. if key:
  76. value = getattr(options, key)
  77. radicale.config.set("server", key, value)
  78. # Print version and exit if the option is given
  79. if options.version:
  80. print(radicale.VERSION)
  81. sys.exit()
  82. # Fork if Radicale is launched as daemon
  83. if options.daemon:
  84. if os.fork():
  85. sys.exit()
  86. sys.stdout = sys.stderr = open(os.devnull, "w")
  87. # Launch calendar server
  88. server_class = radicale.HTTPSServer if options.ssl else radicale.HTTPServer
  89. server = server_class(
  90. (options.host, options.port), radicale.CalendarHTTPHandler)
  91. server.serve_forever()