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

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