DVCheck/build.py

62 lines
1.7 KiB
Python
Raw Normal View History

2024-07-21 23:01:21 -04:00
from pathlib import Path
from subprocess import run
import os
import shutil
import sys
def build_app():
# Change directory to the project's root directory
project_root = Path(__file__).parent
os.chdir(project_root)
# Ensure we're in a virtual env, if we are, install dependencies using Poetry
if sys.prefix == sys.base_prefix:
raise Exception("You must activate your virtual environment first")
else:
# Use Poetry to install dependencies
run(["poetry", "install"])
# Define the PyInstaller output path
pyinstaller_folder = project_root / "pyinstaller_build"
# Delete the old build folder if it exists
shutil.rmtree(pyinstaller_folder, ignore_errors=True)
# Create a folder for the PyInstaller output
pyinstaller_folder.mkdir(exist_ok=True)
# Define paths before changing directory
tool = project_root / "dv_check.py"
# Change directory so PyInstaller outputs all of its files in its own folder
os.chdir(pyinstaller_folder)
# Run PyInstaller command with Poetry's virtual environment
build_job = run(
[
"poetry",
"run",
"pyinstaller",
"--onefile",
str(tool),
]
)
# Ensure the output of the .exe
success = "Did not complete successfully"
exe_path = project_root / pyinstaller_folder / "dist" / "dv_check.exe"
if exe_path.is_file() and str(build_job.returncode) == "0":
success = f"\nSuccess!\nPath to exe: {str(exe_path)}"
# Change directory back to the original directory
os.chdir(project_root)
# Return a success message
return success
if __name__ == "__main__":
build = build_app()
print(build)