#!/usr/bin/python
import os

def purge_src(top_dir):
    if not os.path.exists(top_dir):
        return
    for dir in os.listdir(top_dir):
        if dir.startswith('.'):
            continue
        py = os.path.join (top_dir, dir, dir + '.py')
        if not os.path.exists(py):
            continue
        ret = os.system('grep -q "preserve_srcdir = " ' + py)
        src_path = os.path.abspath(os.path.join('tests', dir, 'src'))
        if not os.path.exists(src_path):
            continue
        if ret:                 # This should have a replaceable src dir
            cmd = 'rm -rf ' + src_path
        else:
            cmd = 'cd %s; make clean > /dev/null 2>&1 ' % src_path

        print 'Cleaning %s test dir' % dir
        os.system(cmd)

if os.path.isdir('tmp'):
    os.system('cd tmp && ls -A | xargs rm -rf')

for dir in ['site_tests', 'site_profilers', 'tests', 'profilers', 'deps']:
    purge_src(dir)