python_dependencies.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. # Note that these both represent runtime dependencies (and the versions
  36. # installed are checked at runtime).
  37. #
  38. # [1] https://pip.pypa.io/en/stable/reference/pip_install/#requirement-specifiers.
  39. REQUIREMENTS = [
  40. "jsonschema>=2.5.1",
  41. "frozendict>=1",
  42. "unpaddedbase64>=1.1.0",
  43. "canonicaljson>=1.4.0",
  44. # we use the type definitions added in signedjson 1.1.
  45. "signedjson>=1.1.0",
  46. "pynacl>=1.2.1",
  47. "idna>=2.5",
  48. # validating SSL certs for IP addresses requires service_identity 18.1.
  49. "service_identity>=18.1.0",
  50. # Twisted 18.9 introduces some logger improvements that the structured
  51. # logger utilises
  52. "Twisted>=18.9.0",
  53. "treq>=15.1",
  54. # Twisted has required pyopenssl 16.0 since about Twisted 16.6.
  55. "pyopenssl>=16.0.0",
  56. "pyyaml>=3.11",
  57. "pyasn1>=0.1.9",
  58. "pyasn1-modules>=0.0.7",
  59. "bcrypt>=3.1.0",
  60. "pillow>=4.3.0",
  61. "sortedcontainers>=1.4.4",
  62. "pymacaroons>=0.13.0",
  63. "msgpack>=0.5.2",
  64. "phonenumbers>=8.2.0",
  65. # we use GaugeHistogramMetric, which was added in prom-client 0.4.0.
  66. # prom-client has a history of breaking backwards compatibility between
  67. # minor versions (https://github.com/prometheus/client_python/issues/317),
  68. # so we also pin the minor version.
  69. "prometheus_client>=0.4.0,<0.9.0",
  70. # we use attr.validators.deep_iterable, which arrived in 19.1.0 (Note:
  71. # Fedora 31 only has 19.1, so if we want to upgrade we should wait until 33
  72. # is out in November.)
  73. "attrs>=19.1.0",
  74. "netaddr>=0.7.18",
  75. "Jinja2>=2.9",
  76. "bleach>=1.4.3",
  77. "typing-extensions>=3.7.4",
  78. ]
  79. CONDITIONAL_REQUIREMENTS = {
  80. "matrix-synapse-ldap3": ["matrix-synapse-ldap3>=0.1"],
  81. # we use execute_batch, which arrived in psycopg 2.7.
  82. "postgres": ["psycopg2>=2.7"],
  83. # ACME support is required to provision TLS certificates from authorities
  84. # that use the protocol, such as Let's Encrypt.
  85. "acme": [
  86. "txacme>=0.9.2",
  87. # txacme depends on eliot. Eliot 1.8.0 is incompatible with
  88. # python 3.5.2, as per https://github.com/itamarst/eliot/issues/418
  89. 'eliot<1.8.0;python_version<"3.5.3"',
  90. ],
  91. "saml2": ["pysaml2>=4.5.0"],
  92. "oidc": ["authlib>=0.14.0"],
  93. "systemd": ["systemd-python>=231"],
  94. "url_preview": ["lxml>=3.5.0"],
  95. "sentry": ["sentry-sdk>=0.7.2"],
  96. "opentracing": ["jaeger-client>=4.0.0", "opentracing>=2.2.0"],
  97. "jwt": ["pyjwt>=1.6.4"],
  98. # hiredis is not a *strict* dependency, but it makes things much faster.
  99. # (if it is not installed, we fall back to slow code.)
  100. "redis": ["txredisapi>=1.4.7", "hiredis"],
  101. }
  102. ALL_OPTIONAL_REQUIREMENTS = set() # type: Set[str]
  103. for name, optional_deps in CONDITIONAL_REQUIREMENTS.items():
  104. # Exclude systemd as it's a system-based requirement.
  105. # Exclude lint as it's a dev-based requirement.
  106. if name not in ["systemd"]:
  107. ALL_OPTIONAL_REQUIREMENTS = set(optional_deps) | ALL_OPTIONAL_REQUIREMENTS
  108. def list_requirements():
  109. return list(set(REQUIREMENTS) | ALL_OPTIONAL_REQUIREMENTS)
  110. class DependencyException(Exception):
  111. @property
  112. def message(self):
  113. return "\n".join(
  114. [
  115. "Missing Requirements: %s" % (", ".join(self.dependencies),),
  116. "To install run:",
  117. " pip install --upgrade --force %s" % (" ".join(self.dependencies),),
  118. "",
  119. ]
  120. )
  121. @property
  122. def dependencies(self):
  123. for i in self.args[0]:
  124. yield "'" + i + "'"
  125. def check_requirements(for_feature=None):
  126. deps_needed = []
  127. errors = []
  128. if for_feature:
  129. reqs = CONDITIONAL_REQUIREMENTS[for_feature]
  130. else:
  131. reqs = REQUIREMENTS
  132. for dependency in reqs:
  133. try:
  134. _check_requirement(dependency)
  135. except VersionConflict as e:
  136. deps_needed.append(dependency)
  137. errors.append(
  138. "Needed %s, got %s==%s"
  139. % (
  140. dependency,
  141. e.dist.project_name, # type: ignore[attr-defined] # noqa
  142. e.dist.version, # type: ignore[attr-defined] # noqa
  143. )
  144. )
  145. except DistributionNotFound:
  146. deps_needed.append(dependency)
  147. if for_feature:
  148. errors.append(
  149. "Needed %s for the '%s' feature but it was not installed"
  150. % (dependency, for_feature)
  151. )
  152. else:
  153. errors.append("Needed %s but it was not installed" % (dependency,))
  154. if not for_feature:
  155. # Check the optional dependencies are up to date. We allow them to not be
  156. # installed.
  157. OPTS = sum(CONDITIONAL_REQUIREMENTS.values(), []) # type: List[str]
  158. for dependency in OPTS:
  159. try:
  160. _check_requirement(dependency)
  161. except VersionConflict as e:
  162. deps_needed.append(dependency)
  163. errors.append(
  164. "Needed optional %s, got %s==%s"
  165. % (
  166. dependency,
  167. e.dist.project_name, # type: ignore[attr-defined] # noqa
  168. e.dist.version, # type: ignore[attr-defined] # noqa
  169. )
  170. )
  171. except DistributionNotFound:
  172. # If it's not found, we don't care
  173. pass
  174. if deps_needed:
  175. for err in errors:
  176. logging.error(err)
  177. raise DependencyException(deps_needed)
  178. def _check_requirement(dependency_string):
  179. """Parses a dependency string, and checks if the specified requirement is installed
  180. Raises:
  181. VersionConflict if the requirement is installed, but with the the wrong version
  182. DistributionNotFound if nothing is found to provide the requirement
  183. """
  184. req = Requirement.parse(dependency_string)
  185. # first check if the markers specify that this requirement needs installing
  186. if req.marker is not None and not req.marker.evaluate():
  187. # not required for this environment
  188. return
  189. get_provider(req)
  190. if __name__ == "__main__":
  191. import sys
  192. sys.stdout.writelines(req + "\n" for req in list_requirements())