__main__.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. # This file is part of Radicale - CalDAV and CardDAV server
  2. # Copyright © 2011-2017 Guillaume Ayoub
  3. # Copyright © 2017-2022 Unrud <unrud@outlook.com>
  4. # Copyright © 2024-2025 Peter Bieringer <pb@bieringer.de>
  5. #
  6. # This library is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This library is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. Radicale executable module.
  20. This module can be executed from a command line with ``$python -m radicale``.
  21. Uses the built-in WSGI server.
  22. """
  23. import argparse
  24. import contextlib
  25. import os
  26. import signal
  27. import socket
  28. import sys
  29. from types import FrameType
  30. from typing import List, Optional, cast
  31. from radicale import VERSION, config, item, log, server, storage, types
  32. from radicale.log import logger
  33. def run() -> None:
  34. """Run Radicale as a standalone server."""
  35. exit_signal_numbers = [signal.SIGTERM, signal.SIGINT]
  36. if sys.platform == "win32":
  37. exit_signal_numbers.append(signal.SIGBREAK)
  38. else:
  39. exit_signal_numbers.append(signal.SIGHUP)
  40. exit_signal_numbers.append(signal.SIGQUIT)
  41. # Raise SystemExit when signal arrives to run cleanup code
  42. # (like destructors, try-finish etc.), otherwise the process exits
  43. # without running any of them
  44. def exit_signal_handler(signal_number: int,
  45. stack_frame: Optional[FrameType]) -> None:
  46. sys.exit(1)
  47. for signal_number in exit_signal_numbers:
  48. signal.signal(signal_number, exit_signal_handler)
  49. log.setup()
  50. # Get command-line arguments
  51. # Configuration options are stored in dest with format "c:SECTION:OPTION"
  52. parser = argparse.ArgumentParser(
  53. prog="radicale", usage="%(prog)s [OPTIONS]", allow_abbrev=False)
  54. parser.add_argument("--version", action="version", version=VERSION)
  55. parser.add_argument("--verify-storage", action="store_true",
  56. help="check the storage for errors and exit")
  57. parser.add_argument("--verify-item", action="store", nargs=1,
  58. help="check the provided item file for errors and exit")
  59. parser.add_argument("-C", "--config",
  60. help="use specific configuration files", nargs="*")
  61. parser.add_argument("-D", "--debug", action="store_const", const="debug",
  62. dest="c:logging:level", default=argparse.SUPPRESS,
  63. help="print debug information")
  64. for section, section_data in config.DEFAULT_CONFIG_SCHEMA.items():
  65. if section.startswith("_"):
  66. continue
  67. assert ":" not in section # check field separator
  68. assert "-" not in section and "_" not in section # not implemented
  69. group_description = None
  70. if section_data.get("_allow_extra"):
  71. group_description = "additional options allowed"
  72. if section == "headers":
  73. group_description += " (e.g. --headers-Pragma=no-cache)"
  74. elif "type" in section_data:
  75. group_description = "backend specific options omitted"
  76. group = parser.add_argument_group(section, group_description)
  77. for option, data in section_data.items():
  78. if option.startswith("_"):
  79. continue
  80. kwargs = data.copy()
  81. long_name = "--%s-%s" % (section, option.replace("_", "-"))
  82. args: List[str] = list(kwargs.pop("aliases", ()))
  83. args.append(long_name)
  84. kwargs["dest"] = "c:%s:%s" % (section, option)
  85. kwargs["metavar"] = "VALUE"
  86. kwargs["default"] = argparse.SUPPRESS
  87. del kwargs["value"]
  88. with contextlib.suppress(KeyError):
  89. del kwargs["internal"]
  90. if kwargs["type"] == bool:
  91. del kwargs["type"]
  92. opposite_args = list(kwargs.pop("opposite_aliases", ()))
  93. opposite_args.append("--no%s" % long_name[1:])
  94. group.add_argument(*args, nargs="?", const="True", **kwargs)
  95. # Opposite argument
  96. kwargs["help"] = "do not %s (opposite of %s)" % (
  97. kwargs["help"], long_name)
  98. group.add_argument(*opposite_args, action="store_const",
  99. const="False", **kwargs)
  100. else:
  101. del kwargs["type"]
  102. group.add_argument(*args, **kwargs)
  103. args_ns, remaining_args = parser.parse_known_args()
  104. unrecognized_args = []
  105. while remaining_args:
  106. arg = remaining_args.pop(0)
  107. for section, data in config.DEFAULT_CONFIG_SCHEMA.items():
  108. if "type" not in data and not data.get("_allow_extra"):
  109. continue
  110. prefix = "--%s-" % section
  111. if arg.startswith(prefix):
  112. arg = arg[len(prefix):]
  113. break
  114. else:
  115. unrecognized_args.append(arg)
  116. continue
  117. value = ""
  118. if "=" in arg:
  119. arg, value = arg.split("=", maxsplit=1)
  120. elif remaining_args and not remaining_args[0].startswith("-"):
  121. value = remaining_args.pop(0)
  122. option = arg
  123. if not data.get("_allow_extra"): # preserve dash in HTTP header names
  124. option = option.replace("-", "_")
  125. vars(args_ns)["c:%s:%s" % (section, option)] = value
  126. if unrecognized_args:
  127. parser.error("unrecognized arguments: %s" %
  128. " ".join(unrecognized_args))
  129. # Preliminary configure logging
  130. with contextlib.suppress(ValueError):
  131. log.set_level(config.DEFAULT_CONFIG_SCHEMA["logging"]["level"]["type"](
  132. vars(args_ns).get("c:logging:level", "")), True)
  133. # Update Radicale configuration according to arguments
  134. arguments_config: types.MUTABLE_CONFIG = {}
  135. for key, value in vars(args_ns).items():
  136. if key.startswith("c:"):
  137. _, section, option = key.split(":", maxsplit=2)
  138. arguments_config[section] = arguments_config.get(section, {})
  139. arguments_config[section][option] = value
  140. try:
  141. configuration = config.load(config.parse_compound_paths(
  142. config.DEFAULT_CONFIG_PATH,
  143. os.environ.get("RADICALE_CONFIG"),
  144. os.pathsep.join(args_ns.config) if args_ns.config is not None
  145. else None))
  146. if arguments_config:
  147. configuration.update(arguments_config, "command line arguments")
  148. except Exception as e:
  149. logger.critical("Invalid configuration: %s", e, exc_info=True)
  150. sys.exit(1)
  151. # Configure logging
  152. log.set_level(cast(str, configuration.get("logging", "level")),
  153. configuration.get("logging", "backtrace_on_debug"),
  154. configuration.get("logging", "trace_on_debug"),
  155. configuration.get("logging", "trace_filter"))
  156. # Log configuration after logger is configured
  157. default_config_active = True
  158. for source, miss in configuration.sources():
  159. logger.info("%s %s", "Skipped missing/unreadable" if miss else "Loaded", source)
  160. if not miss and source != "default config":
  161. default_config_active = False
  162. if default_config_active:
  163. logger.warning("%s", "No config file found/readable - only default config is active")
  164. if args_ns.verify_storage:
  165. logger.info("Verifying storage")
  166. try:
  167. storage_ = storage.load(configuration)
  168. with storage_.acquire_lock("r"):
  169. if not storage_.verify():
  170. logger.critical("Storage verification failed")
  171. sys.exit(1)
  172. except Exception as e:
  173. logger.critical("An exception occurred during storage "
  174. "verification: %s", e, exc_info=True)
  175. sys.exit(1)
  176. return
  177. if args_ns.verify_item:
  178. encoding = configuration.get("encoding", "stock")
  179. logger.info("Item verification start using 'stock' encoding: %s", encoding)
  180. try:
  181. if not item.verify(args_ns.verify_item[0], encoding):
  182. logger.critical("Item verification failed")
  183. sys.exit(1)
  184. except Exception as e:
  185. logger.critical("An exception occurred during item "
  186. "verification: %s", e, exc_info=False)
  187. sys.exit(1)
  188. return
  189. # Create a socket pair to notify the server of program shutdown
  190. shutdown_socket, shutdown_socket_out = socket.socketpair()
  191. # Shutdown server when signal arrives
  192. def shutdown_signal_handler(signal_number: int,
  193. stack_frame: Optional[FrameType]) -> None:
  194. shutdown_socket.close()
  195. for signal_number in exit_signal_numbers:
  196. signal.signal(signal_number, shutdown_signal_handler)
  197. try:
  198. server.serve(configuration, shutdown_socket_out)
  199. except Exception as e:
  200. logger.critical("An exception occurred during server startup: %s", e,
  201. exc_info=False)
  202. sys.exit(1)
  203. if __name__ == "__main__":
  204. run()