2022-08-30 20:24:54 +03:00
|
|
|
|
|
|
|
from watchdog.events import FileSystemEventHandler
|
|
|
|
from watchdog.events import FileSystemEvent
|
|
|
|
import psutil
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import logging
|
|
|
|
|
|
|
|
logger = logging.getLogger('TeleBot')
|
|
|
|
|
|
|
|
class EventHandler(FileSystemEventHandler):
|
|
|
|
def on_any_event(self, event: FileSystemEvent):
|
2022-09-30 22:22:21 +03:00
|
|
|
logger.info('* Detected changes in: %s, reloading', (event.src_path))
|
2022-08-30 20:24:54 +03:00
|
|
|
restart_file()
|
|
|
|
|
|
|
|
def restart_file():
|
|
|
|
try:
|
|
|
|
p = psutil.Process(os.getpid())
|
|
|
|
for handler in p.open_files() + p.connections():
|
|
|
|
os.close(handler.fd)
|
2022-12-13 17:44:41 +03:00
|
|
|
except OSError:
|
|
|
|
pass
|
2022-08-30 20:24:54 +03:00
|
|
|
except Exception as e:
|
|
|
|
logger.error(e)
|
|
|
|
|
|
|
|
python = sys.executable
|
2022-12-13 17:44:41 +03:00
|
|
|
|
|
|
|
if os.name == 'nt':
|
|
|
|
os.execv(sys.executable, ['python'] + sys.argv)
|
|
|
|
else:
|
|
|
|
os.execl(python, python, *sys.argv)
|