xref: /aosp_15_r20/external/grpc-grpc/tools/run_tests/sanity/check_version.py (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1#!/usr/bin/env python3
2
3# Copyright 2016 gRPC 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
17import os
18import re
19import subprocess
20import sys
21
22import yaml
23
24errors = 0
25
26os.chdir(os.path.join(os.path.dirname(sys.argv[0]), "../../.."))
27
28# hack import paths to pick up extra code
29sys.path.insert(0, os.path.abspath("tools/buildgen/plugins"))
30from expand_version import Version
31
32try:
33    branch_name = subprocess.check_output(
34        "git rev-parse --abbrev-ref HEAD", shell=True
35    ).decode()
36except:
37    print("WARNING: not a git repository")
38    branch_name = None
39
40if branch_name is not None:
41    m = re.match(r"^release-([0-9]+)_([0-9]+)$", branch_name)
42    if m:
43        print("RELEASE branch")
44        # version number should align with the branched version
45        check_version = lambda version: (
46            version.major == int(m.group(1))
47            and version.minor == int(m.group(2))
48        )
49        warning = (
50            'Version key "%%s" value "%%s" should have a major version %s and'
51            " minor version %s" % (m.group(1), m.group(2))
52        )
53    elif re.match(r"^debian/.*$", branch_name):
54        # no additional version checks for debian branches
55        check_version = lambda version: True
56    else:
57        # all other branches should have a -dev tag
58        check_version = lambda version: version.tag == "dev"
59        warning = 'Version key "%s" value "%s" should have a -dev tag'
60else:
61    check_version = lambda version: True
62
63with open("build_handwritten.yaml", "r") as f:
64    build_yaml = yaml.safe_load(f.read())
65
66settings = build_yaml["settings"]
67
68top_version = Version(settings["version"])
69if not check_version(top_version):
70    errors += 1
71    print((warning % ("version", top_version)))
72
73for tag, value in list(settings.items()):
74    if re.match(r"^[a-z]+_version$", tag):
75        value = Version(value)
76        if tag != "core_version":
77            if value.major != top_version.major:
78                errors += 1
79                print(
80                    "major version mismatch on %s: %d vs %d"
81                    % (tag, value.major, top_version.major)
82                )
83            if value.minor != top_version.minor:
84                errors += 1
85                print(
86                    "minor version mismatch on %s: %d vs %d"
87                    % (tag, value.minor, top_version.minor)
88                )
89        if not check_version(value):
90            errors += 1
91            print((warning % (tag, value)))
92
93sys.exit(errors)
94