18
18
import os
19
19
import re
20
20
import subprocess
21
+ import sys
21
22
22
23
import click
23
24
24
- DEFAULT_CHIP_ROOT = os .path .abspath (os .path .join (os .path .dirname (__file__ ), '..' , '..' ))
25
- DEFAULT_OUTPUT_DIR = os .path .abspath (os .path .join (DEFAULT_CHIP_ROOT , 'data_model' ))
25
+ DEFAULT_CHIP_ROOT = os .path .abspath (
26
+ os .path .join (os .path .dirname (__file__ ), '..' , '..' ))
27
+ DEFAULT_OUTPUT_DIR = os .path .abspath (
28
+ os .path .join (DEFAULT_CHIP_ROOT , 'data_model' ))
29
+ DEFAULT_DOCUMENTATION_DIR = os .path .abspath (
30
+ os .path .join (DEFAULT_CHIP_ROOT , 'docs' , 'cluster_ids.md' ))
26
31
27
32
28
33
def get_xml_path (filename , output_dir ):
@@ -56,14 +61,17 @@ def main(scraper, spec_root, output_dir, dry_run):
56
61
scrape_device_types (scraper , spec_root , output_dir , dry_run )
57
62
if not dry_run :
58
63
dump_versions (scraper , spec_root , output_dir )
64
+ dump_cluster_ids (output_dir )
59
65
60
66
61
67
def scrape_clusters (scraper , spec_root , output_dir , dry_run ):
62
68
src_dir = os .path .abspath (os .path .join (spec_root , 'src' ))
63
- sdm_clusters_dir = os .path .abspath (os .path .join (src_dir , 'service_device_management' ))
69
+ sdm_clusters_dir = os .path .abspath (
70
+ os .path .join (src_dir , 'service_device_management' ))
64
71
app_clusters_dir = os .path .abspath (os .path .join (src_dir , 'app_clusters' ))
65
72
dm_clusters_dir = os .path .abspath (os .path .join (src_dir , 'data_model' ))
66
- media_clusters_dir = os .path .abspath (os .path .join (app_clusters_dir , 'media' ))
73
+ media_clusters_dir = os .path .abspath (
74
+ os .path .join (app_clusters_dir , 'media' ))
67
75
clusters_output_dir = os .path .abspath (os .path .join (output_dir , 'clusters' ))
68
76
dm_clusters_list = ['ACL-Cluster.adoc' , 'Binding-Cluster.adoc' , 'bridge-clusters.adoc' ,
69
77
'Descriptor-Cluster.adoc' , 'Group-Key-Management-Cluster.adoc' , 'ICDManagement.adoc' ,
@@ -79,7 +87,8 @@ def scrape_clusters(scraper, spec_root, output_dir, dry_run):
79
87
80
88
def scrape_cluster (filename : str ) -> None :
81
89
xml_path = get_xml_path (filename , clusters_output_dir )
82
- cmd = [scraper , 'cluster' , '-i' , filename , '-o' , xml_path , '-nd' , '--define' , 'in-progress' ]
90
+ cmd = [scraper , 'cluster' , '-i' , filename , '-o' ,
91
+ xml_path , '-nd' , '--define' , 'in-progress' ]
83
92
if dry_run :
84
93
print (cmd )
85
94
else :
@@ -100,16 +109,19 @@ def scrape_all_clusters(dir: str, exclude_list: list[str] = []) -> None:
100
109
101
110
102
111
def scrape_device_types (scraper , spec_root , output_dir , dry_run ):
103
- device_type_dir = os .path .abspath (os .path .join (spec_root , 'src' , 'device_types' ))
104
- device_types_output_dir = os .path .abspath (os .path .join (output_dir , 'device_types' ))
112
+ device_type_dir = os .path .abspath (
113
+ os .path .join (spec_root , 'src' , 'device_types' ))
114
+ device_types_output_dir = os .path .abspath (
115
+ os .path .join (output_dir , 'device_types' ))
105
116
clusters_output_dir = os .path .abspath (os .path .join (output_dir , 'clusters' ))
106
117
107
118
if not os .path .exists (device_types_output_dir ):
108
119
os .makedirs (device_types_output_dir )
109
120
110
121
def scrape_device_type (filename : str ) -> None :
111
122
xml_path = get_xml_path (filename , device_types_output_dir )
112
- cmd = [scraper , 'devicetype' , '-c' , clusters_output_dir , '-nd' , '-i' , filename , '-o' , xml_path ]
123
+ cmd = [scraper , 'devicetype' , '-c' , clusters_output_dir ,
124
+ '-nd' , '-i' , filename , '-o' , xml_path ]
113
125
if dry_run :
114
126
print (cmd )
115
127
else :
@@ -125,17 +137,52 @@ def scrape_device_type(filename: str) -> None:
125
137
126
138
def dump_versions (scraper , spec_root , output_dir ):
127
139
sha_file = os .path .abspath (os .path .join (output_dir , 'spec_sha' ))
128
- out = subprocess .run (['git' , 'rev-parse' , 'HEAD' ], capture_output = True , encoding = "utf8" , cwd = spec_root )
140
+ out = subprocess .run (['git' , 'rev-parse' , 'HEAD' ],
141
+ capture_output = True , encoding = "utf8" , cwd = spec_root )
129
142
sha = out .stdout
130
143
with open (sha_file , 'wt' , encoding = 'utf8' ) as output :
131
144
output .write (sha )
132
145
133
146
scraper_file = os .path .abspath (os .path .join (output_dir , 'scraper_version' ))
134
- out = subprocess .run ([scraper , '--version' ], capture_output = True , encoding = "utf8" )
147
+ out = subprocess .run ([scraper , '--version' ],
148
+ capture_output = True , encoding = "utf8" )
135
149
version = out .stdout
136
150
with open (scraper_file , "wt" , encoding = 'utf8' ) as output :
137
151
output .write (version )
138
152
139
153
154
+ def dump_cluster_ids (output_dir ):
155
+ python_testing_path = os .path .abspath (
156
+ os .path .join (DEFAULT_CHIP_ROOT , 'src' , 'python_testing' ))
157
+ sys .path .insert (0 , python_testing_path )
158
+ clusters_output_dir = os .path .abspath (os .path .join (output_dir , 'clusters' ))
159
+ device_types_output_dir = os .path .abspath (
160
+ os .path .join (output_dir , 'device_types' ))
161
+
162
+ from spec_parsing_support import build_xml_clusters
163
+
164
+ header = '# Matter Cluster IDs\n '
165
+ header += 'This file was **AUTOMATICALLY** generated by `python scripts/generate_spec_xml.py`. DO NOT EDIT BY HAND!\n \n '
166
+
167
+ clusters , problems = build_xml_clusters ()
168
+ all_name_lens = [len (c .name ) for c in clusters .values ()]
169
+ name_len = max (all_name_lens )
170
+ title_id_decimal = ' ID (Decimal) '
171
+ title_id_hex = ' ID (hex) '
172
+ title_name_raw = ' Name '
173
+ title_name = f'{ title_name_raw :<{name_len }} '
174
+ dec_len = len (title_id_decimal )
175
+ hex_len = len (title_id_hex )
176
+ title = f'|{ title_id_decimal } |{ title_id_hex } |{ title_name } |\n '
177
+ hashes = f'|{ "-" * dec_len } |{ "-" * hex_len } |{ "-" * name_len } |\n '
178
+ s = title + hashes
179
+ for id , cluster in sorted (clusters .items ()):
180
+ hex_id = f'0x{ id :04X} '
181
+ s += f'|{ id :<{dec_len }} |{ hex_id :<{hex_len }} |{ cluster .name :<{name_len }} |\n '
182
+ with open (DEFAULT_DOCUMENTATION_DIR , 'w' ) as fout :
183
+ fout .write (header )
184
+ fout .write (s )
185
+
186
+
140
187
if __name__ == '__main__' :
141
188
main ()
0 commit comments