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"),
|
|
|
|
('target-version=', 'v',
|
|
|
|
"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"),
|
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
|
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 = ""
|
|
|
|
if 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, \
|
|
|
|
"target version can only be" + 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
|
|
|
|
|
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
|
|
|
|
|
2000-06-27 21:56:20 -03:00
|
|
|
# Write the [metadata] section. Values are written with
|
|
|
|
# repr()[1:-1], so they do not contain unprintable characters, and
|
|
|
|
# are not surrounded by quote chars.
|
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
|
|
|
|
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" % \
|
|
|
|
(string.capitalize(name), data))
|
|
|
|
lines.append("%s=%s" % (name, repr(data)[1:-1]))
|
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)
|
2000-09-30 15:27:54 -03:00
|
|
|
lines.append("info=%s" % repr(info)[1:-1])
|
|
|
|
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()
|
2000-09-30 15:27:54 -03:00
|
|
|
lines.append("title=%s" % repr(title)[1:-1])
|
2000-09-09 18:15:12 -03:00
|
|
|
import time
|
|
|
|
import distutils
|
|
|
|
build_info = "Build %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
|
|
|
|
2000-09-07 12:59:22 -03:00
|
|
|
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)
|
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
|
|
|
|
2000-09-30 15:27:54 -03:00
|
|
|
file.write(cfgdata)
|
2001-02-19 05:20:30 -04:00
|
|
|
header = struct.pack("<iii",
|
|
|
|
0x1234567A, # tag
|
|
|
|
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()
|
|
|
|
|
|
|
|
def get_exe_bytes (self):
|
2002-11-22 17:08:34 -04:00
|
|
|
# wininst.exe is in the same directory as this file
|
|
|
|
directory = os.path.dirname(__file__)
|
|
|
|
filename = os.path.join(directory, "wininst.exe")
|
|
|
|
return open(filename, "rb").read()
|
2000-06-26 22:24:38 -03:00
|
|
|
# class bdist_wininst
|