|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# tools/parsecallstack.py |
| 3 | +# |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | +# |
| 6 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 7 | +# contributor license agreements. See the NOTICE file distributed with |
| 8 | +# this work for additional information regarding copyright ownership. The |
| 9 | +# ASF licenses this file to you under the Apache License, Version 2.0 (the |
| 10 | +# "License"); you may not use this file except in compliance with the |
| 11 | +# License. You may obtain a copy of the License at |
| 12 | +# |
| 13 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | +# |
| 15 | +# Unless required by applicable law or agreed to in writing, software |
| 16 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 17 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 18 | +# License for the specific language governing permissions and limitations |
| 19 | +# under the License. |
| 20 | +# |
| 21 | + |
| 22 | +import re |
| 23 | +import sys |
| 24 | +from pathlib import Path |
| 25 | + |
| 26 | + |
| 27 | +def expand_file(input_path, include_paths, processed=None): |
| 28 | + """ |
| 29 | + Recursively expand the file, returning its contents in order as a list of lines. |
| 30 | + """ |
| 31 | + if processed is None: |
| 32 | + processed = set() |
| 33 | + |
| 34 | + input_path = Path(input_path).resolve() |
| 35 | + if input_path in processed: |
| 36 | + return [] # Already processed, avoid duplicate includes |
| 37 | + processed.add(input_path) |
| 38 | + |
| 39 | + expanded_lines = [] |
| 40 | + |
| 41 | + with input_path.open("r", encoding="utf-8") as f: |
| 42 | + lines = f.readlines() |
| 43 | + |
| 44 | + for line in lines: |
| 45 | + line_strip = line.strip() |
| 46 | + match = re.match(r'#include\s*[<"]([^">]+)[">]', line_strip) |
| 47 | + if match: |
| 48 | + include_file = match.group(1) |
| 49 | + found = False |
| 50 | + |
| 51 | + # Check the current directory first |
| 52 | + |
| 53 | + direct_path = input_path.parent / include_file |
| 54 | + if direct_path.exists(): |
| 55 | + expanded_lines.extend( |
| 56 | + expand_file(direct_path, include_paths, processed) |
| 57 | + ) |
| 58 | + found = True |
| 59 | + else: |
| 60 | + # Then check in the include paths |
| 61 | + |
| 62 | + for path in include_paths: |
| 63 | + candidate = Path(path) / include_file |
| 64 | + if candidate.exists(): |
| 65 | + expanded_lines.extend( |
| 66 | + expand_file(candidate, include_paths, processed) |
| 67 | + ) |
| 68 | + found = True |
| 69 | + break |
| 70 | + |
| 71 | + if not found: |
| 72 | + print( |
| 73 | + f'ERROR: Cannot find "{include_file}" from {input_path}', |
| 74 | + file=sys.stderr, |
| 75 | + ) |
| 76 | + sys.exit(1) |
| 77 | + else: |
| 78 | + expanded_lines.append(line) |
| 79 | + |
| 80 | + expanded_lines.append("\n") # Keep separation between files |
| 81 | + return expanded_lines |
| 82 | + |
| 83 | + |
| 84 | +def process_file(output_path, input_path, include_paths): |
| 85 | + lines = expand_file(input_path, include_paths) |
| 86 | + with open(output_path, "w", encoding="utf-8") as out: |
| 87 | + out.writelines(lines) |
| 88 | + |
| 89 | + |
| 90 | +if __name__ == "__main__": |
| 91 | + if len(sys.argv) < 3: |
| 92 | + print( |
| 93 | + "Usage: process_includes.py <output_file> <input_file> [include_paths...]", |
| 94 | + file=sys.stderr, |
| 95 | + ) |
| 96 | + sys.exit(1) |
| 97 | + |
| 98 | + output_file = Path(sys.argv[1]) |
| 99 | + input_file = sys.argv[2] |
| 100 | + include_dirs = sys.argv[3:] |
| 101 | + |
| 102 | + if output_file.exists(): |
| 103 | + output_file.unlink() |
| 104 | + |
| 105 | + process_file(output_file, input_file, include_dirs) |
0 commit comments