python_dependencies.py 5.5 KB

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