Skip to content

Commit

Permalink
Merge branch 'catmanager_logger'
Browse files Browse the repository at this point in the history
  • Loading branch information
georgy7 committed Oct 29, 2024
2 parents 18396df + 96d5842 commit 6a92c86
Showing 1 changed file with 59 additions and 21 deletions.
80 changes: 59 additions & 21 deletions src/catframes/catmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import threading
import platform
import random
import shutil
import time
import signal
import sys
Expand Down Expand Up @@ -110,6 +111,9 @@ def __exit__(self, exc_type, exc_value, traceback):
def has_console() -> bool:
return (sys.stdin is not None) and sys.stdin.isatty()

def compiled() -> bool:
return '__compiled__' in globals()




Expand Down Expand Up @@ -420,27 +424,53 @@ def get_color(self) -> str:
def convert_to_command(self, for_user: bool = False) -> List[str]:
logger = logging.getLogger('catmanager')

windows = (platform.system() == 'Windows')

catframes_py = Path(__file__).parent.resolve() / 'catframes.py'

custom_exe = not (sys.executable.endswith('python.exe') or sys.executable.endswith('pythonw.exe'))
catframes_exe = Path(sys.executable).parent.resolve() / 'catframes.exe'

if for_user:
command = ['catframes']
elif windows and __file__.endswith('catmanager.py') and catframes_py.exists():
logger.info('Using local catframes.py (Windows)')
command = [
str(Path(sys.executable).parent / 'python.exe'),
str(catframes_py)
]
elif windows and custom_exe and catframes_exe.exists():
logger.info('Using local catframes.exe')
command = [str(catframes_exe)]
else:
logger.info('Using Catframes from PATH.')
command = ['catframes']
windows = (platform.system() == 'Windows')

ran_from_sources: bool = ('main.py' == Path(sys.argv[0]).name)

if ran_from_sources:
catframes_py: Path = Path(sys.argv[0]).resolve().parent.parent / 'catframes.py'
else:
catframes_py: Path = Path(sys.argv[0]).resolve().parent / 'catframes.py'

catframes_exe: Path = Path(sys.argv[0]).resolve().parent / 'catframes.exe'

# Здесь не используется sys.executable напрямую,
# поскольку там может быть pythonw.exe.
python_exe: Path = Path(sys.executable).resolve().parent / 'python.exe'

logger.debug(f'\n windows: {windows}')
logger.debug(f' compiled: {compiled()}')
logger.debug(f' ran from sources: {ran_from_sources}')
logger.debug(f' catframes_py: {catframes_py}')
logger.debug(f' catframes_exe: {catframes_exe}')
logger.debug(f' python_exe: {python_exe}\n')

logger.debug(f' catframes_py exists: {catframes_py.exists()}')
logger.debug(f' catframes_exe exists: {catframes_exe.exists()}')
logger.debug(f' python_exe exists: {python_exe.exists()}\n')

if windows and not compiled() and catframes_py.exists():
logger.info('Using local catframes.py (Windows)')
logger.info(f'Python executable: {python_exe}')
command = [str(python_exe), str(catframes_py)]
elif windows and compiled() and catframes_exe.exists():
logger.info('Using local catframes.exe')
command = [str(catframes_exe)]
elif not compiled() and catframes_py.exists() and shutil.which('python'):
logger.info('Using local catframes.py (POSIX)')
logger.info(f'Python executable: python')
command = ['python', str(catframes_py)]
elif not compiled() and catframes_py.exists() and shutil.which('python3'):
logger.info('Using local catframes.py (POSIX)')
logger.info(f'Python executable: python3')
command = ['python3', str(catframes_py)]
else:
logger.info('Using Catframes from PATH.')
command = ['catframes']

for position, text in self._overlays.items():
if text:
Expand Down Expand Up @@ -524,7 +554,9 @@ def __init__(self, command):
logger = logging.getLogger('catmanager')
windows = (sys.platform == 'win32')

if windows and has_console():
# Почему-то при сборке с помощью Nuitka, даже если
# консоль отключена, она определяется как включенная.
if windows and has_console() and not compiled():
# Обработка сигналов завершения в Windows выглядит как большой беспорядок.
# Если убрать флаг CREATE_NEW_PROCESS_GROUP, CTRL+C будет отправляться как в дочерний, так
# и в родительский процесс. Если использовать его — CTRL+C не работает вообще, зато работает CTRL+Break.
Expand Down Expand Up @@ -596,7 +628,7 @@ def kill(self):
logger = logging.getLogger('catmanager')
windows = (sys.platform == 'win32')

if windows and has_console():
if windows and has_console() and not compiled():
# CTRL_C_EVENT is ignored for process groups
# https://learn.microsoft.com/ru-ru/windows/win32/procthread/process-creation-flags
logger.info('Using CTRL+BREAK signal...')
Expand Down Expand Up @@ -2854,9 +2886,15 @@ def main():
logger = logging.getLogger('catmanager')
logger.info('Logging is on.')

logger.info(f'sys.argv[0]: {sys.argv[0]}')
logger.info(f'Executable: {sys.executable}')
logger.info(f'File: {__file__}')
logger.info(f'File: {__file__}\n')

logger.info(f'platform.system(): {platform.system()}')
logger.info(f'sys.platform: {sys.platform}\n')

logger.info(f'Console: {has_console()}')
logger.info(f'Compiled: {compiled()}\n')

root = LocalWM.open(RootWindow, 'root') # открываем главное окно
root.mainloop()
Expand Down

0 comments on commit 6a92c86

Please sign in to comment.