__main__.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. groups = {}
  36. for section, values in config.INITIAL_CONFIG.items():
  37. group = parser.add_argument_group(section)
  38. groups[group] = []
  39. for option, data in values.items():
  40. kwargs = data.copy()
  41. long_name = "--{0}-{1}".format(
  42. section, option.replace("_", "-"))
  43. args = kwargs.pop("aliases", [])
  44. args.append(long_name)
  45. kwargs["dest"] = "{0}_{1}".format(section, option)
  46. groups[group].append(kwargs["dest"])
  47. del kwargs["value"]
  48. if "internal" in kwargs:
  49. del kwargs["internal"]
  50. if kwargs["type"] == bool:
  51. del kwargs["type"]
  52. kwargs["action"] = "store_const"
  53. kwargs["const"] = "True"
  54. opposite_args = kwargs.pop("opposite", [])
  55. opposite_args.append("--no{0}".format(long_name[1:]))
  56. group.add_argument(*args, **kwargs)
  57. kwargs["const"] = "False"
  58. kwargs["help"] = "do not {0} (opposite of {1})".format(
  59. kwargs["help"], long_name)
  60. group.add_argument(*opposite_args, **kwargs)
  61. else:
  62. group.add_argument(*args, **kwargs)
  63. args = parser.parse_args()
  64. # Preliminary configure logging
  65. log.set_debug(args.logging_debug)
  66. if args.config is not None:
  67. config_paths = [args.config] if args.config else []
  68. ignore_missing_paths = False
  69. else:
  70. config_paths = ["/etc/radicale/config",
  71. os.path.expanduser("~/.config/radicale/config")]
  72. if "RADICALE_CONFIG" in os.environ:
  73. config_paths.append(os.environ["RADICALE_CONFIG"])
  74. ignore_missing_paths = True
  75. try:
  76. configuration = config.load(config_paths,
  77. ignore_missing_paths=ignore_missing_paths)
  78. except Exception as e:
  79. log.error("Invalid configuration: %s", e, exc_info=True)
  80. exit(1)
  81. # Update Radicale configuration according to arguments
  82. for group, actions in groups.items():
  83. section = group.title
  84. for action in actions:
  85. value = getattr(args, action)
  86. if value is not None:
  87. configuration.set(section, action.split('_', 1)[1], value)
  88. # Configure logging
  89. log.set_debug(configuration.getboolean("logging", "debug"))
  90. if args.verify_storage:
  91. logger.info("Verifying storage")
  92. try:
  93. Collection = storage.load(configuration)
  94. with Collection.acquire_lock("r"):
  95. if not Collection.verify():
  96. logger.error("Storage verifcation failed")
  97. exit(1)
  98. except Exception as e:
  99. logger.error("An exception occurred during storage verification: "
  100. "%s", e, exc_info=True)
  101. exit(1)
  102. return
  103. try:
  104. serve(configuration)
  105. except Exception as e:
  106. logger.error("An exception occurred during server startup: %s", e,
  107. exc_info=True)
  108. exit(1)
  109. if __name__ == "__main__":
  110. run()