python_dependencies.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. # Copyright 2015, 2016 OpenMarket Ltd
  2. # Copyright 2017 Vector Creations Ltd
  3. # Copyright 2018 New Vector Ltd
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. import logging
  17. from distutils.version import LooseVersion
  18. logger = logging.getLogger(__name__)
  19. # this dict maps from python package name to a list of modules we expect it to
  20. # provide.
  21. #
  22. # the key is a "requirement specifier", as used as a parameter to `pip
  23. # install`[1], or an `install_requires` argument to `setuptools.setup` [2].
  24. #
  25. # the value is a sequence of strings; each entry should be the name of the
  26. # python module, optionally followed by a version assertion which can be either
  27. # ">=<ver>" or "==<ver>".
  28. #
  29. # [1] https://pip.pypa.io/en/stable/reference/pip_install/#requirement-specifiers.
  30. # [2] https://setuptools.readthedocs.io/en/latest/setuptools.html#declaring-dependencies
  31. REQUIREMENTS = {
  32. "jsonschema>=2.5.1": ["jsonschema>=2.5.1"],
  33. "frozendict>=0.4": ["frozendict"],
  34. "unpaddedbase64>=1.1.0": ["unpaddedbase64>=1.1.0"],
  35. "canonicaljson>=1.1.3": ["canonicaljson>=1.1.3"],
  36. "signedjson>=1.0.0": ["signedjson>=1.0.0"],
  37. "pynacl>=1.2.1": ["nacl>=1.2.1", "nacl.bindings"],
  38. "service_identity>=1.0.0": ["service_identity>=1.0.0"],
  39. "Twisted>=16.0.0": ["twisted>=16.0.0"],
  40. # We use crypto.get_elliptic_curve which is only supported in >=0.15
  41. "pyopenssl>=0.15": ["OpenSSL>=0.15"],
  42. "pyyaml": ["yaml"],
  43. "pyasn1": ["pyasn1"],
  44. "daemonize": ["daemonize"],
  45. "bcrypt": ["bcrypt>=3.1.0"],
  46. "pillow": ["PIL"],
  47. "pydenticon": ["pydenticon"],
  48. "blist": ["blist"],
  49. "pysaml2>=3.0.0": ["saml2>=3.0.0"],
  50. "pymacaroons-pynacl": ["pymacaroons"],
  51. "msgpack-python>=0.3.0": ["msgpack"],
  52. "phonenumbers>=8.2.0": ["phonenumbers"],
  53. "six": ["six"],
  54. "prometheus_client": ["prometheus_client"],
  55. }
  56. CONDITIONAL_REQUIREMENTS = {
  57. "web_client": {
  58. "matrix_angular_sdk>=0.6.8": ["syweb>=0.6.8"],
  59. },
  60. "preview_url": {
  61. "netaddr>=0.7.18": ["netaddr"],
  62. },
  63. "email.enable_notifs": {
  64. "Jinja2>=2.8": ["Jinja2>=2.8"],
  65. "bleach>=1.4.2": ["bleach>=1.4.2"],
  66. },
  67. "matrix-synapse-ldap3": {
  68. "matrix-synapse-ldap3>=0.1": ["ldap_auth_provider"],
  69. },
  70. "psutil": {
  71. "psutil>=2.0.0": ["psutil>=2.0.0"],
  72. },
  73. "affinity": {
  74. "affinity": ["affinity"],
  75. },
  76. }
  77. def requirements(config=None, include_conditional=False):
  78. reqs = REQUIREMENTS.copy()
  79. if include_conditional:
  80. for _, req in CONDITIONAL_REQUIREMENTS.items():
  81. reqs.update(req)
  82. return reqs
  83. def github_link(project, version, egg):
  84. return "https://github.com/%s/tarball/%s/#egg=%s" % (project, version, egg)
  85. DEPENDENCY_LINKS = {
  86. }
  87. class MissingRequirementError(Exception):
  88. def __init__(self, message, module_name, dependency):
  89. super(MissingRequirementError, self).__init__(message)
  90. self.module_name = module_name
  91. self.dependency = dependency
  92. def check_requirements(config=None):
  93. """Checks that all the modules needed by synapse have been correctly
  94. installed and are at the correct version"""
  95. for dependency, module_requirements in (
  96. requirements(config, include_conditional=False).items()):
  97. for module_requirement in module_requirements:
  98. if ">=" in module_requirement:
  99. module_name, required_version = module_requirement.split(">=")
  100. version_test = ">="
  101. elif "==" in module_requirement:
  102. module_name, required_version = module_requirement.split("==")
  103. version_test = "=="
  104. else:
  105. module_name = module_requirement
  106. version_test = None
  107. try:
  108. module = __import__(module_name)
  109. except ImportError:
  110. logging.exception(
  111. "Can't import %r which is part of %r",
  112. module_name, dependency
  113. )
  114. raise MissingRequirementError(
  115. "Can't import %r which is part of %r"
  116. % (module_name, dependency), module_name, dependency
  117. )
  118. version = getattr(module, "__version__", None)
  119. file_path = getattr(module, "__file__", None)
  120. logger.info(
  121. "Using %r version %r from %r to satisfy %r",
  122. module_name, version, file_path, dependency
  123. )
  124. if version_test == ">=":
  125. if version is None:
  126. raise MissingRequirementError(
  127. "Version of %r isn't set as __version__ of module %r"
  128. % (dependency, module_name), module_name, dependency
  129. )
  130. if LooseVersion(version) < LooseVersion(required_version):
  131. raise MissingRequirementError(
  132. "Version of %r in %r is too old. %r < %r"
  133. % (dependency, file_path, version, required_version),
  134. module_name, dependency
  135. )
  136. elif version_test == "==":
  137. if version is None:
  138. raise MissingRequirementError(
  139. "Version of %r isn't set as __version__ of module %r"
  140. % (dependency, module_name), module_name, dependency
  141. )
  142. if LooseVersion(version) != LooseVersion(required_version):
  143. raise MissingRequirementError(
  144. "Unexpected version of %r in %r. %r != %r"
  145. % (dependency, file_path, version, required_version),
  146. module_name, dependency
  147. )
  148. def list_requirements():
  149. result = []
  150. linked = []
  151. for link in DEPENDENCY_LINKS.values():
  152. egg = link.split("#egg=")[1]
  153. linked.append(egg.split('-')[0])
  154. result.append(link)
  155. for requirement in requirements(include_conditional=True):
  156. is_linked = False
  157. for link in linked:
  158. if requirement.replace('-', '_').startswith(link):
  159. is_linked = True
  160. if not is_linked:
  161. result.append(requirement)
  162. return result
  163. if __name__ == "__main__":
  164. import sys
  165. sys.stdout.writelines(req + "\n" for req in list_requirements())