__init__.py 3.2 KB

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