from cidonkey.cid.common import KnownError
from django.conf import settings
import docker
import io
import json
import dateutil.parser


def _get_con():
    return docker.Client(base_url='unix://var/run/docker.sock', version='1.12', timeout=10)


def build_image(docker_file, image_name='cidonkey'):
    df = io.BytesIO(docker_file)
    b = _get_con().build(fileobj=df, tag=image_name)
    results = json.loads('[' + ','.join(b) + ']')
    return ''.join([str(r.values()[0]) for r in results])


def start_ci(image_name, src_dir):
    c = _get_con()
    binds = {
        settings.PERSISTENCE_DIR: {'bind': '/persistence/', 'ro': False},
        src_dir: {'bind': '/src/', 'ro': True}
    }
    volumes = [b['bind'] for b in binds.values()]
    con = c.create_container(image=image_name, volumes=volumes)
    if con['Warnings']:
        print 'Warning:', con['Warnings']
    c.start(con, binds=binds)
    return con['Id']


def check_progress(con_id):
    c = _get_con()
    con = c.inspect_container(con_id)
    state = con['State']
    if state['Running']:
        return
    exit_code = state['ExitCode']
    finish_str = state['FinishedAt']
    finished = dateutil.parser.parse(finish_str)
    logs = c.logs(con_id)
    return exit_code, finished, logs

@@ -1209,24 +1209,55 @@
 ogs(con_id)%0A
+    c.remove_container(con_id)%0A
     return e

remove containers afer ci