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