| 1 |
"""distutils.command.bdist_wininst |
| 2 |
|
| 3 |
Implements the Distutils 'bdist_wininst' command: create a windows installer |
| 4 |
exe-program.""" |
| 5 |
|
| 6 |
__revision__ = "$Id$" |
| 7 |
|
| 8 |
import sys |
| 9 |
import os |
| 10 |
import string |
| 11 |
|
| 12 |
from sysconfig import get_python_version |
| 13 |
|
| 14 |
from distutils.core import Command |
| 15 |
from distutils.dir_util import remove_tree |
| 16 |
from distutils.errors import DistutilsOptionError, DistutilsPlatformError |
| 17 |
from distutils import log |
| 18 |
from distutils.util import get_platform |
| 19 |
|
| 20 |
class bdist_wininst (Command): |
| 21 |
|
| 22 |
description = "create an executable installer for MS Windows" |
| 23 |
|
| 24 |
user_options = [('bdist-dir=', None, |
| 25 |
"temporary directory for creating the distribution"), |
| 26 |
('plat-name=', 'p', |
| 27 |
"platform name to embed in generated filenames " |
| 28 |
"(default: %s)" % get_platform()), |
| 29 |
('keep-temp', 'k', |
| 30 |
"keep the pseudo-installation tree around after " + |
| 31 |
"creating the distribution archive"), |
| 32 |
('target-version=', None, |
| 33 |
"require a specific python version" + |
| 34 |
" on the target system"), |
| 35 |
('no-target-compile', 'c', |
| 36 |
"do not compile .py to .pyc on the target system"), |
| 37 |
('no-target-optimize', 'o', |
| 38 |
"do not compile .py to .pyo (optimized) " |
| 39 |
"on the target system"), |
| 40 |
('dist-dir=', 'd', |
| 41 |
"directory to put final built distributions in"), |
| 42 |
('bitmap=', 'b', |
| 43 |
"bitmap to use for the installer instead of python-powered logo"), |
| 44 |
('title=', 't', |
| 45 |
"title to display on the installer background instead of default"), |
| 46 |
('skip-build', None, |
| 47 |
"skip rebuilding everything (for testing/debugging)"), |
| 48 |
('install-script=', None, |
| 49 |
"basename of installation script to be run after " |
| 50 |
"installation or before deinstallation"), |
| 51 |
('pre-install-script=', None, |
| 52 |
"Fully qualified filename of a script to be run before " |
| 53 |
"any files are installed. This script need not be in the " |
| 54 |
"distribution"), |
| 55 |
('user-access-control=', None, |
| 56 |
"specify Vista's UAC handling - 'none'/default=no " |
| 57 |
"handling, 'auto'=use UAC if target Python installed for " |
| 58 |
"all users, 'force'=always use UAC"), |
| 59 |
] |
| 60 |
|
| 61 |
boolean_options = ['keep-temp', 'no-target-compile', 'no-target-optimize', |
| 62 |
'skip-build'] |
| 63 |
|
| 64 |
def initialize_options (self): |
| 65 |
self.bdist_dir = None |
| 66 |
self.plat_name = None |
| 67 |
self.keep_temp = 0 |
| 68 |
self.no_target_compile = 0 |
| 69 |
self.no_target_optimize = 0 |
| 70 |
self.target_version = None |
| 71 |
self.dist_dir = None |
| 72 |
self.bitmap = None |
| 73 |
self.title = None |
| 74 |
self.skip_build = None |
| 75 |
self.install_script = None |
| 76 |
self.pre_install_script = None |
| 77 |
self.user_access_control = None |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
def finalize_options (self): |
| 83 |
self.set_undefined_options('bdist', ('skip_build', 'skip_build')) |
| 84 |
|
| 85 |
if self.bdist_dir is None: |
| 86 |
if self.skip_build and self.plat_name: |
| 87 |
|
| 88 |
|
| 89 |
bdist = self.distribution.get_command_obj('bdist') |
| 90 |
bdist.plat_name = self.plat_name |
| 91 |
|
| 92 |
bdist_base = self.get_finalized_command('bdist').bdist_base |
| 93 |
self.bdist_dir = os.path.join(bdist_base, 'wininst') |
| 94 |
|
| 95 |
if not self.target_version: |
| 96 |
self.target_version = "" |
| 97 |
|
| 98 |
if not self.skip_build and self.distribution.has_ext_modules(): |
| 99 |
short_version = get_python_version() |
| 100 |
if self.target_version and self.target_version != short_version: |
| 101 |
raise DistutilsOptionError, \ |
| 102 |
"target version can only be %s, or the '--skip-build'" \ |
| 103 |
" option must be specified" % (short_version,) |
| 104 |
self.target_version = short_version |
| 105 |
|
| 106 |
self.set_undefined_options('bdist', |
| 107 |
('dist_dir', 'dist_dir'), |
| 108 |
('plat_name', 'plat_name'), |
| 109 |
) |
| 110 |
|
| 111 |
if self.install_script: |
| 112 |
for script in self.distribution.scripts: |
| 113 |
if self.install_script == os.path.basename(script): |
| 114 |
break |
| 115 |
else: |
| 116 |
raise DistutilsOptionError, \ |
| 117 |
"install_script '%s' not found in scripts" % \ |
| 118 |
self.install_script |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
def run (self): |
| 123 |
if (sys.platform != "win32" and |
| 124 |
(self.distribution.has_ext_modules() or |
| 125 |
self.distribution.has_c_libraries())): |
| 126 |
raise DistutilsPlatformError \ |
| 127 |
("distribution contains extensions and/or C libraries; " |
| 128 |
"must be compiled on a Windows 32 platform") |
| 129 |
|
| 130 |
if not self.skip_build: |
| 131 |
self.run_command('build') |
| 132 |
|
| 133 |
install = self.reinitialize_command('install', reinit_subcommands=1) |
| 134 |
install.root = self.bdist_dir |
| 135 |
install.skip_build = self.skip_build |
| 136 |
install.warn_dir = 0 |
| 137 |
install.plat_name = self.plat_name |
| 138 |
|
| 139 |
install_lib = self.reinitialize_command('install_lib') |
| 140 |
|
| 141 |
install_lib.compile = 0 |
| 142 |
install_lib.optimize = 0 |
| 143 |
|
| 144 |
if self.distribution.has_ext_modules(): |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
target_version = self.target_version |
| 152 |
if not target_version: |
| 153 |
assert self.skip_build, "Should have already checked this" |
| 154 |
target_version = sys.version[0:3] |
| 155 |
plat_specifier = ".%s-%s" % (self.plat_name, target_version) |
| 156 |
build = self.get_finalized_command('build') |
| 157 |
build.build_lib = os.path.join(build.build_base, |
| 158 |
'lib' + plat_specifier) |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
for key in ('purelib', 'platlib', 'headers', 'scripts', 'data'): |
| 163 |
value = string.upper(key) |
| 164 |
if key == 'headers': |
| 165 |
value = value + '/Include/$dist_name' |
| 166 |
setattr(install, |
| 167 |
'install_' + key, |
| 168 |
value) |
| 169 |
|
| 170 |
log.info("installing to %s", self.bdist_dir) |
| 171 |
install.ensure_finalized() |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
sys.path.insert(0, os.path.join(self.bdist_dir, 'PURELIB')) |
| 176 |
|
| 177 |
install.run() |
| 178 |
|
| 179 |
del sys.path[0] |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
from tempfile import mktemp |
| 184 |
archive_basename = mktemp() |
| 185 |
fullname = self.distribution.get_fullname() |
| 186 |
arcname = self.make_archive(archive_basename, "zip", |
| 187 |
root_dir=self.bdist_dir) |
| 188 |
|
| 189 |
self.create_exe(arcname, fullname, self.bitmap) |
| 190 |
if self.distribution.has_ext_modules(): |
| 191 |
pyversion = get_python_version() |
| 192 |
else: |
| 193 |
pyversion = 'any' |
| 194 |
self.distribution.dist_files.append(('bdist_wininst', pyversion, |
| 195 |
self.get_installer_filename(fullname))) |
| 196 |
|
| 197 |
log.debug("removing temporary file '%s'", arcname) |
| 198 |
os.remove(arcname) |
| 199 |
|
| 200 |
if not self.keep_temp: |
| 201 |
remove_tree(self.bdist_dir, dry_run=self.dry_run) |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
def get_inidata (self): |
| 206 |
|
| 207 |
|
| 208 |
lines = [] |
| 209 |
metadata = self.distribution.metadata |
| 210 |
|
| 211 |
|
| 212 |
lines.append("[metadata]") |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
info = (metadata.long_description or '') + '\n' |
| 217 |
|
| 218 |
|
| 219 |
def escape(s): |
| 220 |
return string.replace(s, "\n", "\\n") |
| 221 |
|
| 222 |
for name in ["author", "author_email", "description", "maintainer", |
| 223 |
"maintainer_email", "name", "url", "version"]: |
| 224 |
data = getattr(metadata, name, "") |
| 225 |
if data: |
| 226 |
info = info + ("\n %s: %s" % \ |
| 227 |
(string.capitalize(name), escape(data))) |
| 228 |
lines.append("%s=%s" % (name, escape(data))) |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
lines.append("\n[Setup]") |
| 233 |
if self.install_script: |
| 234 |
lines.append("install_script=%s" % self.install_script) |
| 235 |
lines.append("info=%s" % escape(info)) |
| 236 |
lines.append("target_compile=%d" % (not self.no_target_compile)) |
| 237 |
lines.append("target_optimize=%d" % (not self.no_target_optimize)) |
| 238 |
if self.target_version: |
| 239 |
lines.append("target_version=%s" % self.target_version) |
| 240 |
if self.user_access_control: |
| 241 |
lines.append("user_access_control=%s" % self.user_access_control) |
| 242 |
|
| 243 |
title = self.title or self.distribution.get_fullname() |
| 244 |
lines.append("title=%s" % escape(title)) |
| 245 |
import time |
| 246 |
import distutils |
| 247 |
build_info = "Built %s with distutils-%s" % \ |
| 248 |
(time.ctime(time.time()), distutils.__version__) |
| 249 |
lines.append("build_info=%s" % build_info) |
| 250 |
return string.join(lines, "\n") |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
def create_exe (self, arcname, fullname, bitmap=None): |
| 255 |
import struct |
| 256 |
|
| 257 |
self.mkpath(self.dist_dir) |
| 258 |
|
| 259 |
cfgdata = self.get_inidata() |
| 260 |
|
| 261 |
installer_name = self.get_installer_filename(fullname) |
| 262 |
self.announce("creating %s" % installer_name) |
| 263 |
|
| 264 |
if bitmap: |
| 265 |
bitmapdata = open(bitmap, "rb").read() |
| 266 |
bitmaplen = len(bitmapdata) |
| 267 |
else: |
| 268 |
bitmaplen = 0 |
| 269 |
|
| 270 |
file = open(installer_name, "wb") |
| 271 |
file.write(self.get_exe_bytes()) |
| 272 |
if bitmap: |
| 273 |
file.write(bitmapdata) |
| 274 |
|
| 275 |
|
| 276 |
try: |
| 277 |
unicode |
| 278 |
except NameError: |
| 279 |
pass |
| 280 |
else: |
| 281 |
if isinstance(cfgdata, unicode): |
| 282 |
cfgdata = cfgdata.encode("mbcs") |
| 283 |
|
| 284 |
|
| 285 |
cfgdata = cfgdata + "\0" |
| 286 |
if self.pre_install_script: |
| 287 |
script_data = open(self.pre_install_script, "r").read() |
| 288 |
cfgdata = cfgdata + script_data + "\n\0" |
| 289 |
else: |
| 290 |
|
| 291 |
cfgdata = cfgdata + "\0" |
| 292 |
file.write(cfgdata) |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
header = struct.pack("<iii", |
| 300 |
0x1234567B, |
| 301 |
len(cfgdata), |
| 302 |
bitmaplen, |
| 303 |
) |
| 304 |
file.write(header) |
| 305 |
file.write(open(arcname, "rb").read()) |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
def get_installer_filename(self, fullname): |
| 310 |
|
| 311 |
if self.target_version: |
| 312 |
|
| 313 |
|
| 314 |
installer_name = os.path.join(self.dist_dir, |
| 315 |
"%s.%s-py%s.exe" % |
| 316 |
(fullname, self.plat_name, self.target_version)) |
| 317 |
else: |
| 318 |
installer_name = os.path.join(self.dist_dir, |
| 319 |
"%s.%s.exe" % (fullname, self.plat_name)) |
| 320 |
return installer_name |
| 321 |
|
| 322 |
|
| 323 |
def get_exe_bytes (self): |
| 324 |
from distutils.msvccompiler import get_build_version |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
cur_version = get_python_version() |
| 334 |
if self.target_version and self.target_version != cur_version: |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
if self.target_version > cur_version: |
| 339 |
bv = get_build_version() |
| 340 |
else: |
| 341 |
if self.target_version < "2.4": |
| 342 |
bv = 6.0 |
| 343 |
else: |
| 344 |
bv = 7.1 |
| 345 |
else: |
| 346 |
|
| 347 |
bv = get_build_version() |
| 348 |
|
| 349 |
|
| 350 |
directory = os.path.dirname(__file__) |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
if self.plat_name != 'win32' and self.plat_name[:3] == 'win': |
| 358 |
sfix = self.plat_name[3:] |
| 359 |
else: |
| 360 |
sfix = '' |
| 361 |
|
| 362 |
filename = os.path.join(directory, "wininst-%.1f%s.exe" % (bv, sfix)) |
| 363 |
f = open(filename, "rb") |
| 364 |
try: |
| 365 |
return f.read() |
| 366 |
finally: |
| 367 |
f.close() |
| 368 |
|