__main__.py 4.5 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(
  43. "🚫 Error: you have not installed all dependencies correctly.\n"
  44. f"{str(error)}\n"
  45. "🔄 Attempting dependencies installation... Just wait ⏱"
  46. )
  47. subprocess.run(
  48. [
  49. sys.executable,
  50. "-m",
  51. "pip",
  52. "install",
  53. "--upgrade",
  54. "-q",
  55. "--disable-pip-version-check",
  56. "--no-warn-script-location",
  57. "-r",
  58. "requirements.txt",
  59. ],
  60. check=True,
  61. )
  62. restart()
  63. def restart():
  64. if "HIKKA_DO_NOT_RESTART" in os.environ:
  65. print("Got in a loop, exiting")
  66. sys.exit(0)
  67. print("🔄 Restarting...")
  68. atexit.register(
  69. lambda: os.execl(
  70. sys.executable,
  71. sys.executable,
  72. "-m",
  73. os.path.relpath(
  74. os.path.abspath(
  75. os.path.dirname(
  76. os.path.abspath(__file__),
  77. ),
  78. ),
  79. ),
  80. *(sys.argv[1:]),
  81. )
  82. )
  83. os.environ["HIKKA_DO_NOT_RESTART"] = "1"
  84. sys.exit(0)
  85. if sys.version_info < (3, 8, 0):
  86. print("🚫 Error: you must use at least Python version 3.8.0")
  87. elif __package__ != "hikka": # In case they did python __main__.py
  88. print("🚫 Error: you cannot run this as a script; you must execute as a package")
  89. else:
  90. try:
  91. import telethon # noqa: F401
  92. except Exception:
  93. pass
  94. else:
  95. try:
  96. from telethon.tl.functions.messages import SendReactionRequest # noqa: F401
  97. except ImportError:
  98. print(
  99. "⚠️ Warning: Default telethon is used as main one. This can cause"
  100. " errors and enables DAR. Attempting to reinstall telethon-mod..."
  101. )
  102. subprocess.run(
  103. [
  104. sys.executable,
  105. "-m",
  106. "pip",
  107. "uninstall",
  108. "-y",
  109. "telethon",
  110. ],
  111. check=True,
  112. )
  113. subprocess.run(
  114. [
  115. sys.executable,
  116. "-m",
  117. "pip",
  118. "install",
  119. "-U",
  120. "-q",
  121. "--disable-pip-version-check",
  122. "--no-warn-script-location",
  123. "telethon-mod",
  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. sys.exit(1)
  135. if __name__ == "__main__":
  136. if "HIKKA_DO_NOT_RESTART" in os.environ:
  137. del os.environ["HIKKA_DO_NOT_RESTART"]
  138. main.hikka.main() # Execute main function