pygame / setup.py

#!/usr/bin/env python

import os, sys, glob, time, re

import distutils.sysconfig
from distutils.core import setup
from distutils.command.install_data import install_data

import modules, cfg
from config import helpers, msys, config_modules

VERSION = "2.0.0-alpha5"
DEBUG = True

# Minimum requirements.
PYTHON_MINIMUM = (2, 4)

# Data installer with improved intelligence over distutils.
# Data files are copied into the project directory instead of willy-nilly
class SmartInstallData(install_data):
    def run(self):
        # Need to change self.install_dir to the actual library dir.
        install_cmd = self.get_finalized_command ('install')
        self.install_dir = getattr (install_cmd, 'install_lib')
        return install_data.run (self)

def find_pkg_data (directory, excludedirs=[], excludefiles=[]):
    pkgdata = []
    nodirs = re.compile (r'(.svn)$')
    nofilesrev = re.compile (r'((~.*)|(cyp\..*)|(yp\..*))')
    for subd, directories, files in os.walk (directory):
        directories[:] = [d for d in directories if nodirs.match (d) is None \
                          and d not in excludedirs]
        files[:] = [f for f in files if nofilesrev.match(f[-1::-1]) is None \
                    and f not in excludefiles]
        subd = subd.replace (directory, "", 1)
        subd = subd.lstrip (os.path.sep)
        for f in files:
            pkgdata.append (os.path.join (subd, f))
    return pkgdata

def get_compiler (args):
    coffset = 0
    compiler = ''
    if '-c' in args:
        coffset = args.index ('-c')
        if coffset < len (args) - 1:
            compiler = args[coffset+1]
    else:
        for arg in args:
            if '--compiler=' in arg:
                compiler = arg.split ('=')[1]
    return compiler

def run_checks ():
    # Python version check.
    if helpers.getversion () < PYTHON_MINIMUM: # major, minor check
        raise Exception ("You should have at least Python >= %d.%d.x "
                         "installed." % PYTHON_MINIMUM)

    buildsystem = None
    builddefines = []
    defcompiler = ''
    if sys.platform == "win32":
        if msys.is_msys ():
            buildsystem = "msys"
            builddefines.append (("IS_MSYS", None))
            defcompiler = "mingw32"
        else:
            buildsystem = "win"
            builddefines.append (("IS_WIN32", None))
            builddefines.append (("WIN32", None))
            defcompiler = "msvc"
    elif sys.platform == "darwin":
        buildsystem = "darwin"
        builddefines.append (("IS_DARWIN", None))
        defcompiler = "unix"
    else:
        buildsystem = "unix"
        defcompiler = "unix"
        builddefines.append (("IS_UNIX", None))

    if cfg.build['SDL']:
        sdlversion = config_modules.sdl_get_version (buildsystem)
    compiler = get_compiler (sys.argv) or defcompiler

    print ("\nThe following information will be used to build Pygame:")
    print ("\t System:   %s" % buildsystem)
    print ("\t Python:   %d.%d.%d" % helpers.getversion ())
    print ("\t Compiler: %s" % compiler)
    if cfg.build['SDL']:
        print ("\t SDL:      %s" % sdlversion)
    return buildsystem, builddefines, compiler

if __name__ == "__main__":

    buildsystem = None
    buildcflags = None
    try:
        buildsystem, builddefines, compiler = run_checks ()
    except:
        print (helpers.geterror ())
        print (helpers.gettraceback ())
        sys.exit (1)

    os.environ["CFLAGS"] = ""

    if "bdist_msi" in sys.argv:
        # hack the version name to a format msi doesn't have trouble with
        VERSION = VERSION.replace("-alpha", "a")
        VERSION = VERSION.replace("-rc", "r")

    if DEBUG and buildsystem in ("msys", "unix", "darwin") and \
       compiler in ("unix", "cygwin", "mingw32", "mingw32-console"):
        os.environ["CFLAGS"] += " -W -Wall -Wimplicit-int " + \
                        "-Wimplicit-function-declaration " + \
                        "-Wimplicit -Wreturn-type -Wunused " + \
                        "-Wswitch -Wcomment -Wtrigraphs -Wformat " + \
                        "-Wchar-subscripts -Wuninitialized -Wparentheses " +\
                        "-Wpointer-arith -Wcast-qual -Winline " + \
                        "-Wcast-align -Wconversion -Wstrict-prototypes " + \
                        "-Wmissing-prototypes -Wmissing-declarations " + \
                        "-Wnested-externs -Wshadow -Wredundant-decls -g -pg"

    # When building in Mac OS, we must make sure that all
    # modules are built as universal binaries.
    # FIXME: Find a better place for this?
    if buildsystem == "darwin":
        os.environ["CFLAGS"] += " -arch i386 -arch ppc"

    packages = [ "pygame2",
                 "pygame2.examples",
                 "pygame2.examples.freetype",
                 "pygame2.examples.sdl",
                 "pygame2.examples.sdlext",
                 "pygame2.examples.sdlgfx",
                 "pygame2.sprite",
                 "pygame2.threads",
                 "pygame2.test",
                 "pygame2.dll",
                 ]
    package_dir = { "pygame2" : "lib",
                    "pygame2.examples" : "examples",
                    "pygame2.sprite" : "lib/sprite",
                    "pygame2.threads" : "lib/threads",
                    "pygame2.test" : "test",
                    }
    package_data = {
        "pygame2.examples" : find_pkg_data ("examples"),
        "pygame2.test" : find_pkg_data ("test", ["c_api", "util"]),
        }

    dllfiles = [ os.path.join ("pygame2", "dll"),
                 config_modules.get_install_libs (buildsystem, cfg) ]
    ext_modules = modules.get_extensions (buildsystem, compiler)
    modules.update_packages (ext_modules, packages, package_dir, package_data)

    headerfiles = []
    print ("The following modules will be built:")
    for ext in ext_modules:
        ext.define_macros.extend (builddefines)
        headerfiles += ext.basemodule.installheaders
        print ("\t%s" % ext.name)

    # Allow the user to read what was printed
    time.sleep (2)

    # Create doc headers on demand.
    docincpath = os.path.join ("src", "doc")
    if not os.path.exists (docincpath):
        os.mkdir (docincpath)
    for ext in ext_modules:
        modules.create_docheader (ext.basemodule, docincpath)

    setupdata = {
        "cmdclass" : { "install_data" : SmartInstallData },
        "name" :  "pygame2",
        "version" : VERSION,
        "description" : "Python Game Development Library",
        "author" : "Pete Shinners, Rene Dudfield, Marcus von Appen, Lenard Lindstrom, Brian Fisher, others...",
        "author_email" : "pygame@seul.org",
        "license" : "LGPL",
        "url" : "http://pygame.org",
        "packages" : packages,
        "package_dir" : package_dir,
        "package_data" : package_data,
        "headers" : headerfiles,
        "ext_modules" : ext_modules,
        "data_files" : [ dllfiles ],
        }

    setup (**setupdata)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.