__init__.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. # Check that we're not running on an unsupported Python version.
  21. if sys.version_info < (3, 7):
  22. print("Synapse requires Python 3.7 or above.")
  23. sys.exit(1)
  24. # Allow using the asyncio reactor via env var.
  25. if bool(os.environ.get("SYNAPSE_ASYNC_IO_REACTOR", False)):
  26. try:
  27. from incremental import Version
  28. import twisted
  29. # We need a bugfix that is included in Twisted 21.2.0:
  30. # https://twistedmatrix.com/trac/ticket/9787
  31. if twisted.version < Version("Twisted", 21, 2, 0):
  32. print("Using asyncio reactor requires Twisted>=21.2.0")
  33. sys.exit(1)
  34. import asyncio
  35. from twisted.internet import asyncioreactor
  36. asyncioreactor.install(asyncio.get_event_loop())
  37. except ImportError:
  38. pass
  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()