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

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