fix: rework zipping of files, better build CI

Thanks to @npv12 for this PR!
This commit is contained in:
Pranav Santhosh
2022-12-11 10:39:45 +05:30
committed by GitHub
parent 35597b98d7
commit f523cb1881
6 changed files with 146 additions and 110 deletions

View File

@@ -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:

View File

@@ -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)

View File

@@ -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)

View File

@@ -1,20 +1,26 @@
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
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]}"
@@ -29,7 +35,8 @@ def create_theme(type: str, accent: str, dest: str = tmp_dir, link: bool = False
try:
# Rename colloid generated files as per catppuccin
new_filename = f"{theme_name}-{type.capitalize()}-{size.capitalize()}-{accent.capitalize()}"
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()}"
@@ -37,20 +44,19 @@ def create_theme(type: str, accent: str, dest: str = tmp_dir, link: bool = False
if size == 'compact':
filename += '-Compact'
try:
shutil.rmtree(dest + "/" + new_filename + '-hdpi')
shutil.rmtree(dest + "/" + new_filename + '-xhdpi')
shutil.rmtree(dest + "/" + new_filename)
shutil.rmtree(new_filename + '-hdpi')
shutil.rmtree(new_filename + '-xhdpi')
shutil.rmtree(new_filename)
except:
pass
os.rename(dest + "/" + filename + '-hdpi',
dest + "/" + new_filename + '-hdpi')
new_filename + '-hdpi')
os.rename(dest + "/" + filename + '-xhdpi',
dest + "/" + new_filename + '-xhdpi')
os.rename(dest + "/" + filename, dest + "/" + new_filename)
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 ""
if link:
try:
@@ -63,15 +69,16 @@ def create_theme(type: str, accent: str, dest: str = tmp_dir, link: bool = False
os.makedirs(f"{HOME}/.config/gtk-4.0")
except FileExistsError:
pass
os.symlink(f"{dest + '/' + new_filename}/gtk-4.0/assets",
os.symlink(f"{new_filename}/gtk-4.0/assets",
f"{HOME}/.config/gtk-4.0/assets")
os.symlink(f"{dest + '/' + new_filename}/gtk-4.0/gtk.css",
os.symlink(f"{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",
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)
return ""
return new_filename
if zip:
foldernames = [new_filename, new_filename + '-xhdpi', new_filename + '-hdpi']
zip_multiple_folders(foldernames, new_filename + ".zip", not link)

View File

@@ -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

View File

@@ -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()