python_dependencies.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. # Copyright 2015, 2016 OpenMarket Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import logging
  15. from distutils.version import LooseVersion
  16. logger = logging.getLogger(__name__)
  17. REQUIREMENTS = {
  18. "frozendict>=0.4": ["frozendict"],
  19. "unpaddedbase64>=1.1.0": ["unpaddedbase64>=1.1.0"],
  20. "canonicaljson>=1.0.0": ["canonicaljson>=1.0.0"],
  21. "signedjson>=1.0.0": ["signedjson>=1.0.0"],
  22. "pynacl==0.3.0": ["nacl==0.3.0", "nacl.bindings"],
  23. "service_identity>=1.0.0": ["service_identity>=1.0.0"],
  24. "Twisted>=15.1.0": ["twisted>=15.1.0"],
  25. "pyopenssl>=0.14": ["OpenSSL>=0.14"],
  26. "pyyaml": ["yaml"],
  27. "pyasn1": ["pyasn1"],
  28. "daemonize": ["daemonize"],
  29. "py-bcrypt": ["bcrypt"],
  30. "pillow": ["PIL"],
  31. "pydenticon": ["pydenticon"],
  32. "ujson": ["ujson"],
  33. "blist": ["blist"],
  34. "pysaml2>=3.0.0,<4.0.0": ["saml2>=3.0.0,<4.0.0"],
  35. "pymacaroons-pynacl": ["pymacaroons"],
  36. }
  37. CONDITIONAL_REQUIREMENTS = {
  38. "web_client": {
  39. "matrix_angular_sdk>=0.6.8": ["syweb>=0.6.8"],
  40. },
  41. "preview_url": {
  42. "netaddr>=0.7.18": ["netaddr"],
  43. },
  44. "email.enable_notifs": {
  45. "Jinja2>=2.8": ["Jinja2>=2.8"],
  46. "bleach>=1.4.2": ["bleach>=1.4.2"],
  47. },
  48. }
  49. def requirements(config=None, include_conditional=False):
  50. reqs = REQUIREMENTS.copy()
  51. if include_conditional:
  52. for _, req in CONDITIONAL_REQUIREMENTS.items():
  53. reqs.update(req)
  54. return reqs
  55. def github_link(project, version, egg):
  56. return "https://github.com/%s/tarball/%s/#egg=%s" % (project, version, egg)
  57. DEPENDENCY_LINKS = {
  58. }
  59. class MissingRequirementError(Exception):
  60. def __init__(self, message, module_name, dependency):
  61. super(MissingRequirementError, self).__init__(message)
  62. self.module_name = module_name
  63. self.dependency = dependency
  64. def check_requirements(config=None):
  65. """Checks that all the modules needed by synapse have been correctly
  66. installed and are at the correct version"""
  67. for dependency, module_requirements in (
  68. requirements(config, include_conditional=False).items()):
  69. for module_requirement in module_requirements:
  70. if ">=" in module_requirement:
  71. module_name, required_version = module_requirement.split(">=")
  72. version_test = ">="
  73. elif "==" in module_requirement:
  74. module_name, required_version = module_requirement.split("==")
  75. version_test = "=="
  76. else:
  77. module_name = module_requirement
  78. version_test = None
  79. try:
  80. module = __import__(module_name)
  81. except ImportError:
  82. logging.exception(
  83. "Can't import %r which is part of %r",
  84. module_name, dependency
  85. )
  86. raise MissingRequirementError(
  87. "Can't import %r which is part of %r"
  88. % (module_name, dependency), module_name, dependency
  89. )
  90. version = getattr(module, "__version__", None)
  91. file_path = getattr(module, "__file__", None)
  92. logger.info(
  93. "Using %r version %r from %r to satisfy %r",
  94. module_name, version, file_path, dependency
  95. )
  96. if version_test == ">=":
  97. if version is None:
  98. raise MissingRequirementError(
  99. "Version of %r isn't set as __version__ of module %r"
  100. % (dependency, module_name), module_name, dependency
  101. )
  102. if LooseVersion(version) < LooseVersion(required_version):
  103. raise MissingRequirementError(
  104. "Version of %r in %r is too old. %r < %r"
  105. % (dependency, file_path, version, required_version),
  106. module_name, dependency
  107. )
  108. elif version_test == "==":
  109. if version is None:
  110. raise MissingRequirementError(
  111. "Version of %r isn't set as __version__ of module %r"
  112. % (dependency, module_name), module_name, dependency
  113. )
  114. if LooseVersion(version) != LooseVersion(required_version):
  115. raise MissingRequirementError(
  116. "Unexpected version of %r in %r. %r != %r"
  117. % (dependency, file_path, version, required_version),
  118. module_name, dependency
  119. )
  120. def list_requirements():
  121. result = []
  122. linked = []
  123. for link in DEPENDENCY_LINKS.values():
  124. egg = link.split("#egg=")[1]
  125. linked.append(egg.split('-')[0])
  126. result.append(link)
  127. for requirement in requirements(include_conditional=True):
  128. is_linked = False
  129. for link in linked:
  130. if requirement.replace('-', '_').startswith(link):
  131. is_linked = True
  132. if not is_linked:
  133. result.append(requirement)
  134. return result
  135. if __name__ == "__main__":
  136. import sys
  137. sys.stdout.writelines(req + "\n" for req in list_requirements())