__init__.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. # Check that we're not running on an unsupported Python version.
  22. if sys.version_info < (3, 7):
  23. print("Synapse requires Python 3.7 or above.")
  24. sys.exit(1)
  25. # Allow using the asyncio reactor via env var.
  26. if bool(os.environ.get("SYNAPSE_ASYNC_IO_REACTOR", False)):
  27. try:
  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. except ImportError:
  39. pass
  40. # Twisted and canonicaljson will fail to import when this file is executed to
  41. # get the __version__ during a fresh install. That's OK and subsequent calls to
  42. # actually start Synapse will import these libraries fine.
  43. try:
  44. from twisted.internet import protocol
  45. from twisted.internet.protocol import Factory
  46. from twisted.names.dns import DNSDatagramProtocol
  47. protocol.Factory.noisy = False
  48. Factory.noisy = False
  49. DNSDatagramProtocol.noisy = False
  50. except ImportError:
  51. pass
  52. # Use the standard library json implementation instead of simplejson.
  53. try:
  54. from canonicaljson import set_json_library
  55. set_json_library(json)
  56. except ImportError:
  57. pass
  58. import synapse.util
  59. __version__ = synapse.util.SYNAPSE_VERSION
  60. if bool(os.environ.get("SYNAPSE_TEST_PATCH_LOG_CONTEXTS", False)):
  61. # We import here so that we don't have to install a bunch of deps when
  62. # running the packaging tox test.
  63. from synapse.util.patch_inline_callbacks import do_patch
  64. do_patch()
  65. check_rust_lib_up_to_date()