Skip to content

Commit

Permalink
Optimize build.
Browse files Browse the repository at this point in the history
  • Loading branch information
HsOjo committed Nov 2, 2019
1 parent 612d6ad commit 71f7a13
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 13 deletions.
2 changes: 1 addition & 1 deletion app/res/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class Const:
author = 'HsOjo'
app_name = 'SleeperX'
app_env = '%s_ENV' % app_name.upper()
version = '1.7.1'
version = '1.7.2'
github_page = 'https://github.com/%s/%s' % (author, app_name)
releases_url = '%s/releases' % github_page
protector = '[protector]'
Expand Down
30 changes: 18 additions & 12 deletions build.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from app.res.language.translate_language import TranslateLanguage
from app.util.log import Log
from tools.translate import *
from tools.utils.zip import zip_directory

datas = {}

Expand Down Expand Up @@ -41,17 +42,31 @@ def add_data(src, dest):

add_data('./app/res/icon.png', './app/res')

path_app_dir = './dist/%s.app' % Const.app_name
path_app_zip = './dist/%s-%s.zip' % (Const.app_name, Const.version)

use_py2app = '--py2app' in sys.argv
if use_py2app:
Log.append('Build', 'Info', 'Py2App packing now...')
os.system('python setup.py py2app')
shutil.rmtree('./build', ignore_errors=True)

res_dir = './dist/%s.app/Contents/Resources' % Const.app_name
res_dir = '%s/Contents/Resources' % path_app_dir
for f, fd in datas.items():
dd = ('%s/%s' % (res_dir, fd)).replace('/./', '/')
os.makedirs(dd, exist_ok=True)
shutil.copy(f, dd)

# clean not .pyc files.
Log.append('Build', 'Info', 'Cleaning...')
lib_dir = '%s/lib' % res_dir
[lib_file] = [f for f in os.listdir(lib_dir) if 'python' in f and '.zip' in f]
lib_path = '%s/%s' % (lib_dir, lib_file)
with ZipFile(lib_path, 'r') as zf:
zf.extractall(lib_file)

zip_directory(lib_file, lib_path, filter_='.*\.pyc$', remove='.*__pycache__.*')
shutil.rmtree(lib_file)
else:
data_str = ''
for k, v in datas.items():
Expand All @@ -76,16 +91,7 @@ def add_data(src, dest):
with open(INFO_FILE, 'w') as io:
io.write(info)

Log.append('Build', 'Info', 'Packing release zip file now...')

# pack release zip file.
zf = ZipFile('./dist/%s-%s.zip' % (Const.app_name, Const.version), 'w')
src_dir = './dist/%s.app' % Const.app_name
for d, ds, fs in os.walk(src_dir):
for f in fs:
path = os.path.join(d, f)
z_path = path[7:].strip(os.path.sep)
zf.write(path, z_path)
zf.close()

Log.append('Build', 'Info', 'Packing release zip file now...')
zip_directory(path_app_dir, path_app_zip, remove='.*__pycache__.*', dirname=True)
Log.append('Build', 'Info', 'Build finish.')
Empty file added tools/utils/__init__.py
Empty file.
34 changes: 34 additions & 0 deletions tools/utils/zip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import os
import re
from zipfile import ZipFile


def zip_directory(path_dir, path_zip, filter_=None, remove=None, dirname=None):
reg_filter = None
reg_remove = None

if filter_ is not None:
reg_filter = re.compile(filter_)
if remove is not None:
reg_remove = re.compile(remove)

path_dir = os.path.abspath(path_dir)
with ZipFile(path_zip, 'w') as zf:
for d, ds, fs in os.walk(path_dir):
z_d = d.replace(path_dir, '')
for f in fs:
path = os.path.join(d, f)
z_path = os.path.join(z_d, f).strip(os.path.sep)

if reg_filter is not None and not reg_filter.match(z_path):
continue

if reg_remove is not None and reg_remove.match(z_path):
continue

if dirname is True:
dirname = os.path.basename(path_dir)
if dirname is not None:
z_path = '%s/%s' % (dirname, z_path)

zf.write(path, z_path)

0 comments on commit 71f7a13

Please sign in to comment.