# Path: subuserlib/classes/userOwnedObject.py
# class UserOwnedObject(object):
#   def __init__(self,user):
#     self.user = user
# 
# Path: subuserlib/classes/uhttpConnection.py
# class UHTTPConnection(httplib.HTTPConnection):
#   """
#   Subclass of Python library HTTPConnection that uses a unix-domain socket.
#   """
#   def __init__(self, path):
#     httplib.HTTPConnection.__init__(self, 'localhost')
#     self.path = path
# 
#   def connect(self):
#     sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
#     sock.connect(self.path)
#     self.sock = sock
# 
# Path: subuserlib/classes/docker/container.py
# class Container(UserOwnedObject):
#   def __init__(self,user,containerId):
#     self.id = containerId
#     UserOwnedObject.__init__(self,user)
# 
#   def inspect(self):
#     """
#      Returns a dictionary of container properties.
#      If the container no longer exists, return None.
#     """
#     self.user.dockerDaemon.getConnection().request("GET","/v1.13/containers/"+self.id+"/json")
#     response = self.user.dockerDaemon.getConnection().getresponse()
#     if not response.status == 200:
#       response.read() # Read the response and discard it to prevent the server from getting locked up: https://stackoverflow.com/questions/3231543/python-httplib-responsenotready
#       return None
#     else:
#       return json.loads(response.read().decode("utf-8"))
# 
#   def stop(self):
#     self.user.dockerDaemon.getConnection().request("POST","/v1.13/containers/"+self.id+"/stop")
#     response = self.user.dockerDaemon.getConnection().getresponse()
#     response.read()
# 
#   def remove(self,force=False):
#     queryParameters =  {
#       'force': force
#       }
#     try:
#       queryParametersString = urllib.urlencode(queryParameters)
#     except AttributeError:
#       queryParametersString = urllib.parse.urlencode(queryParameters) # Python 3
#     self.user.dockerDaemon.getConnection().request("DELETE","/v1.13/containers/"+self.id+"?"+queryParametersString)
#     response = self.user.dockerDaemon.getConnection().getresponse()
#     response.read()


import urllib
import tarfile
import os
import tempfile
import fnmatch
import re
import json
import sys
  import httplib
  import http.client
  import StringIO
  import io
import subuserlib.docker
import subuserlib.test
import subuserlib.classes.exceptions as exceptions
from subuserlib.classes.userOwnedObject import UserOwnedObject
from subuserlib.classes.uhttpConnection import UHTTPConnection
from subuserlib.classes.docker.container import Container
  from subuserlib.classes.docker.mockDockerDaemon import MockDockerDaemon
  output = b''
  byte = response.read(1)
  while byte:
    jsonSegmentBytes += byte
    output += byte
    byte = response.read(1)
    try:
      lineDict = json.loads(jsonSegmentBytes.decode("utf-8"))
      if lineDict == {}:
        pass
      elif "stream" in lineDict:
        user.registry.log(lineDict["stream"])
      elif "status" in lineDict:
        user.registry.log(lineDict["status"])
      elif "errorDetail" in lineDict:
        raise exceptions.ImageBuildException("Build error:"+lineDict["errorDetail"]["message"]+"\n"+response.read().decode())
      else:
        raise exceptions.ImageBuildException("Build error:"+jsonSegmentBytes.decode("utf-8")+"\n"+response.read().decode("utf-8"))
      jsonSegmentBytes = b''
    except ValueError:
      pass
  output = output.decode("utf-8")
  if not output.strip().startswith("{"):
    user.registry.log(output)
  return output

class DockerDaemon(UserOwnedObject):
  def __init__(self,user):
    self.__connection = None
    self.__imagePropertiesCache = {}

# Path: subuserlib/classes/userOwnedObject.py
# class UserOwnedObject(object):
#   def __init__(self,user):
#     self.user = user
# 
# Path: subuserlib/classes/uhttpConnection.py
# class UHTTPConnection(httplib.HTTPConnection):
#   """
#   Subclass of Python library HTTPConnection that uses a unix-domain socket.
#   """
#   def __init__(self, path):
#     httplib.HTTPConnection.__init__(self, 'localhost')
#     self.path = path
# 
#   def connect(self):
#     sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
#     sock.connect(self.path)
#     self.sock = sock
# 
# Path: subuserlib/classes/docker/container.py
# class Container(UserOwnedObject):
#   def __init__(self,user,containerId):
#     self.id = containerId
#     UserOwnedObject.__init__(self,user)
# 
#   def inspect(self):
#     """
#      Returns a dictionary of container properties.
#      If the container no longer exists, return None.
#     """
#     self.user.dockerDaemon.getConnection().request("GET","/v1.13/containers/"+self.id+"/json")
#     response = self.user.dockerDaemon.getConnection().getresponse()
#     if not response.status == 200:
#       response.read() # Read the response and discard it to prevent the server from getting locked up: https://stackoverflow.com/questions/3231543/python-httplib-responsenotready
#       return None
#     else:
#       return json.loads(response.read().decode("utf-8"))
# 
#   def stop(self):
#     self.user.dockerDaemon.getConnection().request("POST","/v1.13/containers/"+self.id+"/stop")
#     response = self.user.dockerDaemon.getConnection().getresponse()
#     response.read()
# 
#   def remove(self,force=False):
#     queryParameters =  {
#       'force': force
#       }
#     try:
#       queryParametersString = urllib.urlencode(queryParameters)
#     except AttributeError:
#       queryParametersString = urllib.parse.urlencode(queryParameters) # Python 3
#     self.user.dockerDaemon.getConnection().request("DELETE","/v1.13/containers/"+self.id+"?"+queryParametersString)
#     response = self.user.dockerDaemon.getConnection().getresponse()
#     response.read()


# Path: subuserlib/classes/docker/dockerDaemon.py
import urllib
import tarfile
import os
import tempfile
import fnmatch
import re
import json
import sys
  import httplib
  import http.client
  import StringIO
  import io
import subuserlib.docker
import subuserlib.test
import subuserlib.classes.exceptions as exceptions
from subuserlib.classes.userOwnedObject import UserOwnedObject
from subuserlib.classes.uhttpConnection import UHTTPConnection
from subuserlib.classes.docker.container import Container
  from subuserlib.classes.docker.mockDockerDaemon import MockDockerDaemon

  output = b''
  byte = response.read(1)
  while byte:
    jsonSegmentBytes += byte
    output += byte
    byte = response.read(1)
    try:
      lineDict = json.loads(jsonSegmentBytes.decode("utf-8"))
      if lineDict == {}:
        pass
      elif "stream" in lineDict:
        user.registry.log(lineDict["stream"])
      elif "status" in lineDict:
        user.registry.log(lineDict["status"])
      elif "errorDetail" in lineDict:
        raise exceptions.ImageBuildException("Build error:"+lineDict["errorDetail"]["message"]+"\n"+response.read().decode())
      else:
        raise exceptions.ImageBuildException("Build error:"+jsonSegmentBytes.decode("utf-8")+"\n"+response.read().decode("utf-8"))
      jsonSegmentBytes = b''
    except ValueError:
      pass
  output = output.decode("utf-8")
  if not output.strip().startswith("{"):
    user.registry.log(output)
  return output

class DockerDaemon(UserOwnedObject):
  def __init__(self,user):
    self.__connection = None
    self.__imagePropertiesCache = {}

    UserOwnedObject.__init__(self,user)