source: icXML/icXML-devel/scripts/rootify_includes.py @ 2779

Last change on this file since 2779 was 2776, checked in by cameron, 7 years ago

Script to modify #includes to use root-relative paths

File size: 2.0 KB
Line 
1#
2# rootify_includes.py - Replace relative includes with
3# rooted includes.
4#
5# Licensed under Open Source License, 3.0.
6#
7import sys, re, os, shutil, optparse
8
9include_RE = re.compile(r'(#\s*include\s+)"([^"]*)"')
10
11cur_root_path = "xercesc"
12
13backup_extension = ".orig"
14
15def fix1_path_in_source(matchobj):
16        if os.path.exists(os.path.join(cur_root_path, matchobj.group(2))):
17                return "%s<%s/%s>" % (matchobj.group(1), cur_root_path, matchobj.group(2))
18        else:   return matchobj.group(0)
19
20def source_fixer(s):
21        s = re.sub(include_RE, fix1_path_in_source, s)
22        return s
23
24fixable_source = re.compile("\.(h|c|hpp|cpp|hxx|cxx)$")
25
26def fix_file(path):
27        if fixable_source.search(path):
28                infile = open(path)
29                indata = infile.read()
30                infile.close()
31                outdata = source_fixer(indata)
32                if outdata != indata:
33                        print("Modifying %s" % (path))
34                        if not os.path.exists(path + backup_extension): shutil.move(path, path + backup_extension)
35                        outfile = open(path, 'w')
36                        outfile.write(outdata)
37                        outfile.close()
38
39def fix_directory(path, do_recursive = False):
40        global cur_root_path
41        for f in os.listdir(path):
42                subpath = os.path.join(path, f)
43                if os.path.isdir(subpath):
44                        if do_recursive:
45                                cur_root_path = subpath
46                                fix_directory(subpath, do_recursive)
47                                cur_root_path = path
48                else:
49                        fix_file(subpath)
50                               
51if __name__ == '__main__':
52
53        option_parser = optparse.OptionParser(usage='python %prog [options] <path>', version='1.0')
54        option_parser.add_option('-r', '--recursive', 
55                          dest = 'do_recursive', action='store_true', default=False,
56                          help = 'Process subdirectories recursively.')
57        options, args = option_parser.parse_args(sys.argv[1:])
58        if len(args) != 1:
59                option_parser.print_usage()
60                sys.exit()
61       
62        fix_path = args[0]
63       
64        if os.path.isdir(fix_path):
65                cur_root_path = fix_path
66                fix_directory(fix_path, options.do_recursive)
67        else: 
68                fix_file(fix_path)
69
Note: See TracBrowser for help on using the repository browser.