def check_if_tag_exists()

in appengine/check_if_image_tag_exists/main.py [0:0]


def check_if_tag_exists(raw_image_path, force_build):
    # extract both path to image, and the tag if provided
    image_parts = raw_image_path.split(':')
    image_path = image_parts[0]
    if len(image_parts) == 2:
        image_tag = image_parts[1]
    else:
        image_tag = 'latest'

    p = subprocess.Popen(["gcloud container images list-tags "
                          + "--format='value(tags)' --no-show-occurrences {0}"
                          .format(image_path)],
                         shell=True, stdout=subprocess.PIPE,
                         stderr=subprocess.STDOUT)

    output, error = p.communicate()
    if p.returncode != 0:
        sys.exit('Error encountered when retrieving existing image tags! '
                 + 'Full log: \n\n' + output)

    existing_tags = set(tag.rstrip() for tag in output.split('\n'))
    print 'Existing tags for image {0}:'.format(image_path)
    for tag in existing_tags:
        print tag

    if image_tag in existing_tags:
        print "Tag '{0}' already exists in remote repository!" \
              .format(image_tag)
        if not force_build:
            sys.exit('Exiting build.')
        else:
            print "Forcing build. Tag '{0}' " \
                  "will be overwritten!".format(image_tag)
            return
    print "Tag '{0}' does not exist in remote repository! " \
          "Continuing with build.".format(image_tag)