2022-03-22 10:06:13 +01:00
|
|
|
#! /usr/bin/env python3
|
|
|
|
|
2023-05-23 22:28:56 +02:00
|
|
|
from os import defpath, listdir, environ as env, chdir, getcwd
|
2022-03-22 10:06:13 +01:00
|
|
|
from os.path import isdir, isfile, join, dirname, abspath
|
2021-11-01 22:37:21 +01:00
|
|
|
from json import dump, dumps, loads, load
|
|
|
|
import subprocess
|
2023-05-23 22:28:56 +02:00
|
|
|
from hashlib import md5
|
2022-03-22 10:06:13 +01:00
|
|
|
|
|
|
|
chdir(dirname(abspath(__file__))) # Set working dir
|
2021-11-01 22:37:21 +01:00
|
|
|
|
2023-05-23 22:28:56 +02:00
|
|
|
def get_shas(output) -> dict[str, str]:
|
2021-11-01 22:37:21 +01:00
|
|
|
"""Returns a dict of CSS files and SHAs"""
|
2022-03-22 10:06:13 +01:00
|
|
|
output_lines = output.splitlines() if output else []
|
2021-11-01 22:37:21 +01:00
|
|
|
sha_dict = {}
|
|
|
|
for line in output_lines:
|
|
|
|
line = line.decode('utf-8').replace("0\t", "").split(" ")
|
|
|
|
sha = line[1]
|
|
|
|
css_file = [file for file in line[2].split("/") if "css" in file][-1]
|
|
|
|
sha_dict.update({css_file: sha})
|
|
|
|
return(sha_dict)
|
|
|
|
|
|
|
|
|
2023-05-23 22:28:56 +02:00
|
|
|
def get_md5_hash(file_path) -> str:
|
|
|
|
"""Returns the MD5 hash of a file"""
|
|
|
|
md5_hash = md5()
|
|
|
|
with open(file_path, "rb") as f:
|
|
|
|
for byte_block in iter(lambda: f.read(4096), b""):
|
|
|
|
md5_hash.update(byte_block)
|
|
|
|
return md5_hash.hexdigest()
|
|
|
|
|
|
|
|
def create_addons_json() -> str:
|
|
|
|
#addon_shas = subprocess.check_output(["git", "ls-files", "-s", "./css/addons/*.css"]) if isdir(".git") else []
|
|
|
|
#SHAS = get_shas(addon_shas)
|
2021-11-01 22:37:21 +01:00
|
|
|
ADDONS = {"addons": {}}
|
|
|
|
addon_root = './css/addons'
|
|
|
|
addon_folders = [name for name in listdir(
|
|
|
|
addon_root) if isdir(join(addon_root, name))]
|
|
|
|
for app in addon_folders:
|
2023-08-20 18:00:06 +02:00
|
|
|
app_addons = [addon for addon in listdir(f"{addon_root}/{app}") if isdir(f"{addon_root}/{app}/{addon}")]
|
2021-11-01 22:37:21 +01:00
|
|
|
ADDONS["addons"].update({
|
|
|
|
app: {
|
|
|
|
addon: {} for addon in app_addons
|
|
|
|
}
|
|
|
|
})
|
|
|
|
for addon in app_addons:
|
|
|
|
files = [file for file in listdir(
|
|
|
|
f"{addon_root}/{app}/{addon}") if isfile(join(f"{addon_root}/{app}/{addon}", file))]
|
|
|
|
if len([f for f in files if f.endswith('.css')]) > 1:
|
|
|
|
ADDONS["addons"][app][addon].update({
|
2023-05-23 22:28:56 +02:00
|
|
|
"css": [f"{scheme}://{DOMAIN}/css/addons/{app}/{addon}/{file}?sha={get_md5_hash(join(getcwd(),'css','addons',app,addon,file))}" for file in files if file.split(".")[1] == "css"]
|
2021-11-01 22:37:21 +01:00
|
|
|
}
|
|
|
|
)
|
|
|
|
else:
|
2024-10-03 22:51:56 +02:00
|
|
|
if len([f for f in files if not f.endswith('.css')]) >= 1:
|
|
|
|
ADDONS["addons"][app][addon].update({
|
|
|
|
"files": [f"{scheme}://{DOMAIN}/css/addons/{app}/{addon}/{file}?sha={get_md5_hash(join(getcwd(),'css','addons',app,addon,file))}" for file in files]
|
|
|
|
}
|
|
|
|
)
|
2021-11-01 22:37:21 +01:00
|
|
|
ADDONS["addons"][app].update({
|
2023-05-23 22:28:56 +02:00
|
|
|
addon: f"{scheme}://{DOMAIN}/css/addons/{app}/{addon}/{file}?sha={get_md5_hash(join(getcwd(),'css','addons',app,addon,file))}" for file in files if file.split(".")[1] == "css"
|
2021-11-01 22:37:21 +01:00
|
|
|
}
|
|
|
|
)
|
|
|
|
extra_dirs = [dir for dir in listdir(
|
|
|
|
f"{addon_root}/{app}/{addon}") if isdir(join(f"{addon_root}/{app}/{addon}", dir))]
|
|
|
|
if extra_dirs:
|
|
|
|
for dir in extra_dirs:
|
|
|
|
extra_dir_files = [file for file in listdir(
|
|
|
|
f"{addon_root}/{app}/{addon}/{dir}") if isfile(join(f"{addon_root}/{app}/{addon}/{dir}", file))]
|
|
|
|
ADDONS["addons"][app][addon].update({
|
|
|
|
dir: {
|
2024-10-03 22:51:56 +02:00
|
|
|
"css": [f"{scheme}://{DOMAIN}/css/addons/{app}/{addon}/{dir}/{extra_file}?sha={get_md5_hash(join(getcwd(),'css','addons',app,addon,dir,extra_file))}" for extra_file in extra_dir_files if extra_file.split(".")[1] == "css"],
|
|
|
|
"files": [f"{scheme}://{DOMAIN}/css/addons/{app}/{addon}/{dir}/{extra_file}?sha={get_md5_hash(join(getcwd(),'css','addons',app,addon,dir,extra_file))}" for extra_file in extra_dir_files if extra_file.split(".")[1] != "css"]
|
|
|
|
},
|
2021-11-01 22:37:21 +01:00
|
|
|
}
|
|
|
|
)
|
|
|
|
return dumps(ADDONS, sort_keys=True)
|
|
|
|
|
|
|
|
|
2022-07-10 15:31:02 +02:00
|
|
|
def create_json(app_folders: list = None, themes: list = None, community_themes: list = None ,docker_mods: list = None, no_sub_folders=False) -> str:
|
2021-11-01 22:37:21 +01:00
|
|
|
if no_sub_folders:
|
|
|
|
THEMES_DICT = {}
|
2023-05-23 22:28:56 +02:00
|
|
|
#theme_shas = subprocess.check_output(["git", "ls-files", "-s", "./css/theme-options/*.css"]) if isdir(".git") else []
|
|
|
|
#community_theme_shas = subprocess.check_output(["git", "ls-files", "-s", "./css/community-theme-options/*.css"]) if isdir(".git") else []
|
|
|
|
#THEME_SHAS = get_shas(theme_shas)
|
|
|
|
#COMMUNITY_THEME_SHAS = get_shas(community_theme_shas)
|
2021-11-01 22:37:21 +01:00
|
|
|
THEMES = {
|
|
|
|
theme.split(".")[0].capitalize(): {
|
2023-05-23 22:28:56 +02:00
|
|
|
"url": f"{scheme}://{DOMAIN}/css/theme-options/{theme}?sha={get_md5_hash(join(getcwd(),'css','theme-options', theme))}"
|
2022-07-10 15:31:02 +02:00
|
|
|
}for theme in themes if themes
|
2021-11-01 22:37:21 +01:00
|
|
|
}
|
|
|
|
COMMUNITY_THEMES = {
|
|
|
|
theme.split(".")[0].capitalize(): {
|
2023-05-23 22:28:56 +02:00
|
|
|
"url": f"{scheme}://{DOMAIN}/css/community-theme-options/{theme}?sha={get_md5_hash(join(getcwd(),'css','community-theme-options', theme))}"
|
2022-07-10 15:31:02 +02:00
|
|
|
}for theme in community_themes if community_themes
|
2021-11-01 22:37:21 +01:00
|
|
|
}
|
|
|
|
THEMES_DICT.update(dict(sorted({
|
|
|
|
"themes": {
|
|
|
|
**THEMES
|
|
|
|
},
|
|
|
|
"community-themes": {
|
|
|
|
**COMMUNITY_THEMES
|
|
|
|
},
|
|
|
|
"all-themes": {
|
|
|
|
**THEMES, **COMMUNITY_THEMES
|
|
|
|
}
|
|
|
|
}.items())))
|
|
|
|
return dumps(THEMES_DICT)
|
|
|
|
else:
|
|
|
|
ADDONS = loads(create_addons_json())
|
|
|
|
APPS = {}
|
2023-05-23 22:28:56 +02:00
|
|
|
#app_shas = subprocess.check_output(["git", "ls-files", "-s", "./css/base/*base.css"]) if isdir(".git") else []
|
|
|
|
#SHAS = get_shas(app_shas)
|
2021-11-01 22:37:21 +01:00
|
|
|
APPS.update(dict(sorted({
|
|
|
|
"applications": {
|
|
|
|
app: {
|
2023-05-23 22:28:56 +02:00
|
|
|
"base_css": f"{scheme}://{DOMAIN}/css/base/{app}/{app}-base.css?sha={get_md5_hash(join('css','base', app, f'{app}-base.css'))}",
|
2021-11-01 22:37:21 +01:00
|
|
|
"addons": ADDONS["addons"][app] if app in ADDONS["addons"] else {}
|
|
|
|
} for app in app_folders if not isfile(f'./css/base/{app}/.deprecated')
|
|
|
|
}
|
|
|
|
}.items())))
|
|
|
|
APPS.update(dict(sorted({
|
|
|
|
"deprecated": {
|
|
|
|
app: {
|
2023-05-23 22:28:56 +02:00
|
|
|
"base_css": f"{scheme}://{DOMAIN}/css/base/{app}/{app}-base.css?sha={get_md5_hash(join('css','base', app, f'{app}-base.css'))}",
|
2021-11-01 22:37:21 +01:00
|
|
|
"addons": ADDONS["addons"][app] if app in ADDONS["addons"] else {}
|
|
|
|
} for app in app_folders if isfile(f'./css/base/{app}/.deprecated')
|
|
|
|
}
|
|
|
|
}.items())))
|
2022-07-10 15:31:02 +02:00
|
|
|
APPS.update(dict(sorted({
|
|
|
|
"docker-mods": {
|
|
|
|
mod: f"{scheme}://{DOMAIN}/docker-mods/{mod}/root/etc/cont-init.d/98-themepark" for mod in docker_mods if docker_mods
|
|
|
|
}
|
|
|
|
}.items())))
|
2021-11-01 22:37:21 +01:00
|
|
|
THEMES = loads(create_json(themes=themes, community_themes=community_themes, no_sub_folders=True))
|
|
|
|
APPS.update(ADDONS)
|
|
|
|
APPS.update(THEMES)
|
|
|
|
return dumps(APPS)
|
|
|
|
|
2022-07-10 15:31:02 +02:00
|
|
|
def create_theme_options() -> None:
|
2023-05-23 22:28:56 +02:00
|
|
|
#app_shas = subprocess.check_output(["git", "ls-files", "-s", "./css/base/*base.css"]) if isdir(".git") else []
|
|
|
|
#theme_shas = subprocess.check_output(["git", "ls-files", "-s", "./css/theme-options/*.css"]) if isdir(".git") else []
|
|
|
|
#community_theme_shas = subprocess.check_output(["git", "ls-files", "-s", "./css/community-theme-options/*.css"]) if isdir(".git") else []
|
|
|
|
#THEME_SHAS = get_shas(theme_shas)
|
|
|
|
#COMMUNITY_THEME_SHAS = get_shas(community_theme_shas)
|
|
|
|
#APP_SHAS = get_shas(app_shas)
|
2022-04-09 00:37:07 +02:00
|
|
|
def create_css(theme, theme_type="standard"):
|
|
|
|
folder = "./css/base"
|
2021-11-01 22:37:21 +01:00
|
|
|
with open(f"{folder}/{app}/{theme.lower()}.css", "w") as create_app:
|
2023-05-23 22:28:56 +02:00
|
|
|
content = f'@import url("/css/base/{app}/{app}-base.css?sha={get_md5_hash(join(getcwd(),"css","base",app,f"{app}-base.css"))}");\n@import url("/css/{"theme-options" if theme_type=="standard" else "community-theme-options"}/{theme.lower()}.css?sha={get_md5_hash(join(getcwd(),"css","theme-options",f"{theme.lower()}.css")) if theme_type=="standard" else get_md5_hash(join(getcwd(),"css","community-theme-options",f"{theme.lower()}.css"))}");'
|
2021-11-01 22:37:21 +01:00
|
|
|
create_app.write(content)
|
|
|
|
with open("themes.json") as themes:
|
|
|
|
data = load(themes)
|
2022-04-09 00:37:07 +02:00
|
|
|
themes = data["themes"]
|
|
|
|
community_themes = data["community-themes"]
|
2021-11-01 22:37:21 +01:00
|
|
|
applications = data["applications"]
|
|
|
|
for app in applications:
|
2022-04-09 00:37:07 +02:00
|
|
|
for theme in themes:
|
|
|
|
create_css(theme)
|
|
|
|
for theme in community_themes:
|
|
|
|
create_css(theme,theme_type="community")
|
2021-11-01 22:37:21 +01:00
|
|
|
|
2022-04-09 00:37:07 +02:00
|
|
|
scheme = env.get('TP_SCHEME','https') if env.get('TP_SCHEME') else 'https'
|
2021-11-01 22:37:21 +01:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
app_folders = [name for name in listdir('./css/base') if isdir(join('./css/base', name))]
|
|
|
|
themes = [name for name in listdir('./css/theme-options') if isfile(join('./css/theme-options', name))]
|
2022-07-21 18:10:47 +02:00
|
|
|
docker_mods = [name for name in listdir('./docker-mods')] if isdir('./docker-mods') else []
|
2021-11-01 22:37:21 +01:00
|
|
|
community_themes = [name for name in listdir('./css/community-theme-options') if isfile(join('./css/community-theme-options', name))]
|
2024-10-03 22:51:56 +02:00
|
|
|
branch: str | None = subprocess.check_output(["git", "symbolic-ref", "--short", "HEAD"]).decode('ascii').strip() if isdir(".git") else None
|
|
|
|
|
|
|
|
if env.get('TP_DOMAIN'):
|
|
|
|
DOMAIN = env.get('TP_DOMAIN')
|
2022-03-22 10:06:13 +01:00
|
|
|
else:
|
|
|
|
with open("CNAME", "rt", closefd=True) as cname:
|
|
|
|
CNAME = cname.readline()
|
2024-10-14 21:52:18 +02:00
|
|
|
|
2024-10-14 21:49:43 +02:00
|
|
|
DOMAIN = f"{branch}.{CNAME}" if branch not in ["master","main"] else CNAME
|
2022-07-10 15:31:02 +02:00
|
|
|
apps = loads(create_json(app_folders=app_folders, themes=themes, community_themes=community_themes, docker_mods=docker_mods))
|
2021-11-01 22:37:21 +01:00
|
|
|
with open("themes.json", "w") as outfile:
|
|
|
|
dump(apps, outfile, indent=2, sort_keys=True)
|
2024-10-03 22:53:36 +02:00
|
|
|
create_theme_options()
|