From 18bc5a5ad7bcf386c1fd8065adeb40b09341fb35 Mon Sep 17 00:00:00 2001 From: Steve Arnold Date: Fri, 5 Apr 2024 18:04:41 -0700 Subject: [PATCH] fix: dev: replicate cleanup commits for six and py2, add flake8 cfg Signed-off-by: Steve Arnold --- src/bmaptool/BmapCopy.py | 9 ++++----- src/bmaptool/TransRead.py | 39 +++++++++++++++------------------------ tests/test_api_base.py | 2 +- tests/test_filemap.py | 2 +- tox.ini | 11 +++++++++++ 5 files changed, 32 insertions(+), 31 deletions(-) diff --git a/src/bmaptool/BmapCopy.py b/src/bmaptool/BmapCopy.py index dd21202..ac1fc1d 100644 --- a/src/bmaptool/BmapCopy.py +++ b/src/bmaptool/BmapCopy.py @@ -65,9 +65,8 @@ import datetime from typing import List, Optional -from six import reraise -from six.moves import queue as Queue -from six.moves import _thread as thread +import queue +import _thread as thread from defusedxml import DefusedXmlException from defusedxml.ElementTree import parse @@ -687,7 +686,7 @@ def copy(self, sync=True, verify=True): # Create the queue for block batches and start the reader thread, which # will read the image in batches and put the results to '_batch_queue'. - self._batch_queue = Queue.Queue(self._batch_queue_len) + self._batch_queue = queue.Queue(self._batch_queue_len) thread.start_new_thread(self._get_data, (verify,)) blocks_written = 0 @@ -717,7 +716,7 @@ def copy(self, sync=True, verify=True): # The reader thread encountered an error and passed us the # exception. exc_info = batch[1] - reraise(exc_info[0], exc_info[1], exc_info[2]) + raise exc_info[1] (start, end, buf) = batch[1:4] diff --git a/src/bmaptool/TransRead.py b/src/bmaptool/TransRead.py index 3936157..011e520 100644 --- a/src/bmaptool/TransRead.py +++ b/src/bmaptool/TransRead.py @@ -33,8 +33,12 @@ import threading import subprocess import netrc -from six.moves.urllib import parse as urlparse -from bmaptool import BmapHelpers +import http.client +import urllib.error +import urllib.parse +import urllib.request + +from . import BmapHelpers _log = logging.getLogger(__name__) # pylint: disable=C0103 @@ -578,13 +582,7 @@ def _print_warning(timeout): "proxy configured correctly? Keep trying ..." % timeout ) - import socket - - from six.moves import http_client as httplib - from six.moves.urllib import request as urllib - from six.moves.urllib.error import URLError - - parsed_url = urlparse.urlparse(url) + parsed_url = urllib.parse.urlparse(url) if parsed_url.scheme == "ssh": # Unfortunately, urllib2 does not handle "ssh://" URLs @@ -615,15 +613,15 @@ def _print_warning(timeout): new_url[1] = "%s:%s" % (parsed_url.hostname, parsed_url.port) else: new_url[1] = parsed_url.hostname - url = urlparse.urlunparse(new_url) + url = urllib.parse.urlunparse(new_url) # Build an URL opener which will do the authentication - password_manager = urllib.HTTPPasswordMgrWithDefaultRealm() + password_manager = urllib.request.HTTPPasswordMgrWithDefaultRealm() password_manager.add_password(None, url, username, password) - auth_handler = urllib.HTTPBasicAuthHandler(password_manager) - opener = urllib.build_opener(auth_handler) + auth_handler = urllib.request.HTTPBasicAuthHandler(password_manager) + opener = urllib.request.build_opener(auth_handler) else: - opener = urllib.build_opener() + opener = urllib.request.build_opener() opener.addheaders = [("User-Agent", "Mozilla/5.0")] urllib.install_opener(opener) @@ -639,21 +637,14 @@ def _print_warning(timeout): for timeout in (10, None): try: f_obj = opener.open(url, timeout=timeout) - # Handling the timeout case in Python 2.7 - except socket.timeout as err: + except urllib.error.URLError as err: if timeout is not None: _print_warning(timeout) else: raise Error("cannot open URL '%s': %s" % (url, err)) - except URLError as err: - # Handling the timeout case in Python 2.6 - if timeout is not None and isinstance(err.reason, socket.timeout): - _print_warning(timeout) - else: - raise Error("cannot open URL '%s': %s" % (url, err)) - except (IOError, ValueError, httplib.InvalidURL) as err: + except (IOError, ValueError, http.client.InvalidURL) as err: raise Error("cannot open URL '%s': %s" % (url, err)) - except httplib.BadStatusLine: + except http.client.BadStatusLine: raise Error( "cannot open URL '%s': server responds with an " "HTTP status code that we don't understand" % url diff --git a/tests/test_api_base.py b/tests/test_api_base.py index a48d8de..8f33ee0 100644 --- a/tests/test_api_base.py +++ b/tests/test_api_base.py @@ -34,7 +34,7 @@ import tempfile import filecmp import subprocess -from six.moves import zip_longest +from itertools import zip_longest from tests import helpers from bmaptool import BmapHelpers, BmapCreate, Filemap diff --git a/tests/test_filemap.py b/tests/test_filemap.py index 795b588..8c90631 100644 --- a/tests/test_filemap.py +++ b/tests/test_filemap.py @@ -29,7 +29,7 @@ import random import itertools import tests.helpers -from six.moves import zip_longest +from itertools import zip_longest from bmaptool import Filemap import pytest diff --git a/tox.ini b/tox.ini index 0ae68ee..75552b4 100644 --- a/tox.ini +++ b/tox.ini @@ -247,3 +247,14 @@ deps = commands = python -m isort src/bmaptool/ + +[flake8] +extend-ignore = E203 +max-line-length = 88 +exclude = + .git, + __pycache__, + debian, + build, + dist +max-complexity = 20