forked from timsutton/brigadier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_windows_exe.py
73 lines (63 loc) · 2.16 KB
/
build_windows_exe.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# build_windows_exe.py
#
# Builds a Windows exe of brigadier using PyInstaller. PyInstaller
# will be downloaded to the cwd and its archive kept for later use.
#
# Requires:
# - Python 2.7 for Windows (http://www.python.org/getit)
# - pywin32 (http://sourceforge.net/projects/pywin32)
import urllib
import os
from zipfile import ZipFile, BadZipfile
import subprocess
import shutil
import hashlib
PYINSTALLER_URL = 'https://pypi.python.org/packages/source/P/PyInstaller/PyInstaller-2.1.zip'
PYINST_ZIPFILE = os.path.join(os.getcwd(), 'pyinstaller.zip')
NAME = 'brigadier'
with open('VERSION', 'r') as fd:
version = fd.read().strip()
name_versioned = NAME + '-' + version
exe_name = NAME + '.exe'
pack_dir = os.path.join(os.getcwd(), name_versioned)
build_dir = os.path.join(os.getcwd(), 'build')
need_pyinstaller = False
if os.path.exists(PYINST_ZIPFILE):
print "PyInstaller zipfile found."
try:
ZipFile(PYINST_ZIPFILE)
except BadZipfile:
print "Zipfile is corrupt."
need_pyinstaller = True
else:
need_pyinstaller = True
if need_pyinstaller:
print "Downloading PyInstaller..."
urllib.urlretrieve(PYINSTALLER_URL, filename=PYINST_ZIPFILE)
dlzip = ZipFile(PYINST_ZIPFILE)
pyinst_root = dlzip.namelist()[0].split("/")[0]
dlzip.extractall()
print "Building version %s..." % version
build_cmd = [os.path.join(os.environ["SYSTEMDRIVE"] + "\\", 'Python27', 'python.exe'),
os.path.join(os.getcwd(), pyinst_root, 'pyinstaller.py'),
'-F',
'--distpath', pack_dir,
'--name', NAME,
'brigadier']
subprocess.call(build_cmd)
print "Compressing to zip file..."
zipfile_name = name_versioned + '.zip'
packzip = ZipFile(zipfile_name, 'w')
packzip.write(os.path.basename(pack_dir))
packzip.write(os.path.join(os.path.basename(pack_dir), exe_name))
packzip.close()
with open(zipfile_name, 'r') as zipfd:
sha1 = hashlib.sha1(zipfd.read()).hexdigest()
print "Cleaning up..."
for dirs in [pack_dir, build_dir, pyinst_root]:
shutil.rmtree(dirs)
for f in os.listdir(os.getcwd()):
if f.startswith('logdict'):
os.remove(f)
print "Built and archived to %s." % zipfile_name
print "SHA1: %s" % sha1