You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

98 lines
3.1 KiB

  1. # Copyright 2014-2016 OpenMarket Ltd
  2. # Copyright 2018-2019 New Vector Ltd
  3. # Copyright 2023 The Matrix.org Foundation C.I.C.
  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. """ This is an implementation of a Matrix homeserver.
  17. """
  18. import os
  19. import sys
  20. from typing import Any, Dict
  21. from synapse.util.rust import check_rust_lib_up_to_date
  22. from synapse.util.stringutils import strtobool
  23. # Check that we're not running on an unsupported Python version.
  24. #
  25. # Note that we use an (unneeded) variable here so that pyupgrade doesn't nuke the
  26. # if-statement completely.
  27. py_version = sys.version_info
  28. if py_version < (3, 8):
  29. print("Synapse requires Python 3.8 or above.")
  30. sys.exit(1)
  31. # Allow using the asyncio reactor via env var.
  32. if strtobool(os.environ.get("SYNAPSE_ASYNC_IO_REACTOR", "0")):
  33. from incremental import Version
  34. import twisted
  35. # We need a bugfix that is included in Twisted 21.2.0:
  36. # https://twistedmatrix.com/trac/ticket/9787
  37. if twisted.version < Version("Twisted", 21, 2, 0):
  38. print("Using asyncio reactor requires Twisted>=21.2.0")
  39. sys.exit(1)
  40. import asyncio
  41. from twisted.internet import asyncioreactor
  42. asyncioreactor.install(asyncio.get_event_loop())
  43. # Twisted and canonicaljson will fail to import when this file is executed to
  44. # get the __version__ during a fresh install. That's OK and subsequent calls to
  45. # actually start Synapse will import these libraries fine.
  46. try:
  47. from twisted.internet import protocol
  48. from twisted.internet.protocol import Factory
  49. from twisted.names.dns import DNSDatagramProtocol
  50. protocol.Factory.noisy = False
  51. Factory.noisy = False
  52. DNSDatagramProtocol.noisy = False
  53. except ImportError:
  54. pass
  55. # Teach canonicaljson how to serialise immutabledicts.
  56. try:
  57. from canonicaljson import register_preserialisation_callback
  58. from immutabledict import immutabledict
  59. def _immutabledict_cb(d: immutabledict) -> Dict[str, Any]:
  60. try:
  61. return d._dict
  62. except Exception:
  63. # Paranoia: fall back to a `dict()` call, in case a future version of
  64. # immutabledict removes `_dict` from the implementation.
  65. return dict(d)
  66. register_preserialisation_callback(immutabledict, _immutabledict_cb)
  67. except ImportError:
  68. pass
  69. import synapse.util # noqa: E402
  70. __version__ = synapse.util.SYNAPSE_VERSION
  71. if bool(os.environ.get("SYNAPSE_TEST_PATCH_LOG_CONTEXTS", False)):
  72. # We import here so that we don't have to install a bunch of deps when
  73. # running the packaging tox test.
  74. from synapse.util.patch_inline_callbacks import do_patch
  75. do_patch()
  76. check_rust_lib_up_to_date()