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.
 
 
 
 
 
 

115 lines
3.5 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 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. # Update your remotes folks.
  27. announcement = """
  28. Synapse is no longer being developed under the matrix-org organization. See the
  29. README.rst for more details.
  30. Please update your git remote to pull from element-hq/synapse:
  31. git remote set-url origin git@github.com:element-hq/synapse.git
  32. """
  33. print(announcement)
  34. sys.exit(1)
  35. # Check that we're not running on an unsupported Python version.
  36. #
  37. # Note that we use an (unneeded) variable here so that pyupgrade doesn't nuke the
  38. # if-statement completely.
  39. py_version = sys.version_info
  40. if py_version < (3, 8):
  41. print("Synapse requires Python 3.8 or above.")
  42. sys.exit(1)
  43. # Allow using the asyncio reactor via env var.
  44. if strtobool(os.environ.get("SYNAPSE_ASYNC_IO_REACTOR", "0")):
  45. from incremental import Version
  46. import twisted
  47. # We need a bugfix that is included in Twisted 21.2.0:
  48. # https://twistedmatrix.com/trac/ticket/9787
  49. if twisted.version < Version("Twisted", 21, 2, 0):
  50. print("Using asyncio reactor requires Twisted>=21.2.0")
  51. sys.exit(1)
  52. import asyncio
  53. from twisted.internet import asyncioreactor
  54. asyncioreactor.install(asyncio.get_event_loop())
  55. # Twisted and canonicaljson will fail to import when this file is executed to
  56. # get the __version__ during a fresh install. That's OK and subsequent calls to
  57. # actually start Synapse will import these libraries fine.
  58. try:
  59. from twisted.internet import protocol
  60. from twisted.internet.protocol import Factory
  61. from twisted.names.dns import DNSDatagramProtocol
  62. protocol.Factory.noisy = False
  63. Factory.noisy = False
  64. DNSDatagramProtocol.noisy = False
  65. except ImportError:
  66. pass
  67. # Teach canonicaljson how to serialise immutabledicts.
  68. try:
  69. from canonicaljson import register_preserialisation_callback
  70. from immutabledict import immutabledict
  71. def _immutabledict_cb(d: immutabledict) -> Dict[str, Any]:
  72. try:
  73. return d._dict
  74. except Exception:
  75. # Paranoia: fall back to a `dict()` call, in case a future version of
  76. # immutabledict removes `_dict` from the implementation.
  77. return dict(d)
  78. register_preserialisation_callback(immutabledict, _immutabledict_cb)
  79. except ImportError:
  80. pass
  81. import synapse.util # noqa: E402
  82. __version__ = synapse.util.SYNAPSE_VERSION
  83. if bool(os.environ.get("SYNAPSE_TEST_PATCH_LOG_CONTEXTS", False)):
  84. # We import here so that we don't have to install a bunch of deps when
  85. # running the packaging tox test.
  86. from synapse.util.patch_inline_callbacks import do_patch
  87. do_patch()
  88. check_rust_lib_up_to_date()