_internal.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # ©️ Dan Gazizullin, 2021-2023
  2. # This file is a part of Hikka Userbot
  3. # 🌐 https://github.com/hikariatama/Hikka
  4. # You can redistribute it and/or modify it under the terms of the GNU AGPLv3
  5. # 🔑 https://www.gnu.org/licenses/agpl-3.0.html
  6. import asyncio
  7. import atexit
  8. import logging
  9. import os
  10. import random
  11. import signal
  12. import sys
  13. async def fw_protect():
  14. await asyncio.sleep(random.randint(1000, 3000) / 1000)
  15. def get_startup_callback() -> callable:
  16. return lambda *_: os.execl(
  17. sys.executable,
  18. sys.executable,
  19. "-m",
  20. os.path.relpath(os.path.abspath(os.path.dirname(os.path.abspath(__file__)))),
  21. *sys.argv[1:],
  22. )
  23. def die():
  24. """Platform-dependent way to kill the current process group"""
  25. if "DOCKER" in os.environ:
  26. sys.exit(0)
  27. else:
  28. # This one is actually better, because it kills all subprocesses
  29. # but it can't be used inside the Docker
  30. os.killpg(os.getpgid(os.getpid()), signal.SIGTERM)
  31. def restart():
  32. if "HIKKA_DO_NOT_RESTART" in os.environ:
  33. print(
  34. "Got in a loop, exiting\nYou probably need to manually remove existing"
  35. " packages and then restart Hikka. Run `pip uninstall -y telethon"
  36. " telethon-mod hikka-tl pyrogram hikka-pyro`, then restart Hikka."
  37. )
  38. sys.exit(0)
  39. logging.getLogger().setLevel(logging.CRITICAL)
  40. print("🔄 Restarting...")
  41. if "LAVHOST" in os.environ:
  42. os.system("lavhost restart")
  43. return
  44. os.environ["HIKKA_DO_NOT_RESTART"] = "1"
  45. if "DOCKER" in os.environ:
  46. atexit.register(get_startup_callback())
  47. else:
  48. # This one is requried for better way of killing to work properly,
  49. # since we kill the process group using unix signals
  50. signal.signal(signal.SIGTERM, get_startup_callback())
  51. die()
  52. def print_banner(banner: str):
  53. print("\033[2J\033[3;1f")
  54. with open(
  55. os.path.abspath(
  56. os.path.join(
  57. os.path.dirname(__file__),
  58. "..",
  59. "assets",
  60. banner,
  61. )
  62. ),
  63. "r",
  64. ) as f:
  65. print(f.read())