1#!/usr/bin/python2
2from __future__ import absolute_import
3from __future__ import division
4from __future__ import print_function
5
6import os
7
8def purge_src(top_dir):
9    if not os.path.exists(top_dir):
10        return
11    for dir in os.listdir(top_dir):
12        if dir.startswith('.'):
13            continue
14        py = os.path.join (top_dir, dir, dir + '.py')
15        if not os.path.exists(py):
16            continue
17        ret = os.system('grep -q "preserve_srcdir = " ' + py)
18        src_path = os.path.abspath(os.path.join('tests', dir, 'src'))
19        if not os.path.exists(src_path):
20            continue
21        if ret:                 # This should have a replaceable src dir
22            cmd = 'rm -rf ' + src_path
23        else:
24            cmd = 'cd %s; make clean > /dev/null 2>&1 ' % src_path
25
26        print('Cleaning %s test dir' % dir)
27        os.system(cmd)
28
29if os.path.isdir('tmp'):
30    os.system('cd tmp && ls -A | xargs rm -rf')
31
32for dir in ['site_tests', 'site_profilers', 'tests', 'profilers', 'deps']:
33    purge_src(dir)
34