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