Sept. 12, 2010

Hello!

Here is the build script which I use for deploying my apps such as Infinite8BitPlatformer on Windows and OSX. I have used it to build both Pygame and wxWindows apps in the past. "Cross platform" might be a bit of a misnomer; you need to run it on the environment you are targetting (e.g. be on Windows to build for Windows) and have the correct environment, libraries etc. installed, but the same script should work on both platforms.

For notes about installing Python libraries into somewhere other than the default Python root directory, see this earlier post.

Please pay attention to the comments marked NOTE.

### Cross platform (win32, mac) build script
### By Chris McCormick <chris@mccormick.cx>
### This has worked with pygame and wx
### This script is public domain

from setuptools import setup
from sys import platform, argv
import os
from shutil import rmtree, copytree
import zipfile

# NOTE: you may want to change these next few lines to set these values manuall
# get the current bzr version number of this build
from bzrlib.branch import Branch
revno = Branch.open(".").revno()
# get the name of the project/app from the name of the current directory
app = os.path.basename(os.getcwd())

# remove the build and dist directories
def clean():
    print "Removing build and dist directories"
    for d in ["build", "dist"]:
        if os.path.isdir(d):
            rmtree(d)

clean()

# the default os string and extension for each platform
platforms = {
    "darwin": ("osx", ".app", "dist/" + app + ".app/Contents/Resources"),
    "win32": ("windows", "", "dist"),
}

# more convenient representation of the platform config
config = {
    "os": platforms[platform][0],
    "extension": platforms[platform][1],
    "resources": platforms[platform][2],
    }

# NOTE: this is optional, you might want to remove this bit
# output the correct VERSION file for this build
print "Writing VERSION file"
version_file = file(os.path.join("resources", "VERSION"), "w")
version_file.write("%s\n%s\n%s\n" % (revno, config["os"], config["extension"] + ".zip"))
version_file.close()

### PLATFORM SPECIFIC SECTION ###

# mac osx
if platform == "darwin":
    # add mac specific options
    options = {
        # NOTE: you may want to put other libraries in here, such as wx for wxWindows apps
        # some libraries need forcing
        'includes': ['simplejson', 'pygame'],
        'resources': ['resources',],
        'argv_emulation': True,
        'iconfile': 'resources/icon.icns',
        'semi_standalone': False,
    }

    # force the py2app build
    argv.insert(1, "py2app")

    # setup for mac .app (does the actual build)
    setup(
        setup_requires=['py2app'],
        app=[app + ".py"],
        options={'py2app': options},
    )
elif platform == "win32":
    import py2exe

    # hack to include simplejson egg in the build
    import pkg_resources
    # NOTE: this bit is manually copying the simplejson egg into the current directory
    # you may want to manually import other libraries here
    # some libraries need forcing like this
    eggs = pkg_resources.require("simplejson")
    from setuptools.archive_util import unpack_archive
    for egg in eggs:
        if os.path.isdir(egg.location):
            copytree(egg.location, ".")
        else:
            unpack_archive(egg.location, ".")
            rmtree("EGG-INFO")

    # windows specific options
    options = {
        "script": app + ".py",
        # NOTE: assumes your icon is in a folder called resources and is called 'main.ico'
        "icon_resources": [(1, os.path.join("resources", "main.ico"))],
    }   
    resources = ['resources',]

    # force the py2exe build
    argv.insert(1, "py2exe")

    # setup for windows .exe (does the actual build)
    setup(
        setup_requires = ['py2exe'],
        windows=[options],
    )

    # manually copy resources as I couldn't get it to happen with py2exe
    for r in resources:
        print 'Copying resource "%s"' % r
        copytree(r, os.path.join("dist", r))

    # get rid of simplejson directory
    # NOTE: if you manually forced other libraries above you'll probably want to remove them here
    rmtree("simplejson")

### PLATFORM SECTION DONE ###

# zip up our app to the correctly named zipfile
def recursive_zip(zipf, directory, root=None):
    if not root:
        root = os.path.basename(directory)
    list = os.listdir(directory)
    for file in list:
        realpath = os.path.join(directory, file)
        arcpath = os.path.join(root, file)
        print "Zipping:", arcpath
        if os.path.isfile(realpath):
            zipf.write(realpath, arcpath)
        elif os.path.isdir(realpath):
            recursive_zip(zipf, realpath, arcpath)

outfilename = "%s-%d-%s%s.zip" % (app, revno, config["os"], config["extension"])
zipout = zipfile.ZipFile(outfilename, "w")
recursive_zip(zipout, os.path.join("dist", platform == "darwin" and app + config["extension"] or ""))
zipout.close()

# clean up afterwards
clean()

# output the build-finished message
print "--- done ---"
print "Created %s" % outfilename

You should probably use Esky instead of this script.

Need software development advice? Book a call with me.