python_dependencies.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. "six>=1.10",
  64. "prometheus_client>=0.0.18,<0.8.0",
  65. # we use attr.s(slots), which arrived in 16.0.0
  66. # Twisted 18.7.0 requires attrs>=17.4.0
  67. "attrs>=17.4.0",
  68. "netaddr>=0.7.18",
  69. "Jinja2>=2.9",
  70. "bleach>=1.4.3",
  71. "typing-extensions>=3.7.4",
  72. ]
  73. CONDITIONAL_REQUIREMENTS = {
  74. "matrix-synapse-ldap3": ["matrix-synapse-ldap3>=0.1"],
  75. # we use execute_batch, which arrived in psycopg 2.7.
  76. "postgres": ["psycopg2>=2.7"],
  77. # ConsentResource uses select_autoescape, which arrived in jinja 2.9
  78. "resources.consent": ["Jinja2>=2.9"],
  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. "systemd": ["systemd-python>=231"],
  89. "url_preview": ["lxml>=3.5.0"],
  90. "test": ["mock>=2.0", "parameterized"],
  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. }
  95. ALL_OPTIONAL_REQUIREMENTS = set() # type: Set[str]
  96. for name, optional_deps in CONDITIONAL_REQUIREMENTS.items():
  97. # Exclude systemd as it's a system-based requirement.
  98. if name not in ["systemd"]:
  99. ALL_OPTIONAL_REQUIREMENTS = set(optional_deps) | ALL_OPTIONAL_REQUIREMENTS
  100. def list_requirements():
  101. return list(set(REQUIREMENTS) | ALL_OPTIONAL_REQUIREMENTS)
  102. class DependencyException(Exception):
  103. @property
  104. def message(self):
  105. return "\n".join(
  106. [
  107. "Missing Requirements: %s" % (", ".join(self.dependencies),),
  108. "To install run:",
  109. " pip install --upgrade --force %s" % (" ".join(self.dependencies),),
  110. "",
  111. ]
  112. )
  113. @property
  114. def dependencies(self):
  115. for i in self.args[0]:
  116. yield "'" + i + "'"
  117. def check_requirements(for_feature=None):
  118. deps_needed = []
  119. errors = []
  120. if for_feature:
  121. reqs = CONDITIONAL_REQUIREMENTS[for_feature]
  122. else:
  123. reqs = REQUIREMENTS
  124. for dependency in reqs:
  125. try:
  126. _check_requirement(dependency)
  127. except VersionConflict as e:
  128. deps_needed.append(dependency)
  129. errors.append(
  130. "Needed %s, got %s==%s"
  131. % (
  132. dependency,
  133. e.dist.project_name, # type: ignore[attr-defined] # noqa
  134. e.dist.version, # type: ignore[attr-defined] # noqa
  135. )
  136. )
  137. except DistributionNotFound:
  138. deps_needed.append(dependency)
  139. if for_feature:
  140. errors.append(
  141. "Needed %s for the '%s' feature but it was not installed"
  142. % (dependency, for_feature)
  143. )
  144. else:
  145. errors.append("Needed %s but it was not installed" % (dependency,))
  146. if not for_feature:
  147. # Check the optional dependencies are up to date. We allow them to not be
  148. # installed.
  149. OPTS = sum(CONDITIONAL_REQUIREMENTS.values(), []) # type: List[str]
  150. for dependency in OPTS:
  151. try:
  152. _check_requirement(dependency)
  153. except VersionConflict as e:
  154. deps_needed.append(dependency)
  155. errors.append(
  156. "Needed optional %s, got %s==%s"
  157. % (
  158. dependency,
  159. e.dist.project_name, # type: ignore[attr-defined] # noqa
  160. e.dist.version, # type: ignore[attr-defined] # noqa
  161. )
  162. )
  163. except DistributionNotFound:
  164. # If it's not found, we don't care
  165. pass
  166. if deps_needed:
  167. for err in errors:
  168. logging.error(err)
  169. raise DependencyException(deps_needed)
  170. def _check_requirement(dependency_string):
  171. """Parses a dependency string, and checks if the specified requirement is installed
  172. Raises:
  173. VersionConflict if the requirement is installed, but with the the wrong version
  174. DistributionNotFound if nothing is found to provide the requirement
  175. """
  176. req = Requirement.parse(dependency_string)
  177. # first check if the markers specify that this requirement needs installing
  178. if req.marker is not None and not req.marker.evaluate():
  179. # not required for this environment
  180. return
  181. get_provider(req)
  182. if __name__ == "__main__":
  183. import sys
  184. sys.stdout.writelines(req + "\n" for req in list_requirements())