__main__.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. # This file is part of Radicale Server - Calendar Server
  2. # Copyright © 2011-2017 Guillaume Ayoub
  3. # Copyright © 2017-2018 Unrud <unrud@outlook.com>
  4. #
  5. # This library is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This library is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  17. """
  18. Radicale executable module.
  19. This module can be executed from a command line with ``$python -m radicale``.
  20. """
  21. import argparse
  22. import os
  23. from radicale import VERSION, config, log, storage
  24. from radicale.log import logger
  25. from radicale.server import serve
  26. def run():
  27. """Run Radicale as a standalone server."""
  28. log.setup()
  29. # Get command-line arguments
  30. parser = argparse.ArgumentParser(usage="radicale [OPTIONS]")
  31. parser.add_argument("--version", action="version", version=VERSION)
  32. parser.add_argument("--verify-storage", action="store_true",
  33. help="check the storage for errors and exit")
  34. parser.add_argument(
  35. "-C", "--config", help="use a specific configuration file")
  36. parser.add_argument("-D", "--debug", action="store_true",
  37. help="print debug information")
  38. groups = {}
  39. for section, values in config.INITIAL_CONFIG.items():
  40. group = parser.add_argument_group(section)
  41. groups[group] = []
  42. for option, data in values.items():
  43. kwargs = data.copy()
  44. long_name = "--{0}-{1}".format(
  45. section, option.replace("_", "-"))
  46. args = kwargs.pop("aliases", [])
  47. args.append(long_name)
  48. kwargs["dest"] = "{0}_{1}".format(section, option)
  49. groups[group].append(kwargs["dest"])
  50. del kwargs["value"]
  51. if "internal" in kwargs:
  52. del kwargs["internal"]
  53. if kwargs["type"] == bool:
  54. del kwargs["type"]
  55. kwargs["action"] = "store_const"
  56. kwargs["const"] = "True"
  57. opposite_args = kwargs.pop("opposite", [])
  58. opposite_args.append("--no{0}".format(long_name[1:]))
  59. group.add_argument(*args, **kwargs)
  60. kwargs["const"] = "False"
  61. kwargs["help"] = "do not {0} (opposite of {1})".format(
  62. kwargs["help"], long_name)
  63. group.add_argument(*opposite_args, **kwargs)
  64. else:
  65. group.add_argument(*args, **kwargs)
  66. args = parser.parse_args()
  67. # Preliminary configure logging
  68. if args.debug:
  69. args.logging_level = "debug"
  70. if args.logging_level is not None:
  71. log.set_level(args.logging_level)
  72. if args.config is not None:
  73. config_paths = [args.config] if args.config else []
  74. ignore_missing_paths = False
  75. else:
  76. config_paths = ["/etc/radicale/config",
  77. os.path.expanduser("~/.config/radicale/config")]
  78. if "RADICALE_CONFIG" in os.environ:
  79. config_paths.append(os.environ["RADICALE_CONFIG"])
  80. ignore_missing_paths = True
  81. try:
  82. configuration = config.load(config_paths,
  83. ignore_missing_paths=ignore_missing_paths)
  84. except Exception as e:
  85. log.error("Invalid configuration: %s", e, exc_info=True)
  86. exit(1)
  87. # Update Radicale configuration according to arguments
  88. for group, actions in groups.items():
  89. section = group.title
  90. for action in actions:
  91. value = getattr(args, action)
  92. if value is not None:
  93. configuration.set(section, action.split('_', 1)[1], value)
  94. # Configure logging
  95. log.set_level(configuration.get("logging", "level"))
  96. if args.verify_storage:
  97. logger.info("Verifying storage")
  98. try:
  99. Collection = storage.load(configuration)
  100. with Collection.acquire_lock("r"):
  101. if not Collection.verify():
  102. logger.error("Storage verifcation failed")
  103. exit(1)
  104. except Exception as e:
  105. logger.error("An exception occurred during storage verification: "
  106. "%s", e, exc_info=True)
  107. exit(1)
  108. return
  109. try:
  110. serve(configuration)
  111. except Exception as e:
  112. logger.error("An exception occurred during server startup: %s", e,
  113. exc_info=True)
  114. exit(1)
  115. if __name__ == "__main__":
  116. run()