You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

188 lines
6.9 KiB

  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>=1": ["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>=16.0.0": ["service_identity>=16.0.0"],
  39. "Twisted>=17.1.0": ["twisted>=17.1.0"],
  40. "treq>=15.1": ["treq>=15.1"],
  41. # Twisted has required pyopenssl 16.0 since about Twisted 16.6.
  42. "pyopenssl>=16.0.0": ["OpenSSL>=16.0.0"],
  43. "pyyaml>=3.11": ["yaml"],
  44. "pyasn1>=0.1.9": ["pyasn1"],
  45. "pyasn1-modules>=0.0.7": ["pyasn1_modules"],
  46. "daemonize>=2.3.1": ["daemonize"],
  47. "bcrypt>=3.1.0": ["bcrypt>=3.1.0"],
  48. "pillow>=3.1.2": ["PIL"],
  49. "sortedcontainers>=1.4.4": ["sortedcontainers"],
  50. "psutil>=2.0.0": ["psutil>=2.0.0"],
  51. "pysaml2>=3.0.0": ["saml2"],
  52. "pymacaroons-pynacl>=0.9.3": ["pymacaroons"],
  53. "msgpack-python>=0.4.2": ["msgpack"],
  54. "phonenumbers>=8.2.0": ["phonenumbers"],
  55. "six>=1.10": ["six"],
  56. # prometheus_client 0.4.0 changed the format of counter metrics
  57. # (cf https://github.com/matrix-org/synapse/issues/4001)
  58. "prometheus_client>=0.0.18,<0.4.0": ["prometheus_client"],
  59. # we use attr.s(slots), which arrived in 16.0.0
  60. "attrs>=16.0.0": ["attr>=16.0.0"],
  61. "netaddr>=0.7.18": ["netaddr"],
  62. }
  63. CONDITIONAL_REQUIREMENTS = {
  64. "email.enable_notifs": {
  65. "Jinja2>=2.8": ["Jinja2>=2.8"],
  66. "bleach>=1.4.2": ["bleach>=1.4.2"],
  67. },
  68. "matrix-synapse-ldap3": {
  69. "matrix-synapse-ldap3>=0.1": ["ldap_auth_provider"],
  70. },
  71. "postgres": {
  72. "psycopg2>=2.6": ["psycopg2"]
  73. },
  74. }
  75. def requirements(config=None, include_conditional=False):
  76. reqs = REQUIREMENTS.copy()
  77. if include_conditional:
  78. for _, req in CONDITIONAL_REQUIREMENTS.items():
  79. reqs.update(req)
  80. return reqs
  81. def github_link(project, version, egg):
  82. return "https://github.com/%s/tarball/%s/#egg=%s" % (project, version, egg)
  83. DEPENDENCY_LINKS = {
  84. }
  85. class MissingRequirementError(Exception):
  86. def __init__(self, message, module_name, dependency):
  87. super(MissingRequirementError, self).__init__(message)
  88. self.module_name = module_name
  89. self.dependency = dependency
  90. def check_requirements(config=None):
  91. """Checks that all the modules needed by synapse have been correctly
  92. installed and are at the correct version"""
  93. for dependency, module_requirements in (
  94. requirements(config, include_conditional=False).items()):
  95. for module_requirement in module_requirements:
  96. if ">=" in module_requirement:
  97. module_name, required_version = module_requirement.split(">=")
  98. version_test = ">="
  99. elif "==" in module_requirement:
  100. module_name, required_version = module_requirement.split("==")
  101. version_test = "=="
  102. else:
  103. module_name = module_requirement
  104. version_test = None
  105. try:
  106. module = __import__(module_name)
  107. except ImportError:
  108. logging.exception(
  109. "Can't import %r which is part of %r",
  110. module_name, dependency
  111. )
  112. raise MissingRequirementError(
  113. "Can't import %r which is part of %r"
  114. % (module_name, dependency), module_name, dependency
  115. )
  116. version = getattr(module, "__version__", None)
  117. file_path = getattr(module, "__file__", None)
  118. logger.info(
  119. "Using %r version %r from %r to satisfy %r",
  120. module_name, version, file_path, dependency
  121. )
  122. if version_test == ">=":
  123. if version is None:
  124. raise MissingRequirementError(
  125. "Version of %r isn't set as __version__ of module %r"
  126. % (dependency, module_name), module_name, dependency
  127. )
  128. if LooseVersion(version) < LooseVersion(required_version):
  129. raise MissingRequirementError(
  130. "Version of %r in %r is too old. %r < %r"
  131. % (dependency, file_path, version, required_version),
  132. module_name, dependency
  133. )
  134. elif version_test == "==":
  135. if version is None:
  136. raise MissingRequirementError(
  137. "Version of %r isn't set as __version__ of module %r"
  138. % (dependency, module_name), module_name, dependency
  139. )
  140. if LooseVersion(version) != LooseVersion(required_version):
  141. raise MissingRequirementError(
  142. "Unexpected version of %r in %r. %r != %r"
  143. % (dependency, file_path, version, required_version),
  144. module_name, dependency
  145. )
  146. def list_requirements():
  147. result = []
  148. linked = []
  149. for link in DEPENDENCY_LINKS.values():
  150. egg = link.split("#egg=")[1]
  151. linked.append(egg.split('-')[0])
  152. result.append(link)
  153. for requirement in requirements(include_conditional=True):
  154. is_linked = False
  155. for link in linked:
  156. if requirement.replace('-', '_').startswith(link):
  157. is_linked = True
  158. if not is_linked:
  159. result.append(requirement)
  160. return result
  161. if __name__ == "__main__":
  162. import sys
  163. sys.stdout.writelines(req + "\n" for req in list_requirements())