__init__.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # Copyright 2014-2016 OpenMarket Ltd
  2. # Copyright 2018-9 New Vector Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. """ This is a reference implementation of a Matrix homeserver.
  16. """
  17. import json
  18. import os
  19. import sys
  20. from synapse.util.rust import check_rust_lib_up_to_date
  21. from synapse.util.stringutils import strtobool
  22. # Check that we're not running on an unsupported Python version.
  23. if sys.version_info < (3, 7):
  24. print("Synapse requires Python 3.7 or above.")
  25. sys.exit(1)
  26. # Allow using the asyncio reactor via env var.
  27. if strtobool(os.environ.get("SYNAPSE_ASYNC_IO_REACTOR", "0")):
  28. from incremental import Version
  29. import twisted
  30. # We need a bugfix that is included in Twisted 21.2.0:
  31. # https://twistedmatrix.com/trac/ticket/9787
  32. if twisted.version < Version("Twisted", 21, 2, 0):
  33. print("Using asyncio reactor requires Twisted>=21.2.0")
  34. sys.exit(1)
  35. import asyncio
  36. from twisted.internet import asyncioreactor
  37. asyncioreactor.install(asyncio.get_event_loop())
  38. # Twisted and canonicaljson will fail to import when this file is executed to
  39. # get the __version__ during a fresh install. That's OK and subsequent calls to
  40. # actually start Synapse will import these libraries fine.
  41. try:
  42. from twisted.internet import protocol
  43. from twisted.internet.protocol import Factory
  44. from twisted.names.dns import DNSDatagramProtocol
  45. protocol.Factory.noisy = False
  46. Factory.noisy = False
  47. DNSDatagramProtocol.noisy = False
  48. except ImportError:
  49. pass
  50. # Use the standard library json implementation instead of simplejson.
  51. try:
  52. from canonicaljson import set_json_library
  53. set_json_library(json)
  54. except ImportError:
  55. pass
  56. import synapse.util
  57. __version__ = synapse.util.SYNAPSE_VERSION
  58. if bool(os.environ.get("SYNAPSE_TEST_PATCH_LOG_CONTEXTS", False)):
  59. # We import here so that we don't have to install a bunch of deps when
  60. # running the packaging tox test.
  61. from synapse.util.patch_inline_callbacks import do_patch
  62. do_patch()
  63. check_rust_lib_up_to_date()