python_dependencies.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 pkg_resources import DistributionNotFound, VersionConflict, get_distribution
  18. logger = logging.getLogger(__name__)
  19. # REQUIREMENTS is a simple list of requirement specifiers[1], and must be
  20. # installed. It is passed to setup() as install_requires in setup.py.
  21. #
  22. # CONDITIONAL_REQUIREMENTS is the optional dependencies, represented as a dict
  23. # of lists. The dict key is the optional dependency name and can be passed to
  24. # pip when installing. The list is a series of requirement specifiers[1] to be
  25. # installed when that optional dependency requirement is specified. It is passed
  26. # to setup() as extras_require in setup.py
  27. #
  28. # [1] https://pip.pypa.io/en/stable/reference/pip_install/#requirement-specifiers.
  29. REQUIREMENTS = [
  30. "jsonschema>=2.5.1",
  31. "frozendict>=1",
  32. "unpaddedbase64>=1.1.0",
  33. "canonicaljson>=1.1.3",
  34. "signedjson>=1.0.0",
  35. "pynacl>=1.2.1",
  36. "service_identity>=16.0.0",
  37. # our logcontext handling relies on the ability to cancel inlineCallbacks
  38. # (https://twistedmatrix.com/trac/ticket/4632) which landed in Twisted 18.7.
  39. "Twisted>=18.7.0",
  40. "treq>=15.1",
  41. # Twisted has required pyopenssl 16.0 since about Twisted 16.6.
  42. "pyopenssl>=16.0.0",
  43. "pyyaml>=3.11",
  44. "pyasn1>=0.1.9",
  45. "pyasn1-modules>=0.0.7",
  46. "daemonize>=2.3.1",
  47. "bcrypt>=3.1.0",
  48. "pillow>=3.1.2",
  49. "sortedcontainers>=1.4.4",
  50. "psutil>=2.0.0",
  51. "pymacaroons>=0.13.0",
  52. "msgpack>=0.5.0",
  53. "phonenumbers>=8.2.0",
  54. "six>=1.10",
  55. # prometheus_client 0.4.0 changed the format of counter metrics
  56. # (cf https://github.com/matrix-org/synapse/issues/4001)
  57. "prometheus_client>=0.0.18,<0.4.0",
  58. # we use attr.s(slots), which arrived in 16.0.0
  59. # Twisted 18.7.0 requires attrs>=17.4.0
  60. "attrs>=17.4.0",
  61. "netaddr>=0.7.18",
  62. ]
  63. CONDITIONAL_REQUIREMENTS = {
  64. "email.enable_notifs": ["Jinja2>=2.9", "bleach>=1.4.2"],
  65. "matrix-synapse-ldap3": ["matrix-synapse-ldap3>=0.1"],
  66. "postgres": ["psycopg2>=2.6"],
  67. # ConsentResource uses select_autoescape, which arrived in jinja 2.9
  68. "resources.consent": ["Jinja2>=2.9"],
  69. "saml2": ["pysaml2>=4.5.0"],
  70. "url_preview": ["lxml>=3.5.0"],
  71. "test": ["mock>=2.0"],
  72. }
  73. def list_requirements():
  74. deps = set(REQUIREMENTS)
  75. for opt in CONDITIONAL_REQUIREMENTS.values():
  76. deps = set(opt) | deps
  77. return list(deps)
  78. class DependencyException(Exception):
  79. @property
  80. def message(self):
  81. return "\n".join([
  82. "Missing Requirements: %s" % (", ".join(self.dependencies),),
  83. "To install run:",
  84. " pip install --upgrade --force %s" % (" ".join(self.dependencies),),
  85. "",
  86. ])
  87. @property
  88. def dependencies(self):
  89. for i in self.args[0]:
  90. yield '"' + i + '"'
  91. def check_requirements(for_feature=None, _get_distribution=get_distribution):
  92. deps_needed = []
  93. errors = []
  94. if for_feature:
  95. reqs = CONDITIONAL_REQUIREMENTS[for_feature]
  96. else:
  97. reqs = REQUIREMENTS
  98. for dependency in reqs:
  99. try:
  100. _get_distribution(dependency)
  101. except VersionConflict as e:
  102. deps_needed.append(dependency)
  103. errors.append(
  104. "Needed %s, got %s==%s"
  105. % (dependency, e.dist.project_name, e.dist.version)
  106. )
  107. except DistributionNotFound:
  108. deps_needed.append(dependency)
  109. errors.append("Needed %s but it was not installed" % (dependency,))
  110. if not for_feature:
  111. # Check the optional dependencies are up to date. We allow them to not be
  112. # installed.
  113. OPTS = sum(CONDITIONAL_REQUIREMENTS.values(), [])
  114. for dependency in OPTS:
  115. try:
  116. _get_distribution(dependency)
  117. except VersionConflict:
  118. deps_needed.append(dependency)
  119. errors.append("Needed %s but it was not installed" % (dependency,))
  120. except DistributionNotFound:
  121. # If it's not found, we don't care
  122. pass
  123. if deps_needed:
  124. for e in errors:
  125. logging.error(e)
  126. raise DependencyException(deps_needed)
  127. if __name__ == "__main__":
  128. import sys
  129. sys.stdout.writelines(req + "\n" for req in list_requirements())