Skip to content

Commit c82846c

Browse files
committed
Fixups for old Python versions
1 parent f23d292 commit c82846c

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

assets/amalgamate.py

+25-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python3
22

33
from pathlib import Path
4+
from typing import List, Set
45
from glob import glob
56
from shutil import rmtree
67

@@ -18,9 +19,25 @@
1819
FILE_HEADER = ['// DO NOT EDIT. This file is auto-generated by `amalgamate.py`.', '']
1920

2021

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+
2138
def find_include_path(
2239
include: str,
23-
search_paths: list[Path],
40+
search_paths: List[Path],
2441
) -> Path:
2542
for search_path in search_paths:
2643
path = search_path / include
@@ -33,10 +50,10 @@ def find_include_path(
3350
def merge_headers(
3451
*,
3552
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]:
4057
# Locate and load header contents.
4158
path = find_include_path(header, search_paths)
4259
with path.open() as f:
@@ -84,13 +101,13 @@ def merge_headers(
84101
return filtered
85102

86103

87-
def merge_sources(*, source_dir: Path, covered_headers: set[Path]):
104+
def merge_sources(*, source_dir: Path, covered_headers: Set[Path]):
88105
output = [
89106
'#include <Zydis.h>',
90107
'',
91108
]
92109

93-
for source_file in glob('**/*.c', root_dir=source_dir, recursive=True):
110+
for source_file in glob_in_dir('**/*.c', source_dir):
94111
print(f'Processing source file "{source_file}"')
95112

96113
# Print some comments to show where the code is from.
@@ -163,3 +180,4 @@ def main():
163180

164181
if __name__ == '__main__':
165182
main()
183+

0 commit comments

Comments
 (0)