python_dependencies.py 5.7 KB

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