#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
"""
Builds a Docker image for a project.

Usage:
  build_docker_image.py --project=<PROJECT> [--file=<FILE>] [--variant=<VARIANT>]
  build_docker_image.py -h | --help

Options:
  -h --help                Show this screen
  --project=<PROJECT>      Name of the Docker image to build
  --file=<FILE>            Path to the Dockerfile (if not in docker/<PROJECT>)
  --variant=<VARIANT>      The optional variant of this project (e.g. nginx)
                           Relies on the Dockerfile accepting a variant ARG

"""

import os
import subprocess

import docopt

from tooling import write_release_id, CURRENT_COMMIT, ROOT


if __name__ == '__main__':
    args = docopt.docopt(__doc__)

    project = args['--project']
    variant = args['--variant']
    if args['--file']:
        dockerfile = os.path.join(ROOT, args['--file'])
    else:
        dockerfile = os.path.join(ROOT, 'docker', project, 'Dockerfile')

    print('*** Building image for %s' % project)

    release_id = CURRENT_COMMIT

    if variant is not None:
        tag = '%s_%s:%s' % (project, variant, release_id)
        release_name = '%s_%s' % (project, variant)
    else:
        tag = '%s:%s' % (project, release_id)
        release_name = project

    print('*** Image will be tagged %s' % tag)

    print('*** Building the new image')

    cmd = ['docker', 'build', '--file', dockerfile, '--tag', tag]
    if variant is not None:
        cmd.extend(['--build-arg', 'variant=%s' % variant])
    cmd.append(os.path.dirname(dockerfile))

    subprocess.check_call(cmd)

    print('*** Saving the release ID to .releases')
    write_release_id(project=release_name, release_id=release_id)

@@ -1071,27 +1071,36 @@
 ne:%0A        
-tag
+release_name
  = '%25s_%25s:%25s
@@ -1096,19 +1096,16 @@
 = '%25s_%25s
-:%25s
 ' %25 (pro
@@ -1121,72 +1121,59 @@
 iant
-, release_id)%0A        release_name
+)%0A        tag
  = '%25s
-_
+:
 %25s' %25 (
-project, variant
+release_name, release_id
 )%0A  
@@ -1408,19 +1408,28 @@
 --tag', 
-tag
+release_name
 %5D%0A    if
@@ -1554,17 +1554,16 @@
 rfile))%0A
-%0A
     subp
@@ -1586,16 +1586,81 @@
 l(cmd)%0A%0A
+    subprocess.check_call(%5B'docker', 'tag', release_name, tag%5D)%0A%0A
     prin

Tag images with their Git commit *and* latest, to allow inheritance