import os import sys import py_compile # Function to build a .pyc from a .py def build_pyc(target, source, env): py_compile.compile(str(source[0]), str(target[0]), doraise=True) return None # Read in build options opts = Options('build_options.py') opts.Add('GCCFLAGS', 'Flags passed to GCC') opts.Add('MSVCFLAGS', 'Flags passed to the MSVC compiler') # Create the .pyc builder pyc_builder = Builder(action = build_pyc, suffix = '.pyc', src_suffix = '.py') env = Environment(options = opts) env.Append(BUILDERS={'Pyc' : pyc_builder}) Help(opts.GenerateHelpText(env)) # Find the path for Python.h and then share it to other SConscripts later on # Where to find the Python.h if sys.platform == 'win32': # Prefer MinGW over MSVC Tool('mingw')(env) python_include = os.path.join(sys.exec_prefix, 'include') # Windows installs need to be told the lib path and the python library name # else it won't work right. python_libdir = os.path.join(sys.exec_prefix, 'libs') env.Append(LIBPATH=[python_libdir]) python_libname = 'python%d%d' % (sys.version_info[0:2]) env.Append(LIBS=[python_libname]) else: python_include = os.path.join(os.path.join(sys.exec_prefix, 'include'), 'python' + sys.version[:3]) # Check existence of Python.h headerfile = python_include + os.sep + 'Python.h' try: open(headerfile, 'r') except: raise """Did not find Python header file %s. Make sure files for Python C-extensions are installed. In debian linux, for example, you need to install a package called something like python2.3-dev""" %headerfile env.Append(CPPPATH=[python_include]) # Set appropriate CCFLAGS for the compiler. if env['CC'] == 'gcc': env.Append(CCFLAGS=['${GCCFLAGS}']) elif env['CC'] == 'cl': env.Append(CCFLAGS=['${MSVCFLAGS}']) Export('env') # Build .pyc files of every .py in here and below. files = [] dirs = filter(os.path.isdir, os.listdir('.')) while(dirs != []): dirs += filter(os.path.isdir, map(lambda x : dirs[0] + os.sep + x, os.listdir(dirs[0]))) files += filter(lambda x : x[-3:] == '.py', map(lambda x : dirs[0] + os.sep + x, os.listdir(dirs[0]))) dirs = dirs[1:] for x in files: env.Pyc(x + 'c', x) SConscript(['inundation/SConscript'])