python_dependencies.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. # Copyright 2015, 2016 OpenMarket Ltd
  2. # Copyright 2017 Vector Creations Ltd
  3. # Copyright 2018 New Vector Ltd
  4. # Copyright 2020 The Matrix.org Foundation C.I.C.
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. import logging
  18. from typing import List, Set
  19. from pkg_resources import (
  20. DistributionNotFound,
  21. Requirement,
  22. VersionConflict,
  23. get_provider,
  24. )
  25. logger = logging.getLogger(__name__)
  26. # REQUIREMENTS is a simple list of requirement specifiers[1], and must be
  27. # installed. It is passed to setup() as install_requires in setup.py.
  28. #
  29. # CONDITIONAL_REQUIREMENTS is the optional dependencies, represented as a dict
  30. # of lists. The dict key is the optional dependency name and can be passed to
  31. # pip when installing. The list is a series of requirement specifiers[1] to be
  32. # installed when that optional dependency requirement is specified. It is passed
  33. # to setup() as extras_require in setup.py
  34. #
  35. # [1] https://pip.pypa.io/en/stable/reference/pip_install/#requirement-specifiers.
  36. REQUIREMENTS = [
  37. "jsonschema>=2.5.1",
  38. "frozendict>=1",
  39. "unpaddedbase64>=1.1.0",
  40. "canonicaljson>=1.1.3",
  41. # we use the type definitions added in signedjson 1.1.
  42. "signedjson>=1.1.0",
  43. "pynacl>=1.2.1",
  44. "idna>=2.5",
  45. # validating SSL certs for IP addresses requires service_identity 18.1.
  46. "service_identity>=18.1.0",
  47. # Twisted 18.9 introduces some logger improvements that the structured
  48. # logger utilises
  49. "Twisted>=18.9.0",
  50. "treq>=15.1",
  51. # Twisted has required pyopenssl 16.0 since about Twisted 16.6.
  52. "pyopenssl>=16.0.0",
  53. "pyyaml>=3.11",
  54. "pyasn1>=0.1.9",
  55. "pyasn1-modules>=0.0.7",
  56. "daemonize>=2.3.1",
  57. "bcrypt>=3.1.0",
  58. "pillow>=4.3.0",
  59. "sortedcontainers>=1.4.4",
  60. "pymacaroons>=0.13.0",
  61. "msgpack>=0.5.2",
  62. "phonenumbers>=8.2.0",
  63. "prometheus_client>=0.0.18,<0.8.0",
  64. # we use attr.validators.deep_iterable, which arrived in 19.1.0
  65. "attrs>=19.1.0",
  66. "netaddr>=0.7.18",
  67. "Jinja2>=2.9",
  68. "bleach>=1.4.3",
  69. "typing-extensions>=3.7.4",
  70. ]
  71. CONDITIONAL_REQUIREMENTS = {
  72. "matrix-synapse-ldap3": ["matrix-synapse-ldap3>=0.1"],
  73. # we use execute_batch, which arrived in psycopg 2.7.
  74. "postgres": ["psycopg2>=2.7"],
  75. # ConsentResource uses select_autoescape, which arrived in jinja 2.9
  76. "resources.consent": ["Jinja2>=2.9"],
  77. # ACME support is required to provision TLS certificates from authorities
  78. # that use the protocol, such as Let's Encrypt.
  79. "acme": [
  80. "txacme>=0.9.2",
  81. # txacme depends on eliot. Eliot 1.8.0 is incompatible with
  82. # python 3.5.2, as per https://github.com/itamarst/eliot/issues/418
  83. 'eliot<1.8.0;python_version<"3.5.3"',
  84. ],
  85. "saml2": ["pysaml2>=4.5.0"],
  86. "oidc": ["authlib>=0.14.0"],
  87. "systemd": ["systemd-python>=231"],
  88. "url_preview": ["lxml>=3.5.0"],
  89. "test": ["mock>=2.0", "parameterized"],
  90. "sentry": ["sentry-sdk>=0.7.2"],
  91. "opentracing": ["jaeger-client>=4.0.0", "opentracing>=2.2.0"],
  92. "jwt": ["pyjwt>=1.6.4"],
  93. # hiredis is not a *strict* dependency, but it makes things much faster.
  94. # (if it is not installed, we fall back to slow code.)
  95. "redis": ["txredisapi>=1.4.7", "hiredis"],
  96. }
  97. ALL_OPTIONAL_REQUIREMENTS = set() # type: Set[str]
  98. for name, optional_deps in CONDITIONAL_REQUIREMENTS.items():
  99. # Exclude systemd as it's a system-based requirement.
  100. if name not in ["systemd"]:
  101. ALL_OPTIONAL_REQUIREMENTS = set(optional_deps) | ALL_OPTIONAL_REQUIREMENTS
  102. def list_requirements():
  103. return list(set(REQUIREMENTS) | ALL_OPTIONAL_REQUIREMENTS)
  104. class DependencyException(Exception):
  105. @property
  106. def message(self):
  107. return "\n".join(
  108. [
  109. "Missing Requirements: %s" % (", ".join(self.dependencies),),
  110. "To install run:",
  111. " pip install --upgrade --force %s" % (" ".join(self.dependencies),),
  112. "",
  113. ]
  114. )
  115. @property
  116. def dependencies(self):
  117. for i in self.args[0]:
  118. yield "'" + i + "'"
  119. def check_requirements(for_feature=None):
  120. deps_needed = []
  121. errors = []
  122. if for_feature:
  123. reqs = CONDITIONAL_REQUIREMENTS[for_feature]
  124. else:
  125. reqs = REQUIREMENTS
  126. for dependency in reqs:
  127. try:
  128. _check_requirement(dependency)
  129. except VersionConflict as e:
  130. deps_needed.append(dependency)
  131. errors.append(
  132. "Needed %s, got %s==%s"
  133. % (
  134. dependency,
  135. e.dist.project_name, # type: ignore[attr-defined] # noqa
  136. e.dist.version, # type: ignore[attr-defined] # noqa
  137. )
  138. )
  139. except DistributionNotFound:
  140. deps_needed.append(dependency)
  141. if for_feature:
  142. errors.append(
  143. "Needed %s for the '%s' feature but it was not installed"
  144. % (dependency, for_feature)
  145. )
  146. else:
  147. errors.append("Needed %s but it was not installed" % (dependency,))
  148. if not for_feature:
  149. # Check the optional dependencies are up to date. We allow them to not be
  150. # installed.
  151. OPTS = sum(CONDITIONAL_REQUIREMENTS.values(), []) # type: List[str]
  152. for dependency in OPTS:
  153. try:
  154. _check_requirement(dependency)
  155. except VersionConflict as e:
  156. deps_needed.append(dependency)
  157. errors.append(
  158. "Needed optional %s, got %s==%s"
  159. % (
  160. dependency,
  161. e.dist.project_name, # type: ignore[attr-defined] # noqa
  162. e.dist.version, # type: ignore[attr-defined] # noqa
  163. )
  164. )
  165. except DistributionNotFound:
  166. # If it's not found, we don't care
  167. pass
  168. if deps_needed:
  169. for err in errors:
  170. logging.error(err)
  171. raise DependencyException(deps_needed)
  172. def _check_requirement(dependency_string):
  173. """Parses a dependency string, and checks if the specified requirement is installed
  174. Raises:
  175. VersionConflict if the requirement is installed, but with the the wrong version
  176. DistributionNotFound if nothing is found to provide the requirement
  177. """
  178. req = Requirement.parse(dependency_string)
  179. # first check if the markers specify that this requirement needs installing
  180. if req.marker is not None and not req.marker.evaluate():
  181. # not required for this environment
  182. return
  183. get_provider(req)
  184. if __name__ == "__main__":
  185. import sys
  186. sys.stdout.writelines(req + "\n" for req in list_requirements())