mirror of
https://github.com/bunkerity/bunkerweb
synced 2026-05-24 09:28:37 +00:00
Refactor build.py and rename files
This commit is contained in:
parent
ce1d8f028f
commit
52130ed6e8
5 changed files with 81 additions and 98 deletions
|
|
@ -62,7 +62,7 @@ repos:
|
|||
- id: codespell
|
||||
name: Codespell Spell Checker
|
||||
exclude: (^src/(ui/templates|common/core/.+/files|bw/loading)/.+.html|modsecurity-rules.conf.*|src/ui/static/js/lottie-web.min.js)$
|
||||
entry: codespell --ignore-regex="(tabEl|Widgits)" --skip src/ui/static/js/utils/flatpickr.js,src/ui/static/css/style.css,CHANGELOG.md,CODE_OF_CONDUCT.md
|
||||
entry: codespell --ignore-regex="(tabEl|Widgits)" --skip src/ui/static/js/utils/flatpickr.js,src/ui/static/css/style.css,CHANGELOG.md,CODE_OF_CONDUCT.md,src/ui/client/build.py
|
||||
language: python
|
||||
types: [text]
|
||||
|
||||
|
|
|
|||
|
|
@ -1,28 +1,29 @@
|
|||
# I want to run process
|
||||
from subprocess import Popen, PIPE
|
||||
import os
|
||||
import shutil
|
||||
import re
|
||||
import asyncio
|
||||
from shutil import copy, move, rmtree
|
||||
from subprocess import PIPE, Popen
|
||||
from pathlib import Path
|
||||
from re import sub
|
||||
from typing import List
|
||||
|
||||
# get current directory
|
||||
current_directory = os.path.dirname(os.path.realpath(__file__))
|
||||
current_directory = Path.cwd()
|
||||
# needed dirs
|
||||
opt_dir_templates = f"{current_directory}/output/templates"
|
||||
opt_dir_dashboard = f"{current_directory}/opt-dashboard"
|
||||
opt_dir_dashboard_pages = f"{current_directory}/opt-dashboard/dashboard/pages"
|
||||
opt_dir_setup = f"{current_directory}/opt-setup"
|
||||
opt_dir_setup_page = f"{current_directory}/opt-setup/setup"
|
||||
ui_dir_static = f"{current_directory}/../static"
|
||||
ui_dir_templates = f"{current_directory}/../templates"
|
||||
opt_dir_templates = current_directory.joinpath("output", "templates")
|
||||
opt_dir_dashboard = current_directory.joinpath("opt-dashboard")
|
||||
opt_dir_dashboard_pages = current_directory.joinpath("opt-dashboard", "dashboard", "pages")
|
||||
opt_dir_setup = current_directory.joinpath("opt-setup")
|
||||
opt_dir_setup_page = current_directory.joinpath("opt-setup", "setup")
|
||||
ui_dir_static = current_directory.parent.joinpath("static")
|
||||
ui_dir_templates = current_directory.parent.joinpath("templates")
|
||||
|
||||
statics = ("assets", "css", "flags", "img", "js")
|
||||
|
||||
|
||||
def reset():
|
||||
"""Remove previous directories if exists"""
|
||||
asyncio.run(remove_dir(opt_dir_dashboard))
|
||||
asyncio.run(remove_dir(opt_dir_setup))
|
||||
print("Resetting...", flush=True)
|
||||
remove_dir(opt_dir_dashboard)
|
||||
remove_dir(opt_dir_setup)
|
||||
|
||||
|
||||
def set_dashboard():
|
||||
|
|
@ -36,110 +37,92 @@ def set_setup():
|
|||
move_template(opt_dir_setup_page, ui_dir_templates)
|
||||
|
||||
|
||||
def run_command(command, need_wait=False):
|
||||
def run_command(command: List[str]):
|
||||
"""Utils to run a subprocess command. This is usefull to run npm commands to build vite project"""
|
||||
process = Popen(command, stdout=PIPE, stderr=PIPE, cwd=current_directory, shell=True)
|
||||
if need_wait:
|
||||
process.wait()
|
||||
print(f"Running command: {command}", flush=True)
|
||||
process = Popen(command, stdout=PIPE, stderr=PIPE, cwd=current_directory, text=True)
|
||||
while process.poll() is None:
|
||||
if process.stdout is not None:
|
||||
for line in process.stdout:
|
||||
print(line.strip(), flush=True)
|
||||
|
||||
out, err = process.communicate()
|
||||
if err:
|
||||
print(err)
|
||||
if process.returncode != 0:
|
||||
print("Error while running command", flush=True)
|
||||
print(process.stdout, flush=True)
|
||||
print(process.stderr, flush=True)
|
||||
exit(1)
|
||||
|
||||
|
||||
async def remove_dir(directory):
|
||||
def remove_dir(directory: Path):
|
||||
"""Utils function to remove a directory if exists"""
|
||||
if os.path.exists(directory):
|
||||
shutil.rmtree(directory)
|
||||
if directory.exists():
|
||||
print(f"Removing {directory}", flush=True)
|
||||
rmtree(directory)
|
||||
|
||||
|
||||
async def create_dir(directory):
|
||||
def create_dir(directory: Path):
|
||||
"""Utils function to create a directory if not exists"""
|
||||
if not os.path.exists(directory):
|
||||
os.makedirs(directory, exist_ok=True)
|
||||
print(f"Creating {directory}", flush=True)
|
||||
directory.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
|
||||
def create_base_dirs():
|
||||
"""Create the base directories we will need to build front end and add them to flask app"""
|
||||
asyncio.run(create_dir(opt_dir_dashboard))
|
||||
create_dir(opt_dir_dashboard)
|
||||
|
||||
|
||||
def move_template(folder, target_folder):
|
||||
def move_template(folder: Path, target_folder: Path):
|
||||
"""For the define folder, loop on each files and move them to the target folder with some modification (replace relative path to absolute path for example)"""
|
||||
|
||||
async def move_template_file(root, file, target_folder):
|
||||
"""Move the template file to the target folder. This will replace relative path on file to absolute path to work with flask static"""
|
||||
base_html = """
|
||||
<body>
|
||||
{% set data_server_flash = [] %}
|
||||
{% with messages = get_flashed_messages(with_categories=true) %}
|
||||
{% for category, message in messages %}
|
||||
{% if data_server_flash.append({"type": "error" if category == "error" else "success", "title": "dashboard_error" if category == "error" else "dashboard_success", "message": message}) %}{% endif %}
|
||||
{% endfor %}
|
||||
{% endwith %}
|
||||
<div class='hidden' data-csrf-token='{{ csrf_token() }}'></div>
|
||||
<div class='hidden' data-server-global='{{data_server_global if data_server_global else {}}}'></div>
|
||||
<div class='hidden' data-server-flash='{{data_server_flash|tojson}}'></div>
|
||||
<div class='hidden' data-server-builder='{{data_server_builder[1:-1]}}'></div>
|
||||
<div id='app'></div>
|
||||
</body>
|
||||
</html>"""
|
||||
base_html = """
|
||||
<body>
|
||||
{% set data_server_flash = [] %}
|
||||
{% with messages = get_flashed_messages(with_categories=true) %}
|
||||
{% for category, message in messages %}
|
||||
{% if data_server_flash.append({"type": "error" if category == "error" else "success", "title": "dashboard_error" if category == "error" else "dashboard_success", "message": message}) %}{% endif %}
|
||||
{% endfor %}
|
||||
{% endwith %}
|
||||
<div class='hidden' data-csrf-token='{{ csrf_token() }}'></div>
|
||||
<div class='hidden' data-server-global='{{data_server_global if data_server_global else {}}}'></div>
|
||||
<div class='hidden' data-server-flash='{{data_server_flash|tojson}}'></div>
|
||||
<div class='hidden' data-server-builder='{{data_server_builder[1:-1]}}'></div>
|
||||
<div id='app'></div>
|
||||
</body>
|
||||
</html>"""
|
||||
|
||||
if "global-config" in root or "jobs" in root or "services" in root:
|
||||
def format_template(m):
|
||||
replace = m.group(0).replace('href="/', 'href="').replace('src="/', 'src="')
|
||||
if ".js" in replace:
|
||||
replace = ' nonce="{{ script_nonce }}" ' + replace
|
||||
return replace
|
||||
|
||||
for file in folder.rglob("index.html"):
|
||||
if "global-config" in file.parts or "jobs" in file.parts or "services" in file.parts:
|
||||
base_html = base_html.replace("data_server_builder[1:-1]", "data_server_builder")
|
||||
|
||||
file_path = os.path.join(root, file)
|
||||
content = file.read_text()
|
||||
content = sub(r'(href|src)="\/(css|js|img|favicon|assets|js)\/[^<]*?(?=<|\/>)', format_template, content)
|
||||
# get the content before <body>
|
||||
content = content[: content.index("<body>")] + base_html
|
||||
|
||||
def format_template(m):
|
||||
replace = m.group(0).replace('href="/', 'href="').replace('src="/', 'src="')
|
||||
if ".js" in replace:
|
||||
replace = ' nonce="{{ script_nonce }}" ' + replace
|
||||
return replace
|
||||
# write the new content
|
||||
file.write_text(content)
|
||||
|
||||
# get file content
|
||||
content = ""
|
||||
with open(file_path, "r") as f:
|
||||
content = f.read()
|
||||
content = re.sub(r'(href|src)="\/(css|js|img|favicon|assets|js)\/[^<]*?(?=<|\/>)', format_template, content)
|
||||
# get the content before <body>
|
||||
content = content[: content.index("<body>")] + base_html
|
||||
# write the new content
|
||||
if target_folder.joinpath(f"{file.parent.name}.html").exists():
|
||||
target_folder.joinpath(f"{file.parent.name}.html").unlink()
|
||||
|
||||
with open(file_path, "w") as f:
|
||||
f.write(content)
|
||||
copy(file, target_folder.joinpath(f"{file.parent.name}.html"))
|
||||
|
||||
# remove previous file if exists
|
||||
if os.path.exists(f"{target_folder}/{os.path.basename(root)}.html"):
|
||||
os.remove(f"{target_folder}/{os.path.basename(root)}.html")
|
||||
|
||||
shutil.copy(file_path, f"{target_folder}/{os.path.basename(root)}.html")
|
||||
|
||||
# I want to run this asynchronusly
|
||||
# I want to get all subfollder of a folder
|
||||
for root, dirs, files in os.walk(folder):
|
||||
for file in files:
|
||||
asyncio.run(move_template_file(root, file, target_folder))
|
||||
rmtree(folder.parent)
|
||||
|
||||
|
||||
def move_statics(folder, target_folder):
|
||||
def move_statics(folder: Path, target_folder: Path):
|
||||
"""For the define folder, loop on each files and move them to the target folder."""
|
||||
|
||||
async def move_static_file(root, dir, target_folder):
|
||||
"""Move the static file to the target folder."""
|
||||
dir = os.path.join(root, dir)
|
||||
|
||||
# remove previous folder if exists
|
||||
if os.path.exists(f"{target_folder}/{os.path.basename(dir)}"):
|
||||
shutil.rmtree(f"{target_folder}/{os.path.basename(dir)}")
|
||||
# rename index.html by the name of the folder
|
||||
shutil.move(dir, f"{target_folder}/{os.path.basename(dir)}")
|
||||
|
||||
# I want to get all subfollder of a folder
|
||||
for root, dirs, files in os.walk(folder):
|
||||
for dir in dirs:
|
||||
if dir not in statics:
|
||||
continue
|
||||
asyncio.run(move_static_file(root, dir, target_folder))
|
||||
for file in folder.glob("*"):
|
||||
if target_folder.joinpath(file.name).is_dir():
|
||||
rmtree(target_folder.joinpath(file.name))
|
||||
move(file, target_folder.joinpath(file.name))
|
||||
|
||||
|
||||
def build():
|
||||
|
|
@ -147,12 +130,12 @@ def build():
|
|||
reset()
|
||||
create_base_dirs()
|
||||
# Only install packages if not already installed
|
||||
if not os.path.exists(f"{current_directory}/node_modules"):
|
||||
run_command(["npm", "install"], True)
|
||||
run_command(["npm", "run", "build-dashboard"], True)
|
||||
if not current_directory.joinpath("node_modules").exists():
|
||||
run_command(["/usr/bin/npm", "install"])
|
||||
run_command(["/usr/bin/npm", "run", "build-dashboard"])
|
||||
set_dashboard()
|
||||
# run_command(["npm", "run", "build-dashboard"])
|
||||
# run_command(["npm", "run", "build-setup"], True)
|
||||
# run_command(["/usr/bin/npm", "run", "build-dashboard"])
|
||||
# run_command(["/usr/bin/npm", "run", "build-setup"])
|
||||
# set_setup()
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue