Debian Codegraph @

bypy/windows/wix.py

raw
1 #!/usr/bin/env python
2 # vim:fileencoding=utf-8
3 # License: GPLv3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
4
5 import os
6 import shutil
7 from itertools import count
8
9 from bypy.constants import is64bit
10 from bypy.utils import run
11
12 WIX = os.path.join(os.environ['USERPROFILE'], r'.dotnet\tools\wix.exe')
13 if is64bit:
14 UPGRADE_CODE = '5DD881FF-756B-4097-9D82-8C0F11D521EA'
15 else:
16 UPGRADE_CODE = 'BEB2A80D-E902-4DAD-ADF9-8BD2DA42CFE1'
17 calibre_constants = globals()['calibre_constants']
18
19 j, d, a, b = os.path.join, os.path.dirname, os.path.abspath, os.path.basename
20
21
22 def add_wix_extension(name):
23 if not os.path.exists(os.path.expanduser(f'~/.wix/extensions/{name}')):
24 run(WIX, 'extension', 'add', '-g', name)
25
26
27 def create_installer(env, compression_level='9'):
28 cl = int(compression_level)
29 if cl > 4:
30 dcl = 'high'
31 else:
32 dcl = {1: 'none', 2: 'low', 3: 'medium', 4: 'mszip'}[cl]
33 if os.path.exists(env.installer_dir):
34 shutil.rmtree(env.installer_dir)
35 os.makedirs(env.installer_dir)
36
37 with open(j(d(__file__), 'wix-template.xml'), 'rb') as f:
38 template = f.read().decode('utf-8')
39
40 cmd = [WIX, '--version']
41 WIXVERSION = run(*cmd, get_output=True).decode('utf-8').split('.')[0]
42 if int(WIXVERSION) >= 5:
43 # Virtual Symbol "WixUISupportPerUser" needs to be overridden in WIX V5 https://wixtoolset.org/docs/fivefour/
44 template = template.replace('WixUISupportPerUser', 'override WixUISupportPerUser')
45
46 components, smap = get_components_from_files(env)
47 wxs = template.format(
48 app=calibre_constants['appname'],
49 version=calibre_constants['version'],
50 upgrade_code=UPGRADE_CODE,
51 x64=' 64bit' if is64bit else '',
52 compression='high',
53 app_components=components,
54 main_app_uid=calibre_constants['MAIN_APP_UID'],
55 viewer_app_uid=calibre_constants['VIEWER_APP_UID'],
56 editor_app_uid=calibre_constants['EDITOR_APP_UID'],
57 exe_map=smap,
58 main_icon=j(env.src_root, 'icons', 'library.ico'),
59 viewer_icon=j(env.src_root, 'icons', 'viewer.ico'),
60 editor_icon=j(env.src_root, 'icons', 'ebook-edit.ico'),
61 web_icon=j(env.src_root, 'icons', 'web.ico'),
62 license=j(env.src_root, 'LICENSE.rtf'),
63 banner=j(env.src_root, 'icons', 'wix-banner.bmp'),
64 dialog=j(env.src_root, 'icons', 'wix-dialog.bmp'),
65 )
66 with open(j(d(__file__), 'en-us.xml'), 'rb') as f:
67 template = f.read().decode('utf-8')
68 enus = template.format(app=calibre_constants['appname'])
69
70 enusf = j(env.installer_dir, 'en-us.wxl')
71 wxsf = j(env.installer_dir, calibre_constants['appname'] + '.wxs')
72 with open(wxsf, 'wb') as f:
73 f.write(wxs.encode('utf-8'))
74 with open(enusf, 'wb') as f:
75 f.write(enus.encode('utf-8'))
76 arch = 'x64' if is64bit else 'x86'
77 installer = j(env.dist, '%s%s-%s.msi' % (
78 calibre_constants['appname'], ('-64bit' if is64bit else ''), calibre_constants['version']))
79 add_wix_extension('WixToolset.Util.wixext')
80 add_wix_extension( 'WixToolset.UI.wixext')
81 cmd = [WIX, 'build', '-arch', arch, '-culture', 'en-us', '-loc', enusf, '-dcl', dcl,
82 '-ext', 'WixToolset.Util.wixext', '-ext', 'WixToolset.UI.wixext', '-o', installer, wxsf]
83 run(*cmd)
84 pdb = installer.rpartition('.')[0] + '.wixpdb'
85 os.remove(pdb)
86
87
88 def get_components_from_files(env):
89
90 file_idc = count()
91 file_id_map = {}
92
93 def process_dir(path):
94 components = []
95 for x in os.listdir(path):
96 f = os.path.join(path, x)
97 file_id_map[f] = fid = next(file_idc)
98
99 if os.path.isdir(f):
100 components.append(
101 '<Directory Id="file_%s" FileSource="%s" Name="%s">' %
102 (file_id_map[f], f, x))
103 c = process_dir(f)
104 components.extend(c)
105 components.append('</Directory>')
106 else:
107 checksum = 'Checksum="yes"' if x.endswith('.exe') else ''
108 c = [
109 ('<Component Id="component_%s" Feature="MainApplication" '
110 'Guid="*">') % (fid,),
111 ('<File Id="file_%s" Source="%s" Name="%s" ReadOnly="yes" '
112 'KeyPath="yes" %s/>') %
113 (fid, f, x, checksum),
114 '</Component>'
115 ]
116 if x.endswith('.exe') and not x.startswith('pdf'):
117 # Add the executable to app paths so that users can
118 # launch it from the run dialog even if it is not on
119 # the path. See http://msdn.microsoft.com/en-us/library/windows/desktop/ee872121(v=vs.85).aspx
120 c[-1:-1] = [
121 ('<RegistryValue Root="HKLM" '
122 r'Key="SOFTWARE\Microsoft\Windows\CurrentVersion\App '
123 r'Paths\%s" Value="[#file_%d]" Type="string" />' % (x, fid)),
124 ('<RegistryValue Root="HKLM" '
125 r'Key="SOFTWARE\Microsoft\Windows\CurrentVersion\App '
126 r'Paths\{0}" Name="Path" Value="[APPLICATIONFOLDER]" '
127 'Type="string" />'.format(x)),
128 ]
129 components.append('\n'.join(c))
130 return components
131
132 components = process_dir(a(env.base))
133 smap = {}
134 for x in calibre_constants['basenames']['gui']:
135 smap[x] = 'file_%d' % file_id_map[a(j(env.base, x + '.exe'))]
136
137 return '\t\t\t\t' + '\n\t\t\t\t'.join(components), smap