__init__.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 json
  19. import os
  20. import sys
  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, 7):
  25. print("Synapse requires Python 3.7 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. # Use the standard library json implementation instead of simplejson.
  52. try:
  53. from canonicaljson import set_json_library
  54. set_json_library(json)
  55. except ImportError:
  56. pass
  57. import synapse.util
  58. __version__ = synapse.util.SYNAPSE_VERSION
  59. if bool(os.environ.get("SYNAPSE_TEST_PATCH_LOG_CONTEXTS", False)):
  60. # We import here so that we don't have to install a bunch of deps when
  61. # running the packaging tox test.
  62. from synapse.util.patch_inline_callbacks import do_patch
  63. do_patch()
  64. check_rust_lib_up_to_date()