Blame SOURCES/pycparser-0.91.1-remove-relative-sys-path.py

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