|
| 1 | +# |
| 2 | +# Copyright (c) 2024 Project CHIP Authors |
| 3 | +# All rights reserved. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | +# This script gives a print out of the differences between the two specified spec |
| 18 | +# versions and a description of the provisional elements in the later version. |
| 19 | +# Right now, this is just in print form. The intent is to use this for new |
| 20 | +# data model XML drops to show the differences. This was also used to double-check |
| 21 | +# spec expectations before the 1.4 release and we should continue to do so going forward. |
| 22 | + |
| 23 | +import click |
| 24 | +from chip.testing.conformance import ConformanceDecision |
| 25 | +from chip.testing.spec_parsing import PrebuiltDataModelDirectory, build_xml_clusters, build_xml_device_types |
| 26 | + |
| 27 | + |
| 28 | +def get_changes(old, new): |
| 29 | + added = [e.name for id, e in new.items() if id not in old.keys()] |
| 30 | + removed = [e.name for id, e in old.items() if id not in new.keys()] |
| 31 | + same_ids = set(new.keys()).intersection(set(old.keys())) |
| 32 | + |
| 33 | + return added, removed, same_ids |
| 34 | + |
| 35 | + |
| 36 | +def str_changes(element, added, removed, change_ids, old, new): |
| 37 | + if not added and not removed and not change_ids: |
| 38 | + return [] |
| 39 | + |
| 40 | + ret = [] |
| 41 | + if added: |
| 42 | + ret.append(f'\t{element} added: {added}') |
| 43 | + if removed: |
| 44 | + ret.append(f'\t{element} removed: {removed}') |
| 45 | + if change_ids: |
| 46 | + ret.append(f'\t{element} changed:') |
| 47 | + for id in change_ids: |
| 48 | + name = old[id].name if old[id].name == new[id].name else f'{new[id].name} (previously {old[id].name})' |
| 49 | + ret.append(f'\t\t{name}') |
| 50 | + ret.append(f'\t\t\t{old[id]}') |
| 51 | + ret.append(f'\t\t\t{new[id]}') |
| 52 | + return ret |
| 53 | + |
| 54 | + |
| 55 | +def str_element_changes(element, old, new): |
| 56 | + added, removed, same_ids = get_changes(old, new) |
| 57 | + change_ids = [id for id in same_ids if old[id] != new[id] or str(old[id].conformance) != str(new[id].conformance)] |
| 58 | + return str_changes(element, added, removed, change_ids, old, new) |
| 59 | + |
| 60 | + |
| 61 | +def diff_clusters(prior_revision: PrebuiltDataModelDirectory, new_revision: PrebuiltDataModelDirectory) -> None: |
| 62 | + prior_clusters, _ = build_xml_clusters(PrebuiltDataModelDirectory.k1_3) |
| 63 | + new_clusters, _ = build_xml_clusters(PrebuiltDataModelDirectory.k1_4) |
| 64 | + |
| 65 | + additional_clusters, removed_clusters, same_cluster_ids = get_changes(prior_clusters, new_clusters) |
| 66 | + |
| 67 | + print(f'\n\nClusters newly added in {new_revision.dirname}') |
| 68 | + print(additional_clusters) |
| 69 | + print(f'\n\nClusters removed since {prior_revision.dirname}') |
| 70 | + print(removed_clusters) |
| 71 | + |
| 72 | + for cid in same_cluster_ids: |
| 73 | + new = new_clusters[cid] |
| 74 | + old = prior_clusters[cid] |
| 75 | + |
| 76 | + name = old.name if old.name == new.name else f'{new.name} (previously {old.name})' |
| 77 | + |
| 78 | + changes = [] |
| 79 | + if old.revision != new.revision: |
| 80 | + changes.append(f'\tRevision change - old: {old.revision} new: {new.revision}') |
| 81 | + changes.extend(str_element_changes('Features', old.features, new.features)) |
| 82 | + changes.extend(str_element_changes('Attributes', old.attributes, new.attributes)) |
| 83 | + changes.extend(str_element_changes('Accepted Commands', old.accepted_commands, new.accepted_commands)) |
| 84 | + changes.extend(str_element_changes('Generated Commands', old.generated_commands, new.generated_commands)) |
| 85 | + changes.extend(str_element_changes('Events', old.events, new.events)) |
| 86 | + |
| 87 | + if changes: |
| 88 | + print(f'\n\nCluster {name}') |
| 89 | + print('\n'.join(changes)) |
| 90 | + |
| 91 | + |
| 92 | +def diff_device_types(prior_revision: PrebuiltDataModelDirectory, new_revision: PrebuiltDataModelDirectory) -> None: |
| 93 | + prior_device_types, _ = build_xml_device_types(prior_revision) |
| 94 | + new_device_types, _ = build_xml_device_types(new_revision) |
| 95 | + |
| 96 | + additional_device_types, removed_device_types, same_device_type_ids = get_changes(prior_device_types, new_device_types) |
| 97 | + |
| 98 | + print(f'\n\nDevice Types newly added in {new_revision.dirname}') |
| 99 | + print(additional_device_types) |
| 100 | + print(f'\n\nDevice Types removed since {prior_revision.dirname}') |
| 101 | + print(removed_device_types) |
| 102 | + |
| 103 | + for cid in same_device_type_ids: |
| 104 | + new = new_device_types[cid] |
| 105 | + old = prior_device_types[cid] |
| 106 | + |
| 107 | + name = old.name if old.name == new.name else f'{new.name} (previously {old.name})' |
| 108 | + |
| 109 | + changes = [] |
| 110 | + if old.revision != new.revision: |
| 111 | + changes.append(f'\tRevision change - old: {old.revision} new: {new.revision}') |
| 112 | + changes.extend(str_element_changes('Server Clusters', old.server_clusters, new.server_clusters)) |
| 113 | + changes.extend(str_element_changes('Client Clusters', old.client_clusters, new.client_clusters)) |
| 114 | + |
| 115 | + if changes: |
| 116 | + print(f'\n\nDevice Type {name}') |
| 117 | + print('\n'.join(changes)) |
| 118 | + |
| 119 | + |
| 120 | +def _get_provisional(items): |
| 121 | + return [e.name for e in items if e.conformance(0, [], []).decision == ConformanceDecision.PROVISIONAL] |
| 122 | + |
| 123 | + |
| 124 | +def get_all_provisional_clusters(new_revision: PrebuiltDataModelDirectory): |
| 125 | + clusters, _ = build_xml_clusters(new_revision) |
| 126 | + |
| 127 | + provisional_clusters = [c.name for c in clusters.values() if c.is_provisional] |
| 128 | + print('\n\nProvisional Clusters') |
| 129 | + print(f'\t{sorted(provisional_clusters)}') |
| 130 | + |
| 131 | + for c in clusters.values(): |
| 132 | + features = _get_provisional(c.features.values()) |
| 133 | + attributes = _get_provisional(c.attributes.values()) |
| 134 | + accepted_commands = _get_provisional(c.accepted_commands.values()) |
| 135 | + generated_commands = _get_provisional(c.generated_commands.values()) |
| 136 | + events = _get_provisional(c.events.values()) |
| 137 | + |
| 138 | + if not features and not attributes and not accepted_commands and not generated_commands and not events: |
| 139 | + continue |
| 140 | + |
| 141 | + print(f'\n{c.name}') |
| 142 | + if features: |
| 143 | + print(f'\tProvisional features: {features}') |
| 144 | + if attributes: |
| 145 | + print(f'\tProvisional attributes: {attributes}') |
| 146 | + if accepted_commands: |
| 147 | + print(f'\tProvisional accepted commands: {accepted_commands}') |
| 148 | + if generated_commands: |
| 149 | + print(f'\tProvisional generated commands: {generated_commands}') |
| 150 | + if events: |
| 151 | + print(f'\tProvisional events: {events}') |
| 152 | + |
| 153 | + |
| 154 | +def get_all_provisional_device_types(new_revision: PrebuiltDataModelDirectory): |
| 155 | + device_types, _ = build_xml_device_types(new_revision) |
| 156 | + |
| 157 | + for d in device_types.values(): |
| 158 | + server_clusters = _get_provisional(d.server_clusters.values()) |
| 159 | + client_clusters = _get_provisional(d.client_clusters.values()) |
| 160 | + if not server_clusters and not client_clusters: |
| 161 | + continue |
| 162 | + |
| 163 | + print(f'\n{d.name}') |
| 164 | + if server_clusters: |
| 165 | + print(f'\tProvisional server clusters: {server_clusters}') |
| 166 | + if client_clusters: |
| 167 | + print(f'\tProvisional client clusters: {client_clusters}') |
| 168 | + |
| 169 | + |
| 170 | +REVISIONS = {'1.3': PrebuiltDataModelDirectory.k1_3, |
| 171 | + '1.4': PrebuiltDataModelDirectory.k1_4, 'master': PrebuiltDataModelDirectory.kMaster} |
| 172 | + |
| 173 | + |
| 174 | +@click.command() |
| 175 | +@click.argument('prior_revision', type=click.Choice(list(REVISIONS.keys()))) |
| 176 | +@click.argument('new_revision', type=click.Choice(list(REVISIONS.keys()))) |
| 177 | +def main(prior_revision: str, new_revision: str): |
| 178 | + diff_clusters(REVISIONS[prior_revision], REVISIONS[new_revision]) |
| 179 | + diff_device_types(REVISIONS[prior_revision], REVISIONS[new_revision]) |
| 180 | + get_all_provisional_clusters(REVISIONS[new_revision]) |
| 181 | + get_all_provisional_device_types(REVISIONS[new_revision]) |
| 182 | + |
| 183 | + |
| 184 | +if __name__ == "__main__": |
| 185 | + main() |
0 commit comments