go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/scripts/check_googleapis_in_sync.py (about)

     1  #!/usr/bin/env python3
     2  # Copyright 2020 The LUCI Authors.
     3  
     4  # Licensed under the Apache License, Version 2.0 (the "License");
     5  # you may not use this file except in compliance with the License.
     6  # You may obtain a copy of the License at
     7  
     8  #      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  # Unless required by applicable law or agreed to in writing, software
    11  # distributed under the License is distributed on an "AS IS" BASIS,
    12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  # See the License for the specific language governing permissions and
    14  # limitations under the License.
    15  import os
    16  import shutil
    17  import subprocess
    18  import sys
    19  
    20  
    21  def main(args):
    22    if len(args) != 1:
    23      print('Want 1 argument: a path to a directory with go.mod')
    24      return 1
    25    os.chdir(args[0])
    26  
    27    # Find versions of google.golang.org/genproto[/*] modules in go.mod.
    28    genproto_versions = set()
    29    with open("go.mod",'r') as f:
    30      for line in f:
    31        if "google.golang.org/genproto" in line:
    32          genproto_versions.add(line.split(" ")[1].strip())
    33    if not genproto_versions:
    34      print("No appropriate google.golang.org/genproto version found in go.sum")
    35      return 1
    36    if len(genproto_versions) > 1:
    37      print(
    38        "All google.golang.org/genproto[/*] modules in go.mod must be at the "
    39        "same version since we import all their protos from the single revision"
    40      )
    41      return 1
    42    genproto_version = list(genproto_versions)[0]
    43  
    44    # Find version of google.golang.org/genproto that was used to
    45    # last update googleapis.
    46    googleapis_path = os.path.join(
    47      "common",
    48      "proto",
    49      "googleapis",
    50      "google",
    51      "GENPROTO_REGEN"
    52    )
    53    googleapis_commit_from_googleapis = open(googleapis_path,'r').read().strip()
    54  
    55    if genproto_version != googleapis_commit_from_googleapis:
    56      print(
    57        "googleapis proto version (%s) is out of sync with the version used by "
    58        "google.golang.org/genproto modules (%s) in go.mod. Please update with "
    59        "the import script at common/proto/googleapis/import.sh"
    60        % (googleapis_commit_from_googleapis, genproto_version)
    61      )
    62      return 1
    63    return 0
    64  
    65  
    66  if __name__ == '__main__':
    67    sys.exit(main(sys.argv[1:]))