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