python_dependencies.py 5.0 KB

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