[Python-Dev] Packaging and binary distributions

Vinay Sajip vinay_sajip at yahoo.co.uk
Mon Nov 7 10:26:09 CET 2011

Martin v. Löwis <martin <at> v.loewis.de> writes:

> Again, that's a bdist_msi implementation issue. It could generate custom
> actions that run the "proper" setup.cfg hooks (I presume - I have no
> idea what a setup.cfg hook actually is).

I know that custom hooks are quite powerful, but my comment was about having
the functionality in Python. Here's an example of a working hooks.py:

import os
import sys

if os.name == 'nt':
    def get_personal_path():
        from ctypes import (wintypes, windll, create_unicode_buffer, WinError,
                            c_int, HRESULT)
        from ctypes.wintypes import HWND, HANDLE, DWORD, LPWSTR, MAX_PATH

        CSIDL_PERSONAL = 5

        # We use an older API to remain XP-compatible.

        SHGetFolderPath = windll.shell32.SHGetFolderPathW
        SHGetFolderPath.argtypes = [HWND, c_int, HANDLE, DWORD, LPWSTR]
        SHGetFolderPath.restype = DWORD

        path = create_unicode_buffer(MAX_PATH)
        hr = SHGetFolderPath(0, CSIDL_PERSONAL, 0, 0, path)
        if hr != 0:
            raise WinError()
        return path.value

    path = get_personal_path()
    del get_personal_path
    # Assume ~\Documents\WindowsPowerShell\Modules is in $PSModulePath,
    # which should be true in a default installation of PowerShell 2.0.
    psroot = os.path.join(path, 'WindowsPowerShell')
    psmodules = os.path.join(psroot, 'Modules')
    psscripts = os.path.join(psroot, 'Scripts')

def setup(config):
    files = config['files']
    if os.name != 'nt':
        files_to_add = 'virtualenvwrapper.sh = {scripts}'
        files_to_add = ('winfiles/ *.ps* = '
                              'winfiles/ vew_profile.ps1 = {psscripts}')
    if 'resources' not in files:
        files['resources'] = files_to_add
        files['resources'] += '\n%s' % files_to_add

def pre_install_data(cmd):
    if os.name == 'nt':
        cmd.categories['psmodules'] = psmodules
        cmd.categories['psscripts'] = psscripts
        cmd.categories['psroot'] = psroot

which works with the following setup.cfg:

setup_hooks = hooks.setup

pre-hook.win32 = hooks.pre_install_data
categories =
    cat1 = /path/one
# comment    
    cat2 = /path/two

#post-hook.win32 = hooks.post_install_dist

name = nemo
version = 0.1
summary = New Environments Made, Obviously
description = A tool to manage virtual environments
download_url = UNKNOWN
home_page = https://bitbucket.org/vinay.sajip/nemo
author = Vinay Sajip
author_email = vinay_sajip at yahoo.co.uk
license = BSD
classifier = Development Status :: 3 - Alpha
    Programming Language :: Python :: 3
    Operating System :: OS Independent
    Intended Audience :: System Administrators
    Intended Audience :: Developers
    License :: OSI Approved :: BSD License

requires_python = >= 3.3

packages = nemo

scripts =
    nemo = nemo.main

extra_files =

# Additional esources are added in hooks based on platform
resources =
    nemo/scripts/** = {purelib}

I'm curious to know how this level of flexibility can be achieved with the
MSI format: I know one can code the equivalent logic in C (for example) in
a custom action, but don't know how you can keep the logic in Python.


Vinay Sajip

More information about the Python-Dev mailing list