radicale.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. "-H", "--host",
  47. default=radicale.config.get("server", "host"),
  48. help="set server hostname")
  49. parser.add_option(
  50. "-p", "--port", type="int",
  51. default=radicale.config.getint("server", "port"),
  52. help="set server port")
  53. parser.add_option(
  54. "-s", "--ssl", action="store_true",
  55. default=radicale.config.getboolean("server", "ssl"),
  56. help="use SSL connection")
  57. parser.add_option(
  58. "-k", "--key",
  59. default=radicale.config.get("server", "key"),
  60. help="private key file ")
  61. parser.add_option(
  62. "-c", "--certificate",
  63. default=radicale.config.get("server", "certificate"),
  64. help="certificate file ")
  65. options = parser.parse_args()[0]
  66. # Update Radicale configuration according to options
  67. for option in parser.option_list:
  68. key = option.dest
  69. if key:
  70. value = getattr(options, key)
  71. radicale.config.set("server", key, value)
  72. # Print version and exit if the option is given
  73. if options.version:
  74. print(radicale.VERSION)
  75. sys.exit()
  76. # Fork if Radicale is launched as daemon
  77. if options.daemon:
  78. if os.fork():
  79. sys.exit()
  80. sys.stdout = sys.stderr = open(os.devnull, "w")
  81. # Launch calendar server
  82. server_class = radicale.HTTPSServer if options.ssl else radicale.HTTPServer
  83. server = server_class(
  84. (options.host, options.port), radicale.CalendarHTTPHandler)
  85. server.serve_forever()