Skip to content

Commit f10e90f

Browse files
vatsalghelani-csaandy31415restyled-commits
authored
Adding python code for generating data model xml .gni file (#36437)
* Adding python code for granating data model xml .gni file * Fixed python code and remove buildable * Update src/python_testing/matter_testing_infrastructure/generate_data_model_xmls_gni.py Co-authored-by: Andrei Litvin <andy314@gmail.com> * Update src/python_testing/matter_testing_infrastructure/generate_data_model_xmls_gni.py Co-authored-by: Andrei Litvin <andy314@gmail.com> * Update src/python_testing/matter_testing_infrastructure/generate_data_model_xmls_gni.py Co-authored-by: Andrei Litvin <andy314@gmail.com> * Fixed mismatches and typo from suggestion commits * Fixed file configuraton to figure out the right directory, so this can be run from anywhere * Restyled by autopep8 * Restyled by isort * Update src/python_testing/matter_testing_infrastructure/generate_data_model_xmls_gni.py --------- Co-authored-by: Andrei Litvin <andy314@gmail.com> Co-authored-by: Restyled.io <commits@restyled.io>
1 parent d4d19be commit f10e90f

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# Copyright (c) 2024 Project CHIP Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
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.
18+
19+
These files are to be bundled with whl packages of the matter_testing_infrastructure
20+
so that methods requiring data model files work just by installing the python
21+
package without requiring a full chip SDK checkout.
22+
"""
23+
import os
24+
25+
import jinja2
26+
27+
# Set chip_root to be dynamically based on the script's location
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+
]
39+
40+
# Template for generating the GNI file content with proper indentation
41+
GNI_TEMPLATE = """\
42+
# Copyright (c) 2024 Project CHIP Authors
43+
#
44+
# Licensed under the Apache License, Version 2.0 (the "License");
45+
# you may not use this file except in compliance with the License.
46+
# You may obtain a copy of the License at
47+
#
48+
# http://www.apache.org/licenses/LICENSE-2.0
49+
#
50+
# Unless required by applicable law or agreed to in writing, software
51+
# distributed under the License is distributed on an "AS IS" BASIS,
52+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
53+
# See the License for the specific language governing permissions and
54+
# limitations under the License.
55+
56+
import("//build_overrides/chip.gni")
57+
58+
data_model_XMLS = [
59+
{% for name in file_list %}
60+
"{{ name }}",
61+
{% endfor %}
62+
]
63+
"""
64+
65+
# Function to find and collect all .xml and .json files
66+
67+
68+
def get_data_model_file_names():
69+
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)
77+
# Sort files alphabetically
78+
file_list.sort()
79+
return file_list
80+
81+
# Main function to generate the data_model_xmls.gni file
82+
83+
84+
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
89+
environment = jinja2.Environment(trim_blocks=True, lstrip_blocks=True)
90+
template = environment.from_string(GNI_TEMPLATE)
91+
output_content = template.render(file_list=file_list)
92+
93+
# Step 3: Dynamically generate the output file path
94+
# Get the script's directory (where this script is located)
95+
script_dir = os.path.dirname(os.path.realpath(__file__)) # Directory of the current script
96+
97+
# Step 4: Ensure we are in the correct `src/python_testing/` directory
98+
base_dir = os.path.abspath(os.path.join(script_dir, "../..")) # Go up two levels to src/python_testing/
99+
# Now append `matter_testing_infrastructure`
100+
output_dir = os.path.join(base_dir, "python_testing", "matter_testing_infrastructure")
101+
output_file = os.path.join(output_dir, "data_model_xmls.gni")
102+
103+
# Step 5: Write the rendered content to the output file
104+
os.makedirs(output_dir, exist_ok=True) # Ensure the output directory exists
105+
with open(output_file, "wt") as f:
106+
f.write(output_content)
107+
print(f"{output_file} has been generated successfully.")
108+
109+
110+
# Run the function to generate the .gni file
111+
if __name__ == "__main__":
112+
generate_gni_file()

0 commit comments

Comments
 (0)