radicale.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # This file is part of Radicale Server - Calendar Server
  5. # Copyright © 2008-2010 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 Serve 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. "-k", "--key",
  62. default=radicale.config.get("server", "key"),
  63. help="private key file ")
  64. parser.add_option(
  65. "-c", "--certificate",
  66. default=radicale.config.get("server", "certificate"),
  67. help="certificate file ")
  68. options = parser.parse_args()[0]
  69. # Update Radicale configuration according to options
  70. for option in parser.option_list:
  71. key = option.dest
  72. if key:
  73. value = getattr(options, key)
  74. radicale.config.set("server", key, value)
  75. # Print version and exit if the option is given
  76. if options.version:
  77. print(radicale.VERSION)
  78. sys.exit()
  79. # Fork if Radicale is launched as daemon
  80. if options.daemon:
  81. if os.fork():
  82. sys.exit()
  83. sys.stdout = sys.stderr = open(os.devnull, "w")
  84. # Launch calendar server
  85. server_class = radicale.HTTPSServer if options.ssl else radicale.HTTPServer
  86. server = server_class(
  87. (options.host, options.port), radicale.CalendarHTTPHandler)
  88. server.serve_forever()