|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# Copyright (c) 2024 Project CHIP Authors |
| 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 | +import os |
| 17 | +import subprocess |
| 18 | +import sys |
| 19 | + |
| 20 | +import click |
| 21 | + |
| 22 | + |
| 23 | +@click.command() |
| 24 | +@click.argument('dir', nargs=1, type=click.Path(exists=True)) |
| 25 | +def check_dm_directory(dir): |
| 26 | + clusters = os.path.join(dir, 'clusters') |
| 27 | + device_types = os.path.join(dir, 'device_types') |
| 28 | + if not os.path.isdir(clusters) or not os.path.isdir(device_types): |
| 29 | + print(f"Invalid data model directory {dir}") |
| 30 | + sys.exit(1) |
| 31 | + |
| 32 | + # We are operating in a VM, and although there is a checkout, it is working in a scratch directory |
| 33 | + # where the ownership is different than the runner. |
| 34 | + # Adding an exception for this directory so that git can function properly. |
| 35 | + subprocess.run("git config --global --add safe.directory '*'", shell=True) |
| 36 | + |
| 37 | + def check_dir(dir): |
| 38 | + cmd = f'git diff HEAD^..HEAD --name-only -- {dir}' |
| 39 | + output = subprocess.check_output(cmd, shell=True).decode().splitlines() |
| 40 | + if output and 'spec_sha' not in output and 'scraper_version' not in output: |
| 41 | + print(f'Data model directory {dir} had changes to the following files without a corresponding update to the spec SHA') |
| 42 | + print(output) |
| 43 | + print("Note that the data_model directory files are automatically updated by a spec scraper and should not be manually updated.") |
| 44 | + return 1 |
| 45 | + return 0 |
| 46 | + |
| 47 | + ret = check_dir(clusters) + check_dir(device_types) |
| 48 | + sys.exit(ret) |
| 49 | + |
| 50 | + |
| 51 | +if __name__ == '__main__': |
| 52 | + check_dm_directory() |
0 commit comments