Skip to content

Commit

Permalink
Rebasing docker#7031 into 1.26.x
Browse files Browse the repository at this point in the history
----

Removed Python2 support

Closes: docker#6890

- Removed six
- Removed now useless check for version_info >= 2.7
- Removed now unused get_output_stream method
- Import unittest.mock directly.

How we removed six from docker-compose

We expect this to happen in a couple steps,

1. Remove six entirely, and remove python2.7 from requirements, setup, etc.
2. cleanup code that is left (All the conditionals that are now always True or False)
3. Remove all unnecessary `__future__` commands, by modifying the pre-commit-hook of python sourts to be `--py3-plus`

After removing all occurrances, we made sure `six` wasn't used anymore running a search for

```txt
six\.\w+\(([^()]+)\)
```
- We replaced all types for their corresponding python3 types. Note how
  six returns a tuple for the isinstance (since in python2 there are
  more than one type, but that isn't necessary in python3, so we removed
  the tuple and instead used the class directly)

We then searched for all instances where code could be pruned. (i.e., if/else clauses that now where always `True` or `False`)

After that,

- Removed inheritinng from `object` super class, it isn't necessary in python3
- `# coding ... utf-8` statements are not needed
- `super(ClassName, self)` can now be replaced with `super()`

Signed-off-by: Alejandro G. Recuenco <alejandrogonzalezrecuenco@gmail.com>
  • Loading branch information
ulyssessouza authored and alex-ubitec committed Mar 12, 2020
1 parent 07cab51 commit e3e4c86
Show file tree
Hide file tree
Showing 83 changed files with 175 additions and 507 deletions.
7 changes: 2 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@
sha: v1.3.4
hooks:
- id: reorder-python-imports
language_version: 'python2.7'
language_version: 'python3.7'
args:
- --add-import
- from __future__ import absolute_import
- --add-import
- from __future__ import unicode_literals
- --py3-plus
3 changes: 0 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ RUN apk add --no-cache \
musl-dev \
openssl \
openssl-dev \
python2 \
python2-dev \
zlib-dev
ENV BUILD_BOOTLOADER=1

Expand All @@ -40,7 +38,6 @@ RUN apt-get update && apt-get install --no-install-recommends -y \
libssl-dev \
make \
openssl \
python2.7-dev \
zlib1g-dev

FROM build-${BUILD_PLATFORM} AS build
Expand Down
3 changes: 0 additions & 3 deletions bin/docker-compose
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#!/usr/bin/env python
from __future__ import absolute_import
from __future__ import unicode_literals

from compose.cli.main import main
main()
2 changes: 0 additions & 2 deletions compose/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
from __future__ import absolute_import
from __future__ import unicode_literals

__version__ = '1.26.0-rc1'
3 changes: 0 additions & 3 deletions compose/__main__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
from __future__ import absolute_import
from __future__ import unicode_literals

from compose.cli.main import main

main()
3 changes: 0 additions & 3 deletions compose/cli/colors.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
from __future__ import absolute_import
from __future__ import unicode_literals

from ..const import IS_WINDOWS_PLATFORM

NAMES = [
Expand Down
7 changes: 1 addition & 6 deletions compose/cli/command.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
from __future__ import absolute_import
from __future__ import unicode_literals

import logging
import os
import re

import six

from . import errors
from .. import config
from .. import parallel
Expand Down Expand Up @@ -110,7 +105,7 @@ def get_config_from_options(base_dir, options, additional_options=None):

def get_config_path_from_options(base_dir, options, environment):
def unicode_paths(paths):
return [p.decode('utf-8') if isinstance(p, six.binary_type) else p for p in paths]
return [p.decode('utf-8') if isinstance(p, bytes) else p for p in paths]

file_option = options.get('--file')
if file_option:
Expand Down
6 changes: 1 addition & 5 deletions compose/cli/docker_client.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
from __future__ import absolute_import
from __future__ import unicode_literals

import logging
import os.path
import ssl

import six
from docker import APIClient
from docker import Context
from docker import ContextAPI
Expand Down Expand Up @@ -47,7 +43,7 @@ def get_client(environment, verbose=False, version=None, context=None):
environment=environment, tls_version=get_tls_version(environment)
)
if verbose:
version_info = six.iteritems(client.version())
version_info = client.version().items()
log.info(get_version_info('full'))
log.info("Docker base_url: %s", client.base_url)
log.info("Docker version: %s",
Expand Down
7 changes: 2 additions & 5 deletions compose/cli/docopt_command.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
from __future__ import absolute_import
from __future__ import unicode_literals

from inspect import getdoc

from docopt import docopt
Expand All @@ -14,7 +11,7 @@ def docopt_full_help(docstring, *args, **kwargs):
raise SystemExit(docstring)


class DocoptDispatcher(object):
class DocoptDispatcher:

def __init__(self, command_class, options):
self.command_class = command_class
Expand Down Expand Up @@ -53,7 +50,7 @@ def get_handler(command_class, command):

class NoSuchCommand(Exception):
def __init__(self, command, supercommand):
super(NoSuchCommand, self).__init__("No such command: %s" % command)
super().__init__("No such command: %s" % command)

self.command = command
self.supercommand = supercommand
3 changes: 0 additions & 3 deletions compose/cli/errors.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
from __future__ import absolute_import
from __future__ import unicode_literals

import contextlib
import logging
import socket
Expand Down
8 changes: 2 additions & 6 deletions compose/cli/formatter.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
from __future__ import absolute_import
from __future__ import unicode_literals

import logging
import shutil

import six
import texttable

from compose.cli import colors
Expand Down Expand Up @@ -57,7 +53,7 @@ def get_level_message(self, record):
return ''

def format(self, record):
if isinstance(record.msg, six.binary_type):
if isinstance(record.msg, bytes):
record.msg = record.msg.decode('utf-8')
message = super(ConsoleWarningFormatter, self).format(record)
message = super().format(record)
return '{0}{1}'.format(self.get_level_message(record), message)
16 changes: 6 additions & 10 deletions compose/cli/log_printer.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
from __future__ import absolute_import
from __future__ import unicode_literals

import _thread as thread
import sys
from collections import namedtuple
from itertools import cycle
from queue import Empty
from queue import Queue
from threading import Thread

from docker.errors import APIError
from six.moves import _thread as thread
from six.moves.queue import Empty
from six.moves.queue import Queue

from . import colors
from compose import utils
from compose.cli.signals import ShutdownException
from compose.utils import split_buffer


class LogPresenter(object):
class LogPresenter:

def __init__(self, prefix_width, color_func):
self.prefix_width = prefix_width
Expand Down Expand Up @@ -54,7 +50,7 @@ def max_name_width(service_names, max_index_width=3):
return max(len(name) for name in service_names) + max_index_width


class LogPrinter(object):
class LogPrinter:
"""Print logs from many containers to a single output stream."""

def __init__(self,
Expand All @@ -67,7 +63,7 @@ def __init__(self,
self.containers = containers
self.presenters = presenters
self.event_stream = event_stream
self.output = utils.get_output_stream(output)
self.output = output
self.cascade_stop = cascade_stop
self.log_args = log_args or {}

Expand Down
6 changes: 1 addition & 5 deletions compose/cli/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
from __future__ import absolute_import
from __future__ import print_function
from __future__ import unicode_literals

import contextlib
import functools
import json
Expand Down Expand Up @@ -180,7 +176,7 @@ def parse_doc_section(name, source):
return [s.strip() for s in pattern.findall(source)]


class TopLevelCommand(object):
class TopLevelCommand:
"""Define and run multi-container applications with Docker.
Usage:
Expand Down
3 changes: 0 additions & 3 deletions compose/cli/signals.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
from __future__ import absolute_import
from __future__ import unicode_literals

import signal

from ..const import IS_WINDOWS_PLATFORM
Expand Down
7 changes: 1 addition & 6 deletions compose/cli/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals

import math
import os
import platform
Expand All @@ -10,7 +6,6 @@
import sys

import docker
import six

import compose
from ..const import IS_WINDOWS_PLATFORM
Expand Down Expand Up @@ -144,7 +139,7 @@ def human_readable_file_size(size):


def binarystr_to_unicode(s):
if not isinstance(s, six.binary_type):
if not isinstance(s, bytes):
return s

if IS_WINDOWS_PLATFORM:
Expand Down
11 changes: 3 additions & 8 deletions compose/cli/verbose_proxy.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
from __future__ import absolute_import
from __future__ import unicode_literals

import functools
import logging
import pprint
from itertools import chain

import six


def format_call(args, kwargs):
args = (repr(a) for a in args)
kwargs = ("{0!s}={1!r}".format(*item) for item in six.iteritems(kwargs))
kwargs = ("{0!s}={1!r}".format(*item) for item in kwargs.items())
return "({0})".format(", ".join(chain(args, kwargs)))


Expand All @@ -27,7 +22,7 @@ def format_return(result, max_lines):
return result


class VerboseProxy(object):
class VerboseProxy:
"""Proxy all function calls to another class and log method name, arguments
and return values for each call.
"""
Expand All @@ -41,7 +36,7 @@ def __init__(self, obj_name, obj, log_name=None, max_lines=10):
def __getattr__(self, name):
attr = getattr(self.obj, name)

if not six.callable(attr):
if not callable(attr):
return attr

return functools.partial(self.proxy_callable, name)
Expand Down
3 changes: 0 additions & 3 deletions compose/config/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
# flake8: noqa
from __future__ import absolute_import
from __future__ import unicode_literals

from . import environment
from .config import ConfigurationError
from .config import DOCKER_CONFIG_KEYS
Expand Down
24 changes: 10 additions & 14 deletions compose/config/config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
from __future__ import absolute_import
from __future__ import unicode_literals

import functools
import io
import logging
Expand All @@ -11,7 +8,6 @@
from collections import namedtuple
from operator import attrgetter

import six
import yaml
from cached_property import cached_property

Expand Down Expand Up @@ -204,7 +200,7 @@ def version(self):
'Compose file version 1.'.format(self.filename))
return V1

if not isinstance(version, six.string_types):
if not isinstance(version, str):
raise ConfigurationError(
'Version in "{}" is invalid - it should be a string.'
.format(self.filename))
Expand Down Expand Up @@ -589,7 +585,7 @@ def process_config_file(config_file, environment, service_name=None, interpolate
return config_file


class ServiceExtendsResolver(object):
class ServiceExtendsResolver:
def __init__(self, service_config, config_file, environment, already_seen=None):
self.service_config = service_config
self.working_dir = service_config.working_dir
Expand Down Expand Up @@ -688,12 +684,12 @@ def resolve_environment(service_dict, environment=None):
env.update(env_vars_from_file(env_file))

env.update(parse_environment(service_dict.get('environment')))
return dict(resolve_env_var(k, v, environment) for k, v in six.iteritems(env))
return dict(resolve_env_var(k, v, environment) for k, v in env.items())


def resolve_build_args(buildargs, environment):
args = parse_build_arguments(buildargs)
return dict(resolve_env_var(k, v, environment) for k, v in six.iteritems(args))
return dict(resolve_env_var(k, v, environment) for k, v in args.items())


def validate_extended_service_dict(service_dict, filename, service):
Expand Down Expand Up @@ -780,7 +776,7 @@ def process_service(service_config):


def process_build_section(service_dict, working_dir):
if isinstance(service_dict['build'], six.string_types):
if isinstance(service_dict['build'], str):
service_dict['build'] = resolve_build_path(working_dir, service_dict['build'])
elif isinstance(service_dict['build'], dict):
if 'context' in service_dict['build']:
Expand Down Expand Up @@ -848,7 +844,7 @@ def process_healthcheck(service_dict):
hc['test'] = ['NONE']

for field in ['interval', 'timeout', 'start_period']:
if field not in hc or isinstance(hc[field], six.integer_types):
if field not in hc or isinstance(hc[field], int):
continue
hc[field] = parse_nanoseconds_int(hc[field])

Expand Down Expand Up @@ -1179,7 +1175,7 @@ def parse_sequence_func(seq):
def merge_build(output, base, override):
def to_dict(service):
build_config = service.get('build', {})
if isinstance(build_config, six.string_types):
if isinstance(build_config, str):
return {'context': build_config}
return build_config

Expand Down Expand Up @@ -1389,7 +1385,7 @@ def normalize_build(service_dict, working_dir, environment):
if 'build' in service_dict:
build = {}
# Shortcut where specifying a string is treated as the build context
if isinstance(service_dict['build'], six.string_types):
if isinstance(service_dict['build'], str):
build['context'] = service_dict.pop('build')
else:
build.update(service_dict['build'])
Expand All @@ -1415,7 +1411,7 @@ def validate_paths(service_dict):
if 'build' in service_dict:
build = service_dict.get('build', {})

if isinstance(build, six.string_types):
if isinstance(build, str):
build_path = build
elif isinstance(build, dict) and 'context' in build:
build_path = build['context']
Expand Down Expand Up @@ -1506,7 +1502,7 @@ def merge_list_or_string(base, override):
def to_list(value):
if value is None:
return []
elif isinstance(value, six.string_types):
elif isinstance(value, str):
return [value]
else:
return value
Expand Down
Loading

0 comments on commit e3e4c86

Please sign in to comment.