Skip to content

Commit

Permalink
Remove use of six
Browse files Browse the repository at this point in the history
Remove use of six

Signed-off-by: Velichka Atanasova <avelichka@vmware.com>

Replace the use of dict.items(mydict) with mydict.items(), dict.keys(mydict) with mydict.keys() and dict.values(mydict) with mydict.values()

Signed-off-by: Velichka Atanasova <avelichka@vmware.com>

Replace 'import urllib' and 'import urllib.x' with 'from urllib import x' for vendor compatibility

Signed-off-by: Velichka Atanasova <avelichka@vmware.com>
  • Loading branch information
avelichka committed Apr 9, 2021
1 parent 5220786 commit d8b3554
Show file tree
Hide file tree
Showing 33 changed files with 99 additions and 127 deletions.
4 changes: 2 additions & 2 deletions pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ ignore-on-opaque-inference=yes
# List of class names for which member attributes should not be checked (useful
# for classes with dynamically set attributes). This supports the use of
# qualified names.
ignored-classes=optparse.Values,thread._local,_thread._local, six.moves
ignored-classes=optparse.Values,thread._local,_thread._local

# List of module names for which member attributes should not be checked
# (useful for modules/projects where namespaces are manipulated during runtime
Expand Down Expand Up @@ -334,7 +334,7 @@ init-import=no

# List of qualified module names which can have objects that can redefine
# builtins.
redefining-builtins-modules=six.moves,future.builtins
redefining-builtins-modules=future.builtins


[CLASSES]
Expand Down
1 change: 0 additions & 1 deletion requirements-pinned.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@ pycparser==2.20 # via cffi
pynacl==1.4.0 # via securesystemslib
requests==2.25.1
securesystemslib[crypto,pynacl]==0.20.0
six==1.15.0
urllib3==1.26.4 # via requests
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,3 @@
#
securesystemslib[crypto, pynacl]
requests
six
3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,7 @@
python_requires="~=3.6",
install_requires = [
'requests>=2.19.1',
'securesystemslib>=0.20.0',
'six>=1.11.0'
'securesystemslib>=0.20.0'
],
packages = find_packages(exclude=['tests']),
scripts = [
Expand Down
6 changes: 3 additions & 3 deletions tests/simple_https_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
import sys
import ssl
import os
import six
import http.server

keyfile = os.path.join('ssl_certs', 'ssl_cert.key')
certfile = os.path.join('ssl_certs', 'ssl_cert.crt')
Expand All @@ -49,8 +49,8 @@
if len(sys.argv) > 1 and os.path.exists(sys.argv[1]):
certfile = sys.argv[1]

httpd = six.moves.BaseHTTPServer.HTTPServer(('localhost', 0),
six.moves.SimpleHTTPServer.SimpleHTTPRequestHandler)
httpd = http.server.HTTPServer(('localhost', 0),
http.server.SimpleHTTPRequestHandler)

httpd.socket = ssl.wrap_socket(
httpd.socket, keyfile=keyfile, certfile=certfile, server_side=True)
Expand Down
9 changes: 4 additions & 5 deletions tests/simple_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@

import sys
import random

import six
from six.moves.SimpleHTTPServer import SimpleHTTPRequestHandler
import socketserver
from http.server import SimpleHTTPRequestHandler


class QuietHTTPRequestHandler(SimpleHTTPRequestHandler):
Expand All @@ -64,9 +63,9 @@ def log_request(self, code='-', size='-'):

# Allow re-use so you can re-run tests as often as you want even if the
# tests re-use ports. Otherwise TCP TIME-WAIT prevents reuse for ~1 minute
six.moves.socketserver.TCPServer.allow_reuse_address = True
socketserver.TCPServer.allow_reuse_address = True

httpd = six.moves.socketserver.TCPServer(('localhost', 0), handler)
httpd = socketserver.TCPServer(('localhost', 0), handler)
port_message = 'bind succeeded, server port is: ' \
+ str(httpd.server_address[1])
print(port_message)
Expand Down
7 changes: 3 additions & 4 deletions tests/slow_retrieval_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,12 @@
import os
import sys
import time

import six
import http.server



# HTTP request handler.
class Handler(six.moves.BaseHTTPServer.BaseHTTPRequestHandler):
class Handler(http.server.BaseHTTPRequestHandler):

# Overwrite do_GET.
def do_GET(self):
Expand Down Expand Up @@ -66,7 +65,7 @@ def do_GET(self):
if __name__ == '__main__':
server_address = ('localhost', 0)

httpd = six.moves.BaseHTTPServer.HTTPServer(server_address, Handler)
httpd = http.server.HTTPServer(server_address, Handler)
port_message = 'bind succeeded, server port is: ' \
+ str(httpd.server_address[1])
print(port_message)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_arbitrary_package_attack.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import logging
import unittest
import sys
from urllib import request

import tuf
import tuf.formats
Expand All @@ -55,7 +56,6 @@
from tests import utils

import securesystemslib
import six

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -174,7 +174,7 @@ def test_without_tuf(self):
url_file = os.path.join(url_prefix, 'targets', 'file1.txt')

# On Windows, the URL portion should not contain back slashes.
six.moves.urllib.request.urlretrieve(url_file.replace('\\', '/'), client_target_path)
request.urlretrieve(url_file.replace('\\', '/'), client_target_path)

self.assertTrue(os.path.exists(client_target_path))
length, hashes = securesystemslib.util.get_file_details(client_target_path)
Expand All @@ -188,7 +188,7 @@ def test_without_tuf(self):
malicious_fileinfo = tuf.formats.make_targets_fileinfo(length, hashes)

# On Windows, the URL portion should not contain back slashes.
six.moves.urllib.request.urlretrieve(url_file.replace('\\', '/'), client_target_path)
request.urlretrieve(url_file.replace('\\', '/'), client_target_path)

length, hashes = securesystemslib.util.get_file_details(client_target_path)
download_fileinfo = tuf.formats.make_targets_fileinfo(length, hashes)
Expand Down
8 changes: 4 additions & 4 deletions tests/test_endless_data_attack.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import logging
import unittest
import sys
from urllib import request

import tuf
import tuf.formats
Expand All @@ -57,7 +58,6 @@
from tests import utils

import securesystemslib
import six

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -176,7 +176,7 @@ def test_without_tuf(self):
url_file = os.path.join(url_prefix, 'targets', 'file1.txt')

# On Windows, the URL portion should not contain backslashes.
six.moves.urllib.request.urlretrieve(url_file.replace('\\', '/'), client_target_path)
request.urlretrieve(url_file.replace('\\', '/'), client_target_path)

self.assertTrue(os.path.exists(client_target_path))
length, hashes = securesystemslib.util.get_file_details(client_target_path)
Expand All @@ -194,7 +194,7 @@ def test_without_tuf(self):
self.assertTrue(large_length > length)

# On Windows, the URL portion should not contain backslashes.
six.moves.urllib.request.urlretrieve(url_file.replace('\\', '/'), client_target_path)
request.urlretrieve(url_file.replace('\\', '/'), client_target_path)

length, hashes = securesystemslib.util.get_file_details(client_target_path)
download_fileinfo = tuf.formats.make_targets_fileinfo(length, hashes)
Expand Down Expand Up @@ -269,7 +269,7 @@ def test_with_tuf(self):
self.repository_updater.refresh()

except tuf.exceptions.NoWorkingMirrorError as exception:
for mirror_url, mirror_error in six.iteritems(exception.mirror_errors):
for mirror_url, mirror_error in exception.mirror_errors.items():
self.assertTrue(isinstance(mirror_error, securesystemslib.exceptions.Error))

else:
Expand Down
3 changes: 1 addition & 2 deletions tests/test_extraneous_dependencies_attack.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
from tests import utils

import securesystemslib
import six

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -206,7 +205,7 @@ def test_with_tuf(self):
# Verify that the specific 'tuf.exceptions.ForbiddenTargetError' exception is raised
# by each mirror.
except tuf.exceptions.NoWorkingMirrorError as exception:
for mirror_url, mirror_error in six.iteritems(exception.mirror_errors):
for mirror_url, mirror_error in exception.mirror_errors.items():
url_prefix = self.repository_mirrors['mirror1']['url_prefix']
url_file = os.path.join(url_prefix, 'metadata', 'role1.json')

Expand Down
9 changes: 4 additions & 5 deletions tests/test_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@

import securesystemslib
import securesystemslib.util
import six


class TestFormats(unittest.TestCase):
Expand Down Expand Up @@ -287,15 +286,15 @@ def test_schemas(self):

# Iterate 'valid_schemas', ensuring each 'valid_schema' correctly matches
# its respective 'schema_type'.
for schema_name, (schema_type, valid_schema) in six.iteritems(valid_schemas):
for schema_name, (schema_type, valid_schema) in valid_schemas.items():
if not schema_type.matches(valid_schema):
print('bad schema: ' + repr(valid_schema))
self.assertEqual(True, schema_type.matches(valid_schema))

# Test conditions for invalid schemas.
# Set the 'valid_schema' of 'valid_schemas' to an invalid
# value and test that it does not match 'schema_type'.
for schema_name, (schema_type, valid_schema) in six.iteritems(valid_schemas):
for schema_name, (schema_type, valid_schema) in valid_schemas.items():
invalid_schema = 0xBAD
if isinstance(schema_type, securesystemslib.schema.Integer):
invalid_schema = 'BAD'
Expand Down Expand Up @@ -779,7 +778,7 @@ def test_format_base64(self):
# Test conditions for valid arguments.
data = 'updateframework'.encode('utf-8')
self.assertEqual('dXBkYXRlZnJhbWV3b3Jr', tuf.formats.format_base64(data))
self.assertTrue(isinstance(tuf.formats.format_base64(data), six.string_types))
self.assertTrue(isinstance(tuf.formats.format_base64(data), str))

# Test conditions for invalid arguments.
self.assertRaises(securesystemslib.exceptions.FormatError, tuf.formats.format_base64, 123)
Expand All @@ -791,7 +790,7 @@ def test_parse_base64(self):
# Test conditions for valid arguments.
base64 = 'dXBkYXRlZnJhbWV3b3Jr'
self.assertEqual(b'updateframework', tuf.formats.parse_base64(base64))
self.assertTrue(isinstance(tuf.formats.parse_base64(base64), six.binary_type))
self.assertTrue(isinstance(tuf.formats.parse_base64(base64), bytes))

# Test conditions for invalid arguments.
self.assertRaises(securesystemslib.exceptions.FormatError, tuf.formats.parse_base64, 123)
Expand Down
8 changes: 4 additions & 4 deletions tests/test_indefinite_freeze_attack.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import logging
import unittest
import sys
from urllib import request

if sys.version_info >= (3, 3):
import unittest.mock as mock
Expand All @@ -71,7 +72,6 @@
from tests import utils

import securesystemslib
import six

# The repository tool is imported and logs console messages by default. Disable
# console log messages generated by this unit test.
Expand Down Expand Up @@ -223,7 +223,7 @@ def test_without_tuf(self):
url_prefix = self.repository_mirrors['mirror1']['url_prefix']
url_file = os.path.join(url_prefix, 'metadata', 'timestamp.json')

six.moves.urllib.request.urlretrieve(url_file.replace('\\', '/'), client_timestamp_path)
request.urlretrieve(url_file.replace('\\', '/'), client_timestamp_path)

length, hashes = securesystemslib.util.get_file_details(client_timestamp_path)
download_fileinfo = tuf.formats.make_targets_fileinfo(length, hashes)
Expand Down Expand Up @@ -367,7 +367,7 @@ def test_with_tuf(self):

except tuf.exceptions.NoWorkingMirrorError as e:
# Make sure the contained error is ExpiredMetadataError
for mirror_url, mirror_error in six.iteritems(e.mirror_errors):
for mirror_url, mirror_error in e.mirror_errors.items():
self.assertTrue(isinstance(mirror_error, tuf.exceptions.ExpiredMetadataError))

else:
Expand Down Expand Up @@ -427,7 +427,7 @@ def test_with_tuf(self):

except tuf.exceptions.NoWorkingMirrorError as e:
# Make sure the contained error is ExpiredMetadataError
for mirror_url, mirror_error in six.iteritems(e.mirror_errors):
for mirror_url, mirror_error in e.mirror_errors.items():
self.assertTrue(isinstance(mirror_error, tuf.exceptions.ExpiredMetadataError))
self.assertTrue(mirror_url.endswith('snapshot.json'))

Expand Down
1 change: 0 additions & 1 deletion tests/test_key_revocation_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
from tests import utils

import securesystemslib
import six

logger = logging.getLogger(__name__)
repo_tool.disable_console_log_messages()
Expand Down
4 changes: 2 additions & 2 deletions tests/test_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import os
import shutil
import sys
import importlib

import tuf
import tuf.log
Expand All @@ -35,7 +36,6 @@

from tests import utils

from six.moves import reload_module

# We explicitly create a logger which is a child of the tuf hierarchy,
# instead of using the standard getLogger(__name__) pattern, because the
Expand Down Expand Up @@ -96,7 +96,7 @@ def test_set_filehandler_log_level(self):
# Test that the log level of the file handler cannot be set because
# file logging is disabled (via tuf.settings.ENABLE_FILE_LOGGING).
tuf.settings.ENABLE_FILE_LOGGING = False
reload_module(tuf.log)
importlib.reload(tuf.log)

# Test for improperly formatted argument.
self.assertRaises(securesystemslib.exceptions.FormatError, tuf.log.set_filehandler_log_level, '123')
Expand Down
3 changes: 1 addition & 2 deletions tests/test_mix_and_match_attack.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@

from tests import utils

import six

# The repository tool is imported and logs console messages by default.
# Disable console log messages generated by this unit test.
Expand Down Expand Up @@ -227,7 +226,7 @@ def test_with_tuf(self):
# 'tuf.exceptions.BadVersionNumberError' exception is raised by
# each mirror.
except tuf.exceptions.NoWorkingMirrorError as exception:
for mirror_url, mirror_error in six.iteritems(exception.mirror_errors):
for mirror_url, mirror_error in exception.mirror_errors.items():
url_prefix = self.repository_mirrors['mirror1']['url_prefix']
url_file = os.path.join(url_prefix, 'metadata', 'role1.json')

Expand Down
3 changes: 1 addition & 2 deletions tests/test_multiple_repositories_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@

from tests import utils

import six
import securesystemslib

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -271,7 +270,7 @@ def test_repository_tool(self):
multi_repo_updater = updater.MultiRepoUpdater(self.map_file)
valid_targetinfo = multi_repo_updater.get_valid_targetinfo('file3.txt')

for my_updater, my_targetinfo in six.iteritems(valid_targetinfo):
for my_updater, my_targetinfo in valid_targetinfo.items():
my_updater.download_target(my_targetinfo, self.temporary_directory)
self.assertTrue(os.path.exists(os.path.join(self.temporary_directory, 'file3.txt')))

Expand Down
9 changes: 5 additions & 4 deletions tests/test_replay_attack.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import logging
import unittest
import sys
from urllib import request

import tuf.formats
import tuf.log
Expand All @@ -55,7 +56,7 @@
from tests import utils

import securesystemslib
import six


# The repository tool is imported and logs console messages by default.
# Disable console log messages generated by this unit test.
Expand Down Expand Up @@ -220,7 +221,7 @@ def test_without_tuf(self):
self.repository_name, 'metadata', 'current', 'timestamp.json')

# On Windows, the URL portion should not contain back slashes.
six.moves.urllib.request.urlretrieve(url_file.replace('\\', '/'), client_timestamp_path)
request.urlretrieve(url_file.replace('\\', '/'), client_timestamp_path)

length, hashes = securesystemslib.util.get_file_details(client_timestamp_path)
download_fileinfo = tuf.formats.make_targets_fileinfo(length, hashes)
Expand All @@ -233,7 +234,7 @@ def test_without_tuf(self):
shutil.move(backup_timestamp, timestamp_path)

# On Windows, the URL portion should not contain back slashes.
six.moves.urllib.request.urlretrieve(url_file.replace('\\', '/'), client_timestamp_path)
request.urlretrieve(url_file.replace('\\', '/'), client_timestamp_path)

length, hashes = securesystemslib.util.get_file_details(client_timestamp_path)
download_fileinfo = tuf.formats.make_targets_fileinfo(length, hashes)
Expand Down Expand Up @@ -312,7 +313,7 @@ def test_with_tuf(self):
# Verify that the specific 'tuf.exceptions.ReplayedMetadataError' is raised by each
# mirror.
except tuf.exceptions.NoWorkingMirrorError as exception:
for mirror_url, mirror_error in six.iteritems(exception.mirror_errors):
for mirror_url, mirror_error in exception.mirror_errors.items():
url_prefix = self.repository_mirrors['mirror1']['url_prefix']
url_file = os.path.join(url_prefix, 'metadata', 'timestamp.json')

Expand Down
Loading

0 comments on commit d8b3554

Please sign in to comment.