Skip to content

Commit

Permalink
Flake8 compliance + flake8 tests in tox.ini
Browse files Browse the repository at this point in the history
  • Loading branch information
shin- committed Oct 15, 2013
1 parent 86b341d commit acd2607
Show file tree
Hide file tree
Showing 9 changed files with 243 additions and 107 deletions.
2 changes: 1 addition & 1 deletion docker/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from .client import Client, APIError
from .client import Client, APIError # flake8: noqa
10 changes: 9 additions & 1 deletion docker/auth/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
from .auth import *


from .auth import (
INDEX_URL,
encode_header,
load_config,
resolve_authconfig,
resolve_repository_name
) # flake8: noqa
86 changes: 50 additions & 36 deletions docker/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def __init__(self, message, response, explanation=None):

self.explanation = explanation

if self.explanation is None and response.content and len(response.content) > 0:
if self.explanation is None and response.content:
self.explanation = response.content.strip()

def __str__(self):
Expand Down Expand Up @@ -86,13 +86,15 @@ def _result(self, response, json=False):
return response.text

def _container_config(self, image, command, hostname=None, user=None,
detach=False, stdin_open=False, tty=False, mem_limit=0, ports=None,
environment=None, dns=None, volumes=None, volumes_from=None,
privileged=False):
detach=False, stdin_open=False, tty=False,
mem_limit=0, ports=None, environment=None, dns=None,
volumes=None, volumes_from=None, privileged=False):
if isinstance(command, six.string_types):
command = shlex.split(str(command))
if isinstance(environment, dict):
environment = ['{0}={1}'.format(k, v) for k, v in environment.items()]
environment = [
'{0}={1}'.format(k, v) for k, v in environment.items()
]

attach_stdin = False
attach_stdout = False
Expand Down Expand Up @@ -166,25 +168,31 @@ def attach(self, container):
else:
break

def build(self, path=None, tag=None, quiet=False, fileobj=None, nocache=False, rm=False):
def build(self, path=None, tag=None, quiet=False, fileobj=None,
nocache=False, rm=False):
remote = context = headers = None
if path is None and fileobj is None:
raise Exception("Either path or fileobj needs to be provided.")

if fileobj is not None:
context = utils.mkbuildcontext(fileobj)
elif (path.startswith('http://') or path.startswith('https://') or
path.startswith('git://') or path.startswith('github.com/')):
elif path.startswith(('http://', 'https://', 'git://', 'github.com/')):
remote = path
else:
context = utils.tar(path)

u = self._url('/build')
params = { 't': tag, 'remote': remote, 'q': quiet, 'nocache': nocache, 'rm': rm }
params = {
't': tag,
'remote': remote,
'q': quiet,
'nocache': nocache,
'rm': rm
}
if context is not None:
headers = { 'Content-Type': 'application/tar' }
headers = {'Content-Type': 'application/tar'}
res = self._result(self.post(u, context, params=params,
headers=headers, stream=True))
headers=headers, stream=True))
if context is not None:
context.close()
srch = r'Successfully built ([0-9a-f]+)'
Expand All @@ -194,7 +202,7 @@ def build(self, path=None, tag=None, quiet=False, fileobj=None, nocache=False, r
return match.group(1), res

def commit(self, container, repository=None, tag=None, message=None,
author=None, conf=None):
author=None, conf=None):
params = {
'container': container,
'repo': repository,
Expand All @@ -206,7 +214,7 @@ def commit(self, container, repository=None, tag=None, message=None,
return self._result(self._post_json(u, conf, params=params), json=True)

def containers(self, quiet=False, all=False, trunc=True, latest=False,
since=None, before=None, limit=-1):
since=None, before=None, limit=-1):
params = {
'limit': 1 if latest else limit,
'all': 1 if all else 0,
Expand All @@ -217,25 +225,27 @@ def containers(self, quiet=False, all=False, trunc=True, latest=False,
u = self._url("/containers/ps")
res = self._result(self.get(u, params=params), True)
if quiet:
return [{ 'Id': x['Id'] } for x in res]
return [{'Id': x['Id']} for x in res]
return res

def copy(self, container, resource):
res = self._post_json(self._url("/containers/{0}/copy".format(container)),
res = self._post_json(
self._url("/containers/{0}/copy".format(container)),
{"Resource": resource},
stream=True)
stream=True
)
self._raise_for_status(res)
return res.raw

def create_container(self, image, command, hostname=None, user=None,
detach=False, stdin_open=False, tty=False, mem_limit=0, ports=None,
environment=None, dns=None, volumes=None, volumes_from=None,
privileged=False):


config = self._container_config(image, command, hostname, user,
detach, stdin_open, tty, mem_limit, ports, environment, dns,
volumes, volumes_from, privileged)
detach=False, stdin_open=False, tty=False,
mem_limit=0, ports=None, environment=None, dns=None,
volumes=None, volumes_from=None, privileged=False):

config = self._container_config(
image, command, hostname, user, detach, stdin_open, tty, mem_limit,
ports, environment, dns, volumes, volumes_from, privileged
)
return self.create_container_from_config(config)

def create_container_from_config(self, config):
Expand All @@ -247,13 +257,13 @@ def diff(self, container):
if isinstance(container, dict):
container = container.get('Id')
return self._result(self.get(self._url("/containers/{0}/changes".
format(container))), True)
format(container))), True)

def export(self, container):
if isinstance(container, dict):
container = container.get('Id')
res = self.get(self._url("/containers/{0}/export".format(container)),
stream=True)
stream=True)
self._raise_for_status(res)
return res.raw

Expand All @@ -271,7 +281,7 @@ def images(self, name=None, quiet=False, all=False, viz=False):
'all': 1 if all else 0,
}
res = self._result(self.get(self._url("/images/json"), params=params),
True)
True)
if quiet:
return [x['Id'] for x in res]
return res
Expand Down Expand Up @@ -312,12 +322,14 @@ def insert(self, image, url, path):
def inspect_container(self, container):
if isinstance(container, dict):
container = container.get('Id')
return self._result(self.get(self._url("/containers/{0}/json".
format(container))), True)
return self._result(self.get(
self._url("/containers/{0}/json".format(container))
), True)

def inspect_image(self, image_id):
return self._result(self.get(self._url("/images/{0}/json".
format(image_id))), True)
return self._result(self.get(
self._url("/images/{0}/json".format(image_id))
), True)

def kill(self, container):
if isinstance(container, dict):
Expand Down Expand Up @@ -411,7 +423,7 @@ def push(self, repository):
def remove_container(self, container, v=False):
if isinstance(container, dict):
container = container.get('Id')
params = { 'v': v }
params = {'v': v}
res = self.delete(self._url("/containers/" + container), params=params)
self._raise_for_status(res)

Expand All @@ -422,14 +434,14 @@ def remove_image(self, image):
def restart(self, container, timeout=10):
if isinstance(container, dict):
container = container.get('Id')
params = { 't': timeout }
params = {'t': timeout}
url = self._url("/containers/{0}/restart".format(container))
res = self.post(url, None, params=params)
self._raise_for_status(res)

def search(self, term):
return self._result(self.get(self._url("/images/search"),
params={'term': term}), True)
params={'term': term}), True)

def start(self, container, binds=None, lxc_conf=None):
if isinstance(container, dict):
Expand All @@ -438,7 +450,9 @@ def start(self, container, binds=None, lxc_conf=None):
'LxcConf': lxc_conf
}
if binds:
bind_pairs = ['{0}:{1}'.format(host, dest) for host, dest in binds.items()]
bind_pairs = [
'{0}:{1}'.format(host, dest) for host, dest in binds.items()
]
start_config['Binds'] = bind_pairs

url = self._url("/containers/{0}/start".format(container))
Expand All @@ -448,7 +462,7 @@ def start(self, container, binds=None, lxc_conf=None):
def stop(self, container, timeout=10):
if isinstance(container, dict):
container = container.get('Id')
params = { 't': timeout }
params = {'t': timeout}
url = self._url("/containers/{0}/stop".format(container))
res = self.post(url, None, params=params)
self._raise_for_status(res)
Expand Down
2 changes: 1 addition & 1 deletion docker/unixconn/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .unixconn import UnixAdapter
from .unixconn import UnixAdapter # flake8: noqa
2 changes: 1 addition & 1 deletion docker/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .utils import mkbuildcontext, tar, compare_version
from .utils import mkbuildcontext, tar, compare_version # flake8: noqa
91 changes: 63 additions & 28 deletions tests/fake_api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
import json
# Copyright 2013 dotCloud inc.

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# 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.

CURRENT_VERSION = 'v1.4'

Expand Down Expand Up @@ -30,21 +42,24 @@ def get_fake_search():

def get_fake_images():
status_code = 200
response = [
{'Id': FAKE_IMAGE_ID, 'Created': '2 days ago', 'Repository': 'busybox', 'Tag': 'latest'}
]
response = [{
'Id': FAKE_IMAGE_ID,
'Created': '2 days ago',
'Repository': 'busybox',
'Tag': 'latest'
}]
return status_code, response


def get_fake_containers():
status_code = 200
response = [
{'Id': FAKE_CONTAINER_ID,
response = [{
'Id': FAKE_CONTAINER_ID,
'Image': 'busybox:latest',
'Created': '2 days ago',
'Command': 'true',
'Status': 'fake status'}
]
'Status': 'fake status'
}]
return status_code, response


Expand All @@ -67,7 +82,7 @@ def get_fake_inspect_container():
'Config': {'Privileged': True},
'ID': FAKE_CONTAINER_ID,
'Image': 'busybox:latest',
"State": {
"State": {
"Running": True,
"Pid": 0,
"ExitCode": 0,
Expand Down Expand Up @@ -145,24 +160,44 @@ def post_fake_build_container():


## maps real api url to fake response callback
prefix = 'unix://var/run/docker.sock'
fake_responses = {
'unix://var/run/docker.sock/{0}/version'.format(CURRENT_VERSION): get_fake_version,
'unix://var/run/docker.sock/{0}/info'.format(CURRENT_VERSION): get_fake_info,
'unix://var/run/docker.sock/{0}/images/search'.format(CURRENT_VERSION): get_fake_search,
'unix://var/run/docker.sock/{0}/images/json'.format(CURRENT_VERSION): get_fake_images,
'unix://var/run/docker.sock/{0}/containers/ps'.format(CURRENT_VERSION): get_fake_containers,
'unix://var/run/docker.sock/{0}/containers/3cc2351ab11b/start'.format(CURRENT_VERSION): post_fake_start_container,
'unix://var/run/docker.sock/{0}/containers/3cc2351ab11b/json'.format(CURRENT_VERSION): get_fake_inspect_container,
'unix://var/run/docker.sock/{0}/containers/3cc2351ab11b/wait'.format(CURRENT_VERSION): get_fake_wait,
'unix://var/run/docker.sock/{0}/containers/3cc2351ab11b/attach'.format(CURRENT_VERSION): get_fake_logs,
'unix://var/run/docker.sock/{0}/containers/3cc2351ab11b/changes'.format(CURRENT_VERSION): get_fake_diff,
'unix://var/run/docker.sock/{0}/containers/3cc2351ab11b/stop'.format(CURRENT_VERSION): post_fake_stop_container,
'unix://var/run/docker.sock/{0}/containers/3cc2351ab11b/kill'.format(CURRENT_VERSION): post_fake_kill_container,
'unix://var/run/docker.sock/{0}/containers/3cc2351ab11b/restart'.format(CURRENT_VERSION): post_fake_restart_container,
'unix://var/run/docker.sock/{0}/containers/3cc2351ab11b'.format(CURRENT_VERSION): delete_fake_remove_container,
'unix://var/run/docker.sock/{0}/images/create'.format(CURRENT_VERSION): post_fake_image_create,
'unix://var/run/docker.sock/{0}/images/e9aa60c60128'.format(CURRENT_VERSION): delete_fake_remove_image,
'unix://var/run/docker.sock/{0}/commit'.format(CURRENT_VERSION): post_fake_commit,
'unix://var/run/docker.sock/{0}/containers/create'.format(CURRENT_VERSION): post_fake_create_container,
'unix://var/run/docker.sock/{0}/build'.format(CURRENT_VERSION): post_fake_build_container
'{1}/{0}/version'.format(CURRENT_VERSION, prefix):
get_fake_version,
'{1}/{0}/info'.format(CURRENT_VERSION, prefix):
get_fake_info,
'{1}/{0}/images/search'.format(CURRENT_VERSION, prefix):
get_fake_search,
'{1}/{0}/images/json'.format(CURRENT_VERSION, prefix):
get_fake_images,
'{1}/{0}/containers/ps'.format(CURRENT_VERSION, prefix):
get_fake_containers,
'{1}/{0}/containers/3cc2351ab11b/start'.format(CURRENT_VERSION, prefix):
post_fake_start_container,
'{1}/{0}/containers/3cc2351ab11b/json'.format(CURRENT_VERSION, prefix):
get_fake_inspect_container,
'{1}/{0}/containers/3cc2351ab11b/wait'.format(CURRENT_VERSION, prefix):
get_fake_wait,
'{1}/{0}/containers/3cc2351ab11b/attach'.format(CURRENT_VERSION, prefix):
get_fake_logs,
'{1}/{0}/containers/3cc2351ab11b/changes'.format(CURRENT_VERSION, prefix):
get_fake_diff,
'{1}/{0}/containers/3cc2351ab11b/stop'.format(CURRENT_VERSION, prefix):
post_fake_stop_container,
'{1}/{0}/containers/3cc2351ab11b/kill'.format(CURRENT_VERSION, prefix):
post_fake_kill_container,
'{1}/{0}/containers/3cc2351ab11b/restart'.format(CURRENT_VERSION, prefix):
post_fake_restart_container,
'{1}/{0}/containers/3cc2351ab11b'.format(CURRENT_VERSION, prefix):
delete_fake_remove_container,
'{1}/{0}/images/create'.format(CURRENT_VERSION, prefix):
post_fake_image_create,
'{1}/{0}/images/e9aa60c60128'.format(CURRENT_VERSION, prefix):
delete_fake_remove_image,
'{1}/{0}/commit'.format(CURRENT_VERSION, prefix):
post_fake_commit,
'{1}/{0}/containers/create'.format(CURRENT_VERSION, prefix):
post_fake_create_container,
'{1}/{0}/build'.format(CURRENT_VERSION, prefix):
post_fake_build_container
}
Loading

0 comments on commit acd2607

Please sign in to comment.