blob: b5ed6f041e19a04d69307d7ddf3ff9f7361845e1 [file] [log] [blame]
Greg Ward27199e82000-06-27 01:24:38 +00001"""distutils.command.bdist_wininst
2
3Implements the Distutils 'bdist_wininst' command: create a windows installer
4exe-program."""
5
Miss Islington (bot)b4cd6ba2019-07-05 02:03:23 -07006import os
7import sys
8import warnings
Greg Ward27199e82000-06-27 01:24:38 +00009from distutils.core import Command
Tarek Ziadé8b441d02010-01-29 11:46:31 +000010from distutils.util import get_platform
Tarek Ziadé36797272010-07-22 12:50:05 +000011from distutils.dir_util import create_tree, remove_tree
12from distutils.errors import *
13from distutils.sysconfig import get_python_version
14from distutils import log
Greg Ward27199e82000-06-27 01:24:38 +000015
Collin Winter5b7e9d72007-08-30 03:52:21 +000016class bdist_wininst(Command):
Greg Ward27199e82000-06-27 01:24:38 +000017
Greg Wardb0b98a52000-06-28 00:56:20 +000018 description = "create an executable installer for MS Windows"
Greg Ward27199e82000-06-27 01:24:38 +000019
Thomas Heller5c5ea1a2001-03-16 20:57:37 +000020 user_options = [('bdist-dir=', None,
Greg Ward27199e82000-06-27 01:24:38 +000021 "temporary directory for creating the distribution"),
Christian Heimes5e696852008-04-09 08:37:03 +000022 ('plat-name=', 'p',
23 "platform name to embed in generated filenames "
24 "(default: %s)" % get_platform()),
Greg Ward3bfc8c82000-09-16 15:56:32 +000025 ('keep-temp', 'k',
Greg Ward27199e82000-06-27 01:24:38 +000026 "keep the pseudo-installation tree around after " +
27 "creating the distribution archive"),
Thomas Hellera146fea2004-07-06 19:23:27 +000028 ('target-version=', None,
Greg Ward27199e82000-06-27 01:24:38 +000029 "require a specific python version" +
Greg Ward018cbb12000-08-26 02:40:10 +000030 " on the target system"),
Thomas Heller904ca112000-09-07 15:59:22 +000031 ('no-target-compile', 'c',
32 "do not compile .py to .pyc on the target system"),
33 ('no-target-optimize', 'o',
Serhiy Storchaka34fd4c22018-11-05 16:20:25 +020034 "do not compile .py to .pyo (optimized) "
Thomas Heller1dbe9d52000-09-29 11:36:55 +000035 "on the target system"),
Greg Wardfd9f1682000-07-05 03:08:55 +000036 ('dist-dir=', 'd',
37 "directory to put final built distributions in"),
Thomas Hellere09f6392001-02-19 09:20:30 +000038 ('bitmap=', 'b',
39 "bitmap to use for the installer instead of python-powered logo"),
40 ('title=', 't',
41 "title to display on the installer background instead of default"),
Martin v. Löwis9668b932002-01-12 11:27:42 +000042 ('skip-build', None,
43 "skip rebuilding everything (for testing/debugging)"),
Thomas Hellerd2d58e02002-02-20 08:01:19 +000044 ('install-script=', None,
Serhiy Storchaka34fd4c22018-11-05 16:20:25 +020045 "basename of installation script to be run after "
Thomas Hellerd7c14c62002-11-05 10:06:19 +000046 "installation or before deinstallation"),
Thomas Hellera19cdad2004-02-20 14:43:21 +000047 ('pre-install-script=', None,
48 "Fully qualified filename of a script to be run before "
49 "any files are installed. This script need not be in the "
50 "distribution"),
Christian Heimes81ee3ef2008-05-04 22:42:01 +000051 ('user-access-control=', None,
52 "specify Vista's UAC handling - 'none'/default=no "
53 "handling, 'auto'=use UAC if target Python installed for "
54 "all users, 'force'=always use UAC"),
Greg Ward27199e82000-06-27 01:24:38 +000055 ]
56
Andrew M. Kuchlingda9f0bf2002-03-21 23:44:01 +000057 boolean_options = ['keep-temp', 'no-target-compile', 'no-target-optimize',
58 'skip-build']
Greg Ward99b032e2000-09-25 01:41:15 +000059
Miss Islington (bot)be5bb522019-07-01 05:54:19 -070060 # bpo-10945: bdist_wininst requires mbcs encoding only available on Windows
61 _unsupported = (sys.platform != "win32")
62
Miss Islington (bot)b4cd6ba2019-07-05 02:03:23 -070063 def __init__(self, *args, **kw):
64 super().__init__(*args, **kw)
65 warnings.warn("bdist_wininst command is deprecated since Python 3.8, "
66 "use bdist_wheel (wheel packages) instead",
67 DeprecationWarning, 2)
68
Collin Winter5b7e9d72007-08-30 03:52:21 +000069 def initialize_options(self):
Greg Ward27199e82000-06-27 01:24:38 +000070 self.bdist_dir = None
Christian Heimes5e696852008-04-09 08:37:03 +000071 self.plat_name = None
Greg Ward3bfc8c82000-09-16 15:56:32 +000072 self.keep_temp = 0
Thomas Heller904ca112000-09-07 15:59:22 +000073 self.no_target_compile = 0
74 self.no_target_optimize = 0
Greg Ward27199e82000-06-27 01:24:38 +000075 self.target_version = None
Greg Wardfd9f1682000-07-05 03:08:55 +000076 self.dist_dir = None
Thomas Hellere09f6392001-02-19 09:20:30 +000077 self.bitmap = None
78 self.title = None
Éric Araujofbe37df2011-08-29 21:48:39 +020079 self.skip_build = None
Thomas Hellerd2d58e02002-02-20 08:01:19 +000080 self.install_script = None
Thomas Hellera19cdad2004-02-20 14:43:21 +000081 self.pre_install_script = None
Christian Heimes81ee3ef2008-05-04 22:42:01 +000082 self.user_access_control = None
Greg Ward27199e82000-06-27 01:24:38 +000083
Greg Ward27199e82000-06-27 01:24:38 +000084
Collin Winter5b7e9d72007-08-30 03:52:21 +000085 def finalize_options(self):
Éric Araujofbe37df2011-08-29 21:48:39 +020086 self.set_undefined_options('bdist', ('skip_build', 'skip_build'))
87
Greg Ward27199e82000-06-27 01:24:38 +000088 if self.bdist_dir is None:
Georg Brandlf08a9dd2008-06-10 16:57:31 +000089 if self.skip_build and self.plat_name:
90 # If build is skipped and plat_name is overridden, bdist will
91 # not see the correct 'plat_name' - so set that up manually.
92 bdist = self.distribution.get_command_obj('bdist')
93 bdist.plat_name = self.plat_name
94 # next the command will be initialized using that name
Greg Ward27199e82000-06-27 01:24:38 +000095 bdist_base = self.get_finalized_command('bdist').bdist_base
96 self.bdist_dir = os.path.join(bdist_base, 'wininst')
Éric Araujofbe37df2011-08-29 21:48:39 +020097
Greg Ward27199e82000-06-27 01:24:38 +000098 if not self.target_version:
99 self.target_version = ""
Éric Araujofbe37df2011-08-29 21:48:39 +0200100
Thomas Hellera19cdad2004-02-20 14:43:21 +0000101 if not self.skip_build and self.distribution.has_ext_modules():
Andrew M. Kuchling1cace1a2002-11-14 01:44:35 +0000102 short_version = get_python_version()
Greg Ward27199e82000-06-27 01:24:38 +0000103 if self.target_version and self.target_version != short_version:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000104 raise DistutilsOptionError(
Georg Brandl56be37c2010-08-02 19:16:34 +0000105 "target version can only be %s, or the '--skip-build'" \
Collin Winter5b7e9d72007-08-30 03:52:21 +0000106 " option must be specified" % (short_version,))
Greg Ward27199e82000-06-27 01:24:38 +0000107 self.target_version = short_version
108
Christian Heimes5e696852008-04-09 08:37:03 +0000109 self.set_undefined_options('bdist',
110 ('dist_dir', 'dist_dir'),
111 ('plat_name', 'plat_name'),
112 )
Greg Wardfd9f1682000-07-05 03:08:55 +0000113
Thomas Hellerd7c14c62002-11-05 10:06:19 +0000114 if self.install_script:
115 for script in self.distribution.scripts:
116 if self.install_script == os.path.basename(script):
117 break
118 else:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000119 raise DistutilsOptionError(
120 "install_script '%s' not found in scripts"
121 % self.install_script)
Greg Ward27199e82000-06-27 01:24:38 +0000122
Collin Winter5b7e9d72007-08-30 03:52:21 +0000123 def run(self):
Greg Ward1ac98022000-09-01 01:44:45 +0000124 if (sys.platform != "win32" and
125 (self.distribution.has_ext_modules() or
126 self.distribution.has_c_libraries())):
Thomas Heller904ca112000-09-07 15:59:22 +0000127 raise DistutilsPlatformError \
Greg Ward1ac98022000-09-01 01:44:45 +0000128 ("distribution contains extensions and/or C libraries; "
129 "must be compiled on a Windows 32 platform")
Greg Ward27199e82000-06-27 01:24:38 +0000130
Martin v. Löwis9668b932002-01-12 11:27:42 +0000131 if not self.skip_build:
132 self.run_command('build')
Greg Ward27199e82000-06-27 01:24:38 +0000133
Thomas Hellercd494ad2003-06-12 17:23:58 +0000134 install = self.reinitialize_command('install', reinit_subcommands=1)
Greg Ward27199e82000-06-27 01:24:38 +0000135 install.root = self.bdist_dir
Martin v. Löwis9668b932002-01-12 11:27:42 +0000136 install.skip_build = self.skip_build
Thomas Hellerfd0e82a2002-04-09 14:14:38 +0000137 install.warn_dir = 0
Christian Heimes5e696852008-04-09 08:37:03 +0000138 install.plat_name = self.plat_name
Greg Ward27199e82000-06-27 01:24:38 +0000139
Greg Wardb0b98a52000-06-28 00:56:20 +0000140 install_lib = self.reinitialize_command('install_lib')
Greg Ward018cbb12000-08-26 02:40:10 +0000141 # we do not want to include pyc or pyo files
Greg Ward27199e82000-06-27 01:24:38 +0000142 install_lib.compile = 0
143 install_lib.optimize = 0
Tim Peters182b5ac2004-07-18 06:16:08 +0000144
Mark Hammond79d9bfa2004-10-27 21:54:33 +0000145 if self.distribution.has_ext_modules():
146 # If we are building an installer for a Python version other
147 # than the one we are currently running, then we need to ensure
148 # our build_lib reflects the other Python version rather than ours.
149 # Note that for target_version!=sys.version, we must have skipped the
150 # build step, so there is no issue with enforcing the build of this
151 # version.
152 target_version = self.target_version
153 if not target_version:
154 assert self.skip_build, "Should have already checked this"
Serhiy Storchaka885bdc42016-02-11 13:10:36 +0200155 target_version = '%d.%d' % sys.version_info[:2]
Christian Heimes5e696852008-04-09 08:37:03 +0000156 plat_specifier = ".%s-%s" % (self.plat_name, target_version)
Mark Hammond79d9bfa2004-10-27 21:54:33 +0000157 build = self.get_finalized_command('build')
158 build.build_lib = os.path.join(build.build_base,
159 'lib' + plat_specifier)
Greg Ward27199e82000-06-27 01:24:38 +0000160
Thomas Hellerc010c172001-09-05 13:00:40 +0000161 # Use a custom scheme for the zip-file, because we have to decide
162 # at installation time which scheme to use.
163 for key in ('purelib', 'platlib', 'headers', 'scripts', 'data'):
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000164 value = key.upper()
Thomas Hellerc010c172001-09-05 13:00:40 +0000165 if key == 'headers':
166 value = value + '/Include/$dist_name'
167 setattr(install,
168 'install_' + key,
169 value)
Greg Ward27199e82000-06-27 01:24:38 +0000170
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000171 log.info("installing to %s", self.bdist_dir)
Greg Ward27199e82000-06-27 01:24:38 +0000172 install.ensure_finalized()
Thomas Hellerc010c172001-09-05 13:00:40 +0000173
174 # avoid warning of 'install_lib' about installing
175 # into a directory not in sys.path
176 sys.path.insert(0, os.path.join(self.bdist_dir, 'PURELIB'))
177
Greg Ward27199e82000-06-27 01:24:38 +0000178 install.run()
179
Thomas Hellerc010c172001-09-05 13:00:40 +0000180 del sys.path[0]
181
Greg Ward27199e82000-06-27 01:24:38 +0000182 # And make an archive relative to the root of the
183 # pseudo-installation tree.
Thomas Hellerd98d25e2002-10-15 14:51:58 +0000184 from tempfile import mktemp
185 archive_basename = mktemp()
Greg Wardfd9f1682000-07-05 03:08:55 +0000186 fullname = self.distribution.get_fullname()
Greg Wardcb1f4c42000-09-30 18:27:54 +0000187 arcname = self.make_archive(archive_basename, "zip",
Thomas Hellerc010c172001-09-05 13:00:40 +0000188 root_dir=self.bdist_dir)
Thomas Hellerc7cb9ed2001-12-18 20:13:40 +0000189 # create an exe containing the zip-file
Thomas Hellere09f6392001-02-19 09:20:30 +0000190 self.create_exe(arcname, fullname, self.bitmap)
Martin v. Löwis98da5622005-03-23 18:54:36 +0000191 if self.distribution.has_ext_modules():
192 pyversion = get_python_version()
193 else:
194 pyversion = 'any'
195 self.distribution.dist_files.append(('bdist_wininst', pyversion,
Martin v. Löwis24ff83d2005-03-22 22:23:29 +0000196 self.get_installer_filename(fullname)))
Thomas Hellerc7cb9ed2001-12-18 20:13:40 +0000197 # remove the zip-file again
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000198 log.debug("removing temporary file '%s'", arcname)
Thomas Hellerd98d25e2002-10-15 14:51:58 +0000199 os.remove(arcname)
Greg Ward27199e82000-06-27 01:24:38 +0000200
Greg Ward3bfc8c82000-09-16 15:56:32 +0000201 if not self.keep_temp:
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000202 remove_tree(self.bdist_dir, dry_run=self.dry_run)
Greg Ward27199e82000-06-27 01:24:38 +0000203
Collin Winter5b7e9d72007-08-30 03:52:21 +0000204 def get_inidata(self):
Greg Ward1ac98022000-09-01 01:44:45 +0000205 # Return data describing the installation.
Greg Ward1ac98022000-09-01 01:44:45 +0000206 lines = []
Greg Ward27199e82000-06-27 01:24:38 +0000207 metadata = self.distribution.metadata
Greg Ward27199e82000-06-27 01:24:38 +0000208
Thomas Helleree6fd062004-07-23 19:44:29 +0000209 # Write the [metadata] section.
Greg Wardcb1f4c42000-09-30 18:27:54 +0000210 lines.append("[metadata]")
Greg Ward27199e82000-06-27 01:24:38 +0000211
Greg Wardb0b98a52000-06-28 00:56:20 +0000212 # 'info' will be displayed in the installer's dialog box,
213 # describing the items to be installed.
Greg Ward6f9320b2000-08-27 20:44:13 +0000214 info = (metadata.long_description or '') + '\n'
Greg Ward27199e82000-06-27 01:24:38 +0000215
Thomas Helleree6fd062004-07-23 19:44:29 +0000216 # Escape newline characters
217 def escape(s):
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000218 return s.replace("\n", "\\n")
Thomas Helleree6fd062004-07-23 19:44:29 +0000219
Thomas Hellere138f362001-10-05 20:40:48 +0000220 for name in ["author", "author_email", "description", "maintainer",
221 "maintainer_email", "name", "url", "version"]:
222 data = getattr(metadata, name, "")
223 if data:
224 info = info + ("\n %s: %s" % \
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000225 (name.capitalize(), escape(data)))
Thomas Helleree6fd062004-07-23 19:44:29 +0000226 lines.append("%s=%s" % (name, escape(data)))
Greg Ward27199e82000-06-27 01:24:38 +0000227
228 # The [setup] section contains entries controlling
229 # the installer runtime.
Greg Wardcb1f4c42000-09-30 18:27:54 +0000230 lines.append("\n[Setup]")
Thomas Hellerd2d58e02002-02-20 08:01:19 +0000231 if self.install_script:
232 lines.append("install_script=%s" % self.install_script)
Thomas Helleree6fd062004-07-23 19:44:29 +0000233 lines.append("info=%s" % escape(info))
Greg Wardcb1f4c42000-09-30 18:27:54 +0000234 lines.append("target_compile=%d" % (not self.no_target_compile))
235 lines.append("target_optimize=%d" % (not self.no_target_optimize))
Greg Ward27199e82000-06-27 01:24:38 +0000236 if self.target_version:
Greg Wardcb1f4c42000-09-30 18:27:54 +0000237 lines.append("target_version=%s" % self.target_version)
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000238 if self.user_access_control:
239 lines.append("user_access_control=%s" % self.user_access_control)
Greg Ward27199e82000-06-27 01:24:38 +0000240
Thomas Hellere09f6392001-02-19 09:20:30 +0000241 title = self.title or self.distribution.get_fullname()
Thomas Helleree6fd062004-07-23 19:44:29 +0000242 lines.append("title=%s" % escape(title))
Thomas Hellerecaf0d82000-09-09 21:15:12 +0000243 import time
244 import distutils
Thomas Hellera19cdad2004-02-20 14:43:21 +0000245 build_info = "Built %s with distutils-%s" % \
Greg Wardcb1f4c42000-09-30 18:27:54 +0000246 (time.ctime(time.time()), distutils.__version__)
247 lines.append("build_info=%s" % build_info)
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000248 return "\n".join(lines)
Greg Ward27199e82000-06-27 01:24:38 +0000249
Collin Winter5b7e9d72007-08-30 03:52:21 +0000250 def create_exe(self, arcname, fullname, bitmap=None):
Greg Ward1ac98022000-09-01 01:44:45 +0000251 import struct
Greg Ward27199e82000-06-27 01:24:38 +0000252
Greg Ward1ac98022000-09-01 01:44:45 +0000253 self.mkpath(self.dist_dir)
254
255 cfgdata = self.get_inidata()
Greg Ward27199e82000-06-27 01:24:38 +0000256
Thomas Heller612eb092004-07-23 19:58:28 +0000257 installer_name = self.get_installer_filename(fullname)
Greg Wardcb1f4c42000-09-30 18:27:54 +0000258 self.announce("creating %s" % installer_name)
Greg Ward27199e82000-06-27 01:24:38 +0000259
Thomas Hellere09f6392001-02-19 09:20:30 +0000260 if bitmap:
Mickaël Schoentgen58721a92019-04-08 13:08:48 +0000261 with open(bitmap, "rb") as f:
262 bitmapdata = f.read()
Thomas Hellere09f6392001-02-19 09:20:30 +0000263 bitmaplen = len(bitmapdata)
264 else:
265 bitmaplen = 0
266
Mickaël Schoentgen58721a92019-04-08 13:08:48 +0000267 with open(installer_name, "wb") as file:
268 file.write(self.get_exe_bytes())
269 if bitmap:
270 file.write(bitmapdata)
Fred Drake21d45352001-12-06 21:01:19 +0000271
Mickaël Schoentgen58721a92019-04-08 13:08:48 +0000272 # Convert cfgdata from unicode to ascii, mbcs encoded
273 if isinstance(cfgdata, str):
274 cfgdata = cfgdata.encode("mbcs")
Thomas Helleree6fd062004-07-23 19:44:29 +0000275
Mickaël Schoentgen58721a92019-04-08 13:08:48 +0000276 # Append the pre-install script
Antoine Pitrou1d6a16b2008-09-04 21:32:09 +0000277 cfgdata = cfgdata + b"\0"
Mickaël Schoentgen58721a92019-04-08 13:08:48 +0000278 if self.pre_install_script:
279 # We need to normalize newlines, so we open in text mode and
280 # convert back to bytes. "latin-1" simply avoids any possible
281 # failures.
282 with open(self.pre_install_script, "r",
Inada Naoki5909ad12019-04-09 14:54:30 +0900283 encoding="latin-1") as script:
Mickaël Schoentgen58721a92019-04-08 13:08:48 +0000284 script_data = script.read().encode("latin-1")
285 cfgdata = cfgdata + script_data + b"\n\0"
286 else:
287 # empty pre-install script
288 cfgdata = cfgdata + b"\0"
289 file.write(cfgdata)
Thomas Hellerb8f134e2004-07-19 09:45:46 +0000290
Mickaël Schoentgen58721a92019-04-08 13:08:48 +0000291 # The 'magic number' 0x1234567B is used to make sure that the
292 # binary layout of 'cfgdata' is what the wininst.exe binary
293 # expects. If the layout changes, increment that number, make
294 # the corresponding changes to the wininst.exe sources, and
295 # recompile them.
296 header = struct.pack("<iii",
297 0x1234567B, # tag
298 len(cfgdata), # length
299 bitmaplen, # number of bytes in bitmap
300 )
301 file.write(header)
302 with open(arcname, "rb") as f:
303 file.write(f.read())
Greg Ward27199e82000-06-27 01:24:38 +0000304
Thomas Heller612eb092004-07-23 19:58:28 +0000305 def get_installer_filename(self, fullname):
306 # Factored out to allow overriding in subclasses
307 if self.target_version:
308 # if we create an installer for a specific python version,
309 # it's better to include this in the name
310 installer_name = os.path.join(self.dist_dir,
Christian Heimes5e696852008-04-09 08:37:03 +0000311 "%s.%s-py%s.exe" %
312 (fullname, self.plat_name, self.target_version))
Thomas Heller612eb092004-07-23 19:58:28 +0000313 else:
314 installer_name = os.path.join(self.dist_dir,
Christian Heimes5e696852008-04-09 08:37:03 +0000315 "%s.%s.exe" % (fullname, self.plat_name))
Thomas Heller30d00082004-08-17 10:15:07 +0000316 return installer_name
Thomas Heller612eb092004-07-23 19:58:28 +0000317
Collin Winter5b7e9d72007-08-30 03:52:21 +0000318 def get_exe_bytes(self):
Thomas Hellera146fea2004-07-06 19:23:27 +0000319 # If a target-version other than the current version has been
320 # specified, then using the MSVC version from *this* build is no good.
321 # Without actually finding and executing the target version and parsing
322 # its sys.version, we just hard-code our knowledge of old versions.
323 # NOTE: Possible alternative is to allow "--target-version" to
324 # specify a Python executable rather than a simple version string.
325 # We can then execute this program to obtain any info we need, such
326 # as the real sys.version string for the build.
327 cur_version = get_python_version()
Steve Dowerfd3664b2015-05-23 09:02:50 -0700328
329 # If the target version is *later* than us, then we assume they
330 # use what we use
331 # string compares seem wrong, but are what sysconfig.py itself uses
332 if self.target_version and self.target_version < cur_version:
333 if self.target_version < "2.4":
Steve Dower5fcd5e62017-09-06 10:01:38 -0700334 bv = '6.0'
Steve Dowerfd3664b2015-05-23 09:02:50 -0700335 elif self.target_version == "2.4":
Steve Dower5fcd5e62017-09-06 10:01:38 -0700336 bv = '7.1'
Steve Dowerfd3664b2015-05-23 09:02:50 -0700337 elif self.target_version == "2.5":
Steve Dower5fcd5e62017-09-06 10:01:38 -0700338 bv = '8.0'
Steve Dowerfd3664b2015-05-23 09:02:50 -0700339 elif self.target_version <= "3.2":
Steve Dower5fcd5e62017-09-06 10:01:38 -0700340 bv = '9.0'
Steve Dowerfd3664b2015-05-23 09:02:50 -0700341 elif self.target_version <= "3.4":
Steve Dower5fcd5e62017-09-06 10:01:38 -0700342 bv = '10.0'
Thomas Hellera146fea2004-07-06 19:23:27 +0000343 else:
Steve Dower5fcd5e62017-09-06 10:01:38 -0700344 bv = '14.0'
Thomas Hellera146fea2004-07-06 19:23:27 +0000345 else:
346 # for current version - use authoritative check.
Steve Dowerc7090852015-05-23 12:15:57 -0700347 try:
348 from msvcrt import CRT_ASSEMBLY_VERSION
349 except ImportError:
350 # cross-building, so assume the latest version
Steve Dower5fcd5e62017-09-06 10:01:38 -0700351 bv = '14.0'
Steve Dowerc7090852015-05-23 12:15:57 -0700352 else:
Steve Dower2507e292018-01-19 09:09:36 +1100353 # as far as we know, CRT is binary compatible based on
354 # the first field, so assume 'x.0' until proven otherwise
355 major = CRT_ASSEMBLY_VERSION.partition('.')[0]
356 bv = major + '.0'
Steve Dowerfd3664b2015-05-23 09:02:50 -0700357
Thomas Hellera146fea2004-07-06 19:23:27 +0000358
Thomas Heller0bc9c912004-02-20 19:38:50 +0000359 # wininst-x.y.exe is in the same directory as this file
Thomas Heller450cafa2002-11-22 21:08:34 +0000360 directory = os.path.dirname(__file__)
Thomas Heller0bc9c912004-02-20 19:38:50 +0000361 # we must use a wininst-x.y.exe built with the same C compiler
362 # used for python. XXX What about mingw, borland, and so on?
Tarek Ziadéa6191802009-04-09 22:02:39 +0000363
364 # if plat_name starts with "win" but is not "win32"
365 # we want to strip "win" and leave the rest (e.g. -amd64)
366 # for all other cases, we don't want any suffix
367 if self.plat_name != 'win32' and self.plat_name[:3] == 'win':
368 sfix = self.plat_name[3:]
Christian Heimes5e696852008-04-09 08:37:03 +0000369 else:
Tarek Ziadéa6191802009-04-09 22:02:39 +0000370 sfix = ''
371
Steve Dower5fcd5e62017-09-06 10:01:38 -0700372 filename = os.path.join(directory, "wininst-%s%s.exe" % (bv, sfix))
Éric Araujobee5cef2010-11-05 23:51:56 +0000373 f = open(filename, "rb")
374 try:
375 return f.read()
376 finally:
377 f.close()