import logging import os import sys import textwrap from pathlib import Path log = logging.getLogger("install") def install(): try: if sys.platform == 'win32' or sys.platform == 'cygwin': _windows_set_icon() else: if _is_executable(): _linux_install_desktop_file() _linux_install_icon() except: log.exception("failed to install system stuff") def _is_executable() -> bool: """ Checks if we run from an executable (as opposed to from source by calling 'python krowlog.py') :return: True if this process is run from a distribution binary """ return Path(sys.executable).name == 'krowlog' def _linux_install_desktop_file(): apps_dir = Path.home() / ".local" / "share" / "applications" if apps_dir.exists() and apps_dir.is_dir(): krowlog_desktop_file = apps_dir / "krowlog.desktop" if not krowlog_desktop_file.exists(): desktop_file_data = textwrap.dedent("""\ [Desktop Entry] Type=Application Version=1.5 Terminal=false Name=KrowLog GenericName=Log Viewer Comment=A viewer for text files of arbitrary size. Comment[de]=Betrachter für Textdateien beliebiger Größe. Categories=Qt;Development;Utility;TextTools;IDE;Viewer; Keywords=Text;Viewer; MimeType=text/plain;text/x-log; Icon=krowlog Exec={binary} %F """.format(binary=Path(sys.executable).resolve())) krowlog_desktop_file.write_text(desktop_file_data, "utf8") def _linux_install_icon(): local_share = Path.home().joinpath(".local").joinpath("share") if local_share.is_dir(): icon_dir = local_share / "icons" / "hicolor" / "scalable" / "apps" _linux_install_icon_to_path(icon_dir / "krowlog.svg") def _linux_install_icon_to_path(path: Path): if path.exists(): return if not path.parent.exists(): path.parent.mkdir(parents=True, exist_ok=True) svg = textwrap.dedent("""\ """) path.write_text(svg, "utf8") pass def _windows_set_icon(): # make icon appear in Windows # see https://stackoverflow.com/questions/1551605/how-to-set-applications-taskbar-icon-in-windows-7/1552105#1552105 if sys.platform == 'win32' or sys.platform == 'cygwin': myappid = 'krowlog' # arbitrary string import ctypes ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid)