python_dependencies.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. # we use execute_batch, which arrived in psycopg 2.7.
  67. "postgres": ["psycopg2>=2.7"],
  68. # ConsentResource uses select_autoescape, which arrived in jinja 2.9
  69. "resources.consent": ["Jinja2>=2.9"],
  70. # ACME support is required to provision TLS certificates from authorities
  71. # that use the protocol, such as Let's Encrypt.
  72. "acme": ["txacme>=0.9.2"],
  73. "saml2": ["pysaml2>=4.5.0"],
  74. "systemd": ["systemd-python>=231"],
  75. "url_preview": ["lxml>=3.5.0"],
  76. "test": ["mock>=2.0", "parameterized"],
  77. "sentry": ["sentry-sdk>=0.7.2"],
  78. }
  79. ALL_OPTIONAL_REQUIREMENTS = set()
  80. for name, optional_deps in CONDITIONAL_REQUIREMENTS.items():
  81. # Exclude systemd as it's a system-based requirement.
  82. if name not in ["systemd"]:
  83. ALL_OPTIONAL_REQUIREMENTS = set(optional_deps) | ALL_OPTIONAL_REQUIREMENTS
  84. def list_requirements():
  85. return list(set(REQUIREMENTS) | ALL_OPTIONAL_REQUIREMENTS)
  86. class DependencyException(Exception):
  87. @property
  88. def message(self):
  89. return "\n".join([
  90. "Missing Requirements: %s" % (", ".join(self.dependencies),),
  91. "To install run:",
  92. " pip install --upgrade --force %s" % (" ".join(self.dependencies),),
  93. "",
  94. ])
  95. @property
  96. def dependencies(self):
  97. for i in self.args[0]:
  98. yield '"' + i + '"'
  99. def check_requirements(for_feature=None, _get_distribution=get_distribution):
  100. deps_needed = []
  101. errors = []
  102. if for_feature:
  103. reqs = CONDITIONAL_REQUIREMENTS[for_feature]
  104. else:
  105. reqs = REQUIREMENTS
  106. for dependency in reqs:
  107. try:
  108. _get_distribution(dependency)
  109. except VersionConflict as e:
  110. deps_needed.append(dependency)
  111. errors.append(
  112. "Needed %s, got %s==%s"
  113. % (dependency, e.dist.project_name, e.dist.version)
  114. )
  115. except DistributionNotFound:
  116. deps_needed.append(dependency)
  117. errors.append("Needed %s but it was not installed" % (dependency,))
  118. if not for_feature:
  119. # Check the optional dependencies are up to date. We allow them to not be
  120. # installed.
  121. OPTS = sum(CONDITIONAL_REQUIREMENTS.values(), [])
  122. for dependency in OPTS:
  123. try:
  124. _get_distribution(dependency)
  125. except VersionConflict as e:
  126. deps_needed.append(dependency)
  127. errors.append(
  128. "Needed optional %s, got %s==%s"
  129. % (dependency, e.dist.project_name, e.dist.version)
  130. )
  131. except DistributionNotFound:
  132. # If it's not found, we don't care
  133. pass
  134. if deps_needed:
  135. for e in errors:
  136. logging.error(e)
  137. raise DependencyException(deps_needed)
  138. if __name__ == "__main__":
  139. import sys
  140. sys.stdout.writelines(req + "\n" for req in list_requirements())