From 58f8c654e5c46b27b5d6d6a803c2d76e05ab8394 Mon Sep 17 00:00:00 2001 From: Denis Efremov Date: Wed, 31 Jan 2024 19:58:06 +0400 Subject: [PATCH] Keep same order of files for amalgamation According to the documentation glob returns files in arbitrary order. Let's sort paths to keep the same order of files for amalgamation. Signed-off-by: Denis Efremov --- assets/amalgamate.py | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/assets/amalgamate.py b/assets/amalgamate.py index b471d8e7..4ef633c4 100755 --- a/assets/amalgamate.py +++ b/assets/amalgamate.py @@ -2,7 +2,6 @@ from pathlib import Path from typing import List, Set -from glob import glob from shutil import rmtree import os @@ -19,20 +18,16 @@ FILE_HEADER = ['// DO NOT EDIT. This file is auto-generated by `amalgamate.py`.', ''] -# Python versions before 3.10 don't have the root_dir argument for glob, so we -# crudely emulate it here. -def glob_in_dir( - pattern: str, +def find_files( + pattern: re.Pattern, root_dir: Path, ): - cwd = os.getcwd() root_dir = root_dir.resolve() - os.chdir(root_dir) - try: - for path in glob(pattern, recursive=True): - yield Path(root_dir) / path - finally: - os.chdir(cwd) + paths = [] + for root, dirs, files in os.walk(root_dir): + paths.extend([Path(root) / name for name in files if pattern.match(name)]) + + return sorted(paths) def find_include_path( @@ -107,7 +102,7 @@ def merge_sources(*, source_dir: Path, covered_headers: Set[Path]): '', ] - for source_file in glob_in_dir('**/*.c', source_dir): + for source_file in find_files(re.compile('[\w-]+\.c'), source_dir): print(f'Processing source file "{source_file}"') # Print some comments to show where the code is from.