From cf7632cfd52e351fa78e49dfbc41a9fdae0247fe Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Sun, 28 Jun 2020 19:15:25 +0800 Subject: [PATCH] pybind/mgr/dashboard: drop py2 support Signed-off-by: Kefu Chai --- .../mgr/dashboard/cherrypy_backports.py | 7 +-- .../mgr/dashboard/ci/check_grafana_uids.py | 19 ++------ src/pybind/mgr/dashboard/constraints.txt | 1 - .../mgr/dashboard/controllers/__init__.py | 10 ++-- .../mgr/dashboard/controllers/orchestrator.py | 3 +- src/pybind/mgr/dashboard/plugins/__init__.py | 4 +- src/pybind/mgr/dashboard/requirements.txt | 1 - src/pybind/mgr/dashboard/services/cephfs.py | 3 +- .../mgr/dashboard/services/exception.py | 47 ------------------- .../mgr/dashboard/services/orchestrator.py | 2 +- src/pybind/mgr/dashboard/services/rbd.py | 4 +- .../mgr/dashboard/services/rgw_client.py | 3 +- src/pybind/mgr/dashboard/services/sso.py | 6 +-- src/pybind/mgr/dashboard/settings.py | 4 +- src/pybind/mgr/dashboard/tests/__init__.py | 7 +-- src/pybind/mgr/dashboard/tools.py | 36 ++------------ src/pybind/mgr/dashboard/tox.ini | 2 - 17 files changed, 26 insertions(+), 133 deletions(-) diff --git a/src/pybind/mgr/dashboard/cherrypy_backports.py b/src/pybind/mgr/dashboard/cherrypy_backports.py index d11b541fab1..94c44fe13d4 100644 --- a/src/pybind/mgr/dashboard/cherrypy_backports.py +++ b/src/pybind/mgr/dashboard/cherrypy_backports.py @@ -118,12 +118,7 @@ def accept_socket_error_0(v): pass if v < StrictVersion("9.0.0") or cheroot_version < StrictVersion("6.5.5"): - import six - if six.PY3: - generic_socket_error = OSError - else: - import socket - generic_socket_error = socket.error + generic_socket_error = OSError def accept_socket_error_0(func): def wrapper(self, sock): diff --git a/src/pybind/mgr/dashboard/ci/check_grafana_uids.py b/src/pybind/mgr/dashboard/ci/check_grafana_uids.py index fac3b995397..ab27ab66399 100644 --- a/src/pybind/mgr/dashboard/ci/check_grafana_uids.py +++ b/src/pybind/mgr/dashboard/ci/check_grafana_uids.py @@ -18,18 +18,13 @@ import copy import json import os -import six -from six.moves.html_parser import HTMLParser +from html.parser import HTMLParser class TemplateParser(HTMLParser): def __init__(self, _file, search_tag): - if six.PY3: - super(TemplateParser, self).__init__() - else: - # HTMLParser is not a new-style class in py2 - HTMLParser.__init__(self) + super().__init__() self.search_tag = search_tag self.file = _file self.parsed_data = [] @@ -54,10 +49,6 @@ class TemplateParser(HTMLParser): exit(error_msg) -def stdout(msg): - six.print_(msg) - - def get_files(base_dir, file_ext): result = [] for root, _, files in os.walk(base_dir): @@ -132,7 +123,7 @@ def main(): exit(error_msg) if verbose: - stdout('Found mappings:') + print('Found mappings:') no_dashboard_tags = [] for tag in tags: uid = tag['attrs']['uid'] @@ -144,7 +135,7 @@ def main(): format(uid, tag['file'], tag['line'], grafana_dashboards[uid]['title'], grafana_dashboards[uid]['file']) - stdout(msg) + print(msg) if no_dashboard_tags: title = ('Checking Grafana dashboards UIDs: ERROR\n' @@ -156,7 +147,7 @@ def main(): error_msg = title + '\n'.join(lines) exit(error_msg) else: - stdout('Checking Grafana dashboards UIDs: OK') + print('Checking Grafana dashboards UIDs: OK') if __name__ == '__main__': diff --git a/src/pybind/mgr/dashboard/constraints.txt b/src/pybind/mgr/dashboard/constraints.txt index 5255f89a6c3..3e8532d4514 100644 --- a/src/pybind/mgr/dashboard/constraints.txt +++ b/src/pybind/mgr/dashboard/constraints.txt @@ -7,4 +7,3 @@ bcrypt==3.1.4 python3-saml==1.4.1 requests==2.20.0 Routes==2.4.1 -six==1.14.0 diff --git a/src/pybind/mgr/dashboard/controllers/__init__.py b/src/pybind/mgr/dashboard/controllers/__init__.py index a72ed73ce0a..de66b394589 100644 --- a/src/pybind/mgr/dashboard/controllers/__init__.py +++ b/src/pybind/mgr/dashboard/controllers/__init__.py @@ -11,15 +11,15 @@ import os import pkgutil import re import sys +import urllib -import six -from six.moves.urllib.parse import unquote +from functools import wraps # pylint: disable=wrong-import-position import cherrypy from ..security import Scope, Permission -from ..tools import wraps, getargspec, TaskManager, get_request_body_params +from ..tools import getargspec, TaskManager, get_request_body_params from ..exceptions import ScopeNotValid, PermissionNotValid from ..services.auth import AuthManager, JwtManager from ..plugins import PLUGIN_MANAGER @@ -655,8 +655,8 @@ class BaseController(object): @wraps(func) def inner(*args, **kwargs): for key, value in kwargs.items(): - if isinstance(value, six.text_type): - kwargs[key] = unquote(value) + if isinstance(value, str): + kwargs[key] = urllib.parse.unquote(value) # Process method arguments. params = get_request_body_params(cherrypy.request) diff --git a/src/pybind/mgr/dashboard/controllers/orchestrator.py b/src/pybind/mgr/dashboard/controllers/orchestrator.py index c9172b83942..70ee3d9307d 100644 --- a/src/pybind/mgr/dashboard/controllers/orchestrator.py +++ b/src/pybind/mgr/dashboard/controllers/orchestrator.py @@ -3,6 +3,7 @@ from __future__ import absolute_import import os.path import time +from functools import wraps from . import ApiController, Endpoint, ReadPermission, UpdatePermission from . import RESTController, Task @@ -11,7 +12,7 @@ from ..exceptions import DashboardException from ..security import Scope from ..services.exception import handle_orchestrator_error from ..services.orchestrator import OrchClient -from ..tools import TaskManager, wraps +from ..tools import TaskManager def get_device_osd_map(): diff --git a/src/pybind/mgr/dashboard/plugins/__init__.py b/src/pybind/mgr/dashboard/plugins/__init__.py index 89736547702..9726b971844 100644 --- a/src/pybind/mgr/dashboard/plugins/__init__.py +++ b/src/pybind/mgr/dashboard/plugins/__init__.py @@ -2,13 +2,11 @@ from __future__ import absolute_import import abc -import six from .pluggy import HookspecMarker, HookimplMarker, PluginManager -@six.add_metaclass(abc.ABCMeta) -class Interface(object): +class Interface(object, metaclass=abc.ABCMeta): pass diff --git a/src/pybind/mgr/dashboard/requirements.txt b/src/pybind/mgr/dashboard/requirements.txt index 1b8ce514329..eb0da802673 100644 --- a/src/pybind/mgr/dashboard/requirements.txt +++ b/src/pybind/mgr/dashboard/requirements.txt @@ -7,7 +7,6 @@ pyopenssl python3-saml requests Routes -six -e ../../../python-common prettytable pyyaml diff --git a/src/pybind/mgr/dashboard/services/cephfs.py b/src/pybind/mgr/dashboard/services/cephfs.py index a40a370c8cf..a9d9cb50936 100644 --- a/src/pybind/mgr/dashboard/services/cephfs.py +++ b/src/pybind/mgr/dashboard/services/cephfs.py @@ -6,7 +6,6 @@ import logging import datetime import os -import six import cephfs from .. import mgr @@ -92,7 +91,7 @@ class CephFS(object): ] :rtype: list """ - if isinstance(path, six.string_types): + if isinstance(path, str): path = path.encode() logger.debug("get_dir_list dirpath=%s depth=%s", path, depth) diff --git a/src/pybind/mgr/dashboard/services/exception.py b/src/pybind/mgr/dashboard/services/exception.py index 26b6ee4b19d..61ec52d451d 100644 --- a/src/pybind/mgr/dashboard/services/exception.py +++ b/src/pybind/mgr/dashboard/services/exception.py @@ -4,7 +4,6 @@ from __future__ import absolute_import import json from contextlib import contextmanager import logging -import six import cherrypy @@ -14,57 +13,11 @@ import rados from ..services.ceph_service import SendCommandError from ..exceptions import ViewCacheNoDataException, DashboardException -from ..tools import wraps logger = logging.getLogger('exception') -if six.PY2: - # Monkey-patch a __call__ method into @contextmanager to make - # it compatible to Python 3 - - # pylint: disable=no-name-in-module,ungrouped-imports - from contextlib import GeneratorContextManager - - def init(self, *args): - if len(args) == 1: - self.gen = args[0] - elif len(args) == 3: - self.func, self.args, self.kwargs = args - else: - raise TypeError() - - def enter(self): - if hasattr(self, 'func'): - self.gen = self.func(*self.args, **self.kwargs) - try: - return self.gen.next() - except StopIteration: - raise RuntimeError("generator didn't yield") - - def call(self, f): - @wraps(f) - def wrapper(*args, **kwargs): - with self: - return f(*args, **kwargs) - - return wrapper - - GeneratorContextManager.__init__ = init - GeneratorContextManager.__enter__ = enter - GeneratorContextManager.__call__ = call - - # pylint: disable=function-redefined - def contextmanager(func): # noqa: F811 - - @wraps(func) - def helper(*args, **kwds): - return GeneratorContextManager(func, args, kwds) - - return helper - - def serialize_dashboard_exception(e, include_http_status=False, task=None): """ :type e: Exception diff --git a/src/pybind/mgr/dashboard/services/orchestrator.py b/src/pybind/mgr/dashboard/services/orchestrator.py index ea33b9a37ee..b5cf7142e8a 100644 --- a/src/pybind/mgr/dashboard/services/orchestrator.py +++ b/src/pybind/mgr/dashboard/services/orchestrator.py @@ -2,6 +2,7 @@ from __future__ import absolute_import import logging +from functools import wraps from typing import List, Optional from orchestrator import InventoryFilter, DeviceLightLoc, Completion @@ -9,7 +10,6 @@ from orchestrator import ServiceDescription, DaemonDescription from orchestrator import OrchestratorClientMixin, raise_if_exception, OrchestratorError from orchestrator import HostSpec from .. import mgr -from ..tools import wraps logger = logging.getLogger('orchestrator') diff --git a/src/pybind/mgr/dashboard/services/rbd.py b/src/pybind/mgr/dashboard/services/rbd.py index 4e628342282..fa46356f1c2 100644 --- a/src/pybind/mgr/dashboard/services/rbd.py +++ b/src/pybind/mgr/dashboard/services/rbd.py @@ -2,8 +2,6 @@ # pylint: disable=unused-argument from __future__ import absolute_import -import six - import cherrypy import rbd @@ -57,7 +55,7 @@ def format_features(features): @DISABLEDOCTEST: >>> format_features('deep-flatten, exclusive-lock') 32 """ - if isinstance(features, six.string_types): + if isinstance(features, str): features = features.split(',') if not isinstance(features, list): diff --git a/src/pybind/mgr/dashboard/services/rgw_client.py b/src/pybind/mgr/dashboard/services/rgw_client.py index 5507eb68007..7295afb294a 100644 --- a/src/pybind/mgr/dashboard/services/rgw_client.py +++ b/src/pybind/mgr/dashboard/services/rgw_client.py @@ -6,7 +6,6 @@ import logging import ipaddress from distutils.util import strtobool import xml.etree.ElementTree as ET # noqa: N814 -import six from ..awsauth import S3Auth from ..exceptions import DashboardException from ..settings import Settings, Options @@ -139,7 +138,7 @@ def _parse_addr(value): # Group 2: 2001:db8:85a3::8a2e:370:7334 addr = match.group(3) if match.group(3) else match.group(2) try: - ipaddress.ip_address(six.u(addr)) + ipaddress.ip_address(addr) return addr except ValueError: raise LookupError('Invalid RGW address \'{}\' found'.format(addr)) diff --git a/src/pybind/mgr/dashboard/services/sso.py b/src/pybind/mgr/dashboard/services/sso.py index adab60d5190..7f0bd683085 100644 --- a/src/pybind/mgr/dashboard/services/sso.py +++ b/src/pybind/mgr/dashboard/services/sso.py @@ -9,16 +9,12 @@ import logging import threading import warnings -import six -from six.moves.urllib import parse +from urllib import parse from .. import mgr from ..tools import prepare_url_prefix -if six.PY2: - FileNotFoundError = IOError # pylint: disable=redefined-builtin - logger = logging.getLogger('sso') try: diff --git a/src/pybind/mgr/dashboard/settings.py b/src/pybind/mgr/dashboard/settings.py index 229f0c3d03d..23e95b1f3d2 100644 --- a/src/pybind/mgr/dashboard/settings.py +++ b/src/pybind/mgr/dashboard/settings.py @@ -3,7 +3,6 @@ from __future__ import absolute_import import errno import inspect -from six import add_metaclass from . import mgr @@ -112,8 +111,7 @@ class SettingsMeta(type): # pylint: disable=no-init -@add_metaclass(SettingsMeta) -class Settings(object): +class Settings(object, metaclass=SettingsMeta): pass diff --git a/src/pybind/mgr/dashboard/tests/__init__.py b/src/pybind/mgr/dashboard/tests/__init__.py index 421378a155d..de25866695d 100644 --- a/src/pybind/mgr/dashboard/tests/__init__.py +++ b/src/pybind/mgr/dashboard/tests/__init__.py @@ -5,7 +5,6 @@ from __future__ import absolute_import import json import logging import threading -import sys import time import cherrypy @@ -94,11 +93,7 @@ class FakeFsMixin(object): fs = fake_filesystem.FakeFilesystem() f_open = fake_filesystem.FakeFileOpen(fs) f_os = fake_filesystem.FakeOsModule(fs) - - if sys.version_info > (3, 0): - builtins_open = 'builtins.open' - else: - builtins_open = '__builtin__.open' + builtins_open = 'builtins.open' class ControllerTestCase(helper.CPWebCase): diff --git a/src/pybind/mgr/dashboard/tools.py b/src/pybind/mgr/dashboard/tools.py index 2b6d92ca55f..cfa206c1238 100644 --- a/src/pybind/mgr/dashboard/tools.py +++ b/src/pybind/mgr/dashboard/tools.py @@ -1,10 +1,8 @@ # -*- coding: utf-8 -*- from __future__ import absolute_import -import sys import inspect import json -import functools import ipaddress import logging @@ -14,14 +12,9 @@ from distutils.util import strtobool import fnmatch import time import threading -import six -from six.moves import urllib -import cherrypy +import urllib -try: - from urlparse import urljoin -except ImportError: - from urllib.parse import urljoin +import cherrypy from . import mgr from .exceptions import ViewCacheNoDataException @@ -694,12 +687,7 @@ def build_url(host, scheme=None, port=None): :rtype: str """ try: - try: - u_host = six.u(host) - except TypeError: - u_host = host - - ipaddress.IPv6Address(u_host) + ipaddress.IPv6Address(host) netloc = '[{}]'.format(host) except ValueError: netloc = host @@ -719,7 +707,7 @@ def prepare_url_prefix(url_prefix): """ return '' if no prefix, or '/prefix' without slash in the end. """ - url_prefix = urljoin('/', url_prefix) + url_prefix = urllib.parse.urljoin('/', url_prefix) return url_prefix.rstrip('/') @@ -757,20 +745,6 @@ def dict_get(obj, path, default=None): return current -if sys.version_info > (3, 0): - wraps = functools.wraps - _getargspec = inspect.getfullargspec -else: - def wraps(func): - def decorator(wrapper): - new_wrapper = functools.wraps(func)(wrapper) - new_wrapper.__wrapped__ = func # set __wrapped__ even for Python 2 - return new_wrapper - return decorator - - _getargspec = inspect.getargspec - - def getargspec(func): try: while True: @@ -778,7 +752,7 @@ def getargspec(func): except AttributeError: pass # pylint: disable=deprecated-method - return _getargspec(func) + return inspect.getfullargspec(func) def str_to_bool(val): diff --git a/src/pybind/mgr/dashboard/tox.ini b/src/pybind/mgr/dashboard/tox.ini index 6962d9b4bd7..f611ace1623 100644 --- a/src/pybind/mgr/dashboard/tox.ini +++ b/src/pybind/mgr/dashboard/tox.ini @@ -137,7 +137,5 @@ commands = [testenv:check] -deps = - six==1.14.0 commands = python ci/check_grafana_uids.py frontend/src/app ../../../../monitoring/grafana/dashboards -- 2.39.5