Skip to content

Commit

Permalink
Merge branch 'master' into feat/add_templating_parameter_docker_config
Browse files Browse the repository at this point in the history
  • Loading branch information
aiordache authored Oct 7, 2021
2 parents f42a81d + b825867 commit aae6be0
Show file tree
Hide file tree
Showing 95 changed files with 588 additions and 674 deletions.
12 changes: 6 additions & 6 deletions docker/api/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
log = logging.getLogger(__name__)


class BuildApiMixin(object):
class BuildApiMixin:
def build(self, path=None, tag=None, quiet=False, fileobj=None,
nocache=False, rm=False, timeout=None,
custom_context=False, encoding=None, pull=False,
Expand Down Expand Up @@ -132,7 +132,7 @@ def build(self, path=None, tag=None, quiet=False, fileobj=None,
for key in container_limits.keys():
if key not in constants.CONTAINER_LIMITS_KEYS:
raise errors.DockerException(
'Invalid container_limits key {0}'.format(key)
f'Invalid container_limits key {key}'
)

if custom_context:
Expand All @@ -150,7 +150,7 @@ def build(self, path=None, tag=None, quiet=False, fileobj=None,
dockerignore = os.path.join(path, '.dockerignore')
exclude = None
if os.path.exists(dockerignore):
with open(dockerignore, 'r') as f:
with open(dockerignore) as f:
exclude = list(filter(
lambda x: x != '' and x[0] != '#',
[l.strip() for l in f.read().splitlines()]
Expand Down Expand Up @@ -313,7 +313,7 @@ def _set_auth_headers(self, headers):
auth_data[auth.INDEX_URL] = auth_data.get(auth.INDEX_NAME, {})

log.debug(
'Sending auth config ({0})'.format(
'Sending auth config ({})'.format(
', '.join(repr(k) for k in auth_data.keys())
)
)
Expand Down Expand Up @@ -344,9 +344,9 @@ def process_dockerfile(dockerfile, path):
if (os.path.splitdrive(path)[0] != os.path.splitdrive(abs_dockerfile)[0] or
os.path.relpath(abs_dockerfile, path).startswith('..')):
# Dockerfile not in context - read data to insert into tar later
with open(abs_dockerfile, 'r') as df:
with open(abs_dockerfile) as df:
return (
'.dockerfile.{0:x}'.format(random.getrandbits(160)),
f'.dockerfile.{random.getrandbits(160):x}',
df.read()
)

Expand Down
26 changes: 15 additions & 11 deletions docker/api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def __init__(self, base_url=None, version=None,
user_agent=DEFAULT_USER_AGENT, num_pools=None,
credstore_env=None, use_ssh_client=False,
max_pool_size=DEFAULT_MAX_POOL_SIZE):
super(APIClient, self).__init__()
super().__init__()

if tls and not base_url:
raise TLSParameterError(
Expand Down Expand Up @@ -199,7 +199,7 @@ def __init__(self, base_url=None, version=None,
self._version = version
if not isinstance(self._version, str):
raise DockerException(
'Version parameter must be a string or None. Found {0}'.format(
'Version parameter must be a string or None. Found {}'.format(
type(version).__name__
)
)
Expand All @@ -219,7 +219,7 @@ def _retrieve_server_version(self):
)
except Exception as e:
raise DockerException(
'Error while fetching server API version: {0}'.format(e)
f'Error while fetching server API version: {e}'
)

def _set_request_timeout(self, kwargs):
Expand Down Expand Up @@ -248,19 +248,19 @@ def _url(self, pathfmt, *args, **kwargs):
for arg in args:
if not isinstance(arg, str):
raise ValueError(
'Expected a string but found {0} ({1}) '
'Expected a string but found {} ({}) '
'instead'.format(arg, type(arg))
)

quote_f = partial(urllib.parse.quote, safe="/:")
args = map(quote_f, args)

if kwargs.get('versioned_api', True):
return '{0}/v{1}{2}'.format(
return '{}/v{}{}'.format(
self.base_url, self._version, pathfmt.format(*args)
)
else:
return '{0}{1}'.format(self.base_url, pathfmt.format(*args))
return f'{self.base_url}{pathfmt.format(*args)}'

def _raise_for_status(self, response):
"""Raises stored :class:`APIError`, if one occurred."""
Expand Down Expand Up @@ -341,8 +341,7 @@ def _stream_helper(self, response, decode=False):

if response.raw._fp.chunked:
if decode:
for chunk in json_stream(self._stream_helper(response, False)):
yield chunk
yield from json_stream(self._stream_helper(response, False))
else:
reader = response.raw
while not reader.closed:
Expand Down Expand Up @@ -398,8 +397,13 @@ def _multiplexed_response_stream_helper(self, response):
def _stream_raw_result(self, response, chunk_size=1, decode=True):
''' Stream result for TTY-enabled container and raw binary data'''
self._raise_for_status(response)
for out in response.iter_content(chunk_size, decode):
yield out

# Disable timeout on the underlying socket to prevent
# Read timed out(s) for long running processes
socket = self._get_raw_response_socket(response)
self._disable_socket_timeout(socket)

yield from response.iter_content(chunk_size, decode)

def _read_from_socket(self, response, stream, tty=True, demux=False):
socket = self._get_raw_response_socket(response)
Expand Down Expand Up @@ -477,7 +481,7 @@ def _unmount(self, *args):

def get_adapter(self, url):
try:
return super(APIClient, self).get_adapter(url)
return super().get_adapter(url)
except requests.exceptions.InvalidSchema as e:
if self._custom_adapter:
return self._custom_adapter
Expand Down
8 changes: 2 additions & 6 deletions docker/api/config.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import base64

import six

from .. import utils


class ConfigApiMixin(object):
# TODO: The templating field is only available starting from API v 1.37
class ConfigApiMixin:
@utils.minimum_version('1.30')
def create_config(self, name, data, labels=None, templating=None):
"""
Expand All @@ -26,8 +23,7 @@ def create_config(self, name, data, labels=None, templating=None):
data = data.encode('utf-8')

data = base64.b64encode(data)
if six.PY3:
data = data.decode('ascii')
data = data.decode('ascii')
body = {
'Data': data,
'Name': name,
Expand Down
8 changes: 3 additions & 5 deletions docker/api/container.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from datetime import datetime

import six

from .. import errors
from .. import utils
from ..constants import DEFAULT_DATA_CHUNK_SIZE
Expand All @@ -12,7 +10,7 @@
from ..types import NetworkingConfig


class ContainerApiMixin(object):
class ContainerApiMixin:
@utils.check_resource('container')
def attach(self, container, stdout=True, stderr=True,
stream=False, logs=False, demux=False):
Expand Down Expand Up @@ -408,7 +406,7 @@ def create_container(self, image, command=None, hostname=None, user=None,
:py:class:`docker.errors.APIError`
If the server returns an error.
"""
if isinstance(volumes, six.string_types):
if isinstance(volumes, str):
volumes = [volumes, ]

if isinstance(environment, dict):
Expand Down Expand Up @@ -790,7 +788,7 @@ def kill(self, container, signal=None):
url = self._url("/containers/{0}/kill", container)
params = {}
if signal is not None:
if not isinstance(signal, six.string_types):
if not isinstance(signal, str):
signal = int(signal)
params['signal'] = signal
res = self._post(url, params=params)
Expand Down
2 changes: 1 addition & 1 deletion docker/api/daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from .. import auth, types, utils


class DaemonApiMixin(object):
class DaemonApiMixin:
@utils.minimum_version('1.25')
def df(self):
"""
Expand Down
6 changes: 2 additions & 4 deletions docker/api/exec_api.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import six

from .. import errors
from .. import utils


class ExecApiMixin(object):
class ExecApiMixin:
@utils.check_resource('container')
def exec_create(self, container, cmd, stdout=True, stderr=True,
stdin=False, tty=False, privileged=False, user='',
Expand Down Expand Up @@ -45,7 +43,7 @@ def exec_create(self, container, cmd, stdout=True, stderr=True,
'Setting environment for exec is not supported in API < 1.25'
)

if isinstance(cmd, six.string_types):
if isinstance(cmd, str):
cmd = utils.split_command(cmd)

if isinstance(environment, dict):
Expand Down
10 changes: 4 additions & 6 deletions docker/api/image.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import logging
import os

import six

from .. import auth, errors, utils
from ..constants import DEFAULT_DATA_CHUNK_SIZE

log = logging.getLogger(__name__)


class ImageApiMixin(object):
class ImageApiMixin:

@utils.check_resource('image')
def get_image(self, image, chunk_size=DEFAULT_DATA_CHUNK_SIZE):
Expand Down Expand Up @@ -130,7 +128,7 @@ def import_image(self, src=None, repository=None, tag=None, image=None,

params = _import_image_params(
repository, tag, image,
src=(src if isinstance(src, six.string_types) else None),
src=(src if isinstance(src, str) else None),
changes=changes
)
headers = {'Content-Type': 'application/tar'}
Expand All @@ -139,7 +137,7 @@ def import_image(self, src=None, repository=None, tag=None, image=None,
return self._result(
self._post(u, data=None, params=params)
)
elif isinstance(src, six.string_types): # from file path
elif isinstance(src, str): # from file path
with open(src, 'rb') as f:
return self._result(
self._post(
Expand Down Expand Up @@ -571,7 +569,7 @@ def tag(self, image, repository, tag=None, force=False):
def is_file(src):
try:
return (
isinstance(src, six.string_types) and
isinstance(src, str) and
os.path.isfile(src)
)
except TypeError: # a data string will make isfile() raise a TypeError
Expand Down
2 changes: 1 addition & 1 deletion docker/api/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from .. import utils


class NetworkApiMixin(object):
class NetworkApiMixin:
def networks(self, names=None, ids=None, filters=None):
"""
List networks. Similar to the ``docker network ls`` command.
Expand Down
6 changes: 2 additions & 4 deletions docker/api/plugin.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import six

from .. import auth, utils


class PluginApiMixin(object):
class PluginApiMixin:
@utils.minimum_version('1.25')
@utils.check_resource('name')
def configure_plugin(self, name, options):
Expand All @@ -21,7 +19,7 @@ def configure_plugin(self, name, options):
url = self._url('/plugins/{0}/set', name)
data = options
if isinstance(data, dict):
data = ['{0}={1}'.format(k, v) for k, v in six.iteritems(data)]
data = [f'{k}={v}' for k, v in data.items()]
res = self._post_json(url, data=data)
self._raise_for_status(res)
return True
Expand Down
7 changes: 2 additions & 5 deletions docker/api/secret.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import base64

import six

from .. import errors
from .. import utils


class SecretApiMixin(object):
class SecretApiMixin:
@utils.minimum_version('1.25')
def create_secret(self, name, data, labels=None, driver=None):
"""
Expand All @@ -25,8 +23,7 @@ def create_secret(self, name, data, labels=None, driver=None):
data = data.encode('utf-8')

data = base64.b64encode(data)
if six.PY3:
data = data.decode('ascii')
data = data.decode('ascii')
body = {
'Data': data,
'Name': name,
Expand Down
4 changes: 2 additions & 2 deletions docker/api/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def raise_version_error(param, min_version):
if task_template is not None:
if 'ForceUpdate' in task_template and utils.version_lt(
version, '1.25'):
raise_version_error('force_update', '1.25')
raise_version_error('force_update', '1.25')

if task_template.get('Placement'):
if utils.version_lt(version, '1.30'):
Expand Down Expand Up @@ -113,7 +113,7 @@ def _merge_task_template(current, override):
return merged


class ServiceApiMixin(object):
class ServiceApiMixin:
@utils.minimum_version('1.24')
def create_service(
self, task_template, name=None, labels=None, mode=None,
Expand Down
4 changes: 2 additions & 2 deletions docker/api/swarm.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging
from six.moves import http_client
import http.client as http_client
from ..constants import DEFAULT_SWARM_ADDR_POOL, DEFAULT_SWARM_SUBNET_SIZE
from .. import errors
from .. import types
Expand All @@ -8,7 +8,7 @@
log = logging.getLogger(__name__)


class SwarmApiMixin(object):
class SwarmApiMixin:

def create_swarm_spec(self, *args, **kwargs):
"""
Expand Down
2 changes: 1 addition & 1 deletion docker/api/volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from .. import utils


class VolumeApiMixin(object):
class VolumeApiMixin:
def volumes(self, filters=None):
"""
List volumes currently registered by the docker daemon. Similar to the
Expand Down
Loading

0 comments on commit aae6be0

Please sign in to comment.