python_dependencies.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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>=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": ["yaml"],
  44. "pyasn1": ["pyasn1"],
  45. "daemonize": ["daemonize"],
  46. "bcrypt": ["bcrypt>=3.1.0"],
  47. "pillow": ["PIL"],
  48. "pydenticon": ["pydenticon"],
  49. "sortedcontainers": ["sortedcontainers"],
  50. "pysaml2>=3.0.0": ["saml2>=3.0.0"],
  51. "pymacaroons-pynacl": ["pymacaroons"],
  52. "msgpack-python>=0.3.0": ["msgpack"],
  53. "phonenumbers>=8.2.0": ["phonenumbers"],
  54. "six": ["six"],
  55. "prometheus_client": ["prometheus_client"],
  56. "attrs": ["attr"],
  57. "netaddr>=0.7.18": ["netaddr"],
  58. }
  59. CONDITIONAL_REQUIREMENTS = {
  60. "web_client": {
  61. "matrix_angular_sdk>=0.6.8": ["syweb>=0.6.8"],
  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. "postgres": {
  77. "psycopg2>=2.6": ["psycopg2"]
  78. }
  79. }
  80. def requirements(config=None, include_conditional=False):
  81. reqs = REQUIREMENTS.copy()
  82. if include_conditional:
  83. for _, req in CONDITIONAL_REQUIREMENTS.items():
  84. reqs.update(req)
  85. return reqs
  86. def github_link(project, version, egg):
  87. return "https://github.com/%s/tarball/%s/#egg=%s" % (project, version, egg)
  88. DEPENDENCY_LINKS = {
  89. }
  90. class MissingRequirementError(Exception):
  91. def __init__(self, message, module_name, dependency):
  92. super(MissingRequirementError, self).__init__(message)
  93. self.module_name = module_name
  94. self.dependency = dependency
  95. def check_requirements(config=None):
  96. """Checks that all the modules needed by synapse have been correctly
  97. installed and are at the correct version"""
  98. for dependency, module_requirements in (
  99. requirements(config, include_conditional=False).items()):
  100. for module_requirement in module_requirements:
  101. if ">=" in module_requirement:
  102. module_name, required_version = module_requirement.split(">=")
  103. version_test = ">="
  104. elif "==" in module_requirement:
  105. module_name, required_version = module_requirement.split("==")
  106. version_test = "=="
  107. else:
  108. module_name = module_requirement
  109. version_test = None
  110. try:
  111. module = __import__(module_name)
  112. except ImportError:
  113. logging.exception(
  114. "Can't import %r which is part of %r",
  115. module_name, dependency
  116. )
  117. raise MissingRequirementError(
  118. "Can't import %r which is part of %r"
  119. % (module_name, dependency), module_name, dependency
  120. )
  121. version = getattr(module, "__version__", None)
  122. file_path = getattr(module, "__file__", None)
  123. logger.info(
  124. "Using %r version %r from %r to satisfy %r",
  125. module_name, version, file_path, dependency
  126. )
  127. if version_test == ">=":
  128. if version is None:
  129. raise MissingRequirementError(
  130. "Version of %r isn't set as __version__ of module %r"
  131. % (dependency, module_name), module_name, dependency
  132. )
  133. if LooseVersion(version) < LooseVersion(required_version):
  134. raise MissingRequirementError(
  135. "Version of %r in %r is too old. %r < %r"
  136. % (dependency, file_path, version, required_version),
  137. module_name, dependency
  138. )
  139. elif version_test == "==":
  140. if version is None:
  141. raise MissingRequirementError(
  142. "Version of %r isn't set as __version__ of module %r"
  143. % (dependency, module_name), module_name, dependency
  144. )
  145. if LooseVersion(version) != LooseVersion(required_version):
  146. raise MissingRequirementError(
  147. "Unexpected version of %r in %r. %r != %r"
  148. % (dependency, file_path, version, required_version),
  149. module_name, dependency
  150. )
  151. def list_requirements():
  152. result = []
  153. linked = []
  154. for link in DEPENDENCY_LINKS.values():
  155. egg = link.split("#egg=")[1]
  156. linked.append(egg.split('-')[0])
  157. result.append(link)
  158. for requirement in requirements(include_conditional=True):
  159. is_linked = False
  160. for link in linked:
  161. if requirement.replace('-', '_').startswith(link):
  162. is_linked = True
  163. if not is_linked:
  164. result.append(requirement)
  165. return result
  166. if __name__ == "__main__":
  167. import sys
  168. sys.stdout.writelines(req + "\n" for req in list_requirements())