summaryrefslogtreecommitdiffstats
path: root/remove-relative-sys-path
blob: 20fcb06d8fa1a7d091a84f812567cb972a73e630 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env python

'''
pycparser examples all contain the following boiler plate code
for running in tree. This script removes them:

# This is not required if you've installed pycparser into
# your site-packages/ with setup.py
#
sys.path.extend(['.', '..'])
'''

import sys
import os

boiler_plate = "sys.path.extend(['.', '..'])\n"
d = sys.argv[1]
for (root, dirs, files) in os.walk(d):
    for i in files:
        if not i.endswith('.py'):
            continue
        fname = os.path.join(root, i)
        lines = open(fname).readlines()
        try:
            start = lines.index(boiler_plate)
            end = start
        except ValueError:
            start = None
            end = start
        if start is not None:
            while lines[start-1].startswith('#'):
                start -= 1

        if start is not None and end is not None:
            f = open(fname, 'w')
            f.writelines(lines[:start])
            f.writelines(lines[end+1:])
            f.close()