rectify_include_paths.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/usr/bin/python
  2. import os
  3. import os.path
  4. import re
  5. # Find all the include files, map them to their real names.
  6. def exclude(paths, dirnames):
  7. for p in paths:
  8. if p in dirnames:
  9. dirnames.remove(p)
  10. def get_include_map():
  11. includes = { }
  12. for dirpath,dirnames,fnames in os.walk("src"):
  13. exclude(["ext", "win32"], dirnames)
  14. for fname in fnames:
  15. if fname.endswith(".h"):
  16. assert fname not in includes
  17. include = os.path.join(dirpath, fname)
  18. assert include.startswith("src/")
  19. includes[fname] = include[4:]
  20. return includes
  21. INCLUDE_PAT = re.compile(r'( *# *include +")([^"]+)(".*)')
  22. def get_base_header_name(hdr):
  23. return os.path.split(hdr)[1]
  24. def fix_includes(inp, out, mapping):
  25. for line in inp:
  26. m = INCLUDE_PAT.match(line)
  27. if m:
  28. include,hdr,rest = m.groups()
  29. basehdr = get_base_header_name(hdr)
  30. if basehdr in mapping:
  31. out.write('{}{}{}\n'.format(include,mapping[basehdr],rest))
  32. continue
  33. out.write(line)
  34. incs = get_include_map()
  35. for dirpath,dirnames,fnames in os.walk("src"):
  36. exclude(["trunnel"], dirnames)
  37. for fname in fnames:
  38. if fname.endswith(".c") or fname.endswith(".h"):
  39. fname = os.path.join(dirpath, fname)
  40. tmpfile = fname+".tmp"
  41. f_in = open(fname, 'r')
  42. f_out = open(tmpfile, 'w')
  43. fix_includes(f_in, f_out, incs)
  44. f_in.close()
  45. f_out.close()
  46. os.rename(tmpfile, fname)