__main__.py 4.7 KB

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