#!/usr/bin/python3 # Standard library modules import sys import os import re import json import argparse import urllib.request as request from urllib.error import HTTPError, URLError from argparse import ArgumentParser, RawTextHelpFormatter from os.path import dirname, basename, isdir, exists from shutil import which, rmtree, copytree, copyfile try: from PIL import Image except: has_PIL = False else: has_PIL = True # Local Matter modules from utils import * from svg2png import inkscape_convert_svg2png, magick_convert_svg2png # Configuration constants MIN_PYTHON_VERSION = (3, 6) # Mainly for f-strings THEME_NAME = "Matter" THEME_DESCRIPTION = ( "Matter is a minimalist grub theme originally inspired by material design 2.\n" "Run this script without arguments for next steps on installing Matter." ) if exists("/boot/grub"): BOOT_GRUB_PATH = "/boot/grub" elif exists("/boot/grub2"): BOOT_GRUB_PATH = "/boot/grub2" else: error("Could not find your grub's boot path (tried /boot/grub and /boot/grub2)") INSTALLER_ABSPATH = os.path.abspath(__file__) INSTALLER_NAME = basename(INSTALLER_ABSPATH) INSTALLER_DIR = dirname(INSTALLER_ABSPATH) INSTALLATION_SOURCE_DIR = f"{INSTALLER_DIR}/{THEME_NAME}" INSTALLATION_TARGET_DIR = f"{BOOT_GRUB_PATH}/themes/{THEME_NAME}" THEME_DEFAULT_HIGHLIGHT = "pink" THEME_DEFAULT_FOREGROUND = "white" THEME_DEFAULT_BACKGROUND = "bluegrey-900" THEME_DEFAULT_FONT_NAME = "Josefin Sans Regular" THEME_DEFAULT_FONT = THEME_DEFAULT_FONT_NAME.replace(" ", "_") THEME_DEFAULT_FONT_SIZE = 32 GRUB_DEFAULTS_PATH = "/etc/default/grub" GRUB_SCRIPTS_PATH = "/etc/grub.d" GRUB_CFG_PATH = f"{BOOT_GRUB_PATH}/grub.cfg" GRUB_MKCONFIG_PATH = which("grub-mkconfig") or which("grub2-mkconfig") if GRUB_MKCONFIG_PATH is None: error("Could not find grub-mkconfig command file (grub2-mkconfig neither)") THEME_TEMPLATE_PATH = f"{INSTALLER_DIR}/theme.txt.template" GRUB_DEFAULTS_TEMPLATE_PATH = f"{INSTALLER_DIR}/grub.template" HOOKCHECK_TEMPLATE_PATH = f"{INSTALLER_DIR}/hookcheck.py.template" THEME_OVERRIDES_TITLE = f"{THEME_NAME} Theme Overrides" BEGIN_THEME_OVERRIDES = f"### BEGIN {THEME_OVERRIDES_TITLE}" END_THEME_OVERRIDES = f"### END {THEME_OVERRIDES_TITLE}" ICON_SVG_PATHF = f"{INSTALLER_DIR}/icons/{{}}.svg" ICON_PNG_PATHF = f"{INSTALLATION_SOURCE_DIR}/icons/{{}}.png" BACKGROUND_TMP_PATHF = f"{INSTALLER_DIR}/bg/{{}}.tmp" BACKGROUND_PNG_PATHF = f"{INSTALLER_DIR}/bg/{{}}.png" CONFIG_FILE_PATH = f"{INSTALLER_DIR}/config.json" PALETTE = { "red": "f44336", "pink": "e91e63", "purple": "9c27b0", "deeppurple": "673ab7", "indigo": "3f51b5", "blue": "2196f3", "lightblue": "03a9f4", "cyan": "00bcd4", "teal": "009688", "green": "4caf50", "lightgreen": "8bc34a", "lime": "cddc39", "yellow": "ffeb3b", "amber": "ffc107", "orange": "ff9800", "deeporange": "ff5722", "brown": "795548", "grey": "9e9e9e", "bluegrey": "607d8b", "white": "ffffff", "black": "000000", # Custom default colors "white-350": "9E9E9E", "bluegrey-900": "263238", } AVAILABLE_COLORS = list(PALETTE.keys()) MDI_CDN = "https://raw.githubusercontent.com/Templarian/MaterialDesign-SVG/master/svg/" # Global user arguments set in main() user_args: argparse.Namespace # Utils def check_python_version(): installed = (sys.version_info.major, sys.version_info.minor) required = MIN_PYTHON_VERSION if installed < required: error(f"Python {required[0]}.{required[1]} or later required") def check_root_or_prompt(): if os.geteuid() != 0: info("Request root access") exit_code = sh("sudo -v") if exit_code != 0: error("Could not verify root access, you could try with sudo") # Relaunch the program with sudo args = " ".join(sys.argv[1:]) child_exit_code = sh(f"sudo {INSTALLER_DIR}/{INSTALLER_NAME} {args}") exit(child_exit_code) # Propagate exit code def delete_dir(directory): if isdir(directory): rmtree(directory) def read_cleaned_grub_defaults(): # Read previous defaults with open(GRUB_DEFAULTS_PATH, "r", newline="") as f: grub_defaults = f.read() # Remove previous theme defaults cleaned_grub_defaults = re.sub( f"\n*{BEGIN_THEME_OVERRIDES}.*{END_THEME_OVERRIDES}\n*", "", grub_defaults, flags=re.DOTALL, ) return cleaned_grub_defaults def read_cleaned_grub_mkconfig(): # Read previous defaults with open(GRUB_MKCONFIG_PATH, "r", newline="") as f: grub_mkconfig = f.read() # Remove previous theme defaults cleaned_grub_mkconfig = re.sub( f"\n*{BEGIN_THEME_OVERRIDES}.*{END_THEME_OVERRIDES}\n*", "", grub_mkconfig, flags=re.DOTALL, ) return cleaned_grub_mkconfig def download_icon(icon_name): info(f"Download {icon_name}.svg") url = f"{MDI_CDN}{icon_name}.svg" try: with request.urlopen(url) as f: response = f.read() except HTTPError as err: # A subclass of URLError error(f"Couldn't get icon {icon_name} ({err.reason})", f"At URL {err.geturl()}") except URLError as err: error(f"Couldn't get icon {icon_name} ({err.reason})") svg_path = ICON_SVG_PATHF.format(icon_name) with open(svg_path, "wb") as f: f.write(response) return svg_path def download_background(background_path): if not has_PIL: error("PIL not detected, cannot download background") info(f"Downloading background image") url = f"{background_path}" try: with request.urlopen(url) as f: response = f.read() except HTTPError as err: # A subclass of URLError error(f"Couldn't get background image ({err.reason})", f"At URL {err.geturl()}") except URLError as err: error(f"Couldn't get background image ({err.reason})") bg_path = BACKGROUND_TMP_PATHF.format('background_image') conv_path = BACKGROUND_PNG_PATHF.format('background_image') with open(bg_path, "wb") as f: f.write(response) im = Image.open(bg_path) im.save(conv_path) return conv_path def get_converted_icons(): return [ filename[:-4] # Remove .png for filename in os.listdir(f"{INSTALLATION_SOURCE_DIR}/icons/") if filename.endswith(".png") ] def is_icon_downloaded(icon_name): svg_path = ICON_SVG_PATHF.format(icon_name) return exists(svg_path) def convert_icon_svg2png(icon_name, whisper=False): if not has_command("inkscape"): if not has_command("convert"): error( "Stop. Both `inkscape` and `convert` command from imagemagick were not found", "Consider installing `inkscape` for the best results", ) else: command = "convert" else: command = "inkscape" color = ( parse_color(user_args.iconcolor) if user_args.iconcolor else parse_color(user_args.foreground) ) src_path = ICON_SVG_PATHF.format(icon_name) dst_path = ICON_PNG_PATHF.format(icon_name) if command == "convert": warning("Resulting icons could look a bit off, consider installing inkscape") converter = magick_convert_svg2png elif command == "inkscape": converter = inkscape_convert_svg2png exit_code = converter(color, src_path, dst_path, whisper=whisper) if exit_code != 0: error(f"Stop. The `{command}` command returned an error") def get_available_fonts(): "Returns the fonts present in /fonts" return [ filename[:-4] # Remove .ttf for filename in os.listdir(f"{INSTALLER_DIR}/fonts/") if filename.endswith(".ttf") ] def parse_color(color_string): if color_string in AVAILABLE_COLORS: color = PALETTE[color_string] elif re.match(r"[0-9A-Fa-f]{6}", color_string) is not None: color = color_string else: error( f"Invalid color parsed from {color_string}", f"Color must be an hex code like C00FFE or one of: {AVAILABLE_COLORS}", ) return f"#{color}" def check_icon_converted(icon): available_icons = get_converted_icons() if icon not in available_icons + ["_"]: error(f"Invalid icon name: {icon}", f"Icons present are: {available_icons}") return icon def parse_font(font): """From a given --font check if available and return its font name e.g. Open_Sans_Regular to Open Sans Regular""" available_fonts = get_available_fonts() if font not in available_fonts: error( f"Invalid font name: {font}", f"Available font names: {available_fonts}", ) return font.replace("_", " ") # Procedures def clean_install_dir(): info("Clean install directory") if isdir(INSTALLATION_TARGET_DIR): rmtree(INSTALLATION_TARGET_DIR) def prepare_source_dir(): info("Build theme from user preferences") # Get user color preferences highlight = parse_color(user_args.highlight) foreground = parse_color(user_args.foreground) background = parse_color( THEME_DEFAULT_BACKGROUND if user_args.background is None else user_args.background ) image = ( user_args.image if user_args.downloadbackground is None else download_background(user_args.downloadbackground) ) fontkey = user_args.font fontfile = user_args.fontfile fontname = user_args.fontname fontsize = user_args.fontsize icons = user_args.icons # Image checks if image: if not exists(image): error(f"{image} does not exist") if os.path.splitext(image)[1] not in (".png", ".jpg", ".jpeg", ".tga"): error("Background image must be one of .png, .jpg, .jpeg or .tga formats.") image_name = basename(image) copyfile(image, f"{INSTALLATION_SOURCE_DIR}/{image_name}") if user_args.background: warning( f"Both --background and --image arguments specified. Background color {background} will be ignored." ) else: image_name = "background.png" # Icon checks # Get entries from grub.cfg entries = get_entry_names() # Do icon count match grub entry count? if len(icons) != len(entries): error( f"You must specify {len(entries)} icons ({len(icons)} provided) for entries:", should_exit=False, ) for i, m in enumerate(entries): print(f"{i + 1}. {m['entryname']}") exit(1) # Font checks # grub-mkfont present grub_mkfont = which("grub-mkfont") or which("grub2-mkfont") if grub_mkfont is None: error(f"grub-mkfont command not found in your system (grub2-mkfont neither)") # Valid font arguments if fontfile is None: # User did not specify custom font file fontfile = f"{INSTALLER_DIR}/fonts/{fontkey}.ttf" fontname = f"{parse_font(fontkey)} {fontsize}" elif not fontfile.endswith(".ttf"): # font file is not ttf error(f"{fontfile} is not a .ttf file") elif fontname is None: # User did, but did not gave its name error( "--fontname/-fn not specified for given font file.", "e.g. 'Open Sans Regular'", ) else: # User specified a custom fontfile fontname = " ".join(fontname) # e.g. Open Sans Regular dst_fontfile = f"{INSTALLER_DIR}/fonts/{fontname.replace(' ', '_')}.ttf" # e.g. .../Open_Sans_Regular.ttf copyfile(fontfile, dst_fontfile) fontfile = dst_fontfile fontname = f"{fontname} {fontsize}" # e.g. Open Sans Regular 32 # Prepare Icons # Download not-yet-downloaded icons for icon in icons: if not is_icon_downloaded(icon) and icon != "_": download_icon(icon) # Convert icons info("Convert icons") for i, icon in enumerate(icons): if icon != "_": if i == 0: convert_icon_svg2png(icon) else: convert_icon_svg2png(icon, whisper=True) # Prepare Font # Generate font file info("Build font") stdout = shout( f"{grub_mkfont} -o {INSTALLATION_SOURCE_DIR}/font.pf2 {fontfile} -s {fontsize}" ) if stdout: error( f"{grub_mkfont} execution was not clean", f"for fontfile: {fontfile}", ) # Prepare Theme.txt # Parse theme template with user preferences with open(THEME_TEMPLATE_PATH, "r", newline="") as f: template = f.read() context = { "theme_name": THEME_NAME, "highlight": highlight, "foreground": foreground, "background": background, "image_name": image_name, "fontname": fontname, } parsed_theme = template.format(**context) if image: parsed_theme = parsed_theme.replace("# desktop-image", "desktop-image") theme_file_path = f"{INSTALLATION_SOURCE_DIR}/theme.txt" with open(theme_file_path, "w") as f: f.write(parsed_theme) def prepare_target_dir(): info("Prepare installation directory") clean_install_dir() def copy_source_to_target(): info("Copy built theme to installation directory") copytree(INSTALLATION_SOURCE_DIR, INSTALLATION_TARGET_DIR) def update_grub_cfg(): info("Update grub.cfg") update_command = ( which("update-grub") or which("grub-mkconfig") or which("grub2-mkconfig") ) if update_command is None: error( f"Command for generating grub.cfg not found (tried update-grub, grub-mkconfig and grub2-mkconfig)" ) command = f"{update_command} -o {GRUB_CFG_PATH}" info(f"Remake grub.cfg with {command}") sh(command) def update_grub_defaults(): info(f"Patch {GRUB_DEFAULTS_PATH} with {THEME_OVERRIDES_TITLE}") grub_configs = read_cleaned_grub_defaults() # Parse grub defaults template, append parsed contents, and write back with open(GRUB_DEFAULTS_TEMPLATE_PATH, "r", newline="") as f: template = f.read() context = {"installation_dir": INSTALLATION_TARGET_DIR} parsed_extra_grub = template.format(**context) grub_configs += ( f"\n\n{BEGIN_THEME_OVERRIDES}\n{parsed_extra_grub}\n{END_THEME_OVERRIDES}\n\n" ) with open(GRUB_DEFAULTS_PATH, "w") as f: f.write(grub_configs) def clean_grub_defaults(): info(f"Clean {THEME_OVERRIDES_TITLE} from {GRUB_DEFAULTS_PATH}") cleaned_grub_defaults = read_cleaned_grub_defaults() with open(GRUB_DEFAULTS_PATH, "w") as f: f.write(cleaned_grub_defaults) def clean_grub_mkconfig(): info(f"Clean {THEME_OVERRIDES_TITLE} from {GRUB_MKCONFIG_PATH}") cleaned_grub_mkconfig = read_cleaned_grub_mkconfig() with open(GRUB_MKCONFIG_PATH, "w") as f: f.write(cleaned_grub_mkconfig) def clean_hookcheck(): info(f"Remove hookcheck script from {GRUB_SCRIPTS_PATH}") hookcheck = f"{GRUB_SCRIPTS_PATH}/99_matter" if exists(hookcheck): os.remove(hookcheck) def get_entry_names(): "Gets the entry names from grub.cfg contents" with open(GRUB_CFG_PATH, "r", newline="") as f: grub_cfg = f.read() pattern = ( r"(?P
(?:submenu|menuentry) ?)" # menuentry or submenu r"(?:\"|')" # " or ' r"(?P