3 # Future imports for Python 2.7, mandatory in 3.0
4 from __future__
import division
5 from __future__
import print_function
6 from __future__
import unicode_literals
14 sys
.stderr
.write("WARNING: %s\n"%msg
)
16 # Find all the include files, map them to their real names.
18 def exclude(paths
, dirnames
):
25 def get_include_map():
28 for dirpath
,dirnames
,fnames
in os
.walk("src"):
29 exclude(["ext", "win32"], dirnames
)
32 # Avoid editor temporary files
33 if fname
.startswith("."):
35 if fname
.startswith("#"):
38 if fname
.endswith(".h"):
40 warn("Multiple headers named %s"%fname
)
41 includes
[fname
] = DUPLICATE
43 include
= os
.path
.join(dirpath
, fname
)
44 assert include
.startswith("src/")
45 includes
[fname
] = include
[4:]
49 INCLUDE_PAT
= re
.compile(r
'( *# *include +")([^"]+)(".*)')
51 def get_base_header_name(hdr
):
52 return os
.path
.split(hdr
)[1]
54 def fix_includes(inp
, out
, mapping
):
56 m
= INCLUDE_PAT
.match(line
)
58 include
,hdr
,rest
= m
.groups()
59 basehdr
= get_base_header_name(hdr
)
60 if basehdr
in mapping
and mapping
[basehdr
] is not DUPLICATE
:
61 out
.write('{}{}{}\n'.format(include
,mapping
[basehdr
],rest
))
66 incs
= get_include_map()
68 for dirpath
,dirnames
,fnames
in os
.walk("src"):
69 exclude(["trunnel"], dirnames
)
72 # Avoid editor temporary files
73 if fname
.startswith("."):
75 if fname
.startswith("#"):
78 if fname
.endswith(".c") or fname
.endswith(".h"):
79 fname
= os
.path
.join(dirpath
, fname
)
80 tmpfile
= fname
+".tmp"
81 f_in
= open(fname
, 'r')
82 f_out
= open(tmpfile
, 'w')
83 fix_includes(f_in
, f_out
, incs
)
86 os
.rename(tmpfile
, fname
)