__init__.py 2.9 KB

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