# Path: mcloud/util.py
# class Interface(object):
#     __metaclass__ = ABCMeta


from StringIO import StringIO
from tempfile import mkdtemp
from abc import abstractmethod
from mcloud.util import Interface
from twisted.internet import reactor, defer
from twisted.python import log
import logging
import tarfile
class PrebuiltImageBuilder(IImageBuilder):
    def __init__(self, image):
        super(PrebuiltImageBuilder, self).__init__()

        self.image = image

    @defer.inlineCallbacks
    def build_image(self, ticket_id, service):

        log.msg('[%s] Building image "%s".', ticket_id, self.image)

        name = self.image
        tag = None
        if ':' in name:
            name, tag = name.split(':')

        images = yield service.client.images(name=name)

        if tag:
            images = [x for x in images if self.image in x['RepoTags']]

        if not images:
            log.msg('[%s] Image is not there. Pulling "%s" ...', ticket_id, self.image)

            yield service.client.pull(name, ticket_id, tag)

        log.msg('[%s] Image "%s" is ready to use.', ticket_id, self.image)
        defer.returnValue(self.image)



# Path: mcloud/util.py
# class Interface(object):
#     __metaclass__ = ABCMeta


# Path: mcloud/container.py
from StringIO import StringIO
from tempfile import mkdtemp
from abc import abstractmethod
from mcloud.util import Interface
from twisted.internet import reactor, defer
from twisted.python import log
import logging
import tarfile

class PrebuiltImageBuilder(IImageBuilder):
    def __init__(self, image):
        super(PrebuiltImageBuilder, self).__init__()

        self.image = image

    @defer.inlineCallbacks
    def build_image(self, ticket_id, service):

        log.msg('[%s] Building image "%s".', ticket_id, self.image)

        name = self.image
        tag = None
        if ':' in name:
            name, tag = name.split(':')

        images = yield service.client.images(name=name)

        if tag:
            images = [x for x in images if self.image in x['RepoTags']]

        if not images:
            log.msg('[%s] Image is not there. Pulling "%s" ...', ticket_id, self.image)

            yield service.client.pull(name, ticket_id, tag)

        log.msg('[%s] Image "%s" is ready to use.', ticket_id, self.image)
        defer.returnValue(self.image)



class CanNotAccessPath(Exception):