-
Notifications
You must be signed in to change notification settings - Fork 4
/
mkmsi.py
540 lines (455 loc) · 17.3 KB
/
mkmsi.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
#/usr/bin/env python3
import argparse
import json
import uuid
import hashlib
import glob
import os, sys
import subprocess
from ctypes import windll, POINTER
from ctypes.wintypes import LPWSTR, DWORD, BOOL
import xml.etree.ElementTree
from xml.etree import ElementTree
from xml.etree.ElementTree import (
Element, SubElement, Comment, tostring,
)
from xml.dom import minidom
parser = argparse.ArgumentParser(description='Create a Windows Installer .msi file')
parser.add_argument('project',
help='Name of the project, without file extention')
parser.add_argument('--auto-create',
choices=['simple', 'qt'],
help='Create a project-file if required (Required extra arguments should be present)')
parser.add_argument('--project-name', help='Project name')
parser.add_argument('--executable', help='The main .exe file')
parser.add_argument('--project-version', help='Version of the project (n.n.n)')
parser.add_argument('--manufacturer', help='Manufacturer of the project (Person or company name)')
parser.add_argument('--version', help='Version of the project (n.n.n)')
parser.add_argument('--description', help='Description of the application')
parser.add_argument('--source-dir', help='The root-directory with the files to package')
parser.add_argument('--icon', help='Name of the icon file to use')
parser.add_argument('--add-desktop-shortcut', help='Add a desktop shortcut', action='store_true',)
parser.add_argument('--license', help='The license to use. This must be a text-file in .rtf format.')
parser.add_argument('--full-upgrade',
action='store_true',
help='Make sure the application is reinstalled if it is previously installed')
parser.add_argument('--wix-root', help='The location where the WiX toolset is installed')
parser.add_argument('--wix-ui', help='WiX UI flavor to use',
choices=['WixUI_Mondo', 'WixUI_FeatureTree', 'WixUI_InstallDir', 'WixUI_Minimal', 'WixUI_Advanced'])
parser.add_argument('--wix-banner', help='Bitmap-file (.bmp) with a banner to show in the installer')
parser.add_argument('--merge-module', help='Add a merge module (like a VC runtime)', action='append')
args = parser.parse_args()
if not args.project_name:
args.project_name = args.project
project_file = args.project
wxs_file = project_file + '.wxs'
msi_file = project_file + '.msi'
wixobj_file = project_file + '.wixobj'
json_file = project_file + '.json'
project = {}
wix_path = ''
for f in [wxs_file, msi_file, wixobj_file]:
if os.path.isfile(f):
os.remove(f)
extra_components = ['MainExecutable', 'ProgramMenuDir']
def prettify(elem):
rough_string = ElementTree.tostring(elem, 'utf-8')
reparsed = minidom.parseString(rough_string)
return reparsed.toprettyxml(indent=' ')
SCS_32BIT_BINARY = 0 # A 32-bit Windows-based application
SCS_64BIT_BINARY = 6 # A 64-bit Windows-based application
SCS_DOS_BINARY = 1 # An MS-DOS-based application
SCS_OS216_BINARY = 5 # A 16-bit OS/2-based application
SCS_PIF_BINARY = 3 # A PIF file that executes an MS-DOS-based application
SCS_POSIX_BINARY = 4 # A POSIX-based application
SCS_WOW_BINARY = 2 # A 16-bit Windows-based application
_GetBinaryType = windll.kernel32.GetBinaryTypeW
_GetBinaryType.argtypes = (LPWSTR, POINTER(DWORD))
_GetBinaryType.restype = BOOL
# https://stackoverflow.com/questions/1345632/determine-if-an-executable-or-library-is-32-or-64-bits-on-windows
def get_binary_type(filepath):
res = DWORD(0)
if _GetBinaryType(filepath, res) == 1:
return res.value
return None
def add_architecture(path, attr):
type = get_binary_type(path)
if type == SCS_64BIT_BINARY:
attr['ProcessorArchitecture'] = 'x64'
elif type == SCS_32BIT_BINARY:
attr['ProcessorArchitecture'] = 'x86'
return attr
def get_path(spec):
if spec[0] == '.': # Relative
if spec == '.':
return project['program']['dir']
if spec.startswith('.\\'):
return project['program']['dir'] + '\\' + spec[2:]
else:
return project['program']['dir'] + '\\' + spec
# Absolute
return spec
def get_hash(path):
hash_md5 = hashlib.md5()
hash_md5.update(path.encode('utf-8'))
return hash_md5.hexdigest()
def do_add_dependencies(dep, dir, directory, component, relpath):
pattern = dep['pattern']
recurse = 'recurse' in dep and dep['recurse'] == 'yes'
preserve = 'preserve-hierarchy' in dep and dep['preserve-hierarchy'] == 'yes'
src_dir = get_path(dir)
for current_dir in glob.iglob(src_dir):
if not os.path.isdir(current_dir):
continue
if (preserve):
dir_hash = get_hash(current_dir)
dir_name = os.path.basename(current_dir)
current_relpath = relpath + '/' + dir
current_directory = SubElement(directory, 'Directory', {
'Id' : 'Dir' + dir_hash,
'Name' : dir_name
})
else:
current_directory = directory
current_relpath = relpath
for src_path in glob.iglob(current_dir + '\\' + pattern):
file_name = os.path.basename(src_path)
if os.path.isfile(src_path):
if preserve:
# We need to remember the component-id
hash = get_hash(src_path)
if not 'generated-components' in project:
project['generated-components'] = {}
if hash in project['generated-components']:
component_info = project['generated-components'][hash]
else:
component_info = project['generated-components'][hash] = {
'Guid' : str(uuid.uuid4()),
'Id' : 'Generated_' + hash,
'Path' : src_path
}
current_component = SubElement(current_directory, 'Component', {
'Id' : component_info['Id'],
'Guid' : component_info['Guid'],
'Win64' : 'yes' if is_64_bit else 'no'
})
extra_components.append(component_info['Id'])
else:
current_component = component
SubElement(current_component, 'File', add_architecture(src_path, {
'Id' : 'File_' + get_hash(src_path),
'Name' : file_name,
'DiskId' : '1',
'Source' : src_path,
'KeyPath' : 'yes' if preserve else 'no'
}))
if os.path.isdir(src_path) and recurse:
do_add_dependencies(dep, os.path.basename(src_path), directory, component, current_relpath)
def add_dependencies(directory, component):
for d in project['program']['dependencies']:
do_add_dependencies(d, d['dir'], directory, component, '')
def bootstrap():
print('Bootstrapping project ' + args.project)
# General stuff
project['program'] = {}
project['merge-modules'] = args.merge_module if args.merge_module else []
project['manufacturer'] = args.manufacturer if args.manufacturer else "jgaa's Fan Club!"
project['product'] = args.project_name
project['version'] = args.project_version if args.project_version else '1.0.0'
project['program']['dir'] = args.source_dir if args.source_dir else os.getcwd()
project['program']['name'] = args.project_name
# TODO: Try to search for an exe file if the guess is wrong
project['program']['binary'] = args.executable if args.executable else args.project_name + '.exe'
# TODO: Try to search for an icon file if the guess is wrong. If not found, use the exe
project['program']['icon'] = args.icon if args.icon else args.project_name + '.ico'
if args.license:
project['program']['license'] = args.license
project['program']['shortcuts'] = ['startmenu']
project['program']['dependencies'] = [{
'dir': '.',
'pattern': '*.dll',
'recurse': 'no'
}]
if args.add_desktop_shortcut:
project['program']['shortcuts'].append('desktop')
project['wix'] = {}
if args.wix_ui:
project['wix']['ui'] = args.wix_ui
else:
project['wix']['ui'] = 'WixUI_InstallDir'
if args.wix_root:
project['wix']['root-folder'] = args.wix_root
if args.wix_banner:
project['program']['banner'] = args.wix_banner
# Add more specifics
if args.auto_create == 'qt':
# Add everything in subdirs
project['program']['dependencies'].append({
'dir': '.\\*',
'pattern': '*',
'preserve-hierarchy': 'yes',
'recurse': 'yes'
})
elif args.auto_create == 'simple':
pass
if os.path.isfile(json_file):
with open(json_file, 'r') as f:
project = json.load(f)
elif args.auto_create:
bootstrap()
else:
print("No project file '" + json_file + ", and no --auto-create argument specified!")
sys.exit(-1)
if args.wix_root:
wix_path = args.wix_root
elif 'wix' in project and 'root-folder' in project['wix']:
wix_path = project['wix']['root-folder']
if wix_path and not wix_path.endswith('\\'):
wix_path += '\\'
wix_path += 'bin\\'
# Handle update logic
# Basically, enforce a full update if the version has changed
if args.project_version:
if project['version'] != args.project_version:
args.full_upgrade = True
project['version'] = args.project_version
print('Resetting product-id')
if args.full_upgrade or not 'id' in project:
project['id'] = str(uuid.uuid4())
if not 'upgrade-code' in project:
project['upgrade-code'] = str(uuid.uuid4())
if not 'component-id' in project:
project['component-id'] = str(uuid.uuid4())
if not 'rf-component-id' in project:
project['rf-component-id'] = str(uuid.uuid4())
if not 'language' in project:
project['language'] = '1033'
if not 'codepage' in project:
project['codepage'] = '1252'
if not 'version' in project:
project['version'] = '1.0.0'
exepath = project['program']['dir'] + '\\' + project['program']['binary']
# If the main application is 64 bit, assume that all binaries are 64 bits
# TODO: Support stand-alone 32 bit binaries as well as separate features(?)
is_64_bit = get_binary_type(exepath) == SCS_64BIT_BINARY
wix_root = Element('Wix', {'xmlns' : 'http://schemas.microsoft.com/wix/2006/wi'})
wix_product = SubElement(wix_root, 'Product', {
'Name' : project['product'],
'Manufacturer' : project['manufacturer'],
'Id' : project['id'],
'UpgradeCode' : project['upgrade-code'],
'Language' : project['language'],
'Codepage' : project['codepage'],
'Version' : project['version']
})
wix_package = SubElement(wix_product, 'Package ', {
'Id' : '*',
'Manufacturer' : project['manufacturer'],
'Keywords' : 'Installer',
'Description' : project['product'] + ' Installer',
'InstallerVersion': '100',
'Languages' : project['language'],
'Compressed' : 'yes',
'SummaryCodepage' : project['codepage'],
'Platform': 'x64' if is_64_bit else 'x86'
})
SubElement(wix_product, 'Media ', {
'Id' : '1',
'Cabinet' : 'mkmsi.cab',
'EmbedCab' : 'yes'
})
wix_targetdir = SubElement(wix_product, 'Directory', {
'Id' : 'TARGETDIR',
'Name' : 'SourceDir'
})
wix_pfdir = SubElement(wix_targetdir, 'Directory', {
'Id' : 'ProgramFiles64Folder' if is_64_bit else 'ProgramFilesFolder',
'Name' : 'PFiles'
})
wix_companydir = SubElement(wix_pfdir, 'Directory', {
'Id' : 'OrgDir',
'Name' : project['manufacturer']
})
wix_installdir = SubElement(wix_companydir, 'Directory', {
'Id' : 'INSTALLDIR',
'Name' : project['product']
})
wix_component_main = SubElement(wix_installdir, 'Component ', {
'Id' : 'MainExecutable',
'Guid' : project['component-id'],
'Win64' : 'yes' if is_64_bit else 'no'
})
wix_executable_file = SubElement(wix_component_main, 'File ', add_architecture(exepath, {
'Id' : 'MainExecutableFile',
'Name' : project['program']['binary'],
'DiskId' : '1',
'Source' : exepath,
'KeyPath' : 'yes',
'Vital' : 'yes'
}))
if 'startmenu' in project['program']['shortcuts']:
SubElement(wix_executable_file, 'Shortcut ', {
'Id' : 'startmenu',
'Directory' : 'ProgramMenuDir',
'Name' : project['product'],
'WorkingDirectory' : 'INSTALLDIR',
'Icon' : project['program']['icon'],
'IconIndex' : '0',
'Advertise' : 'yes'
})
if 'desktop' in project['program']['shortcuts']:
SubElement(wix_executable_file, 'Shortcut ', {
'Id' : 'desktopshortcut',
'Directory' : 'DesktopFolder',
'Name' : project['product'],
'WorkingDirectory' : 'INSTALLDIR',
'Icon' : project['program']['icon'],
'IconIndex' : '0',
'Advertise' : 'yes'
})
wix_rf_component = SubElement(
SubElement(
SubElement(wix_targetdir, 'Directory', {
'Id' : 'ProgramMenuFolder',
'Name' : 'Programs'
}), 'Directory', {
'Id' : 'ProgramMenuDir',
'Name' : project['product']
}), 'Component ', {
'Id' : 'ProgramMenuDir',
'Guid' : project['rf-component-id']})
SubElement(wix_rf_component, 'RemoveFolder', {
'Id' : 'ProgramMenuDir',
'On' : 'uninstall'
})
SubElement(wix_rf_component, 'RegistryValue', {
'Root' : 'HKCU',
'Key' : 'Software\\[Manufacturer]\\[ProductName]',
'Type' : 'string',
'Value' : '',
'KeyPath' : 'yes'
})
SubElement(wix_product, 'Icon', {
'Id' : project['program']['icon'],
'SourceFile' : project['program']['dir'] + '\\' + project['program']['icon']
})
SubElement(wix_targetdir, 'Directory', {
'Id' : 'DesktopFolder',
'Name' : 'Desktop'
})
add_dependencies(wix_installdir, wix_component_main)
wix_feature = SubElement(wix_product, 'Feature', {
'Id' : 'Complete',
'Level' : '1',
'Title' : project['product'],
'Description' : 'The complete package.',
'Display' : 'expand',
'ConfigurableDirectory' : 'INSTALLDIR'
})
wix_feature_product = SubElement(wix_feature, 'Feature', {
'Id' : 'MainProgram',
'Level' : '1',
'Title' : project['product'],
'Description' : 'The application'
})
for id in extra_components:
SubElement(wix_feature_product, 'ComponentRef', {
'Id' : id
})
SubElement(wix_product, 'Property', {
'Id' : 'WIXUI_INSTALLDIR',
'Value' : 'INSTALLDIR'
})
wix_ui = SubElement(wix_product, 'UI')
SubElement(wix_ui, 'UIRef', {
'Id' : project['wix']['ui']
})
SubElement(wix_product, 'UIRef', {
'Id' : 'WixUI_ErrorProgressText'
})
if project['program']['license']:
SubElement(wix_product, 'WixVariable', {
'Id' : 'WixUILicenseRtf',
'Value' : get_path(project['program']['license'])
})
if 'banner' in project['program']:
SubElement(wix_product, 'WixVariable', {
'Id' : 'WixUIBannerBmp',
'Value' : project['program']['banner']
})
wix_upgrade = SubElement(wix_product, 'Upgrade', {
'Id' : project['upgrade-code']
})
SubElement(wix_upgrade, 'UpgradeVersion', {
'OnlyDetect' : 'yes',
'Property' : 'NEWERFOUND',
'Minimum' : project['version'].split('.')[0] + '.0.0',
'IncludeMinimum' : 'no'
})
SubElement(wix_product, 'CustomAction', {
'Id' : 'NoDowngrade',
'Error' : 'A later version of [ProductName] is already installed.'
})
SubElement(SubElement(wix_product, 'InstallExecuteSequence'), 'Custom', {
'Action' : 'NoDowngrade',
'After' : 'FindRelatedProducts'
}).text='NEWERFOUND'
# Launch
SubElement(wix_product, 'Property', {
'Id' : 'WIXUI_EXITDIALOGOPTIONALCHECKBOXTEXT',
'Value' : 'Launch ' + project['program']['name']
})
SubElement(wix_product, 'Property', {
'Id' : 'WixShellExecTarget',
# Broken!
#'Value' : '[#' + project['program']['binary'] + ']'
'Value' : '[INSTALLDIR]\\' + project['program']['binary']
})
SubElement(wix_product, 'CustomAction', {
'Id' : 'LaunchApplication',
'BinaryKey': 'WixCA',
'DllEntry': 'WixShellExec',
'Impersonate': 'yes'
})
SubElement(wix_ui, 'Publish ', {
'Dialog' : 'ExitDialog',
'Control' : 'Finish',
'Event' : 'DoAction',
'Value' : 'LaunchApplication'
}).text = 'WIXUI_EXITDIALOGOPTIONALCHECKBOX = 1 and NOT Installed'
# End launch
for mm in project['merge-modules']:
id = 'Merge_' + get_hash(mm)
SubElement(SubElement(wix_product, 'DirectoryRef', {
'Id' : 'TARGETDIR'
}), 'Merge', {
'Id' : id,
'SourceFile' : mm,
'DiskId' : '1',
'Language' : '0'
})
SubElement(SubElement(wix_feature, 'Feature', {
'Id' : id,
'Title': "Merge module",
'AllowAdvertise' : 'no',
'Display' : 'hidden',
'Level' : '1'
}), 'MergeRef', {'Id' : id})
with open(wxs_file, 'w') as f:
print(prettify(wix_root))
f.write(prettify(wix_root))
with open(project_file + '.json', 'w') as f:
f.write(json.dumps(project, sort_keys=True, indent=4))
candle = wix_path + 'candle'
print('Executing: ' + candle)
result = subprocess.run([candle, wxs_file])
if result.returncode != 0:
print("Candle.exe failed with error: " + str(result.returncode))
sys.exit(-1)
light = wix_path + 'light'
print('Executing: ' + light)
result = subprocess.run([light, '-ext', 'WixUIExtension', '-ext', 'WixUtilExtension.dll', wixobj_file])
if result.returncode != 0:
print("Light.exe failed with error: " + str(result.returncode))
sys.exit(-1)
print("Done")