python_dependencies.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. "prometheus_client>=0.0.18,<0.9.0",
  66. # we use attr.validators.deep_iterable, which arrived in 19.1.0 (Note:
  67. # Fedora 31 only has 19.1, so if we want to upgrade we should wait until 33
  68. # is out in November.)
  69. "attrs>=19.1.0",
  70. "netaddr>=0.7.18",
  71. "Jinja2>=2.9",
  72. "bleach>=1.4.3",
  73. "typing-extensions>=3.7.4",
  74. ]
  75. CONDITIONAL_REQUIREMENTS = {
  76. "matrix-synapse-ldap3": ["matrix-synapse-ldap3>=0.1"],
  77. # we use execute_batch, which arrived in psycopg 2.7.
  78. "postgres": ["psycopg2>=2.7"],
  79. # ACME support is required to provision TLS certificates from authorities
  80. # that use the protocol, such as Let's Encrypt.
  81. "acme": [
  82. "txacme>=0.9.2",
  83. # txacme depends on eliot. Eliot 1.8.0 is incompatible with
  84. # python 3.5.2, as per https://github.com/itamarst/eliot/issues/418
  85. 'eliot<1.8.0;python_version<"3.5.3"',
  86. ],
  87. "saml2": ["pysaml2>=4.5.0"],
  88. "oidc": ["authlib>=0.14.0"],
  89. "systemd": ["systemd-python>=231"],
  90. "url_preview": ["lxml>=3.5.0"],
  91. "sentry": ["sentry-sdk>=0.7.2"],
  92. "opentracing": ["jaeger-client>=4.0.0", "opentracing>=2.2.0"],
  93. "jwt": ["pyjwt>=1.6.4"],
  94. # hiredis is not a *strict* dependency, but it makes things much faster.
  95. # (if it is not installed, we fall back to slow code.)
  96. "redis": ["txredisapi>=1.4.7", "hiredis"],
  97. }
  98. ALL_OPTIONAL_REQUIREMENTS = set() # type: Set[str]
  99. for name, optional_deps in CONDITIONAL_REQUIREMENTS.items():
  100. # Exclude systemd as it's a system-based requirement.
  101. # Exclude lint as it's a dev-based requirement.
  102. if name not in ["systemd"]:
  103. ALL_OPTIONAL_REQUIREMENTS = set(optional_deps) | ALL_OPTIONAL_REQUIREMENTS
  104. def list_requirements():
  105. return list(set(REQUIREMENTS) | ALL_OPTIONAL_REQUIREMENTS)
  106. class DependencyException(Exception):
  107. @property
  108. def message(self):
  109. return "\n".join(
  110. [
  111. "Missing Requirements: %s" % (", ".join(self.dependencies),),
  112. "To install run:",
  113. " pip install --upgrade --force %s" % (" ".join(self.dependencies),),
  114. "",
  115. ]
  116. )
  117. @property
  118. def dependencies(self):
  119. for i in self.args[0]:
  120. yield "'" + i + "'"
  121. def check_requirements(for_feature=None):
  122. deps_needed = []
  123. errors = []
  124. if for_feature:
  125. reqs = CONDITIONAL_REQUIREMENTS[for_feature]
  126. else:
  127. reqs = REQUIREMENTS
  128. for dependency in reqs:
  129. try:
  130. _check_requirement(dependency)
  131. except VersionConflict as e:
  132. deps_needed.append(dependency)
  133. errors.append(
  134. "Needed %s, got %s==%s"
  135. % (
  136. dependency,
  137. e.dist.project_name, # type: ignore[attr-defined] # noqa
  138. e.dist.version, # type: ignore[attr-defined] # noqa
  139. )
  140. )
  141. except DistributionNotFound:
  142. deps_needed.append(dependency)
  143. if for_feature:
  144. errors.append(
  145. "Needed %s for the '%s' feature but it was not installed"
  146. % (dependency, for_feature)
  147. )
  148. else:
  149. errors.append("Needed %s but it was not installed" % (dependency,))
  150. if not for_feature:
  151. # Check the optional dependencies are up to date. We allow them to not be
  152. # installed.
  153. OPTS = sum(CONDITIONAL_REQUIREMENTS.values(), []) # type: List[str]
  154. for dependency in OPTS:
  155. try:
  156. _check_requirement(dependency)
  157. except VersionConflict as e:
  158. deps_needed.append(dependency)
  159. errors.append(
  160. "Needed optional %s, got %s==%s"
  161. % (
  162. dependency,
  163. e.dist.project_name, # type: ignore[attr-defined] # noqa
  164. e.dist.version, # type: ignore[attr-defined] # noqa
  165. )
  166. )
  167. except DistributionNotFound:
  168. # If it's not found, we don't care
  169. pass
  170. if deps_needed:
  171. for err in errors:
  172. logging.error(err)
  173. raise DependencyException(deps_needed)
  174. def _check_requirement(dependency_string):
  175. """Parses a dependency string, and checks if the specified requirement is installed
  176. Raises:
  177. VersionConflict if the requirement is installed, but with the the wrong version
  178. DistributionNotFound if nothing is found to provide the requirement
  179. """
  180. req = Requirement.parse(dependency_string)
  181. # first check if the markers specify that this requirement needs installing
  182. if req.marker is not None and not req.marker.evaluate():
  183. # not required for this environment
  184. return
  185. get_provider(req)
  186. if __name__ == "__main__":
  187. import sys
  188. sys.stdout.writelines(req + "\n" for req in list_requirements())