From f523cb18818cc7ad7d6f73074f77e3976a549249 Mon Sep 17 00:00:00 2001 From: Pranav Santhosh <59979143+npv12@users.noreply.github.com> Date: Sun, 11 Dec 2022 10:39:45 +0530 Subject: [PATCH] fix: rework zipping of files, better build CI Thanks to @npv12 for this PR! --- .github/workflows/release.yml | 2 +- build.py | 17 ----- install.py | 83 +++++++++++++--------- scripts/create_theme.py | 125 ++++++++++++++++++---------------- scripts/ctp_colors.py | 4 +- scripts/utils.py | 25 +++++++ 6 files changed, 146 insertions(+), 110 deletions(-) delete mode 100644 build.py diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index fb6111db..65983b9f 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -21,7 +21,7 @@ jobs: - name: Install dependencies run: pip install -r requirements.txt - name: Generate themes - run: python ./build.py + run: python ./install.py all -a all --zip - name: Add zips to release uses: softprops/action-gh-release@v1 with: diff --git a/build.py b/build.py deleted file mode 100644 index d27fbf04..00000000 --- a/build.py +++ /dev/null @@ -1,17 +0,0 @@ -import shutil -from zipfile import ZipFile - -from scripts.create_theme import create_theme -from scripts.ctp_colors import ctp_colors, get_all_accent -from scripts.var import tmp_dir - -for flavor, value in ctp_colors.items(): - for accent in get_all_accent(value).keys(): - print("\n\n\nCreating theme for", flavor, "with accent", accent) - filename = tmp_dir + '/' + create_theme(flavor, accent) - filenames = [filename, filename + '-xhdpi', filename + '-hdpi'] - - with ZipFile(filename + '.zip', 'w') as zip: - for filename in filenames: - zip.write(filename) - shutil.rmtree(filename) diff --git a/install.py b/install.py index 8ef2df9b..71956e76 100644 --- a/install.py +++ b/install.py @@ -7,64 +7,85 @@ Usage: """ import argparse +from scripts.ctp_colors import ctp_colors, get_all_accent from scripts.create_theme import create_theme from scripts.var import theme_name, tmp_dir -parser = argparse.ArgumentParser(description='Catppuccin theme') -parser.add_argument('type', - metavar='theme type', +parser = argparse.ArgumentParser(description="Catppuccin theme") +parser.add_argument("flavor", + metavar="theme flavor", type=str, - choices=['mocha', 'frappe', 'macchiato', 'latte'], - help='Type of the theme to apply. Can be frappe, mocha, macchiato, latte') + nargs="+", + choices=["mocha", "frappe", "macchiato", "latte", "all"], + help="Flavor of the theme to apply. Can be frappe, mocha, macchiato, latte") -parser.add_argument('--name', '-n', - metavar='theme name', +parser.add_argument("--name", "-n", + metavar="theme name", type=str, default=theme_name, dest="name", - help='Name of the theme to apply. Defaults to Catppuccin') + help="Name of the theme to apply. Defaults to Catppuccin") -parser.add_argument('--dest', '-d', - metavar='destination', +parser.add_argument("--dest", "-d", + metavar="destination", type=str, default=tmp_dir, dest="dest", - help='Destination of the files. defaults to releases folder inside the root') + help="Destination of the files. defaults to releases folder inside the root") -parser.add_argument('--accent', '-a', - metavar='Accent of the theme', +parser.add_argument("--accent", "-a", + metavar="Accent of the theme", type=str, - default="blue", + nargs="+", + default=["blue"], dest="accent", - choices=['rosewater', 'flamingo', 'pink', 'mauve', 'red', 'maroon', 'peach', - 'yellow', 'green', 'teal', 'sky', 'sapphire', 'blue', 'lavender'], - help="Accent of the theme. Can include 'rosewater', 'flamingo', 'pink', 'mauve', 'red', 'maroon', 'peach', 'yellow', 'green', 'teal', 'sky', 'sapphire', 'blue', 'lavender'") + choices=["rosewater", "flamingo", "pink", "mauve", "red", "maroon", "peach", + "yellow", "green", "teal", "sky", "sapphire", "blue", "lavender", "all"], + help="Accent of the theme. Can include 'rosewater', 'flamingo', 'pink', 'mauve', 'red', 'maroon', \ + 'peach', 'yellow', 'green', 'teal', 'sky', 'sapphire', 'blue', 'lavender'") parser.add_argument("--size", "-s", - metavar='Size of the theme', + metavar="Size of the theme", type=str, default="standard", dest="size", - choices=['standard', 'compact'], - help='Size variant of the theme. Can be standard or compact') + choices=["standard", "compact"], + help="Size variant of the theme. Can be standard or compact") -parser.add_argument('--tweaks', - metavar='Colloid specific tweaks', +parser.add_argument("--tweaks", + metavar="Colloid specific tweaks", type=str, default=[], - nargs='+', + nargs="+", dest="tweaks", - choices=['black', 'rimless', 'normal'], - help='Some specifc tweaks. like black, rimless, normal buttons') + choices=["black", "rimless", "normal"], + help="Some specifc tweaks. like black, rimless, normal buttons") -parser.add_argument('--link', - help='Link advaita themes to our catppuccin theme', +parser.add_argument("-l", "--link", + help="Link advaita themes to our catppuccin theme", type=bool, - default=True, + default=False, action=argparse.BooleanOptionalAction, - dest="link",) + dest="link") + +parser.add_argument("--zip", + help="Zip catppuccin theme", + type=bool, + default=False, + action=argparse.BooleanOptionalAction, + dest="zip") args = parser.parse_args() -filename = create_theme(args.type, args.accent, args.dest, - args.link, args.name, args.size, args.tweaks) +if "all" in args.flavor: + flavors = ctp_colors.keys() +else: + flavors = args.flavor + +if "all" in args.accent: + accents = get_all_accent().keys() +else: + accents = args.accent + +filename = create_theme(flavors, accents, args.dest, + args.link, args.name, args.size, args.tweaks, args.zip) diff --git a/scripts/create_theme.py b/scripts/create_theme.py index 99ad580d..f82a7c71 100644 --- a/scripts/create_theme.py +++ b/scripts/create_theme.py @@ -1,77 +1,84 @@ import os import shutil import subprocess +from typing import List from scripts.ctp_colors import ctp_colors from scripts.recolor import recolor +from scripts.utils import zip_multiple_folders from scripts.var import def_color_map, theme_name, tmp_dir, work_dir -def create_theme(type: str, accent: str, dest: str = tmp_dir, link: bool = False, name: str = theme_name, size: str = "standard", tweaks=[]) -> str: +def create_theme(types: List[str], accents: List[str], dest: str = tmp_dir, link: bool = False, + name: str = theme_name, size: str = "standard", tweaks=[], zip = False) -> None: try: - os.makedirs(tmp_dir) # Create our temporary directory + os.makedirs(dest) # Create our destination directory except FileExistsError: pass - - # Recolor colloid wrt our selection like mocha. latte - recolor(ctp_colors[type], accent) - theme_style: str = "light" if type == "latte" else "dark" - install_cmd: str = f"./install.sh -c {theme_style} -s {size} -n {name} -d {dest} -t {def_color_map[accent]}" - if tweaks: - install_cmd += f" --tweaks {' '.join([tweak for tweak in tweaks])}" - os.chdir(work_dir) - # Install the theme globally for you - subprocess.call(install_cmd, shell=True) - # reset colloid repo to original state - subprocess.call("git reset --hard HEAD", shell=True) + for type in types: + for accent in accents: + # Recolor colloid wrt our selection like mocha. latte + print(type, accent) + recolor(ctp_colors[type], accent) + theme_style: str = "light" if type == "latte" else "dark" + install_cmd: str = f"./install.sh -c {theme_style} -s {size} -n {name} -d {dest} -t {def_color_map[accent]}" + if tweaks: + install_cmd += f" --tweaks {' '.join([tweak for tweak in tweaks])}" - try: - # Rename colloid generated files as per catppuccin - new_filename = f"{theme_name}-{type.capitalize()}-{size.capitalize()}-{accent.capitalize()}" - filename = f"{theme_name}" - if def_color_map[accent] != 'default': - filename += f"-{def_color_map[accent].capitalize()}" - filename += f"-{theme_style.capitalize()}" - if size == 'compact': - filename += '-Compact' - try: - shutil.rmtree(dest + "/" + new_filename + '-hdpi') - shutil.rmtree(dest + "/" + new_filename + '-xhdpi') - shutil.rmtree(dest + "/" + new_filename) - except: - pass - os.rename(dest + "/" + filename + '-hdpi', - dest + "/" + new_filename + '-hdpi') - os.rename(dest + "/" + filename + '-xhdpi', - dest + "/" + new_filename + '-xhdpi') - os.rename(dest + "/" + filename, dest + "/" + new_filename) - print("Successfully renamed file") - except Exception as e: - print("Failed to rename the files due to:", e) - return "" - - if link: - try: - # Attempte relinking all the libadvaita files - subprocess.call( - 'rm -rf "${HOME}/.config/gtk-4.0/"{assets,gtk.css,gtk-dark.css}', shell=True) - HOME = os.path.expanduser('~') + os.chdir(work_dir) + # Install the theme globally for you + subprocess.call(install_cmd, shell=True) + # reset colloid repo to original state + subprocess.call("git reset --hard HEAD", shell=True) try: - os.makedirs(f"{HOME}/.config/gtk-4.0") - except FileExistsError: - pass - os.symlink(f"{dest + '/' + new_filename}/gtk-4.0/assets", - f"{HOME}/.config/gtk-4.0/assets") - os.symlink(f"{dest + '/' + new_filename}/gtk-4.0/gtk.css", - f"{HOME}/.config/gtk-4.0/gtk.css") - os.symlink(f"{dest + '/' + new_filename}/gtk-4.0/gtk-dark.css", - f"{HOME}/.config/gtk-4.0/gtk-dark.css") - print("Successfully created symlinks for libadvaita") - except Exception as e: - print("Failed to link due to :", e) - return "" + # Rename colloid generated files as per catppuccin + new_filename = dest + \ + f"/{theme_name}-{type.capitalize()}-{size.capitalize()}-{accent.capitalize()}" + filename = f"{theme_name}" + if def_color_map[accent] != 'default': + filename += f"-{def_color_map[accent].capitalize()}" + filename += f"-{theme_style.capitalize()}" + if size == 'compact': + filename += '-Compact' + try: + shutil.rmtree(new_filename + '-hdpi') + shutil.rmtree(new_filename + '-xhdpi') + shutil.rmtree(new_filename) + except: + pass + os.rename(dest + "/" + filename + '-hdpi', + new_filename + '-hdpi') + os.rename(dest + "/" + filename + '-xhdpi', + new_filename + '-xhdpi') + os.rename(dest + "/" + filename, new_filename) + print("Successfully renamed file") + except Exception as e: + print("Failed to rename the files due to:", e) - return new_filename + if link: + try: + # Attempte relinking all the libadvaita files + subprocess.call( + 'rm -rf "${HOME}/.config/gtk-4.0/"{assets,gtk.css,gtk-dark.css}', shell=True) + HOME = os.path.expanduser('~') + + try: + os.makedirs(f"{HOME}/.config/gtk-4.0") + except FileExistsError: + pass + os.symlink(f"{new_filename}/gtk-4.0/assets", + f"{HOME}/.config/gtk-4.0/assets") + os.symlink(f"{new_filename}/gtk-4.0/gtk.css", + f"{HOME}/.config/gtk-4.0/gtk.css") + os.symlink(f"{new_filename}/gtk-4.0/gtk-dark.css", + f"{HOME}/.config/gtk-4.0/gtk-dark.css") + print("Successfully created symlinks for libadvaita") + except Exception as e: + print("Failed to link due to :", e) + + if zip: + foldernames = [new_filename, new_filename + '-xhdpi', new_filename + '-hdpi'] + zip_multiple_folders(foldernames, new_filename + ".zip", not link) diff --git a/scripts/ctp_colors.py b/scripts/ctp_colors.py index 27476a8e..788537ff 100644 --- a/scripts/ctp_colors.py +++ b/scripts/ctp_colors.py @@ -7,9 +7,9 @@ ctp_colors = { "macchiato": Flavour.macchiato() } -def get_all_accent(flavor): +def get_all_accent(): accent = {} - for key, value in flavor.__dict__.items(): + for key, value in Flavour.latte().__dict__.items(): if key not in ['white', 'black', 'text', 'subtext0', 'subtext1', 'overlay0', 'overlay1', 'overlay2', 'surface0', 'surface1', 'surface2', 'base', 'mantle', 'crust']: accent[key] = value diff --git a/scripts/utils.py b/scripts/utils.py index ec4026f5..df883643 100644 --- a/scripts/utils.py +++ b/scripts/utils.py @@ -1,4 +1,7 @@ +import os import re +import shutil +import zipfile def replacetext(file_name: str, search_text: str, replace_text: str) -> None: @@ -20,3 +23,25 @@ def replacetext(file_name: str, search_text: str, replace_text: str) -> None: f.seek(0) f.write(file) f.truncate() + + +def zipdir(path, ziph): + """ + Takes in a oath of a directory and zips it in a ziph. + Util to zip a directory. + Thanks https://stackoverflow.com/questions/46229764/python-zip-multiple-directories-into-one-zip-file + """ + for root, _, files in os.walk(path): + for file in files: + ziph.write(os.path.join(root, file), + os.path.relpath(os.path.join(root, file), + os.path.join(path, '..'))) + + +def zip_multiple_folders(dir_list, zip_name, remove = True): + zipf = zipfile.ZipFile(zip_name, 'w', zipfile.ZIP_DEFLATED) + for dir in dir_list: + zipdir(dir, zipf) + if remove: + shutil.rmtree(dir) + zipf.close()