1
1
#!/usr/bin/env python3
2
2
3
3
from pathlib import Path
4
+ from typing import List , Set
4
5
from glob import glob
5
6
from shutil import rmtree
6
7
18
19
FILE_HEADER = ['// DO NOT EDIT. This file is auto-generated by `amalgamate.py`.' , '' ]
19
20
20
21
22
+ # Python versions before 3.10 don't have the root_dir argument for glob, so we
23
+ # crudely emulate it here.
24
+ def glob_in_dir (
25
+ pattern : str ,
26
+ root_dir : Path ,
27
+ ):
28
+ cwd = os .getcwd ()
29
+ root_dir = root_dir .resolve ()
30
+ os .chdir (root_dir )
31
+ try :
32
+ for path in glob (pattern , recursive = True ):
33
+ yield Path (root_dir ) / path
34
+ finally :
35
+ os .chdir (cwd )
36
+
37
+
21
38
def find_include_path (
22
39
include : str ,
23
- search_paths : list [Path ],
40
+ search_paths : List [Path ],
24
41
) -> Path :
25
42
for search_path in search_paths :
26
43
path = search_path / include
@@ -33,10 +50,10 @@ def find_include_path(
33
50
def merge_headers (
34
51
* ,
35
52
header : str ,
36
- search_paths : list [Path ],
37
- covered_headers : set [Path ],
38
- stack : list [str ],
39
- ) -> list [str ]:
53
+ search_paths : List [Path ],
54
+ covered_headers : Set [Path ],
55
+ stack : List [str ],
56
+ ) -> List [str ]:
40
57
# Locate and load header contents.
41
58
path = find_include_path (header , search_paths )
42
59
with path .open () as f :
@@ -84,13 +101,13 @@ def merge_headers(
84
101
return filtered
85
102
86
103
87
- def merge_sources (* , source_dir : Path , covered_headers : set [Path ]):
104
+ def merge_sources (* , source_dir : Path , covered_headers : Set [Path ]):
88
105
output = [
89
106
'#include <Zydis.h>' ,
90
107
'' ,
91
108
]
92
109
93
- for source_file in glob ('**/*.c' , root_dir = source_dir , recursive = True ):
110
+ for source_file in glob_in_dir ('**/*.c' , source_dir ):
94
111
print (f'Processing source file "{ source_file } "' )
95
112
96
113
# Print some comments to show where the code is from.
@@ -163,3 +180,4 @@ def main():
163
180
164
181
if __name__ == '__main__' :
165
182
main ()
183
+
0 commit comments