2000-06-26 22:24:38 -03:00
|
|
|
"""distutils.command.bdist_wininst
|
|
|
|
|
|
|
|
Implements the Distutils 'bdist_wininst' command: create a windows installer
|
|
|
|
exe-program."""
|
|
|
|
|
|
|
|
__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"),
|
2008-04-06 22:53:39 -03:00
|
|
|
('plat-name=', 'p',
|
|
|
|
"platform name to embed in generated filenames "
|
|
|
|
"(default: %s)" % get_platform()),
|
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"),
|
2008-05-02 09:48:15 -03:00
|
|
|
('user-access-control=', None,
|
|
|
|
"specify Vista's UAC handling - 'none'/default=no "
|
|
|
|
"handling, 'auto'=use UAC if target Python installed for "
|
|
|
|
"all users, 'force'=always use UAC"),
|
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
|
2008-04-06 22:53:39 -03:00
|
|
|
self.plat_name = 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
|
2008-05-02 09:48:15 -03:00
|
|
|
self.user_access_control = None
|
2000-06-26 22:24:38 -03:00
|
|
|
|
|
|
|
# initialize_options()
|
|
|
|
|
|
|
|
|
|
|
|
def finalize_options (self):
|
|
|
|
if self.bdist_dir is None:
|
2008-05-27 22:54:55 -03:00
|
|
|
if self.skip_build and self.plat_name:
|
|
|
|
# If build is skipped and plat_name is overridden, bdist will
|
|
|
|
# not see the correct 'plat_name' - so set that up manually.
|
|
|
|
bdist = self.distribution.get_command_obj('bdist')
|
|
|
|
bdist.plat_name = self.plat_name
|
|
|
|
# next the command will be initialized using that name
|
2000-06-26 22:24:38 -03:00
|
|
|
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
|
|
|
|
|
2008-04-06 22:53:39 -03:00
|
|
|
self.set_undefined_options('bdist',
|
|
|
|
('dist_dir', 'dist_dir'),
|
|
|
|
('plat_name', 'plat_name'),
|
|
|
|
)
|
2000-07-05 00:08:55 -03:00
|
|
|
|
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
|
2008-04-06 22:53:39 -03:00
|
|
|
install.plat_name = self.plat_name
|
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]
|
2008-04-06 22:53:39 -03:00
|
|
|
plat_specifier = ".%s-%s" % (self.plat_name, target_version)
|
2004-10-27 18:54:33 -03:00
|
|
|
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)
|
2005-03-23 14:54:36 -04:00
|
|
|
if self.distribution.has_ext_modules():
|
|
|
|
pyversion = get_python_version()
|
|
|
|
else:
|
|
|
|
pyversion = 'any'
|
|
|
|
self.distribution.dist_files.append(('bdist_wininst', pyversion,
|
2005-03-22 18:23:29 -04:00
|
|
|
self.get_installer_filename(fullname)))
|
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)
|
2008-05-02 09:48:15 -03:00
|
|
|
if self.user_access_control:
|
|
|
|
lines.append("user_access_control=%s" % self.user_access_control)
|
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,
|
2008-04-06 22:53:39 -03:00
|
|
|
"%s.%s-py%s.exe" %
|
|
|
|
(fullname, self.plat_name, self.target_version))
|
2004-07-23 16:58:28 -03:00
|
|
|
else:
|
|
|
|
installer_name = os.path.join(self.dist_dir,
|
2008-04-06 22:53:39 -03:00
|
|
|
"%s.%s.exe" % (fullname, self.plat_name))
|
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":
|
2008-04-06 22:53:39 -03:00
|
|
|
bv = 6.0
|
2004-07-06 16:23:27 -03:00
|
|
|
else:
|
2008-04-06 22:53:39 -03:00
|
|
|
bv = 7.1
|
2004-07-06 16:23:27 -03:00
|
|
|
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?
|
2008-04-06 22:53:39 -03:00
|
|
|
if self.plat_name == 'win32':
|
|
|
|
sfix = ''
|
|
|
|
else:
|
|
|
|
sfix = self.plat_name[3:] # strip 'win' - leaves eg '-amd64'
|
|
|
|
filename = os.path.join(directory, "wininst-%.1f%s.exe" % (bv, sfix))
|
2002-11-22 17:08:34 -04:00
|
|
|
return open(filename, "rb").read()
|
2000-06-26 22:24:38 -03:00
|
|
|
# class bdist_wininst
|