Skip to content

Commit ca7a46b

Browse files
committed
Stop using six library
Since we've dropped support for Python 2.7, it's time to look at the bright future that Python 3.x will bring and stop forcing compatibility with older versions. This patch removes the six library from requirements, not looking back. Change-Id: I4795417aa649be75ba7162a8cf30eacbb88c7b5e
1 parent 8e246f4 commit ca7a46b

File tree

14 files changed

+60
-88
lines changed

14 files changed

+60
-88
lines changed

ironic_python_agent/agent.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import socket
2020
import threading
2121
import time
22+
from urllib import parse as urlparse
2223
from wsgiref import simple_server
2324

2425
from ironic_lib import exception as lib_exc
@@ -29,7 +30,6 @@
2930
from oslo_log import log
3031
from oslo_utils import netutils
3132
import pkg_resources
32-
from six.moves.urllib import parse as urlparse
3333
from stevedore import extension
3434

3535
from ironic_python_agent.api import app

ironic_python_agent/api/controllers/v1/base.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
# License for the specific language governing permissions and limitations
1313
# under the License.
1414

15-
import six
1615
from wsme import types as wtypes
1716

1817

@@ -67,7 +66,7 @@ def validate(self, value):
6766
type=self.types, value=type(value)))
6867

6968

70-
json_type = MultiType(list, dict, six.integer_types, wtypes.text)
69+
json_type = MultiType(list, dict, int, wtypes.text)
7170

7271

7372
class APIBase(wtypes.Base):

ironic_python_agent/extensions/base.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
from oslo_log import log
2121
from oslo_utils import uuidutils
22-
import six
2322

2423
from ironic_python_agent import encoding
2524
from ironic_python_agent import errors
@@ -90,8 +89,7 @@ def __init__(self, command_name, command_params, success, result_or_error):
9089

9190
super(SyncCommandResult, self).__init__(command_name,
9291
command_params)
93-
94-
if isinstance(result_or_error, (bytes, six.text_type)):
92+
if isinstance(result_or_error, (bytes, str)):
9593
result_key = 'result' if success else 'error'
9694
result_or_error = {result_key: result_or_error}
9795

@@ -159,7 +157,7 @@ def run(self):
159157
try:
160158
result = self.execute_method(**self.command_params)
161159

162-
if isinstance(result, (bytes, six.text_type)):
160+
if isinstance(result, (bytes, str)):
163161
result = {'result': '{}: {}'.format(self.command_name, result)}
164162
LOG.info('Command: %(name)s, result: %(result)s',
165163
{'name': self.command_name, 'result': result})
@@ -282,7 +280,7 @@ def async_command(command_name, validator=None):
282280
def async_decorator(func):
283281
func.command_name = command_name
284282

285-
@six.wraps(func)
283+
@functools.wraps(func)
286284
def wrapper(self, **command_params):
287285
# Run a validator before passing everything off to async.
288286
# validators should raise exceptions or return silently.
@@ -311,7 +309,7 @@ def sync_command(command_name, validator=None):
311309
def sync_decorator(func):
312310
func.command_name = command_name
313311

314-
@six.wraps(func)
312+
@functools.wraps(func)
315313
def wrapper(self, **command_params):
316314
# Run a validator before invoking the function.
317315
# validators should raise exceptions or return silently.

ironic_python_agent/extensions/standby.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,14 @@
1616
import os
1717
import tempfile
1818
import time
19+
from urllib import parse as urlparse
1920

2021
from ironic_lib import disk_utils
2122
from ironic_lib import exception
2223
from oslo_concurrency import processutils
2324
from oslo_config import cfg
2425
from oslo_log import log
2526
import requests
26-
import six
27-
from six.moves.urllib import parse as urlparse
2827

2928
from ironic_python_agent import errors
3029
from ironic_python_agent.extensions import base
@@ -97,8 +96,8 @@ def _fetch_checksum(checksum, image_info):
9796
return lines[0]
9897

9998
# FIXME(dtantsur): can we assume the same name for all images?
100-
expected_fname = os.path.basename(
101-
urlparse.urlparse(image_info['urls'][0]).path)
99+
expected_fname = os.path.basename(urlparse.urlparse(
100+
image_info['urls'][0]).path)
102101
for line in lines:
103102
checksum, fname = line.strip().split(None, 1)
104103
# The star symbol designates binary mode, which is the same as text
@@ -393,7 +392,7 @@ def _validate_image_info(ext, image_info=None, **kwargs):
393392
'Image \'urls\' must be a list with at least one element.')
394393

395394
if 'checksum' in image_info:
396-
if (not isinstance(image_info['checksum'], six.string_types)
395+
if (not isinstance(image_info['checksum'], str)
397396
or not image_info['checksum']):
398397
raise errors.InvalidCommandParamsError(
399398
'Image \'checksum\' must be a non-empty string.')
@@ -402,11 +401,11 @@ def _validate_image_info(ext, image_info=None, **kwargs):
402401
os_hash_algo = image_info.get('os_hash_algo')
403402
os_hash_value = image_info.get('os_hash_value')
404403
if os_hash_algo or os_hash_value:
405-
if (not isinstance(os_hash_algo, six.string_types) or
404+
if (not isinstance(os_hash_algo, str) or
406405
not os_hash_algo):
407406
raise errors.InvalidCommandParamsError(
408407
'Image \'os_hash_algo\' must be a non-empty string.')
409-
if (not isinstance(os_hash_value, six.string_types) or
408+
if (not isinstance(os_hash_value, str) or
410409
not os_hash_value):
411410
raise errors.InvalidCommandParamsError(
412411
'Image \'os_hash_value\' must be a non-empty string.')

ironic_python_agent/hardware.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import pint
3232
import psutil
3333
import pyudev
34-
import six
3534
import stevedore
3635
import yaml
3736

@@ -510,8 +509,7 @@ def __init__(self, current_boot_mode, pxe_interface=None):
510509
self.pxe_interface = pxe_interface
511510

512511

513-
@six.add_metaclass(abc.ABCMeta)
514-
class HardwareManager(object):
512+
class HardwareManager(object, metaclass=abc.ABCMeta):
515513
@abc.abstractmethod
516514
def evaluate_hardware_support(self):
517515
pass
@@ -1865,11 +1863,8 @@ def _get_managers():
18651863

18661864
# There will always be at least one extension available (the
18671865
# GenericHardwareManager).
1868-
if six.PY2:
1869-
extensions = sorted(extension_manager, _compare_extensions)
1870-
else:
1871-
extensions = sorted(extension_manager,
1872-
key=functools.cmp_to_key(_compare_extensions))
1866+
extensions = sorted(extension_manager,
1867+
key=functools.cmp_to_key(_compare_extensions))
18731868

18741869
preferred_managers = []
18751870

ironic_python_agent/tests/unit/extensions/test_standby.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def test_image_location(self):
150150
expected_loc = os.path.join(tempfile.gettempdir(), 'fake_id')
151151
self.assertEqual(expected_loc, location)
152152

153-
@mock.patch('six.moves.builtins.open', autospec=True)
153+
@mock.patch('builtins.open', autospec=True)
154154
@mock.patch('ironic_python_agent.utils.execute', autospec=True)
155155
def test_write_image(self, execute_mock, open_mock):
156156
image_info = _build_fake_image_info()
@@ -175,7 +175,7 @@ def test_write_image(self, execute_mock, open_mock):
175175
execute_mock.assert_called_once_with(*command, check_exit_code=[0])
176176

177177
@mock.patch.object(hardware, 'dispatch_to_managers', autospec=True)
178-
@mock.patch('six.moves.builtins.open', autospec=True)
178+
@mock.patch('builtins.open', autospec=True)
179179
@mock.patch('ironic_python_agent.utils.execute', autospec=True)
180180
@mock.patch('ironic_lib.disk_utils.get_image_mb', autospec=True)
181181
@mock.patch('ironic_lib.disk_utils.work_on_disk', autospec=True)
@@ -221,7 +221,7 @@ def test_write_partition_image_exception(self, work_on_disk_mock,
221221
cpu_arch=cpu_arch)
222222

223223
@mock.patch.object(hardware, 'dispatch_to_managers', autospec=True)
224-
@mock.patch('six.moves.builtins.open', autospec=True)
224+
@mock.patch('builtins.open', autospec=True)
225225
@mock.patch('ironic_python_agent.utils.execute', autospec=True)
226226
@mock.patch('ironic_lib.disk_utils.get_image_mb', autospec=True)
227227
@mock.patch('ironic_lib.disk_utils.work_on_disk', autospec=True)
@@ -271,7 +271,7 @@ def test_write_partition_image_no_node_uuid(self, work_on_disk_mock,
271271
self.assertIsNone(node_uuid)
272272

273273
@mock.patch.object(hardware, 'dispatch_to_managers', autospec=True)
274-
@mock.patch('six.moves.builtins.open', autospec=True)
274+
@mock.patch('builtins.open', autospec=True)
275275
@mock.patch('ironic_python_agent.utils.execute', autospec=True)
276276
@mock.patch('ironic_lib.disk_utils.get_image_mb', autospec=True)
277277
@mock.patch('ironic_lib.disk_utils.work_on_disk', autospec=True)
@@ -296,7 +296,7 @@ def test_write_partition_image_exception_image_mb(self,
296296
self.assertFalse(work_on_disk_mock.called)
297297

298298
@mock.patch.object(hardware, 'dispatch_to_managers', autospec=True)
299-
@mock.patch('six.moves.builtins.open', autospec=True)
299+
@mock.patch('builtins.open', autospec=True)
300300
@mock.patch('ironic_python_agent.utils.execute', autospec=True)
301301
@mock.patch('ironic_lib.disk_utils.work_on_disk', autospec=True)
302302
@mock.patch('ironic_lib.disk_utils.get_image_mb', autospec=True)
@@ -340,7 +340,7 @@ def test_write_partition_image(self, image_mb_mock, work_on_disk_mock,
340340
self.assertEqual(expected_uuid, work_on_disk_mock.return_value)
341341

342342
@mock.patch('hashlib.md5', autospec=True)
343-
@mock.patch('six.moves.builtins.open', autospec=True)
343+
@mock.patch('builtins.open', autospec=True)
344344
@mock.patch('requests.get', autospec=True)
345345
def test_download_image(self, requests_mock, open_mock, md5_mock):
346346
image_info = _build_fake_image_info()
@@ -363,7 +363,7 @@ def test_download_image(self, requests_mock, open_mock, md5_mock):
363363
self.assertEqual(2, write.call_count)
364364

365365
@mock.patch('hashlib.md5', autospec=True)
366-
@mock.patch('six.moves.builtins.open', autospec=True)
366+
@mock.patch('builtins.open', autospec=True)
367367
@mock.patch('requests.get', autospec=True)
368368
@mock.patch.dict(os.environ, {})
369369
def test_download_image_proxy(
@@ -403,7 +403,7 @@ def test_download_image_bad_status(self, requests_mock):
403403
image_info)
404404

405405
@mock.patch('hashlib.md5', autospec=True)
406-
@mock.patch('six.moves.builtins.open', autospec=True)
406+
@mock.patch('builtins.open', autospec=True)
407407
@mock.patch('requests.get', autospec=True)
408408
def test_download_image_verify_fails(self, requests_mock, open_mock,
409409
md5_mock):
@@ -417,7 +417,7 @@ def test_download_image_verify_fails(self, requests_mock, open_mock,
417417
image_info)
418418

419419
@mock.patch('hashlib.md5', autospec=True)
420-
@mock.patch('six.moves.builtins.open', autospec=True)
420+
@mock.patch('builtins.open', autospec=True)
421421
@mock.patch('requests.get', autospec=True)
422422
def test_verify_image_success(self, requests_mock, open_mock, md5_mock):
423423
image_info = _build_fake_image_info()
@@ -430,7 +430,7 @@ def test_verify_image_success(self, requests_mock, open_mock, md5_mock):
430430
image_download.verify_image(image_location)
431431

432432
@mock.patch('hashlib.new', autospec=True)
433-
@mock.patch('six.moves.builtins.open', autospec=True)
433+
@mock.patch('builtins.open', autospec=True)
434434
@mock.patch('requests.get', autospec=True)
435435
def test_verify_image_success_with_new_hash_fields(self, requests_mock,
436436
open_mock,
@@ -448,7 +448,7 @@ def test_verify_image_success_with_new_hash_fields(self, requests_mock,
448448
hashlib_mock.assert_called_with('sha512')
449449

450450
@mock.patch('hashlib.new', autospec=True)
451-
@mock.patch('six.moves.builtins.open', autospec=True)
451+
@mock.patch('builtins.open', autospec=True)
452452
@mock.patch('requests.get', autospec=True)
453453
def test_verify_image_success_without_md5(self, requests_mock,
454454
open_mock, hashlib_mock):
@@ -466,7 +466,7 @@ def test_verify_image_success_without_md5(self, requests_mock,
466466
hashlib_mock.assert_called_with('sha512')
467467

468468
@mock.patch('hashlib.md5', autospec=True)
469-
@mock.patch('six.moves.builtins.open', autospec=True)
469+
@mock.patch('builtins.open', autospec=True)
470470
@mock.patch('requests.get', autospec=True)
471471
def test_verify_image_success_with_md5_fallback(self, requests_mock,
472472
open_mock, md5_mock):
@@ -482,7 +482,7 @@ def test_verify_image_success_with_md5_fallback(self, requests_mock,
482482
image_download.verify_image(image_location)
483483

484484
@mock.patch('hashlib.new', autospec=True)
485-
@mock.patch('six.moves.builtins.open', autospec=True)
485+
@mock.patch('builtins.open', autospec=True)
486486
@mock.patch('requests.get', autospec=True)
487487
def test_verify_image_failure_with_new_hash_fields(self, requests_mock,
488488
open_mock,
@@ -502,7 +502,7 @@ def test_verify_image_failure_with_new_hash_fields(self, requests_mock,
502502
hashlib_mock.assert_called_with('sha512')
503503

504504
@mock.patch('hashlib.md5', autospec=True)
505-
@mock.patch('six.moves.builtins.open', autospec=True)
505+
@mock.patch('builtins.open', autospec=True)
506506
@mock.patch('requests.get', autospec=True)
507507
def test_verify_image_failure(self, requests_mock, open_mock, md5_mock):
508508
image_info = _build_fake_image_info()
@@ -517,7 +517,7 @@ def test_verify_image_failure(self, requests_mock, open_mock, md5_mock):
517517
image_location)
518518

519519
@mock.patch('hashlib.new', autospec=True)
520-
@mock.patch('six.moves.builtins.open', autospec=True)
520+
@mock.patch('builtins.open', autospec=True)
521521
@mock.patch('requests.get', autospec=True)
522522
def test_verify_image_failure_without_fallback(self, requests_mock,
523523
open_mock, hashlib_mock):
@@ -1047,7 +1047,7 @@ def test_cache_and_write_image(self, download_mock, write_mock):
10471047
write_mock.assert_called_once_with(image_info, device)
10481048

10491049
@mock.patch('hashlib.md5', autospec=True)
1050-
@mock.patch('six.moves.builtins.open', autospec=True)
1050+
@mock.patch('builtins.open', autospec=True)
10511051
@mock.patch('requests.get', autospec=True)
10521052
def test_stream_raw_image_onto_device(self, requests_mock, open_mock,
10531053
md5_mock):
@@ -1070,7 +1070,7 @@ def test_stream_raw_image_onto_device(self, requests_mock, open_mock,
10701070
file_mock.write.assert_has_calls(expected_calls)
10711071

10721072
@mock.patch('hashlib.md5', autospec=True)
1073-
@mock.patch('six.moves.builtins.open', autospec=True)
1073+
@mock.patch('builtins.open', autospec=True)
10741074
@mock.patch('requests.get', autospec=True)
10751075
def test_stream_raw_image_onto_device_write_error(self, requests_mock,
10761076
open_mock, md5_mock):

ironic_python_agent/tests/unit/hardware_managers/test_cna.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def test_disable_embedded_lldp_agent_in_cna_card(self, mock_exists,
8484
mock_exists.return_value = True
8585
mock_listdir.return_value = ['foo', 'bar']
8686
write_mock = mock.mock_open()
87-
with mock.patch('six.moves.builtins.open', write_mock, create=True):
87+
with mock.patch('builtins.open', write_mock, create=True):
8888
cna._disable_embedded_lldp_agent_in_cna_card()
8989
write_mock().write.assert_called_with('lldp stop')
9090
self.assertFalse(mock_log.warning.called)
@@ -107,7 +107,7 @@ def test_disable_embedded_lldp_agent_write_error(self, mock_exists,
107107
listdir_dict = ['foo', 'bar']
108108
mock_listdir.return_value = listdir_dict
109109
write_mock = mock.mock_open()
110-
with mock.patch('six.moves.builtins.open', write_mock, create=True):
110+
with mock.patch('builtins.open', write_mock, create=True):
111111
write_mock.side_effect = IOError('fake error')
112112
cna._disable_embedded_lldp_agent_in_cna_card()
113113
expected_log_message = ('Failed to disable the embedded LLDP on '

0 commit comments

Comments
 (0)