2000-06-26 22:24:38 -03:00
|
|
|
"""distutils.command.bdist_wininst
|
|
|
|
|
|
|
|
Implements the Distutils 'bdist_wininst' command: create a windows installer
|
|
|
|
exe-program."""
|
|
|
|
|
2002-11-19 09:12:28 -04:00
|
|
|
# This module should be kept compatible with Python 1.5.2.
|
|
|
|
|
2000-06-26 22:24:38 -03:00
|
|
|
__revision__ = "$Id$"
|
|
|
|
|
2000-06-27 21:56:20 -03:00
|
|
|
import sys, os, string
|
2000-06-26 22:24:38 -03:00
|
|
|
from distutils.core import Command
|
2000-08-04 22:31:54 -03:00
|
|
|
from distutils.util import get_platform
|
|
|
|
from distutils.dir_util import create_tree, remove_tree
|
2000-06-26 22:24:38 -03:00
|
|
|
from distutils.errors import *
|
2002-11-22 16:57:20 -04:00
|
|
|
from distutils.sysconfig import get_python_version
|
2002-06-04 17:14:43 -03:00
|
|
|
from distutils import log
|
2000-06-26 22:24:38 -03:00
|
|
|
|
|
|
|
class bdist_wininst (Command):
|
|
|
|
|
2000-06-27 21:56:20 -03:00
|
|
|
description = "create an executable installer for MS Windows"
|
2000-06-26 22:24:38 -03:00
|
|
|
|
2001-03-16 16:57:37 -04:00
|
|
|
user_options = [('bdist-dir=', None,
|
2000-06-26 22:24:38 -03:00
|
|
|
"temporary directory for creating the distribution"),
|
2000-09-16 12:56:32 -03:00
|
|
|
('keep-temp', 'k',
|
2000-06-26 22:24:38 -03:00
|
|
|
"keep the pseudo-installation tree around after " +
|
|
|
|
"creating the distribution archive"),
|
2004-07-06 16:23:27 -03:00
|
|
|
('target-version=', None,
|
2000-06-26 22:24:38 -03:00
|
|
|
"require a specific python version" +
|
2000-08-25 23:40:10 -03:00
|
|
|
" on the target system"),
|
2000-09-07 12:59:22 -03:00
|
|
|
('no-target-compile', 'c',
|
|
|
|
"do not compile .py to .pyc on the target system"),
|
|
|
|
('no-target-optimize', 'o',
|
2000-09-29 08:36:55 -03:00
|
|
|
"do not compile .py to .pyo (optimized)"
|
|
|
|
"on the target system"),
|
2000-07-05 00:08:55 -03:00
|
|
|
('dist-dir=', 'd',
|
|
|
|
"directory to put final built distributions in"),
|
2001-02-19 05:20:30 -04:00
|
|
|
('bitmap=', 'b',
|
|
|
|
"bitmap to use for the installer instead of python-powered logo"),
|
|
|
|
('title=', 't',
|
|
|
|
"title to display on the installer background instead of default"),
|
2002-01-12 07:27:42 -04:00
|
|
|
('skip-build', None,
|
|
|
|
"skip rebuilding everything (for testing/debugging)"),
|
2002-02-20 04:01:19 -04:00
|
|
|
('install-script=', None,
|
2002-11-05 06:06:19 -04:00
|
|
|
"basename of installation script to be run after"
|
|
|
|
"installation or before deinstallation"),
|
2004-02-20 10:43:21 -04:00
|
|
|
('pre-install-script=', None,
|
|
|
|
"Fully qualified filename of a script to be run before "
|
|
|
|
"any files are installed. This script need not be in the "
|
|
|
|
"distribution"),
|
2000-06-26 22:24:38 -03:00
|
|
|
]
|
|
|
|
|
2002-03-21 19:44:01 -04:00
|
|
|
boolean_options = ['keep-temp', 'no-target-compile', 'no-target-optimize',
|
|
|
|
'skip-build']
|
2000-09-24 22:41:15 -03:00
|
|
|
|
2000-06-26 22:24:38 -03:00
|
|
|
def initialize_options (self):
|
|
|
|
self.bdist_dir = None
|
2000-09-16 12:56:32 -03:00
|
|
|
self.keep_temp = 0
|
2000-09-07 12:59:22 -03:00
|
|
|
self.no_target_compile = 0
|
|
|
|
self.no_target_optimize = 0
|
2000-06-26 22:24:38 -03:00
|
|
|
self.target_version = None
|
2000-07-05 00:08:55 -03:00
|
|
|
self.dist_dir = None
|
2001-02-19 05:20:30 -04:00
|
|
|
self.bitmap = None
|
|
|
|
self.title = None
|
2002-01-12 07:27:42 -04:00
|
|
|
self.skip_build = 0
|
2002-02-20 04:01:19 -04:00
|
|
|
self.install_script = None
|
2004-02-20 10:43:21 -04:00
|
|
|
self.pre_install_script = None
|
2000-06-26 22:24:38 -03:00
|
|
|
|
|
|
|
# initialize_options()
|
|
|
|
|
|
|
|
|
|
|
|
def finalize_options (self):
|
|
|
|
if self.bdist_dir is None:
|
|
|
|
bdist_base = self.get_finalized_command('bdist').bdist_base
|
|
|
|
self.bdist_dir = os.path.join(bdist_base, 'wininst')
|
|
|
|
if not self.target_version:
|
|
|
|
self.target_version = ""
|
2004-02-20 10:43:21 -04:00
|
|
|
if not self.skip_build and self.distribution.has_ext_modules():
|
2002-11-13 21:44:35 -04:00
|
|
|
short_version = get_python_version()
|
2000-06-26 22:24:38 -03:00
|
|
|
if self.target_version and self.target_version != short_version:
|
2000-09-30 15:27:54 -03:00
|
|
|
raise DistutilsOptionError, \
|
2004-02-20 10:43:21 -04:00
|
|
|
"target version can only be %s, or the '--skip_build'" \
|
|
|
|
" option must be specified" % (short_version,)
|
2000-06-26 22:24:38 -03:00
|
|
|
self.target_version = short_version
|
|
|
|
|
2000-07-05 00:08:55 -03:00
|
|
|
self.set_undefined_options('bdist', ('dist_dir', 'dist_dir'))
|
|
|
|
|
2002-11-05 06:06:19 -04:00
|
|
|
if self.install_script:
|
|
|
|
for script in self.distribution.scripts:
|
|
|
|
if self.install_script == os.path.basename(script):
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
raise DistutilsOptionError, \
|
|
|
|
"install_script '%s' not found in scripts" % \
|
|
|
|
self.install_script
|
2000-06-26 22:24:38 -03:00
|
|
|
# finalize_options()
|
|
|
|
|
|
|
|
|
|
|
|
def run (self):
|
2000-08-31 22:44:45 -03:00
|
|
|
if (sys.platform != "win32" and
|
|
|
|
(self.distribution.has_ext_modules() or
|
|
|
|
self.distribution.has_c_libraries())):
|
2000-09-07 12:59:22 -03:00
|
|
|
raise DistutilsPlatformError \
|
2000-08-31 22:44:45 -03:00
|
|
|
("distribution contains extensions and/or C libraries; "
|
|
|
|
"must be compiled on a Windows 32 platform")
|
2000-06-26 22:24:38 -03:00
|
|
|
|
2002-01-12 07:27:42 -04:00
|
|
|
if not self.skip_build:
|
|
|
|
self.run_command('build')
|
2000-06-26 22:24:38 -03:00
|
|
|
|
2003-06-12 14:23:58 -03:00
|
|
|
install = self.reinitialize_command('install', reinit_subcommands=1)
|
2000-06-26 22:24:38 -03:00
|
|
|
install.root = self.bdist_dir
|
2002-01-12 07:27:42 -04:00
|
|
|
install.skip_build = self.skip_build
|
2002-04-09 11:14:38 -03:00
|
|
|
install.warn_dir = 0
|
2000-06-26 22:24:38 -03:00
|
|
|
|
2000-06-27 21:56:20 -03:00
|
|
|
install_lib = self.reinitialize_command('install_lib')
|
2000-08-25 23:40:10 -03:00
|
|
|
# we do not want to include pyc or pyo files
|
2000-06-26 22:24:38 -03:00
|
|
|
install_lib.compile = 0
|
|
|
|
install_lib.optimize = 0
|
2004-07-18 03:16:08 -03:00
|
|
|
|
2004-10-27 18:54:33 -03:00
|
|
|
if self.distribution.has_ext_modules():
|
|
|
|
# If we are building an installer for a Python version other
|
|
|
|
# than the one we are currently running, then we need to ensure
|
|
|
|
# our build_lib reflects the other Python version rather than ours.
|
|
|
|
# Note that for target_version!=sys.version, we must have skipped the
|
|
|
|
# build step, so there is no issue with enforcing the build of this
|
|
|
|
# version.
|
|
|
|
target_version = self.target_version
|
|
|
|
if not target_version:
|
|
|
|
assert self.skip_build, "Should have already checked this"
|
|
|
|
target_version = sys.version[0:3]
|
|
|
|
plat_specifier = ".%s-%s" % (get_platform(), target_version)
|
|
|
|
build = self.get_finalized_command('build')
|
|
|
|
build.build_lib = os.path.join(build.build_base,
|
|
|
|
'lib' + plat_specifier)
|
2000-06-26 22:24:38 -03:00
|
|
|
|
2001-09-05 10:00:40 -03:00
|
|
|
# Use a custom scheme for the zip-file, because we have to decide
|
|
|
|
# at installation time which scheme to use.
|
|
|
|
for key in ('purelib', 'platlib', 'headers', 'scripts', 'data'):
|
|
|
|
value = string.upper(key)
|
|
|
|
if key == 'headers':
|
|
|
|
value = value + '/Include/$dist_name'
|
|
|
|
setattr(install,
|
|
|
|
'install_' + key,
|
|
|
|
value)
|
2000-06-26 22:24:38 -03:00
|
|
|
|
2002-06-04 17:14:43 -03:00
|
|
|
log.info("installing to %s", self.bdist_dir)
|
2000-06-26 22:24:38 -03:00
|
|
|
install.ensure_finalized()
|
2001-09-05 10:00:40 -03:00
|
|
|
|
|
|
|
# avoid warning of 'install_lib' about installing
|
|
|
|
# into a directory not in sys.path
|
|
|
|
sys.path.insert(0, os.path.join(self.bdist_dir, 'PURELIB'))
|
|
|
|
|
2000-06-26 22:24:38 -03:00
|
|
|
install.run()
|
|
|
|
|
2001-09-05 10:00:40 -03:00
|
|
|
del sys.path[0]
|
|
|
|
|
2000-06-26 22:24:38 -03:00
|
|
|
# And make an archive relative to the root of the
|
|
|
|
# pseudo-installation tree.
|
2002-10-15 11:51:58 -03:00
|
|
|
from tempfile import mktemp
|
|
|
|
archive_basename = mktemp()
|
2000-07-05 00:08:55 -03:00
|
|
|
fullname = self.distribution.get_fullname()
|
2000-09-30 15:27:54 -03:00
|
|
|
arcname = self.make_archive(archive_basename, "zip",
|
2001-09-05 10:00:40 -03:00
|
|
|
root_dir=self.bdist_dir)
|
2001-12-18 16:13:40 -04:00
|
|
|
# create an exe containing the zip-file
|
2001-02-19 05:20:30 -04:00
|
|
|
self.create_exe(arcname, fullname, self.bitmap)
|
2001-12-18 16:13:40 -04:00
|
|
|
# remove the zip-file again
|
2002-06-04 17:14:43 -03:00
|
|
|
log.debug("removing temporary file '%s'", arcname)
|
2002-10-15 11:51:58 -03:00
|
|
|
os.remove(arcname)
|
2000-06-26 22:24:38 -03:00
|
|
|
|
2000-09-16 12:56:32 -03:00
|
|
|
if not self.keep_temp:
|
2002-06-04 17:14:43 -03:00
|
|
|
remove_tree(self.bdist_dir, dry_run=self.dry_run)
|
2000-06-26 22:24:38 -03:00
|
|
|
|
|
|
|
# run()
|
|
|
|
|
2000-08-31 22:44:45 -03:00
|
|
|
def get_inidata (self):
|
|
|
|
# Return data describing the installation.
|
2000-06-26 22:24:38 -03:00
|
|
|
|
2000-08-31 22:44:45 -03:00
|
|
|
lines = []
|
2000-06-26 22:24:38 -03:00
|
|
|
metadata = self.distribution.metadata
|
|
|
|
|
2004-07-23 16:44:29 -03:00
|
|
|
# Write the [metadata] section.
|
2000-09-30 15:27:54 -03:00
|
|
|
lines.append("[metadata]")
|
2000-06-26 22:24:38 -03:00
|
|
|
|
2000-06-27 21:56:20 -03:00
|
|
|
# 'info' will be displayed in the installer's dialog box,
|
|
|
|
# describing the items to be installed.
|
2000-08-27 17:44:13 -03:00
|
|
|
info = (metadata.long_description or '') + '\n'
|
2000-06-26 22:24:38 -03:00
|
|
|
|
2004-07-23 16:44:29 -03:00
|
|
|
# Escape newline characters
|
|
|
|
def escape(s):
|
|
|
|
return string.replace(s, "\n", "\\n")
|
|
|
|
|
2001-10-05 17:40:48 -03:00
|
|
|
for name in ["author", "author_email", "description", "maintainer",
|
|
|
|
"maintainer_email", "name", "url", "version"]:
|
|
|
|
data = getattr(metadata, name, "")
|
|
|
|
if data:
|
|
|
|
info = info + ("\n %s: %s" % \
|
2004-07-23 16:44:29 -03:00
|
|
|
(string.capitalize(name), escape(data)))
|
|
|
|
lines.append("%s=%s" % (name, escape(data)))
|
2000-06-26 22:24:38 -03:00
|
|
|
|
|
|
|
# The [setup] section contains entries controlling
|
|
|
|
# the installer runtime.
|
2000-09-30 15:27:54 -03:00
|
|
|
lines.append("\n[Setup]")
|
2002-02-20 04:01:19 -04:00
|
|
|
if self.install_script:
|
|
|
|
lines.append("install_script=%s" % self.install_script)
|
2004-07-23 16:44:29 -03:00
|
|
|
lines.append("info=%s" % escape(info))
|
2000-09-30 15:27:54 -03:00
|
|
|
lines.append("target_compile=%d" % (not self.no_target_compile))
|
|
|
|
lines.append("target_optimize=%d" % (not self.no_target_optimize))
|
2000-06-26 22:24:38 -03:00
|
|
|
if self.target_version:
|
2000-09-30 15:27:54 -03:00
|
|
|
lines.append("target_version=%s" % self.target_version)
|
2000-06-26 22:24:38 -03:00
|
|
|
|
2001-02-19 05:20:30 -04:00
|
|
|
title = self.title or self.distribution.get_fullname()
|
2004-07-23 16:44:29 -03:00
|
|
|
lines.append("title=%s" % escape(title))
|
2000-09-09 18:15:12 -03:00
|
|
|
import time
|
|
|
|
import distutils
|
2004-02-20 10:43:21 -04:00
|
|
|
build_info = "Built %s with distutils-%s" % \
|
2000-09-30 15:27:54 -03:00
|
|
|
(time.ctime(time.time()), distutils.__version__)
|
|
|
|
lines.append("build_info=%s" % build_info)
|
|
|
|
return string.join(lines, "\n")
|
2000-06-26 22:24:38 -03:00
|
|
|
|
2000-08-31 22:44:45 -03:00
|
|
|
# get_inidata()
|
2000-06-26 22:24:38 -03:00
|
|
|
|
2001-02-19 05:20:30 -04:00
|
|
|
def create_exe (self, arcname, fullname, bitmap=None):
|
2000-08-31 22:44:45 -03:00
|
|
|
import struct
|
|
|
|
|
|
|
|
self.mkpath(self.dist_dir)
|
2000-06-26 22:24:38 -03:00
|
|
|
|
2000-08-31 22:44:45 -03:00
|
|
|
cfgdata = self.get_inidata()
|
2000-06-26 22:24:38 -03:00
|
|
|
|
2004-07-23 16:58:28 -03:00
|
|
|
installer_name = self.get_installer_filename(fullname)
|
2000-09-30 15:27:54 -03:00
|
|
|
self.announce("creating %s" % installer_name)
|
2000-06-26 22:24:38 -03:00
|
|
|
|
2001-02-19 05:20:30 -04:00
|
|
|
if bitmap:
|
|
|
|
bitmapdata = open(bitmap, "rb").read()
|
|
|
|
bitmaplen = len(bitmapdata)
|
|
|
|
else:
|
|
|
|
bitmaplen = 0
|
|
|
|
|
2000-09-30 15:27:54 -03:00
|
|
|
file = open(installer_name, "wb")
|
|
|
|
file.write(self.get_exe_bytes())
|
2001-02-19 05:20:30 -04:00
|
|
|
if bitmap:
|
|
|
|
file.write(bitmapdata)
|
2001-12-06 17:01:19 -04:00
|
|
|
|
2004-07-23 16:44:29 -03:00
|
|
|
# Convert cfgdata from unicode to ascii, mbcs encoded
|
|
|
|
try:
|
|
|
|
unicode
|
|
|
|
except NameError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
if isinstance(cfgdata, unicode):
|
|
|
|
cfgdata = cfgdata.encode("mbcs")
|
|
|
|
|
2004-02-20 10:43:21 -04:00
|
|
|
# Append the pre-install script
|
|
|
|
cfgdata = cfgdata + "\0"
|
|
|
|
if self.pre_install_script:
|
|
|
|
script_data = open(self.pre_install_script, "r").read()
|
|
|
|
cfgdata = cfgdata + script_data + "\n\0"
|
2004-02-20 15:38:50 -04:00
|
|
|
else:
|
|
|
|
# empty pre-install script
|
|
|
|
cfgdata = cfgdata + "\0"
|
2000-09-30 15:27:54 -03:00
|
|
|
file.write(cfgdata)
|
2004-07-19 06:45:46 -03:00
|
|
|
|
|
|
|
# The 'magic number' 0x1234567B is used to make sure that the
|
|
|
|
# binary layout of 'cfgdata' is what the wininst.exe binary
|
|
|
|
# expects. If the layout changes, increment that number, make
|
|
|
|
# the corresponding changes to the wininst.exe sources, and
|
|
|
|
# recompile them.
|
2001-02-19 05:20:30 -04:00
|
|
|
header = struct.pack("<iii",
|
2004-07-19 06:45:46 -03:00
|
|
|
0x1234567B, # tag
|
2001-02-19 05:20:30 -04:00
|
|
|
len(cfgdata), # length
|
|
|
|
bitmaplen, # number of bytes in bitmap
|
|
|
|
)
|
2000-09-30 15:27:54 -03:00
|
|
|
file.write(header)
|
|
|
|
file.write(open(arcname, "rb").read())
|
2000-06-26 22:24:38 -03:00
|
|
|
|
|
|
|
# create_exe()
|
|
|
|
|
2004-07-23 16:58:28 -03:00
|
|
|
def get_installer_filename(self, fullname):
|
|
|
|
# Factored out to allow overriding in subclasses
|
|
|
|
if self.target_version:
|
|
|
|
# if we create an installer for a specific python version,
|
|
|
|
# it's better to include this in the name
|
|
|
|
installer_name = os.path.join(self.dist_dir,
|
|
|
|
"%s.win32-py%s.exe" %
|
|
|
|
(fullname, self.target_version))
|
|
|
|
else:
|
|
|
|
installer_name = os.path.join(self.dist_dir,
|
|
|
|
"%s.win32.exe" % fullname)
|
2004-08-17 07:15:07 -03:00
|
|
|
return installer_name
|
2004-07-23 16:58:28 -03:00
|
|
|
# get_installer_filename()
|
|
|
|
|
2000-06-26 22:24:38 -03:00
|
|
|
def get_exe_bytes (self):
|
2004-02-20 15:38:50 -04:00
|
|
|
from distutils.msvccompiler import get_build_version
|
2004-07-06 16:23:27 -03:00
|
|
|
# If a target-version other than the current version has been
|
|
|
|
# specified, then using the MSVC version from *this* build is no good.
|
|
|
|
# Without actually finding and executing the target version and parsing
|
|
|
|
# its sys.version, we just hard-code our knowledge of old versions.
|
|
|
|
# NOTE: Possible alternative is to allow "--target-version" to
|
|
|
|
# specify a Python executable rather than a simple version string.
|
|
|
|
# We can then execute this program to obtain any info we need, such
|
|
|
|
# as the real sys.version string for the build.
|
|
|
|
cur_version = get_python_version()
|
|
|
|
if self.target_version and self.target_version != cur_version:
|
|
|
|
# If the target version is *later* than us, then we assume they
|
|
|
|
# use what we use
|
|
|
|
# string compares seem wrong, but are what sysconfig.py itself uses
|
|
|
|
if self.target_version > cur_version:
|
|
|
|
bv = get_build_version()
|
|
|
|
else:
|
|
|
|
if self.target_version < "2.4":
|
|
|
|
bv = "6"
|
|
|
|
else:
|
|
|
|
bv = "7.1"
|
|
|
|
else:
|
|
|
|
# for current version - use authoritative check.
|
|
|
|
bv = get_build_version()
|
|
|
|
|
2004-02-20 15:38:50 -04:00
|
|
|
# wininst-x.y.exe is in the same directory as this file
|
2002-11-22 17:08:34 -04:00
|
|
|
directory = os.path.dirname(__file__)
|
2004-02-20 15:38:50 -04:00
|
|
|
# we must use a wininst-x.y.exe built with the same C compiler
|
|
|
|
# used for python. XXX What about mingw, borland, and so on?
|
2004-07-06 16:23:27 -03:00
|
|
|
filename = os.path.join(directory, "wininst-%s.exe" % bv)
|
2002-11-22 17:08:34 -04:00
|
|
|
return open(filename, "rb").read()
|
2000-06-26 22:24:38 -03:00
|
|
|
# class bdist_wininst
|