-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmake_distribution.py
173 lines (138 loc) · 6.5 KB
/
make_distribution.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env python
# Copyright 2009-2011 Ram Rachum.
# This program is distributed under the LGPL2.1 license.
'''
Script for packaging GarlicSim as a complete program to end users.
Currently implemented only for Windows, using `py2exe`.
The distribution files for Windows will be put in the `win_dist` folder.
Options:
--help
Show this help screen
--installer [OR] -i
After making distribution directory, create installer.
On Windows, this uses Inno Setup.
Windows-only options:
--issc=[PATH]
Path to `issc.exe`, needed only if (a) making a Windows installer
and (b) `issc.exe` is in a non-standard location)
'''
import shutil
import os.path
import platform
import sys
import glob
repo_root_path = os.path.realpath(os.path.split(__file__)[0])
garlicsim_wx_path = os.path.join(repo_root_path, 'garlicsim_wx')
assert __name__ == '__main__'
if '--help' in sys.argv:
sys.stdout.write(__doc__ + '\n')
exit()
operating_system = platform.system()
if operating_system != 'Windows':
raise NotImplementedError("You're not on Windows, and making a "
"distribution on Linux or Mac is not yet "
"supported.")
produce_installer = ('--installer' in sys.argv) or ('-i' in sys.argv)
if produce_installer:
sys.stdout.write('Preparing to package GarlicSim for Windows users using '
'py2exe and produce Windows installer.\n')
else: # not produce_installer
sys.stdout.write('Preparing to package GarlicSim for Windows users using '
'py2exe.\n')
if produce_installer:
### Figuring out location of Inno Setup compiler: #########################
# #
issc_specifiers = [arg for arg in sys.argv if arg.startswith('--issc=')]
if issc_specifiers:
(issc_specifier,) = issc_specifiers
path_to_issc = issc_specifier[7:]
if path_to_issc[0] == path_to_issc[-1] == '"':
path_to_issc = path_to_issc[1:-1]
if not os.path.isfile(path_to_issc):
raise Exception('The path to `issc.exe` that you specified does '
'not exist. Make sure to include the `.exe` file '
'itself in the path.')
else:
path_to_issc = \
'c:\\Program Files\\Inno Setup 5\\ISCC.exe'
if not os.path.isfile(path_to_issc):
raise Exception("The Inno Setup compiler `issc.exe` could not be "
"found. If you don't have Inno Setup installed, "
"install it. If it's installed and you still get "
"this message, specify the path to `issc.exe` by "
"using the `--issc=[PATH]` flag.")
# #
### Finished figuring out location of Inno Setup compiler. ################
### Deleting old build files: #################################################
# #
def assert_no_unknown_folders():
'''Assert there are no unknown folders in `garlicsim_wx`.'''
existing_folders = set(
[name for name in os.listdir(garlicsim_wx_path) if
os.path.isdir(os.path.join(garlicsim_wx_path, name))]
)
assert existing_folders == \
set(('garlicsim_wx', 'test_garlicsim_wx', 'py2exe_cruft'))
folders_to_delete = []
for folder in [os.path.join(garlicsim_wx_path, 'build'),
os.path.join(garlicsim_wx_path, 'garlicsim_wx.egg-info'),
os.path.join(repo_root_path, 'win_dist')]:
if os.path.exists(folder):
folders_to_delete.append(folder)
if folders_to_delete:
sys.stdout.write('Preparing to delete old build folders.\n')
for folder_to_delete in folders_to_delete:
short_name = os.path.split(folder_to_delete)[1]
sys.stdout.write("Deleting the '%s' folder... " % short_name)
shutil.rmtree(folder_to_delete)
sys.stdout.write('Done.\n')
assert_no_unknown_folders()
else: # No folders to delete
assert_no_unknown_folders()
sys.stdout.write('No previous build folders to delete.\n')
if produce_installer:
existing_installers = \
glob.glob(os.path.join(repo_root_path, 'GarlicSim-*.exe'))
if existing_installers:
sys.stdout.write('Preparing to remove old installer file%s.\n' % \
('s' if (len(existing_installers) > 1) else ''))
for existing_installer in existing_installers:
sys.stdout.write('Removing old `%s` file... ' % existing_installer)
os.remove(existing_installer)
sys.stdout.write('Done.')
sys.stdout.write('Working area clean.\n')
# #
### Finished deleting old build files. ########################################
### Packaging with py2exe: ####################################################
# #
sys.stdout.write('Launching py2exe.\n')
old_cwd = os.getcwd()
os.chdir(garlicsim_wx_path)
try:
temp_result = os.system('"%s" setup.py py2exe' % sys.executable)
if temp_result != 0:
sys.exit(temp_result)
finally:
os.chdir(old_cwd)
sys.stdout.write('Py2exe packaging complete. Distribution files are in the '
'`win_dist` folder.\n')
# #
### Finished packaging with py2exe. ###########################################
if produce_installer:
### Creating Windows installer with Inno Setup: ###########################
# #
sys.stdout.write('Preparing to create Windows installer using Inno '
'Setup.\n')
os.chdir(garlicsim_wx_path)
try:
# (There are no less than six quotes in this command, because of weird
# `cmd.exe /C` conventions.)
create_installer_command = '""%s" "%s""' % (
path_to_issc,
os.path.join(garlicsim_wx_path, 'installer_script.iss')
)
sys.exit(os.system(create_installer_command))
finally:
os.chdir(old_cwd)
# #
### Finished creating Windows installer with Inno Setup. ##################