|
13 | 13 | # limitations under the License.
|
14 | 14 |
|
15 | 15 | """
|
16 |
| -Generates a gni file containing all data_model files (generally XML and JSON files) |
17 |
| -that are typically used by matter_testing_infrastructure. |
| 16 | +Generates a gni file containing all data_model files (XML) |
| 17 | +that are used by matter_testing_infrastructure. |
18 | 18 |
|
19 | 19 | These files are to be bundled with whl packages of the matter_testing_infrastructure
|
20 | 20 | so that methods requiring data model files work just by installing the python
|
|
26 | 26 |
|
27 | 27 | # Set chip_root to be dynamically based on the script's location
|
28 | 28 | chip_root = os.path.abspath(os.path.join(os.path.dirname(__file__), "../../.."))
|
29 |
| - |
30 |
| -# Directories to search for .xml and .json files relative to chip_root |
31 |
| -directories = [ |
32 |
| - os.path.join(chip_root, "data_model/1.3/clusters/"), |
33 |
| - os.path.join(chip_root, "data_model/1.3/device_types/"), |
34 |
| - os.path.join(chip_root, "data_model/1.4/clusters/"), |
35 |
| - os.path.join(chip_root, "data_model/1.4/device_types/"), |
36 |
| - os.path.join(chip_root, "data_model/master/clusters/"), |
37 |
| - os.path.join(chip_root, "data_model/master/device_types/"), |
38 |
| -] |
| 29 | +data_model_dir = os.path.join(chip_root, "data_model") |
39 | 30 |
|
40 | 31 | # Template for generating the GNI file content with proper indentation
|
41 |
| -GNI_TEMPLATE = """\ |
| 32 | +GNI_HEADER = """\ |
42 | 33 | # Copyright (c) 2024 Project CHIP Authors
|
43 | 34 | #
|
44 | 35 | # Licensed under the Apache License, Version 2.0 (the "License");
|
|
52 | 43 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
53 | 44 | # See the License for the specific language governing permissions and
|
54 | 45 | # limitations under the License.
|
| 46 | +# |
| 47 | +# This file is generated by |
| 48 | +# src/python_testing/matter_testing_infrastructure/generate_data_model_xmls_gni.py |
| 49 | +# |
| 50 | +# Manually re-generate this file on any changes to the data_model directory. |
55 | 51 |
|
56 | 52 | import("//build_overrides/chip.gni")
|
| 53 | +""" |
| 54 | + |
| 55 | +GNI_TEMPLATE = """\ |
57 | 56 |
|
58 |
| -data_model_XMLS = [ |
| 57 | +data_model_XMLS_{{ dir }} = [ |
59 | 58 | {% for name in file_list %}
|
60 |
| - "{{ name }}", |
| 59 | + "{{ name }}", |
61 | 60 | {% endfor %}
|
62 | 61 | ]
|
63 |
| -""" |
64 | 62 |
|
65 |
| -# Function to find and collect all .xml and .json files |
| 63 | +""" |
66 | 64 |
|
67 | 65 |
|
68 |
| -def get_data_model_file_names(): |
| 66 | +def get_data_model_file_names(directory: str): |
| 67 | + ''' Function to find and collect all .xml files''' |
69 | 68 | file_list = []
|
70 |
| - for directory in directories: |
71 |
| - for root, _, files in os.walk(directory): |
72 |
| - for file in files: |
73 |
| - if file.endswith(".xml") or file.endswith(".json"): |
74 |
| - # Replace absolute path with `${chip_root}` for GNI compatibility |
75 |
| - relative_path = os.path.join("${chip_root}", os.path.relpath(root, chip_root), file) |
76 |
| - file_list.append(relative_path) |
| 69 | + for root, _, files in os.walk(directory): |
| 70 | + for file in files: |
| 71 | + if file.endswith(".xml"): |
| 72 | + # Replace absolute path with `${chip_root}` for GNI compatibility |
| 73 | + relative_path = os.path.join("${chip_root}", os.path.relpath(root, chip_root), file) |
| 74 | + file_list.append(relative_path) |
77 | 75 | # Sort files alphabetically
|
78 | 76 | file_list.sort()
|
79 | 77 | return file_list
|
80 | 78 |
|
81 |
| -# Main function to generate the data_model_xmls.gni file |
82 |
| - |
83 | 79 |
|
84 | 80 | def generate_gni_file():
|
85 |
| - # Step 1: Find all files and create the sorted file list |
86 |
| - file_list = get_data_model_file_names() |
87 |
| - |
88 |
| - # Step 2: Render the template with the file list |
| 81 | + '''Main function to generate the data_model_xmls.gni file''' |
89 | 82 | environment = jinja2.Environment(trim_blocks=True, lstrip_blocks=True)
|
90 | 83 | template = environment.from_string(GNI_TEMPLATE)
|
91 |
| - output_content = template.render(file_list=file_list) |
| 84 | + output_content_per_dir = [] |
| 85 | + |
| 86 | + # Step 1: Find all files and create the sorted file list |
| 87 | + dirs = sorted([d for d in os.listdir(data_model_dir) if os.path.isdir(os.path.join(data_model_dir, d))]) |
| 88 | + for directory in dirs: |
| 89 | + file_list = get_data_model_file_names(os.path.join(data_model_dir, directory)) |
| 90 | + # Step 2: Render the template with the file list |
| 91 | + output_content_per_dir.append(template.render(dir=directory.replace('.', '_'), file_list=file_list)) |
| 92 | + |
| 93 | + output_content = GNI_HEADER + "".join(output_content_per_dir) |
92 | 94 |
|
93 | 95 | # Step 3: Dynamically generate the output file path
|
94 | 96 | # Get the script's directory (where this script is located)
|
|
0 commit comments