88efec
#!/usr/bin/env python
88efec
88efec
'''
88efec
pycparser examples all contain the following boiler plate code
88efec
for running in tree. This script removes them:
88efec
88efec
# This is not required if you've installed pycparser into
88efec
# your site-packages/ with setup.py
88efec
#
88efec
sys.path.extend(['.', '..'])
88efec
'''
88efec
88efec
import sys
88efec
import os
88efec
88efec
boiler_plate = "sys.path.extend(['.', '..'])\n"
88efec
d = sys.argv[1]
88efec
for (root, dirs, files) in os.walk(d):
88efec
    for i in files:
88efec
        if not i.endswith('.py'):
88efec
            continue
88efec
        fname = os.path.join(root, i)
88efec
        lines = open(fname).readlines()
88efec
        try:
88efec
            start = lines.index(boiler_plate)
88efec
            end = start
88efec
        except ValueError:
88efec
            start = None
88efec
            end = start
88efec
        if start is not None:
88efec
            while lines[start-1].startswith('#'):
88efec
                start -= 1
88efec
88efec
        if start is not None and end is not None:
88efec
            f = open(fname, 'w')
88efec
            f.writelines(lines[:start])
88efec
            f.writelines(lines[end+1:])
88efec
            f.close()