summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDeni Bertovic <deni@kset.org>2013-10-03 11:47:22 +0200
committerDeni Bertovic <deni@kset.org>2013-10-03 11:55:02 +0200
commit1ce8dc962c71251110fc465286a665723754e6cb (patch)
treeb67f9ad162f66332324273260db9088294b990f8
parent7fbc41ea7f80a66085923ecbd6815f2712586b4f (diff)
downloaddocker-py-1ce8dc962c71251110fc465286a665723754e6cb.tar.gz
python3 fixes
Fixed imports to be compatible with python3 on various places in the client lib and in the tests themselves.
-rw-r--r--docker/__init__.py1
-rw-r--r--docker/auth.py4
-rw-r--r--docker/client.py39
-rw-r--r--docker/unixconn.py6
-rw-r--r--tests/test.py66
5 files changed, 61 insertions, 55 deletions
diff --git a/docker/__init__.py b/docker/__init__.py
index 3c6a8b4..96338bb 100644
--- a/docker/__init__.py
+++ b/docker/__init__.py
@@ -13,4 +13,3 @@
# limitations under the License.
from .client import Client, APIError
-import auth
diff --git a/docker/auth.py b/docker/auth.py
index aab9603..7467aaf 100644
--- a/docker/auth.py
+++ b/docker/auth.py
@@ -18,7 +18,7 @@ import os
import six
-import utils
+from .utils import ping
INDEX_URL = 'https://index.docker.io/v1/'
@@ -35,7 +35,7 @@ def expand_registry_url(hostname):
if '/' not in hostname[9:]:
hostname = hostname + '/v1/'
return hostname
- if utils.ping('https://' + hostname + '_ping'):
+ if ping('https://' + hostname + '_ping'):
return 'https://' + hostname + '/v1/'
return 'http://' + hostname + '/v1/'
diff --git a/docker/client.py b/docker/client.py
index 10f44fb..ece7581 100644
--- a/docker/client.py
+++ b/docker/client.py
@@ -20,9 +20,11 @@ import requests
import requests.exceptions
import six
-import auth
-import unixconn
-import utils
+from .auth import (load_config, resolve_repository_name,
+ resolve_authconfig, encode_header)
+
+from .unixconn import UnixAdapter
+from .utils import tar, compare_version, mkbuildcontext
class APIError(requests.exceptions.HTTPError):
@@ -60,11 +62,11 @@ class APIError(requests.exceptions.HTTPError):
class Client(requests.Session):
def __init__(self, base_url="unix://var/run/docker.sock", version="1.4"):
super(Client, self).__init__()
- self.mount('unix://', unixconn.UnixAdapter(base_url))
+ self.mount('unix://', UnixAdapter(base_url))
self.base_url = base_url
self._version = version
try:
- self._cfg = auth.load_config()
+ self._cfg = load_config()
except Exception:
pass
@@ -172,12 +174,12 @@ class Client(requests.Session):
raise Exception("Either path or fileobj needs to be provided.")
if fileobj is not None:
- context = utils.mkbuildcontext(fileobj)
+ context = mkbuildcontext(fileobj)
elif (path.startswith('http://') or path.startswith('https://') or
path.startswith('git://') or path.startswith('github.com/')):
remote = path
else:
- context = utils.tar(path)
+ context = tar(path)
u = self._url('/build')
params = { 't': tag, 'remote': remote, 'q': quiet, 'nocache': nocache, 'rm': rm }
@@ -336,7 +338,7 @@ class Client(requests.Session):
registry = auth.INDEX_URL
if getattr(self, '_cfg', None) is None:
self._cfg = auth.load_config()
- authcfg = auth.resolve_authconfig(self._cfg, registry)
+ authcfg = resolve_authconfig(self._cfg, registry)
if 'username' in authcfg and authcfg['username'] == username:
return authcfg
req_data = {
@@ -376,7 +378,7 @@ class Client(requests.Session):
return f_port
def pull(self, repository, tag=None):
- registry, repo_name = auth.resolve_repository_name(repository)
+ registry, repo_name = resolve_repository_name(repository)
if repo_name.count(":") == 1:
repository, tag = repository.rsplit(":", 1)
@@ -385,29 +387,30 @@ class Client(requests.Session):
'fromImage': repository
}
headers = {}
- if utils.compare_version('1.5', self._version) >= 0:
+
+ if compare_version('1.5', self._version) >= 0:
if getattr(self, '_cfg', None) is None:
- self._cfg = auth.load_config()
- authcfg = auth.resolve_authconfig(self._cfg, registry)
+ self._cfg = load_config()
+ authcfg = resolve_authconfig(self._cfg, registry)
# do not fail if no atuhentication exists
# for this specific registry as we can have a readonly pull
if authcfg:
- headers['X-Registry-Auth'] = auth.encode_header(authcfg)
+ headers['X-Registry-Auth'] = encode_header(authcfg)
u = self._url("/images/create")
return self._result(self.post(u, params=params, headers=headers))
def push(self, repository):
- registry, repository = auth.resolve_repository_name(repository)
+ registry, repository = resolve_repository_name(repository)
u = self._url("/images/{0}/push".format(repository))
headers = {}
if getattr(self, '_cfg', None) is None:
- self._cfg = auth.load_config()
- authcfg = auth.resolve_authconfig(self._cfg, registry)
- if utils.compare_version('1.5', self._version) >= 0:
+ self._cfg = load_config()
+ authcfg = resolve_authconfig(self._cfg, registry)
+ if compare_version('1.5', self._version) >= 0:
# do not fail if no atuhentication exists
# for this specific registry as we can have an anon push
if authcfg:
- headers['X-Registry-Auth'] = auth.encode_header(authcfg)
+ headers['X-Registry-Auth'] = encode_header(authcfg)
return self._result(self._post_json(u, None, headers=headers))
return self._result(self._post_json(u, authcfg))
diff --git a/docker/unixconn.py b/docker/unixconn.py
index 561230c..b6f3264 100644
--- a/docker/unixconn.py
+++ b/docker/unixconn.py
@@ -11,8 +11,12 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
+import six
-import httplib
+if six.PY3:
+ from http import client as httplib
+else:
+ import httplib
import requests.adapters
import socket
diff --git a/tests/test.py b/tests/test.py
index 109f3ef..8f335b7 100644
--- a/tests/test.py
+++ b/tests/test.py
@@ -32,10 +32,10 @@ except ImportError:
from io import StringIO
-try:
- import mock
-except ImportError:
+if six.PY3:
from unittest import mock
+else:
+ import mock
# FIXME: missing tests for
@@ -75,7 +75,7 @@ class DockerClientTest(unittest.TestCase):
try:
self.client.version()
except Exception as e:
- self.fail('Command should not raise exception: ' + str(e))
+ self.fail('Command should not raise exception: {0}'.format(e))
fake_request.assert_called_with('unix://var/run/docker.sock/v1.4/version')
@@ -83,7 +83,7 @@ class DockerClientTest(unittest.TestCase):
try:
self.client.info()
except Exception as e:
- self.fail('Command should not raise exception: ' + str(e))
+ self.fail('Command should not raise exception: {0}'.format(e))
fake_request.assert_called_with('unix://var/run/docker.sock/v1.4/info')
@@ -91,7 +91,7 @@ class DockerClientTest(unittest.TestCase):
try:
self.client.search('busybox')
except Exception as e:
- self.fail('Command should not raise exception: ' + str(e))
+ self.fail('Command should not raise exception: {0}'.format(e))
fake_request.assert_called_with('unix://var/run/docker.sock/v1.4/images/search',
params={'term': 'busybox'})
@@ -104,7 +104,7 @@ class DockerClientTest(unittest.TestCase):
try:
self.client.images(all=True)
except Exception as e:
- self.fail('Command should not raise exception: ' + str(e))
+ self.fail('Command should not raise exception: {0}'.format(e))
fake_request.assert_called_with('unix://var/run/docker.sock/v1.4/images/json',
params={'filter': None, 'only_ids': 0, 'all': 1})
@@ -112,7 +112,7 @@ class DockerClientTest(unittest.TestCase):
try:
self.client.images(quiet=True)
except Exception as e:
- self.fail('Command should not raise exception: ' + str(e))
+ self.fail('Command should not raise exception: {0}'.format(e))
fake_request.assert_called_with('unix://var/run/docker.sock/v1.4/images/json',
params={'filter': None, 'only_ids': 1, 'all': 0})
@@ -121,7 +121,7 @@ class DockerClientTest(unittest.TestCase):
try:
self.client.containers(all=True)
except Exception as e:
- self.fail('Command should not raise exception: ' + str(e))
+ self.fail('Command should not raise exception: {0}'.format(e))
fake_request.assert_called_with('unix://var/run/docker.sock/v1.4/containers/ps',
params={
@@ -142,7 +142,7 @@ class DockerClientTest(unittest.TestCase):
try:
self.client.create_container('busybox', 'true')
except Exception as e:
- self.fail('Command should not raise exception: ' + str(e))
+ self.fail('Command should not raise exception: {0}'.format(e))
fake_request.assert_called_with('unix://var/run/docker.sock/v1.4/containers/create',
'{"Tty": false, "Image": "busybox", "Cmd": ["true"], "AttachStdin": false, "Memory": 0, "AttachStderr": true, "Privileged": false, "AttachStdout": true, "OpenStdin": false}',
@@ -156,7 +156,7 @@ class DockerClientTest(unittest.TestCase):
self.client.create_container('busybox',
['ls', mount_dest], volumes={mount_dest: {}})
except Exception as e:
- self.fail('Command should not raise exception: ' + str(e))
+ self.fail('Command should not raise exception: {0}'.format(e))
fake_request.assert_called_with('unix://var/run/docker.sock/v1.4/containers/create',
'{"Tty": false, "Image": "busybox", "Cmd": ["ls", "/mnt"], "AttachStdin": false, "Volumes": {"/mnt": {}}, "Memory": 0, "AttachStderr": true, "Privileged": false, "AttachStdout": true, "OpenStdin": false}',
@@ -166,7 +166,7 @@ class DockerClientTest(unittest.TestCase):
try:
self.client.create_container('busybox', 'true', privileged=True)
except Exception as e:
- self.fail('Command should not raise exception: ' + str(e))
+ self.fail('Command should not raise exception: {0}'.format(e))
fake_request.assert_called_with('unix://var/run/docker.sock/v1.4/containers/create',
'{"Tty": false, "Image": "busybox", "Cmd": ["true"], "AttachStdin": false, "Memory": 0, "AttachStderr": true, "Privileged": true, "AttachStdout": true, "OpenStdin": false}',
@@ -176,7 +176,7 @@ class DockerClientTest(unittest.TestCase):
try:
self.client.start(FAKE_CONTAINER_ID)
except Exception as e:
- self.fail('Command should not raise exception: ' + str(e))
+ self.fail('Command should not raise exception: {0}'.format(e))
fake_request.assert_called_with(
'unix://var/run/docker.sock/v1.4/containers/3cc2351ab11b/start',
@@ -190,7 +190,7 @@ class DockerClientTest(unittest.TestCase):
mount_origin = '/tmp'
self.client.start(FAKE_CONTAINER_ID, binds={mount_origin: mount_dest})
except Exception as e:
- self.fail('Command should not raise exception: ' + str(e))
+ self.fail('Command should not raise exception: {0}'.format(e))
fake_request.assert_called_with(
'unix://var/run/docker.sock/v1.4/containers/3cc2351ab11b/start',
@@ -202,7 +202,7 @@ class DockerClientTest(unittest.TestCase):
try:
self.client.start({'Id': FAKE_CONTAINER_ID})
except Exception as e:
- self.fail('Command should not raise exception: ' + str(e))
+ self.fail('Command should not raise exception: {0}'.format(e))
fake_request.assert_called_with(
'unix://var/run/docker.sock/v1.4/containers/3cc2351ab11b/start',
'{}', headers={'Content-Type': 'application/json'}
@@ -212,7 +212,7 @@ class DockerClientTest(unittest.TestCase):
try:
self.client.wait(FAKE_CONTAINER_ID)
except Exception as e:
- self.fail('Command should not raise exception: ' + str(e))
+ self.fail('Command should not raise exception: {0}'.format(e))
fake_request.assert_called_with(
'unix://var/run/docker.sock/v1.4/containers/3cc2351ab11b/wait',
@@ -224,7 +224,7 @@ class DockerClientTest(unittest.TestCase):
try:
self.client.wait({'Id': FAKE_CONTAINER_ID})
except Exception as e:
- self.fail('Command should not raise exception: ' + str(e))
+ self.fail('Command should not raise exception: {0}'.format(e))
fake_request.assert_called_with(
'unix://var/run/docker.sock/v1.4/containers/3cc2351ab11b/wait',
@@ -236,7 +236,7 @@ class DockerClientTest(unittest.TestCase):
try:
self.client.logs(FAKE_CONTAINER_ID)
except Exception as e:
- self.fail('Command should not raise exception: ' + str(e))
+ self.fail('Command should not raise exception: {0}'.format(e))
fake_request.assert_called_with(
'unix://var/run/docker.sock/v1.4/containers/3cc2351ab11b/attach',
@@ -248,7 +248,7 @@ class DockerClientTest(unittest.TestCase):
try:
self.client.logs({'Id': FAKE_CONTAINER_ID})
except Exception as e:
- self.fail('Command should not raise exception: ' + str(e))
+ self.fail('Command should not raise exception: {0}'.format(e))
fake_request.assert_called_with(
'unix://var/run/docker.sock/v1.4/containers/3cc2351ab11b/attach',
@@ -260,7 +260,7 @@ class DockerClientTest(unittest.TestCase):
try:
self.client.diff(FAKE_CONTAINER_ID)
except Exception as e:
- self.fail('Command should not raise exception: ' + str(e))
+ self.fail('Command should not raise exception: {0}'.format(e))
fake_request.assert_called_with(
'unix://var/run/docker.sock/v1.4/containers/3cc2351ab11b/changes')
@@ -269,7 +269,7 @@ class DockerClientTest(unittest.TestCase):
try:
self.client.diff({'Id': FAKE_CONTAINER_ID})
except Exception as e:
- self.fail('Command should not raise exception: ' + str(e))
+ self.fail('Command should not raise exception: {0}'.format(e))
fake_request.assert_called_with(
'unix://var/run/docker.sock/v1.4/containers/3cc2351ab11b/changes')
@@ -278,7 +278,7 @@ class DockerClientTest(unittest.TestCase):
try:
self.client.stop(FAKE_CONTAINER_ID, timeout=2)
except Exception as e:
- self.fail('Command should not raise exception: ' + str(e))
+ self.fail('Command should not raise exception: {0}'.format(e))
fake_request.assert_called_with(
'unix://var/run/docker.sock/v1.4/containers/3cc2351ab11b/stop',
@@ -290,7 +290,7 @@ class DockerClientTest(unittest.TestCase):
try:
self.client.stop({'Id': FAKE_CONTAINER_ID}, timeout=2)
except Exception as e:
- self.fail('Command should not raise exception: ' + str(e))
+ self.fail('Command should not raise exception: {0}'.format(e))
fake_request.assert_called_with(
'unix://var/run/docker.sock/v1.4/containers/3cc2351ab11b/stop',
@@ -302,7 +302,7 @@ class DockerClientTest(unittest.TestCase):
try:
self.client.kill(FAKE_CONTAINER_ID)
except Exception as e:
- self.fail('Command should not raise exception: ' + str(e))
+ self.fail('Command should not raise exception: {0}'.format(e))
fake_request.assert_called_with(
'unix://var/run/docker.sock/v1.4/containers/3cc2351ab11b/kill',
@@ -313,7 +313,7 @@ class DockerClientTest(unittest.TestCase):
try:
self.client.kill({'Id': FAKE_CONTAINER_ID})
except Exception as e:
- self.fail('Command should not raise exception: ' + str(e))
+ self.fail('Command should not raise exception: {0}'.format(e))
fake_request.assert_called_with(
'unix://var/run/docker.sock/v1.4/containers/3cc2351ab11b/kill',
@@ -324,7 +324,7 @@ class DockerClientTest(unittest.TestCase):
try:
self.client.restart(FAKE_CONTAINER_ID, timeout=2)
except Exception as e:
- self.fail('Command should not raise exception : ' + str(e))
+ self.fail('Command should not raise exception : {0}'.format(e))
fake_request.assert_called_with(
'unix://var/run/docker.sock/v1.4/containers/3cc2351ab11b/restart',
@@ -336,7 +336,7 @@ class DockerClientTest(unittest.TestCase):
try:
self.client.restart({'Id': FAKE_CONTAINER_ID}, timeout=2)
except Exception as e:
- self.fail('Command should not raise exception: ' + str(e))
+ self.fail('Command should not raise exception: {0}'.format(e))
fake_request.assert_called_with(
'unix://var/run/docker.sock/v1.4/containers/3cc2351ab11b/restart',
@@ -348,7 +348,7 @@ class DockerClientTest(unittest.TestCase):
try:
self.client.remove_container(FAKE_CONTAINER_ID)
except Exception as e:
- self.fail('Command should not raise exception: ' + str(e))
+ self.fail('Command should not raise exception: {0}'.format(e))
fake_request.assert_called_with(
'unix://var/run/docker.sock/v1.4/containers/3cc2351ab11b',
@@ -359,7 +359,7 @@ class DockerClientTest(unittest.TestCase):
try:
self.client.remove_container({'Id': FAKE_CONTAINER_ID})
except Exception as e:
- self.fail('Command should not raise exception: ' + str(e))
+ self.fail('Command should not raise exception: {0}'.format(e))
fake_request.assert_called_with(
'unix://var/run/docker.sock/v1.4/containers/3cc2351ab11b',
@@ -374,7 +374,7 @@ class DockerClientTest(unittest.TestCase):
try:
self.client.pull('joffrey/test001')
except Exception as e:
- self.fail('Command should not raise exception: ' + str(e))
+ self.fail('Command should not raise exception: {0}'.format(e))
fake_request.assert_called_with('unix://var/run/docker.sock/v1.4/images/create',
headers={},
@@ -385,7 +385,7 @@ class DockerClientTest(unittest.TestCase):
try:
self.client.commit(FAKE_CONTAINER_ID)
except Exception as e:
- self.fail('Command should not raise exception: ' + str(e))
+ self.fail('Command should not raise exception: {0}'.format(e))
fake_request.assert_called_with(
'unix://var/run/docker.sock/v1.4/commit',
@@ -404,7 +404,7 @@ class DockerClientTest(unittest.TestCase):
try:
self.client.remove_image(FAKE_IMAGE_ID)
except Exception as e:
- self.fail('Command should not raise exception: ' + str(e))
+ self.fail('Command should not raise exception: {0}'.format(e))
fake_request.assert_called_with('unix://var/run/docker.sock/v1.4/images/e9aa60c60128')
@@ -423,7 +423,7 @@ class DockerClientTest(unittest.TestCase):
try:
self.client.build(fileobj=script)
except Exception as e:
- self.fail('Command should not raise exception: ' + str(e))
+ self.fail('Command should not raise exception: {0}'.format(e))
#######################
## PY SPECIFIC TESTS ##