python_dependencies.py 5.5 KB

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