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

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