aboutsummaryrefslogtreecommitdiff
path: root/scripts/maint/rectify_include_paths.py
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2018-06-20 09:25:16 -0400
committerNick Mathewson <nickm@torproject.org>2018-06-20 09:25:16 -0400
commit178b738be0e1cb7a20dbb8b0d01eadc0cf6db0ba (patch)
treeb7e98b6fb226cd9039be5c692dfdceb0a8be5acf /scripts/maint/rectify_include_paths.py
parentd7301a456ab15ab84030b6e0fcf5a6fbd9e43fa9 (diff)
downloadtor-178b738be0e1cb7a20dbb8b0d01eadc0cf6db0ba.tar.gz
tor-178b738be0e1cb7a20dbb8b0d01eadc0cf6db0ba.zip
Script to replace include paths with full paths under src/
This will let us move around header files without having to fix up all the include sites manually.
Diffstat (limited to 'scripts/maint/rectify_include_paths.py')
-rwxr-xr-xscripts/maint/rectify_include_paths.py60
1 files changed, 60 insertions, 0 deletions
diff --git a/scripts/maint/rectify_include_paths.py b/scripts/maint/rectify_include_paths.py
new file mode 100755
index 0000000000..dc91634455
--- /dev/null
+++ b/scripts/maint/rectify_include_paths.py
@@ -0,0 +1,60 @@
+#!/usr/bin/python3
+
+import os
+import os.path
+import re
+
+# Find all the include files, map them to their real names.
+
+def exclude(paths, dirnames):
+ for p in paths:
+ if p in dirnames:
+ dirnames.remove(p)
+
+def get_include_map():
+ includes = { }
+
+ for dirpath,dirnames,fnames in os.walk("src"):
+ exclude(["ext", "win32"], dirnames)
+
+ for fname in fnames:
+ if fname.endswith(".h"):
+ assert fname not in includes
+ include = os.path.join(dirpath, fname)
+ assert include.startswith("src/")
+ includes[fname] = include[4:]
+
+ return includes
+
+INCLUDE_PAT = re.compile(r'( *# *include +")([^"]+)(".*)')
+
+def get_base_header_name(hdr):
+ return os.path.split(hdr)[1]
+
+def fix_includes(inp, out, mapping):
+ for line in inp:
+ m = INCLUDE_PAT.match(line)
+ if m:
+ include,hdr,rest = m.groups()
+ basehdr = get_base_header_name(hdr)
+ if basehdr in mapping:
+ out.write('{}{}{}\n'.format(include,mapping[basehdr],rest))
+ continue
+
+ out.write(line)
+
+incs = get_include_map()
+
+for dirpath,dirnames,fnames in os.walk("src"):
+ exclude(["ext", "trunnel"], dirnames)
+
+ for fname in fnames:
+ if fname.endswith(".c") or fname.endswith(".h"):
+ fname = os.path.join(dirpath, fname)
+ tmpfile = fname+".tmp"
+ f_in = open(fname, 'r')
+ f_out = open(tmpfile, 'w')
+ fix_includes(f_in, f_out, incs)
+ f_in.close()
+ f_out.close()
+ os.rename(tmpfile, fname)