old_contents: #!/usr/bin/env python3
  # Copyright 2021 The LUCI Authors. All rights reserved.
  # Use of this source code is governed under the Apache License, Version 2.0
  # that can be found in the LICENSE file.
  """Updates and compiles proto files needed to use buildbucket/proto.
  
  Proto files are copied over from:
  https://chromium.googlesource.com/infra/luci/luci-go/+/refs/heads/main
  
  See SUB_PATHS for the specific list of protos.
  This script only compiles buildbucket/proto protos.
  Other protos are needed because they're imported by buildbucket/proto files.
  """
  
  from __future__ import print_function
  
  import json
  import os
  import re
  import subprocess
  import tarfile
  import urllib.request
  
  BASE_URL = 'https://chromium.googlesource.com/infra/luci/luci-go'
  LOG_URL = BASE_URL + '/+log/main/%s?format=JSON&n=1'
  TAR_URL = BASE_URL + '/+archive/%s/%s.tar.gz'
  
  SUB_PATHS = [
      'buildbucket/proto',
      'common/proto',
      'common/bq/pb',
      'resultdb/proto/v1',
      'swarming/proto/api',
  ]
  
  
  def main():
    """Updates all .proto files and compiles buildbucket/proto/*.proto."""
  
    base = os.path.dirname(__file__)
    # All protos in SUB_PATHS expect imports live in go.chormium.org/luci.
    base_dir = os.path.join(base, "go.chromium.org/luci/")
  
    for sub in SUB_PATHS:
      sub_dir = os.path.join(base_dir, os.path.normpath(sub))
      if not os.path.exists(sub_dir):
        os.makedirs(sub_dir)
  
      resp = urllib.request.urlopen(LOG_URL % sub)
      html = resp.read()
  
      commit = json.loads(html[4:])['log'][0]['commit']
      print('Updating %r to %r' % (sub, commit))
  
      resp = urllib.request.urlopen(TAR_URL % (commit, sub))
      with tarfile.open(mode='r|*', fileobj=resp) as tar:
        for item in tar:
          if item.name.endswith('.proto'):
            print('Extracting %r' % item.name)
            tar.extract(item, sub_dir)
  
      with open(sub_dir + '/README.md', 'w') as rmd:
        print('// Generated by update_taskbackend_protos.py. DO NOT EDIT.',
              file=rmd)
        print('These protos were copied from:', file=rmd)
        print(BASE_URL + '/+/' + commit + '/' + sub, file=rmd)
  
    subprocess.run([
        os.path.join(base, "../../../components/tools/compile_proto.py"),
        "--proto_path", base,
        "go.chromium.org/luci/buildbucket/proto/"])
  
    print('Done.')
  
  
  if __name__ == '__main__':
    main()
  
new_contents: #!/usr/bin/env python3
  # Copyright 2021 The LUCI Authors. All rights reserved.
  # Use of this source code is governed under the Apache License, Version 2.0
  # that can be found in the LICENSE file.
  """Updates and compiles proto files needed to use buildbucket/proto.
  
  Proto files are copied over from:
  https://chromium.googlesource.com/infra/luci/luci-go/+/refs/heads/main
  
  See SUB_PATHS for the specific list of protos.
  This script only compiles buildbucket/proto protos.
  Other protos are needed because they're imported by buildbucket/proto files.
  """
  
  from __future__ import print_function
  
  import json
  import os
  import re
  import subprocess
  import tarfile
  import urllib.request
  
  BASE_URL = 'https://chromium.googlesource.com/infra/luci/luci-go'
  LOG_URL = BASE_URL + '/+log/main/%s?format=JSON&n=1'
  TAR_URL = BASE_URL + '/+archive/%s/%s.tar.gz'
  
  SUB_PATHS = [
      'buildbucket/proto',
      'common/proto',
      'common/bq/pb',
      'resultdb/proto/v1',
      'swarming/proto/api',
  ]
  
  
  def main():
    """Updates all .proto files and compiles buildbucket/proto/*.proto."""
  
    base = os.path.dirname(__file__)
    # All protos in SUB_PATHS expect imports live in go.chormium.org/luci.
    base_dir = os.path.join(base, "go.chromium.org/luci/")
  
    for sub in SUB_PATHS:
      sub_dir = os.path.join(base_dir, os.path.normpath(sub))
      if not os.path.exists(sub_dir):
        os.makedirs(sub_dir)
  
      resp = urllib.request.urlopen(LOG_URL % sub)
      html = resp.read()
  
      commit = json.loads(html[4:])['log'][0]['commit']
      print('Updating %r to %r' % (sub, commit))
  
      resp = urllib.request.urlopen(TAR_URL % (commit, sub))
      with tarfile.open(mode='r|*', fileobj=resp) as tar:
        for item in tar:
          if item.name.endswith('.proto'):
            print('Extracting %r' % item.name)
            tar.extract(item, sub_dir)
  
      with open(sub_dir + '/README.md', 'w') as rmd:
        print('// Generated by update_taskbackend_protos.py. DO NOT EDIT.',
              file=rmd)
        print('These protos were copied from:', file=rmd)
        print(BASE_URL + '/+/' + commit + '/' + sub, file=rmd)
  
    subprocess.run([
        os.path.join(base, "../../../components/tools/compile_proto.py"),
        "--proto_path", base, "go.chromium.org/luci/buildbucket/proto/"
    ],
                   check=True)
  
    print('Done.')
  
  
  if __name__ == '__main__':
    main()
  
subject: Add check=True when calling compile_proto.