__main__.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. """Entry point. Checks for user and starts main script"""
  2. # Friendly Telegram (telegram userbot)
  3. # Copyright (C) 2018-2021 The Authors
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Affero General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU Affero General Public License for more details.
  12. # You should have received a copy of the GNU Affero General Public License
  13. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. # █ █ ▀ █▄▀ ▄▀█ █▀█ ▀
  15. # █▀█ █ █ █ █▀█ █▀▄ █
  16. # © Copyright 2022
  17. # https://t.me/hikariatama
  18. #
  19. # 🔒 Licensed under the GNU AGPLv3
  20. # 🌐 https://www.gnu.org/licenses/agpl-3.0.html
  21. import atexit
  22. import getpass
  23. import os
  24. import subprocess
  25. import sys
  26. if (
  27. getpass.getuser() == "root"
  28. and "--root" not in " ".join(sys.argv)
  29. and "OKTETO" not in os.environ
  30. and "DOCKER" not in os.environ
  31. ):
  32. print("🚫" * 15)
  33. print("You attempted to run Hikka on behalf of root user")
  34. print("Please, create a new user and restart script")
  35. print("If this action was intentional, pass --root argument instead")
  36. print("🚫" * 15)
  37. print()
  38. print("Type force_insecure to ignore this warning")
  39. if input("> ").lower() != "force_insecure":
  40. sys.exit(1)
  41. def deps(error):
  42. print(f"{str(error)}\n🔄 Attempting dependencies installation... Just wait ⏱")
  43. subprocess.run(
  44. [
  45. sys.executable,
  46. "-m",
  47. "pip",
  48. "install",
  49. "--upgrade",
  50. "-q",
  51. "--disable-pip-version-check",
  52. "--no-warn-script-location",
  53. "-r",
  54. "requirements.txt",
  55. ],
  56. check=True,
  57. )
  58. restart()
  59. def restart():
  60. if "HIKKA_DO_NOT_RESTART" in os.environ:
  61. print("Got in a loop, exiting")
  62. sys.exit(0)
  63. print("🔄 Restarting...")
  64. atexit.register(
  65. lambda: os.execl(
  66. sys.executable,
  67. sys.executable,
  68. "-m",
  69. os.path.relpath(
  70. os.path.abspath(
  71. os.path.dirname(
  72. os.path.abspath(__file__),
  73. ),
  74. ),
  75. ),
  76. *(sys.argv[1:]),
  77. )
  78. )
  79. os.environ["HIKKA_DO_NOT_RESTART"] = "1"
  80. sys.exit(0)
  81. if sys.version_info < (3, 8, 0):
  82. print("🚫 Error: you must use at least Python version 3.8.0")
  83. elif __package__ != "hikka": # In case they did python __main__.py
  84. print("🚫 Error: you cannot run this as a script; you must execute as a package")
  85. else:
  86. try:
  87. import telethon # noqa: F401
  88. except Exception:
  89. pass
  90. else:
  91. try:
  92. # This is used as verification markers to ensure that supported
  93. # version is installed
  94. from telethon.tl.types import MessageEntityCustomEmoji # noqa: F401
  95. from telethon.extensions.html import CUSTOM_EMOJIS # noqa: F401
  96. import telethon
  97. if tuple(map(int, telethon.__version__.split("."))) < (1, 24, 8):
  98. raise ImportError
  99. except ImportError:
  100. print("🔄 Reinstalling Hikka-specific telethon version")
  101. subprocess.run(
  102. [
  103. sys.executable,
  104. "-m",
  105. "pip",
  106. "uninstall",
  107. "-y",
  108. "telethon",
  109. "telethon-mod",
  110. ],
  111. check=True,
  112. )
  113. subprocess.run(
  114. [
  115. sys.executable,
  116. "-m",
  117. "pip",
  118. "install",
  119. "--force-reinstall",
  120. "-q",
  121. "--disable-pip-version-check",
  122. "--no-warn-script-location",
  123. "hikka-tl",
  124. ],
  125. check=True,
  126. )
  127. restart()
  128. try:
  129. from . import log
  130. log.init()
  131. from . import main
  132. except ModuleNotFoundError as e: # pragma: no cover
  133. deps(e)
  134. if __name__ == "__main__":
  135. if "HIKKA_DO_NOT_RESTART" in os.environ:
  136. del os.environ["HIKKA_DO_NOT_RESTART"]
  137. main.hikka.main() # Execute main function