import os
import re

import SCons.Builder
import SCons.Defaults
import SCons.Scanner
import SCons.Script

FAUST = 'faust'
INCLUDE_RE = re.compile(r'import\s*\(\s*"([^"]+)"\s*\)\s*;', re.M)

def dsp_source_scanner(node, env, path):
    """Scan source files for imported files in `path'."""

    contents = node.get_contents()
    includes = INCLUDE_RE.findall(contents)
    path = [os.path.dirname(str(node))] + list(path)

    return filter(os.path.exists, env.Flatten(map(lambda f: map(lambda d: os.path.join(os.path.realpath(str(d)), f), path), includes)))

def dsp_target_scanner(node, env, path):
    """Search for architecture file in `path'."""

    arch = env['FAUST_ARCHITECTURE'] + '.cpp'
    return filter(os.path.exists, map(lambda d: os.path.join(str(d), arch), path))

def svg_emitter(target, source, env):
    target = target + map(lambda x: os.path.join(str(x), 'process.svg'), target)
    print map(str, target)
    return (target, source)
    #return (target + [os.path.join(str(target[0]), 'process.svg')], source)

def svg_scanner(node, env, path):
    return [os.path.join(str(node), 'process.svg')]

dsp = SCons.Builder.Builder(
        action = 'faust ${FAUST_FLAGS} -a ${FAUST_ARCHITECTURE}.cpp -o $TARGET $SOURCE',
        suffix = '.cpp',
        src_suffix = '.dsp',
        target_scanner = Scons.Scanner.Scanner(
            function = dsp_target_scanner,
            path_function = SCons.Scanner.FindPathDirs('FAUST_PATH'))
)

xml = SCons.Builder.Builder(
        action = ['faust ${FAUST_FLAGS} -o /dev/null -xml $SOURCE', SCons.Defaults.Move('$TARGET', '${SOURCE}.xml')],
        suffix = '.dsp.xml',
        src_suffix = '.dsp'
)

svg = SCons.Builder.Builder(
        action = ['faust ${FAUST_FLAGS} -o /dev/null -svg $SOURCE', SCons.Defaults.Move('$TARGET', '${SOURCE}-svg')],
        suffix = '.dsp-svg',
        src_suffix = '.dsp',
        single_source = True,
        target_factory = SCons.Script.Dir,
        target_scanner = SCons.Scanner.Scanner(function = svg_scanner)
)

sc  = SCons.Builder.Builder(
        action = '$FAUST2SC --lang=sclang --prefix="${FAUST2SC_PREFIX}" -o $TARGET $SOURCES',
        suffix = '.sc',
        src_suffix = '.dsp.xml',
        multi = True
)

hs  = SCons.Builder.Builder(
        action = '$FAUST2SC --lang=haskell --prefix="${FAUST2SC_HASKELL_MODULE}" -o $TARGET $SOURCES',
        suffix = '.hs',
        src_suffix = '.dsp.xml',
        multi = True
)

@@ -416,30 +416,30 @@
 (path)%0A%0A    
-return
+deps =
  filter(os.p
@@ -453,110 +453,115 @@
 sts,
- env.Flatten(map(lambda f: map(lambda d: os.path.join(os.path.realpath(str(d)), f), path), includes)))
+%0A        %5Bos.path.join(os.path.realpath(str(d)), f) for d in path for f in includes%5D%0A    )%0A%0A    return deps
 %0A%0Ade
@@ -734,22 +734,9 @@
 ts, 
-map(lambda d: 
+%5B
 os.p
@@ -757,23 +757,31 @@
 ), arch)
-,
+ for d in
  path
-)
+%5D
 )%0A%0Adef s
@@ -839,22 +839,9 @@
 t + 
-map(lambda x: 
+%5B
 os.p
@@ -853,17 +853,17 @@
 oin(str(
-x
+t
 ), 'proc
@@ -871,25 +871,33 @@
 ss.svg')
-,
+ for t in
  target
-)
+%5D
 %0A    pri

Convert map()'s to list comprehensions.