python_dependencies.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 typing import Set
  18. from pkg_resources import (
  19. DistributionNotFound,
  20. Requirement,
  21. VersionConflict,
  22. get_provider,
  23. )
  24. logger = logging.getLogger(__name__)
  25. # REQUIREMENTS is a simple list of requirement specifiers[1], and must be
  26. # installed. It is passed to setup() as install_requires in setup.py.
  27. #
  28. # CONDITIONAL_REQUIREMENTS is the optional dependencies, represented as a dict
  29. # of lists. The dict key is the optional dependency name and can be passed to
  30. # pip when installing. The list is a series of requirement specifiers[1] to be
  31. # installed when that optional dependency requirement is specified. It is passed
  32. # to setup() as extras_require in setup.py
  33. #
  34. # [1] https://pip.pypa.io/en/stable/reference/pip_install/#requirement-specifiers.
  35. REQUIREMENTS = [
  36. "jsonschema>=2.5.1",
  37. "frozendict>=1",
  38. "unpaddedbase64>=1.1.0",
  39. "canonicaljson>=1.1.3",
  40. "signedjson>=1.0.0",
  41. "pynacl>=1.2.1",
  42. "idna>=2.5",
  43. # validating SSL certs for IP addresses requires service_identity 18.1.
  44. "service_identity>=18.1.0",
  45. # Twisted 18.9 introduces some logger improvements that the structured
  46. # logger utilises
  47. "Twisted>=18.9.0",
  48. "treq>=15.1",
  49. # Twisted has required pyopenssl 16.0 since about Twisted 16.6.
  50. "pyopenssl>=16.0.0",
  51. "pyyaml>=3.11",
  52. "pyasn1>=0.1.9",
  53. "pyasn1-modules>=0.0.7",
  54. "daemonize>=2.3.1",
  55. "bcrypt>=3.1.0",
  56. "pillow>=4.3.0",
  57. "sortedcontainers>=1.4.4",
  58. "psutil>=2.0.0",
  59. "pymacaroons>=0.13.0",
  60. "msgpack>=0.5.2",
  61. "phonenumbers>=8.2.0",
  62. "six>=1.10",
  63. "prometheus_client>=0.0.18,<0.8.0",
  64. # we use attr.s(slots), which arrived in 16.0.0
  65. # Twisted 18.7.0 requires attrs>=17.4.0
  66. "attrs>=17.4.0",
  67. "netaddr>=0.7.18",
  68. "Jinja2>=2.9",
  69. "bleach>=1.4.3",
  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. "systemd": ["systemd-python>=231"],
  87. "url_preview": ["lxml>=3.5.0"],
  88. "test": ["mock>=2.0", "parameterized"],
  89. "sentry": ["sentry-sdk>=0.7.2"],
  90. "opentracing": ["jaeger-client>=4.0.0", "opentracing>=2.2.0"],
  91. "jwt": ["pyjwt>=1.6.4"],
  92. }
  93. ALL_OPTIONAL_REQUIREMENTS = set() # type: Set[str]
  94. for name, optional_deps in CONDITIONAL_REQUIREMENTS.items():
  95. # Exclude systemd as it's a system-based requirement.
  96. if name not in ["systemd"]:
  97. ALL_OPTIONAL_REQUIREMENTS = set(optional_deps) | ALL_OPTIONAL_REQUIREMENTS
  98. def list_requirements():
  99. return list(set(REQUIREMENTS) | ALL_OPTIONAL_REQUIREMENTS)
  100. class DependencyException(Exception):
  101. @property
  102. def message(self):
  103. return "\n".join(
  104. [
  105. "Missing Requirements: %s" % (", ".join(self.dependencies),),
  106. "To install run:",
  107. " pip install --upgrade --force %s" % (" ".join(self.dependencies),),
  108. "",
  109. ]
  110. )
  111. @property
  112. def dependencies(self):
  113. for i in self.args[0]:
  114. yield "'" + i + "'"
  115. def check_requirements(for_feature=None):
  116. deps_needed = []
  117. errors = []
  118. if for_feature:
  119. reqs = CONDITIONAL_REQUIREMENTS[for_feature]
  120. else:
  121. reqs = REQUIREMENTS
  122. for dependency in reqs:
  123. try:
  124. _check_requirement(dependency)
  125. except VersionConflict as e:
  126. deps_needed.append(dependency)
  127. errors.append(
  128. "Needed %s, got %s==%s"
  129. % (dependency, e.dist.project_name, e.dist.version)
  130. )
  131. except DistributionNotFound:
  132. deps_needed.append(dependency)
  133. if for_feature:
  134. errors.append(
  135. "Needed %s for the '%s' feature but it was not installed"
  136. % (dependency, for_feature)
  137. )
  138. else:
  139. errors.append("Needed %s but it was not installed" % (dependency,))
  140. if not for_feature:
  141. # Check the optional dependencies are up to date. We allow them to not be
  142. # installed.
  143. OPTS = sum(CONDITIONAL_REQUIREMENTS.values(), [])
  144. for dependency in OPTS:
  145. try:
  146. _check_requirement(dependency)
  147. except VersionConflict as e:
  148. deps_needed.append(dependency)
  149. errors.append(
  150. "Needed optional %s, got %s==%s"
  151. % (dependency, e.dist.project_name, e.dist.version)
  152. )
  153. except DistributionNotFound:
  154. # If it's not found, we don't care
  155. pass
  156. if deps_needed:
  157. for err in errors:
  158. logging.error(err)
  159. raise DependencyException(deps_needed)
  160. def _check_requirement(dependency_string):
  161. """Parses a dependency string, and checks if the specified requirement is installed
  162. Raises:
  163. VersionConflict if the requirement is installed, but with the the wrong version
  164. DistributionNotFound if nothing is found to provide the requirement
  165. """
  166. req = Requirement.parse(dependency_string)
  167. # first check if the markers specify that this requirement needs installing
  168. if req.marker is not None and not req.marker.evaluate():
  169. # not required for this environment
  170. return
  171. get_provider(req)
  172. if __name__ == "__main__":
  173. import sys
  174. sys.stdout.writelines(req + "\n" for req in list_requirements())