From 969b0b9451011c18391c8925db03e91e875fb86b Mon Sep 17 00:00:00 2001 From: developomp Date: Wed, 2 Feb 2022 21:49:06 +0900 Subject: [PATCH] added font install script --- .vscode/settings.json | 1 + src/initialize.py | 14 ++++++--- src/setup/__init__.py | 61 --------------------------------------- src/setup/system/fonts.py | 54 ++++++++++++++++++++++++++++++++++ src/util.py | 22 +++++++++++++- 5 files changed, 86 insertions(+), 66 deletions(-) create mode 100644 src/setup/system/fonts.py diff --git a/.vscode/settings.json b/.vscode/settings.json index 0ad9afd..5fb655d 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -9,6 +9,7 @@ "developomp", "flatpak", "FOSS", + "noto", "pamac", "ungoogled" ], diff --git a/src/initialize.py b/src/initialize.py index 694e1c8..252b27e 100644 --- a/src/initialize.py +++ b/src/initialize.py @@ -10,22 +10,28 @@ def initialize(): log.log("Initializing flatpak") if os.system("sudo pacman -S --noconfirm --needed flatpak &> /dev/null"): - log.error("Failed to install flatpak form archlinux package repository") + log.error("Failed to install flatpak via pacman") exit(1) log.log("Initializing pip") if os.system("sudo pacman -S --noconfirm --needed python-pip &> /dev/null"): - log.error("Failed to install pip from archlinux package repository") + log.error("Failed to install pip via pacman") + exit(1) + + # https://pypi.org/project/requests + log.log("Initializing requests") + if os.system("pip install requests &> /dev/null"): + log.error("Failed to install requests via pip") exit(1) # https://pypi.org/project/PyYAML log.log("Initializing PyYAML") if os.system("pip install PyYAML &> /dev/null"): - log.error("Failed to install PyYAML from pip") + log.error("Failed to install PyYAML via pip") exit(1) # https://github.com/bczsalba/pytermgui log.log("Initializing pytermgui") if os.system("pip install pytermgui &> /dev/null"): - log.error("Failed to install pytermgui from pip") + log.error("Failed to install pytermgui via pip") exit(1) diff --git a/src/setup/__init__.py b/src/setup/__init__.py index e96309d..a918ba8 100644 --- a/src/setup/__init__.py +++ b/src/setup/__init__.py @@ -49,67 +49,6 @@ setup_filezilla() { package_install filezilla } -setup_fonts() { - log "installing fonts" - - # wget: For downloading zip files - # noto-fonts-emoji: Emoji fonts - # nerd-fonts-noto-sans-mono: Terminal font - # ttf-baekmuk: Korean font - - package_install \ - wget \ - noto-fonts-emoji \ - nerd-fonts-noto-sans-mono \ - ttf-baekmuk - - # path to temporarily save font related files - fonts_directory="$SCRIPT_DIR/tmp/fonts" - - # fonts to download - font_names=( - "Audiowide" - "Varela Round" - "Ubuntu Mono" - "Nanum Gothic Coding" - ) - - # create fonts directory if it does not exist - if [ ! -d "$fonts_directory" ]; then - mkdir -p "$fonts_directory" - fi - - # download and unzip font files if they're not downloaded already - for font_name in "${font_names[@]}"; do - zip_path="$fonts_directory/$font_name.zip" - - # download and unzip if either zip file or unzipped directory exists - if [ ! -f "$zip_path" ] && [ ! -d "$fonts_directory/$font_name" ]; then - wget -O "$zip_path" "https://fonts.google.com/download?family=$font_name" # download zip file - unzip "$zip_path" -d "$fonts_directory/$font_name" # unzip file - rm "$zip_path" # remove zip file - fi - done - - font_install_dir="$HOME/.local/share/fonts" - - # create local fonts directory if it does not exist already - if [ ! -d "$font_install_dir" ]; then - mkdir -p "$font_install_dir" - fi - - # "install" all fonts - find "$fonts_directory" -type f -name "*.ttf" | while read ttf_file_path; do - mv -f "$ttf_file_path" "$font_install_dir/${ttf_file_path##*/}" - done - - # regenerate font cache - fc-cache -vf - - # cleanup - rm -rf $fonts_directory -} - setup_fstab() { if cat /etc/fstab | grep "/media/pomp/data" &>/dev/null; then return diff --git a/src/setup/system/fonts.py b/src/setup/system/fonts.py new file mode 100644 index 0000000..ecd5e3d --- /dev/null +++ b/src/setup/system/fonts.py @@ -0,0 +1,54 @@ +from ...util import pamac_install, smart_mkdir, download, unzip +from ...constants import tmp_dir +from shutil import rmtree, move +from os import remove, system +from os.path import exists +import requests +import glob + +name = "fonts" + +# path to temporarily save font related files +TMP_FONTS_DIRECTORY = f"{tmp_dir}/tmp/fonts" + +# fonts to download +FONT_NAMES = ("Audiowide", "Varela Round", "Ubuntu Mono", "Nanum Gothic Coding") + +# where to unzip the fonts +FONT_INSTALL_DIR = "~/.local/share/fonts" + + +def setup(): + """ + System fonts + + noto-fonts-emoji: Emoji fonts + nerd-fonts-noto-sans-mono: Terminal font + ttf-baekmuk: Korean font + """ + + pamac_install(["noto-fonts-emoji", "nerd-fonts-noto-sans-mono", "ttf-baekmuk"]) + + smart_mkdir(TMP_FONTS_DIRECTORY) + + # download and unzip font files if they're not downloaded already + for font_name in FONT_NAMES: + zip_path = f"{TMP_FONTS_DIRECTORY}/{font_name}.zip" + + # download and unzip if either zip file or unzipped directory exists + if exists(zip_path) and exists("{TMP_FONTS_DIRECTORY}/{font_name}"): + download(zip_path, f"https://fonts.google.com/download?family={font_name}") + unzip(zip_path, f"{TMP_FONTS_DIRECTORY}/{font_name}") + remove(zip_path) + + smart_mkdir(FONT_INSTALL_DIR) + + # "install" fonts + for ttf_file_path in glob.glob(f"{TMP_FONTS_DIRECTORY}/**/*.ttf"): + move(ttf_file_path, f"{FONT_INSTALL_DIR}/{ttf_file_path}") + + # regenerate font cache + system("fc-cache -vf") + + # cleanup + rmtree(TMP_FONTS_DIRECTORY) diff --git a/src/util.py b/src/util.py index ba6a2ad..ff2d63b 100644 --- a/src/util.py +++ b/src/util.py @@ -1,6 +1,9 @@ +from .constants import tmp_dir + from os import system, makedirs from shutil import copy -from .constants import tmp_dir +import zipfile + # todo: remove all uses of AUR (use pacman instead of pamac) def pamac_install(packages: str | list[str]) -> None: @@ -33,6 +36,7 @@ def smart_mkdir(path: str): """ Recursively create directories if it doesn't exist already. """ + try: makedirs(path) except OSError: @@ -58,6 +62,22 @@ def load_dconf(file_name: str): system(f'dconf load / < "{tmp_dir}/dconf/{file_name}"') +def download(file_name: str, url: str): + """Downloads a file from a url.""" + r = requests.get(url) + + with open(file_name, "wb") as f: + f.write(r.content) + + +def unzip(zip_path: str, dst_dir: str): + """Unzips a .zip file to a directory.""" + + smart_mkdir(dst_dir) + with zipfile.ZipFile(zip_path, "r") as zip_ref: + zip_ref.extractall(dst_dir) + + """ def setup_essentials(): setup_fstab