python_dependencies.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. "msgpack-python>=0.3.0": ["msgpack"],
  37. }
  38. CONDITIONAL_REQUIREMENTS = {
  39. "web_client": {
  40. "matrix_angular_sdk>=0.6.8": ["syweb>=0.6.8"],
  41. },
  42. "preview_url": {
  43. "netaddr>=0.7.18": ["netaddr"],
  44. },
  45. "email.enable_notifs": {
  46. "Jinja2>=2.8": ["Jinja2>=2.8"],
  47. "bleach>=1.4.2": ["bleach>=1.4.2"],
  48. },
  49. "matrix-synapse-ldap3": {
  50. "matrix-synapse-ldap3>=0.1": ["ldap_auth_provider"],
  51. },
  52. "psutil": {
  53. "psutil>=2.0.0": ["psutil>=2.0.0"],
  54. },
  55. }
  56. def requirements(config=None, include_conditional=False):
  57. reqs = REQUIREMENTS.copy()
  58. if include_conditional:
  59. for _, req in CONDITIONAL_REQUIREMENTS.items():
  60. reqs.update(req)
  61. return reqs
  62. def github_link(project, version, egg):
  63. return "https://github.com/%s/tarball/%s/#egg=%s" % (project, version, egg)
  64. DEPENDENCY_LINKS = {
  65. }
  66. class MissingRequirementError(Exception):
  67. def __init__(self, message, module_name, dependency):
  68. super(MissingRequirementError, self).__init__(message)
  69. self.module_name = module_name
  70. self.dependency = dependency
  71. def check_requirements(config=None):
  72. """Checks that all the modules needed by synapse have been correctly
  73. installed and are at the correct version"""
  74. for dependency, module_requirements in (
  75. requirements(config, include_conditional=False).items()):
  76. for module_requirement in module_requirements:
  77. if ">=" in module_requirement:
  78. module_name, required_version = module_requirement.split(">=")
  79. version_test = ">="
  80. elif "==" in module_requirement:
  81. module_name, required_version = module_requirement.split("==")
  82. version_test = "=="
  83. else:
  84. module_name = module_requirement
  85. version_test = None
  86. try:
  87. module = __import__(module_name)
  88. except ImportError:
  89. logging.exception(
  90. "Can't import %r which is part of %r",
  91. module_name, dependency
  92. )
  93. raise MissingRequirementError(
  94. "Can't import %r which is part of %r"
  95. % (module_name, dependency), module_name, dependency
  96. )
  97. version = getattr(module, "__version__", None)
  98. file_path = getattr(module, "__file__", None)
  99. logger.info(
  100. "Using %r version %r from %r to satisfy %r",
  101. module_name, version, file_path, dependency
  102. )
  103. if version_test == ">=":
  104. if version is None:
  105. raise MissingRequirementError(
  106. "Version of %r isn't set as __version__ of module %r"
  107. % (dependency, module_name), module_name, dependency
  108. )
  109. if LooseVersion(version) < LooseVersion(required_version):
  110. raise MissingRequirementError(
  111. "Version of %r in %r is too old. %r < %r"
  112. % (dependency, file_path, version, required_version),
  113. module_name, dependency
  114. )
  115. elif version_test == "==":
  116. if version is None:
  117. raise MissingRequirementError(
  118. "Version of %r isn't set as __version__ of module %r"
  119. % (dependency, module_name), module_name, dependency
  120. )
  121. if LooseVersion(version) != LooseVersion(required_version):
  122. raise MissingRequirementError(
  123. "Unexpected version of %r in %r. %r != %r"
  124. % (dependency, file_path, version, required_version),
  125. module_name, dependency
  126. )
  127. def list_requirements():
  128. result = []
  129. linked = []
  130. for link in DEPENDENCY_LINKS.values():
  131. egg = link.split("#egg=")[1]
  132. linked.append(egg.split('-')[0])
  133. result.append(link)
  134. for requirement in requirements(include_conditional=True):
  135. is_linked = False
  136. for link in linked:
  137. if requirement.replace('-', '_').startswith(link):
  138. is_linked = True
  139. if not is_linked:
  140. result.append(requirement)
  141. return result
  142. if __name__ == "__main__":
  143. import sys
  144. sys.stdout.writelines(req + "\n" for req in list_requirements())