| 1 |
# -*- mode: python -*- |
| 2 |
|
| 3 |
from ConfigParser import ConfigParser |
| 4 |
from distutils.util import get_platform |
| 5 |
from py_compile import compile |
| 6 |
from zipfile import ZipFile, ZIP_DEFLATED |
| 7 |
|
| 8 |
sys.path.insert(0, '') |
| 9 |
from meta import name as appname, version, script2pywname |
| 10 |
from DisplayCAL.setup import (config, create_app_symlinks, get_data, |
| 11 |
get_scripts, plist_dict) |
| 12 |
|
| 13 |
sys.path.insert(1, 'util') |
| 14 |
import winversion |
| 15 |
|
| 16 |
library_zip = True |
| 17 |
|
| 18 |
build_dir = os.path.join(os.getcwd(), 'build', 'pyi.%s-%s' % (get_platform(), sys.version[:3]), |
| 19 |
appname + '.pyi') |
| 20 |
dist_dir = os.path.join(os.getcwd(), 'dist', 'pyi.%s-py%s' % (get_platform(), sys.version[:3]), |
| 21 |
'%s-%s' % (appname, version)) |
| 22 |
|
| 23 |
if sys.platform == 'win32': |
| 24 |
exe_ext = '.exe' |
| 25 |
else: |
| 26 |
exe_ext = '' |
| 27 |
|
| 28 |
if sys.platform == 'darwin': |
| 29 |
coll_name = appname |
| 30 |
data = os.path.join(dist_dir, '..', appname + '.app', 'Contents', 'Resources') |
| 31 |
else: |
| 32 |
coll_name = dist_dir |
| 33 |
data = dist_dir |
| 34 |
|
| 35 |
# Scripts |
| 36 |
if sys.platform == 'darwin': |
| 37 |
scripts = get_scripts(excludes=[appname + '-apply-profiles']) |
| 38 |
else: |
| 39 |
scripts = get_scripts() |
| 40 |
|
| 41 |
# Analyze ALL scripts at once |
| 42 |
excludes = config['excludes']['all'] + config['excludes'][sys.platform] |
| 43 |
a = Analysis([os.path.join('scripts', script) for script, desc in scripts], |
| 44 |
pathex=['.'], |
| 45 |
hiddenimports=[], |
| 46 |
hookspath=None, |
| 47 |
excludes=excludes) |
| 48 |
|
| 49 |
# We need a dummy PYZ for our executables otherwise FrozenImporter will fail |
| 50 |
dummy = os.path.join(build_dir, 'dummy') |
| 51 |
open(dummy, 'wb').close() |
| 52 |
compile(dummy, dummy + '.pyc', dfile='dummy') |
| 53 |
pyz = PYZ(TOC([('dummy', dummy + '.pyc', 'PYMODULE')])) |
| 54 |
|
| 55 |
if library_zip: |
| 56 |
# Don't use PYZ, instead a regular zipfile |
| 57 |
# will contain all our pure python modules |
| 58 |
lib = 'library.zip' |
| 59 |
libzip = ZipFile(os.path.join(build_dir, lib), 'w', ZIP_DEFLATED) |
| 60 |
for name, py_path, dtype in a.pure: |
| 61 |
py_name, py_ext = os.path.splitext(os.path.basename(py_path)) |
| 62 |
if not name.endswith(py_name): |
| 63 |
name += '.' + py_name |
| 64 |
libzip.write(py_path, name.replace('.', '/') + py_ext) |
| 65 |
# We need to add PyInstaller's fake site.py |
| 66 |
libzip.write(os.path.join(HOMEPATH, 'PyInstaller', 'fake', 'fake-site' + py_ext), |
| 67 |
'site' + py_ext) |
| 68 |
libzip.close() |
| 69 |
|
| 70 |
# Remove default PYZ dependencies (pyi_ modules are already part of library) |
| 71 |
pyz.dependencies = [] |
| 72 |
else: |
| 73 |
# Use PYZ |
| 74 |
lib = 'library.pyz' |
| 75 |
PYZ(a.pure, name=os.path.join(build_dir, lib)) |
| 76 |
|
| 77 |
# Remove analyzed scripts from scripts TOC |
| 78 |
# Also remove win32comgenpy runtime hook as we don't use genpy |
| 79 |
global scripts # Use global otherwise the below filter will result in a |
| 80 |
# NameError: global name 'scripts' is not defined -- but WHY??? |
| 81 |
a.scripts = list(filter(lambda (name, script, dtype): |
| 82 |
name not in ['pyi_rth_win32comgenpy'] + |
| 83 |
[script for script, desc in scripts], a.scripts)) |
| 84 |
|
| 85 |
# Bootstrap code which inserts library into sys.path |
| 86 |
lib_bootstrap = os.path.join(build_dir, 'lib_bootstrap') |
| 87 |
with open(lib_bootstrap, 'wb') as py_file: |
| 88 |
py_file.write('\n'.join(['import sys', |
| 89 |
'sys.path[:0] = [sys.exec_prefix + "/%s"]' % lib])) |
| 90 |
|
| 91 |
# Generate executables for each of our scripts |
| 92 |
# If using library.zip, do NOT use a.scripts except for PYI bootstrap |
| 93 |
exes = [] |
| 94 |
for script, desc in scripts: |
| 95 |
if sys.platform == 'darwin': |
| 96 |
# For Mac OS X we need to add the icon to the bundle |
| 97 |
icon = None |
| 98 |
elif sys.platform == 'win32': |
| 99 |
icon = script2pywname(script) + '.ico' |
| 100 |
else: |
| 101 |
icon = os.path.join('256x256', '%s.png' % script) |
| 102 |
tempver_path = winversion.mktempver(os.path.join('misc', 'winversion.txt'), |
| 103 |
script2pywname(script), desc) |
| 104 |
exes += [EXE(pyz, # Dummy PYZ |
| 105 |
[('lib_bootstrap', lib_bootstrap, 'PYSOURCE')], # library bootstrap |
| 106 |
filter(lambda (name, script, dtype): |
| 107 |
name == '_pyi_bootstrap', a.scripts) if not pyz.dependencies else a.scripts, # PYI bootstrap |
| 108 |
[(script, os.path.join('scripts', script), 'PYSOURCE')], |
| 109 |
exclude_binaries=1, |
| 110 |
name=script2pywname(script) + exe_ext, |
| 111 |
debug=False, |
| 112 |
strip=sys.platform != 'win32', |
| 113 |
upx=True, |
| 114 |
console=False, |
| 115 |
icon=icon and os.path.join(appname, 'theme', 'icons', icon), |
| 116 |
version=tempver_path)] |
| 117 |
os.remove(tempver_path) |
| 118 |
os.rmdir(os.path.dirname(tempver_path)) |
| 119 |
|
| 120 |
if sys.platform == 'win32': |
| 121 |
# Substitute for python.exe |
| 122 |
# This has very limited functionality - basically just the -c parameter is |
| 123 |
# supported, which is exactly what we need for wexpect.Wtty.startChild |
| 124 |
python = os.path.join(build_dir, 'python') |
| 125 |
with open(python, 'wb') as py: |
| 126 |
py.write('\n'.join(['if __name__ == "__main__":', |
| 127 |
f' import {appname}.wexpect', |
| 128 |
f' sys.modules["wexpect"] = sys.modules["{appname}.wexpect"]', |
| 129 |
' for i, arg in enumerate(sys.argv[1:]):', |
| 130 |
' if arg == "-c":', |
| 131 |
' exec("".join(sys.argv[i + 2]))'])) |
| 132 |
exes += [EXE(pyz, # Dummy PYZ |
| 133 |
[('lib_bootstrap', lib_bootstrap, 'PYSOURCE')], # library bootstrap |
| 134 |
filter(lambda (name, script, dtype): |
| 135 |
name == '_pyi_bootstrap', a.scripts) if not pyz.dependencies else a.scripts, # PYI bootstrap |
| 136 |
[('python', python, 'PYSOURCE')], |
| 137 |
exclude_binaries=1, |
| 138 |
name='python' + exe_ext, |
| 139 |
debug=False, |
| 140 |
strip=sys.platform != 'win32', |
| 141 |
upx=True, |
| 142 |
console=True, |
| 143 |
icon=sys.executable)] |
| 144 |
|
| 145 |
# Collect executables + dependencies |
| 146 |
coll = COLLECT(*exes + |
| 147 |
[a.binaries, a.zipfiles, a.datas, |
| 148 |
[(lib, os.path.join(build_dir, lib), 'DATA')]], |
| 149 |
strip=sys.platform != 'win32', |
| 150 |
upx=True, |
| 151 |
name=coll_name) |
| 152 |
|
| 153 |
# Mac OS X: Create app bundle |
| 154 |
if sys.platform == 'darwin': |
| 155 |
iconpth = os.path.join(appname, 'theme', 'icons', appname + '.icns') |
| 156 |
app = BUNDLE(coll, |
| 157 |
icon=iconpth, |
| 158 |
info_plist=dict((k, v.encode('UTF-8')) for k, v in plist_dict.items()), |
| 159 |
name=appname + '.app') |
| 160 |
|
| 161 |
# Collect data |
| 162 |
data_files = (get_data('', 'data') + get_data('', 'doc') + |
| 163 |
get_data('', 'package_data', appname) + |
| 164 |
get_data('', 'xtra_package_data', appname, sys.platform)) |
| 165 |
for dirname, files in data_files: |
| 166 |
for filename in files: |
| 167 |
tgt = os.path.join(data, dirname, os.path.basename(filename)) |
| 168 |
if os.path.exists(tgt): |
| 169 |
print 'NOTICE: Skipping already existing', tgt |
| 170 |
continue |
| 171 |
if not os.path.isdir(os.path.dirname(tgt)): |
| 172 |
os.makedirs(os.path.dirname(tgt)) |
| 173 |
print 'Copying', filename, '->', tgt |
| 174 |
shutil.copy(filename, tgt) |
| 175 |
|
| 176 |
# Mac OS X: Move app to dist directory |
| 177 |
if sys.platform == 'darwin': |
| 178 |
for src, dst in [(os.path.join(dist_dir, '..', appname + '.app'), |
| 179 |
os.path.join(dist_dir, appname + '.app'))]: |
| 180 |
if os.path.isdir(dst): |
| 181 |
_rmtree(dst) |
| 182 |
os.renames(src, dst) |
| 183 |
create_app_symlinks(dist_dir, scripts) |