1 | n/a | """distutils.command.bdist_wininst |
---|
2 | n/a | |
---|
3 | n/a | Implements the Distutils 'bdist_wininst' command: create a windows installer |
---|
4 | n/a | exe-program.""" |
---|
5 | n/a | |
---|
6 | n/a | import sys, os |
---|
7 | n/a | from distutils.core import Command |
---|
8 | n/a | from distutils.util import get_platform |
---|
9 | n/a | from distutils.dir_util import create_tree, remove_tree |
---|
10 | n/a | from distutils.errors import * |
---|
11 | n/a | from distutils.sysconfig import get_python_version |
---|
12 | n/a | from distutils import log |
---|
13 | n/a | |
---|
14 | n/a | class bdist_wininst(Command): |
---|
15 | n/a | |
---|
16 | n/a | description = "create an executable installer for MS Windows" |
---|
17 | n/a | |
---|
18 | n/a | user_options = [('bdist-dir=', None, |
---|
19 | n/a | "temporary directory for creating the distribution"), |
---|
20 | n/a | ('plat-name=', 'p', |
---|
21 | n/a | "platform name to embed in generated filenames " |
---|
22 | n/a | "(default: %s)" % get_platform()), |
---|
23 | n/a | ('keep-temp', 'k', |
---|
24 | n/a | "keep the pseudo-installation tree around after " + |
---|
25 | n/a | "creating the distribution archive"), |
---|
26 | n/a | ('target-version=', None, |
---|
27 | n/a | "require a specific python version" + |
---|
28 | n/a | " on the target system"), |
---|
29 | n/a | ('no-target-compile', 'c', |
---|
30 | n/a | "do not compile .py to .pyc on the target system"), |
---|
31 | n/a | ('no-target-optimize', 'o', |
---|
32 | n/a | "do not compile .py to .pyo (optimized)" |
---|
33 | n/a | "on the target system"), |
---|
34 | n/a | ('dist-dir=', 'd', |
---|
35 | n/a | "directory to put final built distributions in"), |
---|
36 | n/a | ('bitmap=', 'b', |
---|
37 | n/a | "bitmap to use for the installer instead of python-powered logo"), |
---|
38 | n/a | ('title=', 't', |
---|
39 | n/a | "title to display on the installer background instead of default"), |
---|
40 | n/a | ('skip-build', None, |
---|
41 | n/a | "skip rebuilding everything (for testing/debugging)"), |
---|
42 | n/a | ('install-script=', None, |
---|
43 | n/a | "basename of installation script to be run after" |
---|
44 | n/a | "installation or before deinstallation"), |
---|
45 | n/a | ('pre-install-script=', None, |
---|
46 | n/a | "Fully qualified filename of a script to be run before " |
---|
47 | n/a | "any files are installed. This script need not be in the " |
---|
48 | n/a | "distribution"), |
---|
49 | n/a | ('user-access-control=', None, |
---|
50 | n/a | "specify Vista's UAC handling - 'none'/default=no " |
---|
51 | n/a | "handling, 'auto'=use UAC if target Python installed for " |
---|
52 | n/a | "all users, 'force'=always use UAC"), |
---|
53 | n/a | ] |
---|
54 | n/a | |
---|
55 | n/a | boolean_options = ['keep-temp', 'no-target-compile', 'no-target-optimize', |
---|
56 | n/a | 'skip-build'] |
---|
57 | n/a | |
---|
58 | n/a | def initialize_options(self): |
---|
59 | n/a | self.bdist_dir = None |
---|
60 | n/a | self.plat_name = None |
---|
61 | n/a | self.keep_temp = 0 |
---|
62 | n/a | self.no_target_compile = 0 |
---|
63 | n/a | self.no_target_optimize = 0 |
---|
64 | n/a | self.target_version = None |
---|
65 | n/a | self.dist_dir = None |
---|
66 | n/a | self.bitmap = None |
---|
67 | n/a | self.title = None |
---|
68 | n/a | self.skip_build = None |
---|
69 | n/a | self.install_script = None |
---|
70 | n/a | self.pre_install_script = None |
---|
71 | n/a | self.user_access_control = None |
---|
72 | n/a | |
---|
73 | n/a | |
---|
74 | n/a | def finalize_options(self): |
---|
75 | n/a | self.set_undefined_options('bdist', ('skip_build', 'skip_build')) |
---|
76 | n/a | |
---|
77 | n/a | if self.bdist_dir is None: |
---|
78 | n/a | if self.skip_build and self.plat_name: |
---|
79 | n/a | # If build is skipped and plat_name is overridden, bdist will |
---|
80 | n/a | # not see the correct 'plat_name' - so set that up manually. |
---|
81 | n/a | bdist = self.distribution.get_command_obj('bdist') |
---|
82 | n/a | bdist.plat_name = self.plat_name |
---|
83 | n/a | # next the command will be initialized using that name |
---|
84 | n/a | bdist_base = self.get_finalized_command('bdist').bdist_base |
---|
85 | n/a | self.bdist_dir = os.path.join(bdist_base, 'wininst') |
---|
86 | n/a | |
---|
87 | n/a | if not self.target_version: |
---|
88 | n/a | self.target_version = "" |
---|
89 | n/a | |
---|
90 | n/a | if not self.skip_build and self.distribution.has_ext_modules(): |
---|
91 | n/a | short_version = get_python_version() |
---|
92 | n/a | if self.target_version and self.target_version != short_version: |
---|
93 | n/a | raise DistutilsOptionError( |
---|
94 | n/a | "target version can only be %s, or the '--skip-build'" \ |
---|
95 | n/a | " option must be specified" % (short_version,)) |
---|
96 | n/a | self.target_version = short_version |
---|
97 | n/a | |
---|
98 | n/a | self.set_undefined_options('bdist', |
---|
99 | n/a | ('dist_dir', 'dist_dir'), |
---|
100 | n/a | ('plat_name', 'plat_name'), |
---|
101 | n/a | ) |
---|
102 | n/a | |
---|
103 | n/a | if self.install_script: |
---|
104 | n/a | for script in self.distribution.scripts: |
---|
105 | n/a | if self.install_script == os.path.basename(script): |
---|
106 | n/a | break |
---|
107 | n/a | else: |
---|
108 | n/a | raise DistutilsOptionError( |
---|
109 | n/a | "install_script '%s' not found in scripts" |
---|
110 | n/a | % self.install_script) |
---|
111 | n/a | |
---|
112 | n/a | def run(self): |
---|
113 | n/a | if (sys.platform != "win32" and |
---|
114 | n/a | (self.distribution.has_ext_modules() or |
---|
115 | n/a | self.distribution.has_c_libraries())): |
---|
116 | n/a | raise DistutilsPlatformError \ |
---|
117 | n/a | ("distribution contains extensions and/or C libraries; " |
---|
118 | n/a | "must be compiled on a Windows 32 platform") |
---|
119 | n/a | |
---|
120 | n/a | if not self.skip_build: |
---|
121 | n/a | self.run_command('build') |
---|
122 | n/a | |
---|
123 | n/a | install = self.reinitialize_command('install', reinit_subcommands=1) |
---|
124 | n/a | install.root = self.bdist_dir |
---|
125 | n/a | install.skip_build = self.skip_build |
---|
126 | n/a | install.warn_dir = 0 |
---|
127 | n/a | install.plat_name = self.plat_name |
---|
128 | n/a | |
---|
129 | n/a | install_lib = self.reinitialize_command('install_lib') |
---|
130 | n/a | # we do not want to include pyc or pyo files |
---|
131 | n/a | install_lib.compile = 0 |
---|
132 | n/a | install_lib.optimize = 0 |
---|
133 | n/a | |
---|
134 | n/a | if self.distribution.has_ext_modules(): |
---|
135 | n/a | # If we are building an installer for a Python version other |
---|
136 | n/a | # than the one we are currently running, then we need to ensure |
---|
137 | n/a | # our build_lib reflects the other Python version rather than ours. |
---|
138 | n/a | # Note that for target_version!=sys.version, we must have skipped the |
---|
139 | n/a | # build step, so there is no issue with enforcing the build of this |
---|
140 | n/a | # version. |
---|
141 | n/a | target_version = self.target_version |
---|
142 | n/a | if not target_version: |
---|
143 | n/a | assert self.skip_build, "Should have already checked this" |
---|
144 | n/a | target_version = '%d.%d' % sys.version_info[:2] |
---|
145 | n/a | plat_specifier = ".%s-%s" % (self.plat_name, target_version) |
---|
146 | n/a | build = self.get_finalized_command('build') |
---|
147 | n/a | build.build_lib = os.path.join(build.build_base, |
---|
148 | n/a | 'lib' + plat_specifier) |
---|
149 | n/a | |
---|
150 | n/a | # Use a custom scheme for the zip-file, because we have to decide |
---|
151 | n/a | # at installation time which scheme to use. |
---|
152 | n/a | for key in ('purelib', 'platlib', 'headers', 'scripts', 'data'): |
---|
153 | n/a | value = key.upper() |
---|
154 | n/a | if key == 'headers': |
---|
155 | n/a | value = value + '/Include/$dist_name' |
---|
156 | n/a | setattr(install, |
---|
157 | n/a | 'install_' + key, |
---|
158 | n/a | value) |
---|
159 | n/a | |
---|
160 | n/a | log.info("installing to %s", self.bdist_dir) |
---|
161 | n/a | install.ensure_finalized() |
---|
162 | n/a | |
---|
163 | n/a | # avoid warning of 'install_lib' about installing |
---|
164 | n/a | # into a directory not in sys.path |
---|
165 | n/a | sys.path.insert(0, os.path.join(self.bdist_dir, 'PURELIB')) |
---|
166 | n/a | |
---|
167 | n/a | install.run() |
---|
168 | n/a | |
---|
169 | n/a | del sys.path[0] |
---|
170 | n/a | |
---|
171 | n/a | # And make an archive relative to the root of the |
---|
172 | n/a | # pseudo-installation tree. |
---|
173 | n/a | from tempfile import mktemp |
---|
174 | n/a | archive_basename = mktemp() |
---|
175 | n/a | fullname = self.distribution.get_fullname() |
---|
176 | n/a | arcname = self.make_archive(archive_basename, "zip", |
---|
177 | n/a | root_dir=self.bdist_dir) |
---|
178 | n/a | # create an exe containing the zip-file |
---|
179 | n/a | self.create_exe(arcname, fullname, self.bitmap) |
---|
180 | n/a | if self.distribution.has_ext_modules(): |
---|
181 | n/a | pyversion = get_python_version() |
---|
182 | n/a | else: |
---|
183 | n/a | pyversion = 'any' |
---|
184 | n/a | self.distribution.dist_files.append(('bdist_wininst', pyversion, |
---|
185 | n/a | self.get_installer_filename(fullname))) |
---|
186 | n/a | # remove the zip-file again |
---|
187 | n/a | log.debug("removing temporary file '%s'", arcname) |
---|
188 | n/a | os.remove(arcname) |
---|
189 | n/a | |
---|
190 | n/a | if not self.keep_temp: |
---|
191 | n/a | remove_tree(self.bdist_dir, dry_run=self.dry_run) |
---|
192 | n/a | |
---|
193 | n/a | def get_inidata(self): |
---|
194 | n/a | # Return data describing the installation. |
---|
195 | n/a | lines = [] |
---|
196 | n/a | metadata = self.distribution.metadata |
---|
197 | n/a | |
---|
198 | n/a | # Write the [metadata] section. |
---|
199 | n/a | lines.append("[metadata]") |
---|
200 | n/a | |
---|
201 | n/a | # 'info' will be displayed in the installer's dialog box, |
---|
202 | n/a | # describing the items to be installed. |
---|
203 | n/a | info = (metadata.long_description or '') + '\n' |
---|
204 | n/a | |
---|
205 | n/a | # Escape newline characters |
---|
206 | n/a | def escape(s): |
---|
207 | n/a | return s.replace("\n", "\\n") |
---|
208 | n/a | |
---|
209 | n/a | for name in ["author", "author_email", "description", "maintainer", |
---|
210 | n/a | "maintainer_email", "name", "url", "version"]: |
---|
211 | n/a | data = getattr(metadata, name, "") |
---|
212 | n/a | if data: |
---|
213 | n/a | info = info + ("\n %s: %s" % \ |
---|
214 | n/a | (name.capitalize(), escape(data))) |
---|
215 | n/a | lines.append("%s=%s" % (name, escape(data))) |
---|
216 | n/a | |
---|
217 | n/a | # The [setup] section contains entries controlling |
---|
218 | n/a | # the installer runtime. |
---|
219 | n/a | lines.append("\n[Setup]") |
---|
220 | n/a | if self.install_script: |
---|
221 | n/a | lines.append("install_script=%s" % self.install_script) |
---|
222 | n/a | lines.append("info=%s" % escape(info)) |
---|
223 | n/a | lines.append("target_compile=%d" % (not self.no_target_compile)) |
---|
224 | n/a | lines.append("target_optimize=%d" % (not self.no_target_optimize)) |
---|
225 | n/a | if self.target_version: |
---|
226 | n/a | lines.append("target_version=%s" % self.target_version) |
---|
227 | n/a | if self.user_access_control: |
---|
228 | n/a | lines.append("user_access_control=%s" % self.user_access_control) |
---|
229 | n/a | |
---|
230 | n/a | title = self.title or self.distribution.get_fullname() |
---|
231 | n/a | lines.append("title=%s" % escape(title)) |
---|
232 | n/a | import time |
---|
233 | n/a | import distutils |
---|
234 | n/a | build_info = "Built %s with distutils-%s" % \ |
---|
235 | n/a | (time.ctime(time.time()), distutils.__version__) |
---|
236 | n/a | lines.append("build_info=%s" % build_info) |
---|
237 | n/a | return "\n".join(lines) |
---|
238 | n/a | |
---|
239 | n/a | def create_exe(self, arcname, fullname, bitmap=None): |
---|
240 | n/a | import struct |
---|
241 | n/a | |
---|
242 | n/a | self.mkpath(self.dist_dir) |
---|
243 | n/a | |
---|
244 | n/a | cfgdata = self.get_inidata() |
---|
245 | n/a | |
---|
246 | n/a | installer_name = self.get_installer_filename(fullname) |
---|
247 | n/a | self.announce("creating %s" % installer_name) |
---|
248 | n/a | |
---|
249 | n/a | if bitmap: |
---|
250 | n/a | bitmapdata = open(bitmap, "rb").read() |
---|
251 | n/a | bitmaplen = len(bitmapdata) |
---|
252 | n/a | else: |
---|
253 | n/a | bitmaplen = 0 |
---|
254 | n/a | |
---|
255 | n/a | file = open(installer_name, "wb") |
---|
256 | n/a | file.write(self.get_exe_bytes()) |
---|
257 | n/a | if bitmap: |
---|
258 | n/a | file.write(bitmapdata) |
---|
259 | n/a | |
---|
260 | n/a | # Convert cfgdata from unicode to ascii, mbcs encoded |
---|
261 | n/a | if isinstance(cfgdata, str): |
---|
262 | n/a | cfgdata = cfgdata.encode("mbcs") |
---|
263 | n/a | |
---|
264 | n/a | # Append the pre-install script |
---|
265 | n/a | cfgdata = cfgdata + b"\0" |
---|
266 | n/a | if self.pre_install_script: |
---|
267 | n/a | # We need to normalize newlines, so we open in text mode and |
---|
268 | n/a | # convert back to bytes. "latin-1" simply avoids any possible |
---|
269 | n/a | # failures. |
---|
270 | n/a | with open(self.pre_install_script, "r", |
---|
271 | n/a | encoding="latin-1") as script: |
---|
272 | n/a | script_data = script.read().encode("latin-1") |
---|
273 | n/a | cfgdata = cfgdata + script_data + b"\n\0" |
---|
274 | n/a | else: |
---|
275 | n/a | # empty pre-install script |
---|
276 | n/a | cfgdata = cfgdata + b"\0" |
---|
277 | n/a | file.write(cfgdata) |
---|
278 | n/a | |
---|
279 | n/a | # The 'magic number' 0x1234567B is used to make sure that the |
---|
280 | n/a | # binary layout of 'cfgdata' is what the wininst.exe binary |
---|
281 | n/a | # expects. If the layout changes, increment that number, make |
---|
282 | n/a | # the corresponding changes to the wininst.exe sources, and |
---|
283 | n/a | # recompile them. |
---|
284 | n/a | header = struct.pack("<iii", |
---|
285 | n/a | 0x1234567B, # tag |
---|
286 | n/a | len(cfgdata), # length |
---|
287 | n/a | bitmaplen, # number of bytes in bitmap |
---|
288 | n/a | ) |
---|
289 | n/a | file.write(header) |
---|
290 | n/a | file.write(open(arcname, "rb").read()) |
---|
291 | n/a | |
---|
292 | n/a | def get_installer_filename(self, fullname): |
---|
293 | n/a | # Factored out to allow overriding in subclasses |
---|
294 | n/a | if self.target_version: |
---|
295 | n/a | # if we create an installer for a specific python version, |
---|
296 | n/a | # it's better to include this in the name |
---|
297 | n/a | installer_name = os.path.join(self.dist_dir, |
---|
298 | n/a | "%s.%s-py%s.exe" % |
---|
299 | n/a | (fullname, self.plat_name, self.target_version)) |
---|
300 | n/a | else: |
---|
301 | n/a | installer_name = os.path.join(self.dist_dir, |
---|
302 | n/a | "%s.%s.exe" % (fullname, self.plat_name)) |
---|
303 | n/a | return installer_name |
---|
304 | n/a | |
---|
305 | n/a | def get_exe_bytes(self): |
---|
306 | n/a | # If a target-version other than the current version has been |
---|
307 | n/a | # specified, then using the MSVC version from *this* build is no good. |
---|
308 | n/a | # Without actually finding and executing the target version and parsing |
---|
309 | n/a | # its sys.version, we just hard-code our knowledge of old versions. |
---|
310 | n/a | # NOTE: Possible alternative is to allow "--target-version" to |
---|
311 | n/a | # specify a Python executable rather than a simple version string. |
---|
312 | n/a | # We can then execute this program to obtain any info we need, such |
---|
313 | n/a | # as the real sys.version string for the build. |
---|
314 | n/a | cur_version = get_python_version() |
---|
315 | n/a | |
---|
316 | n/a | # If the target version is *later* than us, then we assume they |
---|
317 | n/a | # use what we use |
---|
318 | n/a | # string compares seem wrong, but are what sysconfig.py itself uses |
---|
319 | n/a | if self.target_version and self.target_version < cur_version: |
---|
320 | n/a | if self.target_version < "2.4": |
---|
321 | n/a | bv = 6.0 |
---|
322 | n/a | elif self.target_version == "2.4": |
---|
323 | n/a | bv = 7.1 |
---|
324 | n/a | elif self.target_version == "2.5": |
---|
325 | n/a | bv = 8.0 |
---|
326 | n/a | elif self.target_version <= "3.2": |
---|
327 | n/a | bv = 9.0 |
---|
328 | n/a | elif self.target_version <= "3.4": |
---|
329 | n/a | bv = 10.0 |
---|
330 | n/a | else: |
---|
331 | n/a | bv = 14.0 |
---|
332 | n/a | else: |
---|
333 | n/a | # for current version - use authoritative check. |
---|
334 | n/a | try: |
---|
335 | n/a | from msvcrt import CRT_ASSEMBLY_VERSION |
---|
336 | n/a | except ImportError: |
---|
337 | n/a | # cross-building, so assume the latest version |
---|
338 | n/a | bv = 14.0 |
---|
339 | n/a | else: |
---|
340 | n/a | bv = float('.'.join(CRT_ASSEMBLY_VERSION.split('.', 2)[:2])) |
---|
341 | n/a | |
---|
342 | n/a | |
---|
343 | n/a | # wininst-x.y.exe is in the same directory as this file |
---|
344 | n/a | directory = os.path.dirname(__file__) |
---|
345 | n/a | # we must use a wininst-x.y.exe built with the same C compiler |
---|
346 | n/a | # used for python. XXX What about mingw, borland, and so on? |
---|
347 | n/a | |
---|
348 | n/a | # if plat_name starts with "win" but is not "win32" |
---|
349 | n/a | # we want to strip "win" and leave the rest (e.g. -amd64) |
---|
350 | n/a | # for all other cases, we don't want any suffix |
---|
351 | n/a | if self.plat_name != 'win32' and self.plat_name[:3] == 'win': |
---|
352 | n/a | sfix = self.plat_name[3:] |
---|
353 | n/a | else: |
---|
354 | n/a | sfix = '' |
---|
355 | n/a | |
---|
356 | n/a | filename = os.path.join(directory, "wininst-%.1f%s.exe" % (bv, sfix)) |
---|
357 | n/a | f = open(filename, "rb") |
---|
358 | n/a | try: |
---|
359 | n/a | return f.read() |
---|
360 | n/a | finally: |
---|
361 | n/a | f.close() |
---|