Files
krowlog/src/install.py
2022-08-30 19:40:06 +02:00

108 lines
3.7 KiB
Python

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("""\
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg width="64" height="64" viewBox="0 0 128 128" version="1.1" xmlns="http://www.w3.org/2000/svg">
<defs>
<linearGradient id="bg1" x1="0%" y1="100%" x2="100%" y2="0%">
<stop offset="0%" style="stop-color:#9f8700"/>
<stop offset="100%" style="stop-color:#ffdb00"/>
</linearGradient>
<linearGradient id="fg1" x1="0%" y1="100%" x2="100%" y2="0%">
<stop offset="0%" style="stop-color:#8caec7"/>
<stop offset="100%" style="stop-color:#6289a3"/>
</linearGradient>
</defs>
<rect x="8" y="8" width="112" height="112" style="fill:url(#fg1)" rx="10"/>
<path style="fill:black"
d="M76,113
L15,113
L15,70
C15,70 25,55 45,36
C53,29 60,26 78,32
C85,34 90,32 90,32
C95,30 108,35 110,40
L110,40
C83,48 80,60 76,71
C74,80 76,113 76,113
z"/>
<rect x="8" y="8" width="112" height="112"
style="fill:none; stroke:url(#bg1); stroke-width:14" rx="10"/>
<circle cx="63" cy="43" r="4.5" fill="white"/>
</svg>""")
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)