|
| 1 | +import shutil |
| 2 | +import site |
| 3 | +import subprocess |
| 4 | +from datetime import timedelta |
| 5 | +from pathlib import Path |
| 6 | +from time import perf_counter |
| 7 | + |
| 8 | +import utilities.utils as utils |
| 9 | + |
| 10 | + |
| 11 | +def run_command(command: list, use_shell: bool = False) -> None: |
| 12 | + subprocess.run(command, check=True, shell=use_shell) |
| 13 | + |
| 14 | + |
| 15 | +def install_requirements() -> None: |
| 16 | + print("\nInstalling requirements...") |
| 17 | + run_command(['pip', 'install', '-r', 'requirements.txt']) |
| 18 | + |
| 19 | + |
| 20 | +def install_package(name: str) -> None: |
| 21 | + print(f"\n...Installing package {name}...") |
| 22 | + run_command(["pip", "install", name]) |
| 23 | + |
| 24 | + |
| 25 | +def uninstall_package(name: str) -> None: |
| 26 | + temp_dir = Path(f"{site.getsitepackages()[1]}/~addle") |
| 27 | + if temp_dir.exists(): |
| 28 | + print("\nRemoving undeleted temp directory...") |
| 29 | + shutil.rmtree(temp_dir, ignore_errors=True) |
| 30 | + print(f"\n...Uninstalling package {name}...") |
| 31 | + run_command(["pip", "uninstall", name]) |
| 32 | + |
| 33 | + |
| 34 | +def download_all_models() -> None: |
| 35 | + from paddleocr.paddleocr import MODEL_URLS, DEFAULT_OCR_MODEL_VERSION, PaddleOCR |
| 36 | + |
| 37 | + languages = MODEL_URLS['OCR'][DEFAULT_OCR_MODEL_VERSION]['rec'].keys() |
| 38 | + for lang in languages: |
| 39 | + print(f"\nChecking for {lang} language models...") |
| 40 | + utils.Config.ocr_opts["base_dir"] = utils.Config.model_dir |
| 41 | + _ = PaddleOCR(lang=lang, **utils.Config.ocr_opts) |
| 42 | + |
| 43 | + |
| 44 | +def remove_non_onnx_models() -> None: |
| 45 | + print("\nRemoving all non Onnx Models...") |
| 46 | + for file in utils.Config.model_dir.rglob("*.*"): |
| 47 | + if not file.is_dir() and file.name != "model.onnx": |
| 48 | + print(f"Removing file: {file}") |
| 49 | + file.unlink() |
| 50 | + |
| 51 | + |
| 52 | +def compile_program() -> None: |
| 53 | + cmd = [ |
| 54 | + "nuitka", |
| 55 | + "--standalone", |
| 56 | + "--enable-plugin=tk-inter", |
| 57 | + "--windows-console-mode=disable", |
| 58 | + "--include-package-data=paddleocr", |
| 59 | + "--include-data-files=VSE.ico=VSE.ico", |
| 60 | + "--include-data-dir=models=models", |
| 61 | + "--windows-icon-from-ico=VSE.ico", |
| 62 | + "--remove-output", |
| 63 | + "gui.py" |
| 64 | + ] |
| 65 | + print(f"\nCompiling program with Nuitka... \nCommand: {cmd}") |
| 66 | + run_command(cmd, True) |
| 67 | + |
| 68 | + |
| 69 | +def rename_exe() -> None: |
| 70 | + print("\nRenaming exe file...") |
| 71 | + exe_file = Path("gui.dist/gui.exe") |
| 72 | + exe_file.rename("gui.dist/VSE.exe") |
| 73 | + |
| 74 | + |
| 75 | +def zip_files() -> None: |
| 76 | + print("\nZipping distribution files...") |
| 77 | + shutil.make_archive("gui.dist", "zip", "gui.dist") |
| 78 | + |
| 79 | + |
| 80 | +def delete_dist_dir() -> None: |
| 81 | + print("\nRemoving distribution directory...") |
| 82 | + shutil.rmtree("gui.dist") |
| 83 | + |
| 84 | + |
| 85 | +def main() -> None: |
| 86 | + start_time = perf_counter() |
| 87 | + |
| 88 | + install_requirements() |
| 89 | + install_package("Nuitka==2.6.7") |
| 90 | + download_all_models() |
| 91 | + remove_non_onnx_models() |
| 92 | + uninstall_package("paddlepaddle") |
| 93 | + uninstall_package("requests") |
| 94 | + compile_program() |
| 95 | + rename_exe() |
| 96 | + zip_files() |
| 97 | + delete_dist_dir() |
| 98 | + |
| 99 | + print(f"\nCompilation Duration: {timedelta(seconds=round(perf_counter() - start_time))}") |
| 100 | + |
| 101 | + |
| 102 | +if __name__ == '__main__': |
| 103 | + main() |
0 commit comments