python_dependencies.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. # Copyright 2015 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. "unpaddedbase64>=1.0.1": ["unpaddedbase64>=1.0.1"],
  19. "canonicaljson>=1.0.0": ["canonicaljson>=1.0.0"],
  20. "signedjson>=1.0.0": ["signedjson>=1.0.0"],
  21. "Twisted>=15.1.0": ["twisted>=15.1.0"],
  22. "service_identity>=1.0.0": ["service_identity>=1.0.0"],
  23. "pyopenssl>=0.14": ["OpenSSL>=0.14"],
  24. "pyyaml": ["yaml"],
  25. "pyasn1": ["pyasn1"],
  26. "pynacl>=0.3.0": ["nacl>=0.3.0"],
  27. "daemonize": ["daemonize"],
  28. "py-bcrypt": ["bcrypt"],
  29. "frozendict>=0.4": ["frozendict"],
  30. "pillow": ["PIL"],
  31. "pydenticon": ["pydenticon"],
  32. "ujson": ["ujson"],
  33. "blist": ["blist"],
  34. "pysaml2": ["saml2"],
  35. "pymacaroons-pynacl": ["pymacaroons"],
  36. }
  37. CONDITIONAL_REQUIREMENTS = {
  38. "web_client": {
  39. "matrix_angular_sdk>=0.6.6": ["syweb>=0.6.6"],
  40. }
  41. }
  42. def requirements(config=None, include_conditional=False):
  43. reqs = REQUIREMENTS.copy()
  44. if include_conditional:
  45. for _, req in CONDITIONAL_REQUIREMENTS.items():
  46. reqs.update(req)
  47. return reqs
  48. def github_link(project, version, egg):
  49. return "https://github.com/%s/tarball/%s/#egg=%s" % (project, version, egg)
  50. DEPENDENCY_LINKS = {
  51. }
  52. class MissingRequirementError(Exception):
  53. pass
  54. def check_requirements(config=None):
  55. """Checks that all the modules needed by synapse have been correctly
  56. installed and are at the correct version"""
  57. for dependency, module_requirements in (
  58. requirements(config, include_conditional=False).items()):
  59. for module_requirement in module_requirements:
  60. if ">=" in module_requirement:
  61. module_name, required_version = module_requirement.split(">=")
  62. version_test = ">="
  63. elif "==" in module_requirement:
  64. module_name, required_version = module_requirement.split("==")
  65. version_test = "=="
  66. else:
  67. module_name = module_requirement
  68. version_test = None
  69. try:
  70. module = __import__(module_name)
  71. except ImportError:
  72. logging.exception(
  73. "Can't import %r which is part of %r",
  74. module_name, dependency
  75. )
  76. raise MissingRequirementError(
  77. "Can't import %r which is part of %r"
  78. % (module_name, dependency)
  79. )
  80. version = getattr(module, "__version__", None)
  81. file_path = getattr(module, "__file__", None)
  82. logger.info(
  83. "Using %r version %r from %r to satisfy %r",
  84. module_name, version, file_path, dependency
  85. )
  86. if version_test == ">=":
  87. if version is None:
  88. raise MissingRequirementError(
  89. "Version of %r isn't set as __version__ of module %r"
  90. % (dependency, module_name)
  91. )
  92. if LooseVersion(version) < LooseVersion(required_version):
  93. raise MissingRequirementError(
  94. "Version of %r in %r is too old. %r < %r"
  95. % (dependency, file_path, version, required_version)
  96. )
  97. elif version_test == "==":
  98. if version is None:
  99. raise MissingRequirementError(
  100. "Version of %r isn't set as __version__ of module %r"
  101. % (dependency, module_name)
  102. )
  103. if LooseVersion(version) != LooseVersion(required_version):
  104. raise MissingRequirementError(
  105. "Unexpected version of %r in %r. %r != %r"
  106. % (dependency, file_path, version, required_version)
  107. )
  108. def list_requirements():
  109. result = []
  110. linked = []
  111. for link in DEPENDENCY_LINKS.values():
  112. egg = link.split("#egg=")[1]
  113. linked.append(egg.split('-')[0])
  114. result.append(link)
  115. for requirement in requirements(include_conditional=True):
  116. is_linked = False
  117. for link in linked:
  118. if requirement.replace('-', '_').startswith(link):
  119. is_linked = True
  120. if not is_linked:
  121. result.append(requirement)
  122. return result
  123. if __name__ == "__main__":
  124. import sys
  125. sys.stdout.writelines(req + "\n" for req in list_requirements())