Skip to content

Commit

Permalink
build script
Browse files Browse the repository at this point in the history
  • Loading branch information
Dadangdut33 committed Feb 20, 2023
1 parent 33a887a commit c11bc00
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 5 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ build/
log/
dist/
output/
export/
export/

# created when building
LICENSE.txt
*.spec
113 changes: 113 additions & 0 deletions build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
"""
Pyinstaller script to move stuff, rename, and also make a clean output folder
"""

import os, shutil, sys
from PyInstaller.__main__ import generate_parser, run # type: ignore
from speech_translate._version import __version__


def run_makespec(filenames, **opts):
# Split pathex by using the path separator
temppaths = opts["pathex"][:]
pathex = opts["pathex"] = []
for p in temppaths:
pathex.extend(p.split(os.pathsep))

import PyInstaller.building.makespec

spec_file = PyInstaller.building.makespec.main(filenames, **opts)
return spec_file


def get_env_name():
return os.path.basename(sys.prefix)


def get_base_prefix_compat():
"""Get base/real prefix, or sys.prefix if there is none."""
return getattr(sys, "base_prefix", None) or getattr(sys, "real_prefix", None) or sys.prefix


def in_virtualenv():
return get_base_prefix_compat() != sys.prefix


if not in_virtualenv():
print("Please run this script in a virtual environment")
sys.exit(1)

options = [
"Main.py",
"-c", # console window. Console window cannot be hidden because it will cause error on whisper transformer logging
"--clean",
"--noconfirm",
"--additional-hooks-dir=./_pyinstaller_hooks",
"--runtime-hook=./_pyinstaller_hooks/add_lib.py",
"--icon=./assets/icon.ico",
"--add-data=./theme;theme",
"--add-data=./assets;assets",
"--add-data=./LICENSE.txt;.",
f"--add-data={get_env_name()}/Lib/site-packages/whisper/assets;whisper/assets/",
"--copy-metadata=tqdm",
"--copy-metadata=regex",
"--copy-metadata=requests",
"--copy-metadata=packaging",
"--copy-metadata=filelock",
"--copy-metadata=numpy",
"--copy-metadata=tokenizers",
"--exclude-module=pyinstaller",
]

print(f"Currently running in virtual environment {get_env_name()} using python {sys.version}")
specName = f"SpeechTranslate {__version__}"
argsName = f"-n{specName}" # name of the spec file

# -----------------
# make spec folder
parser = generate_parser()
args = parser.parse_args(options)
run_makespec(**vars(args))

# Edit spec folder
specFile = f"{specName}.spec"
spec = ""
with open(specFile, "r") as f:
spec = f.read()
spec = spec.replace(f"name='{specName}'", f"name='SpeechTranslate'", 1)

# write spec file
with open(specFile, "w") as f:
f.write(spec)

# create license.txt file
with open("LICENSE", "r") as f:
license = f.read()
with open("LICENSE.txt", "w") as f2:
f2.write(license)

# run pyinstaller
run([specFile, "--noconfirm", "--clean"])

# delete license.txt file
print(">> Deleting created license.txt file")
os.remove("LICENSE.txt")

output_folder = f"dist/{specName}"

# create lib folder in output folder
lib_folder = f"{output_folder}/lib"
os.mkdir(lib_folder)

# move all .dll .pyd files to lib folder with some exception
print(">> Moving .dll files to lib folder")
dontMove = ["python3.dll", "python310.dll", "python38.dll", "python39.dll", "libopenblas64__v0.3.21-gcc_10_3_0.dll"]
for file in os.listdir(output_folder):
if file.endswith(".dll") or file.endswith(".pyd"):
if file not in dontMove:
shutil.move(f"{output_folder}/{file}", f"{lib_folder}/{file}")

# open folder
print(">> Opening output folder")
output_folder = os.path.abspath(output_folder)
os.startfile(output_folder)
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
deep_translator==1.10.0
notify-py==0.3.42
Pillow == 9.4.0
pillow == 9.4.0
PyAudioWPatch==0.2.12.5
pystray==0.19.4
requests==2.28.1
scipy==1.9.3
sounddevice==0.4.5
sounddevice==0.4.6
soundfile==0.11.0
darkdetect==0.8.0
pyinstaller==5.8.0
Expand Down
4 changes: 2 additions & 2 deletions requirements_notwindows.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
deep_translator==1.10.0
notify-py==0.3.42
Pillow == 9.4.0
pillow == 9.4.0
PyAudio==0.2.12
pystray==0.19.4
requests==2.28.1
scipy==1.9.3
sounddevice==0.4.5
sounddevice==0.4.6
soundfile==0.11.0
darkdetect==0.8.0
pyinstaller==5.8.0
Expand Down
1 change: 1 addition & 0 deletions uninstall.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Pillow==9.4.0

0 comments on commit c11bc00

Please sign in to comment.