Skip to content

Commit

Permalink
Replace 'import urllib' and 'import urllib.x' with 'from urllib impor…
Browse files Browse the repository at this point in the history
…t x' for vendor compatibility

Signed-off-by: Velichka Atanasova <avelichka@vmware.com>
  • Loading branch information
avelichka committed Apr 8, 2021
1 parent 51897fa commit f51ac22
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 20 deletions.
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,7 +43,7 @@
import logging
import unittest
import sys
import urllib
from urllib import request

import tuf
import tuf.formats
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.
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.
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
6 changes: 3 additions & 3 deletions tests/test_endless_data_attack.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
import logging
import unittest
import sys
import urllib
from urllib import request

import tuf
import tuf.formats
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.
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.
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
4 changes: 2 additions & 2 deletions tests/test_indefinite_freeze_attack.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
import logging
import unittest
import sys
import urllib
from urllib import request

if sys.version_info >= (3, 3):
import unittest.mock as mock
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')

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
7 changes: 4 additions & 3 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 urllib


# 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.
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.
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
4 changes: 2 additions & 2 deletions tuf/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
from __future__ import unicode_literals

import logging
import urllib
import timeit
import tempfile
from urllib import parse

import securesystemslib # pylint: disable=unused-import
from securesystemslib import formats as sslib_formats
Expand Down Expand Up @@ -185,7 +185,7 @@ def _download_file(url, required_length, fetcher, STRICT_REQUIRED_LENGTH=True):
# This converts it to the common format. unquote() replaces %xx escapes in a
# url with their single-character equivalent. A back-slash may be encoded as
# %5c in the url, which should also be replaced with a forward slash.
url = urllib.parse.unquote(url).replace('\\', '/')
url = parse.unquote(url).replace('\\', '/')
logger.info('Downloading: ' + repr(url))

# This is the temporary file that we will return to contain the contents of
Expand Down
4 changes: 2 additions & 2 deletions tuf/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from __future__ import division
from __future__ import unicode_literals

import urllib
from urllib import parse

import logging
logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -299,7 +299,7 @@ def __str__(self):
for mirror_url, mirror_error in self.mirror_errors.items():
try:
# http://docs.python.org/2/library/urlparse.html#urlparse.urlparse
mirror_url_tokens = urllib.parse.urlparse(mirror_url)
mirror_url_tokens = parse.urlparse(mirror_url)

except Exception:
logger.exception('Failed to parse mirror URL: ' + repr(mirror_url))
Expand Down
6 changes: 3 additions & 3 deletions tuf/mirrors.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from __future__ import unicode_literals

import os
import urllib
from urllib import parse

import securesystemslib # pylint: disable=unused-import
from securesystemslib import exceptions as sslib_exceptions
Expand Down Expand Up @@ -114,12 +114,12 @@ def get_list_of_mirrors(file_type, file_path, mirrors_dict):
confined_target_dirs):
continue

# urllib.quote(string) replaces special characters in string using the %xx
# parse.quote(string) replaces special characters in string using the %xx
# escape. This is done to avoid parsing issues of the URL on the server
# side. Do *NOT* pass URLs with Unicode characters without first encoding
# the URL as UTF-8. We need a long-term solution with #61.
# http://bugs.python.org/issue1712522
file_path = urllib.parse.quote(file_path)
file_path = parse.quote(file_path)
url = os.path.join(mirror_info['url_prefix'], path, file_path)

# The above os.path.join() result as well as input file_path may be
Expand Down
4 changes: 2 additions & 2 deletions tuf/requests_fetcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import requests
import logging
import time
import urllib
from urllib import parse
from urllib3.exceptions import ReadTimeoutError

import tuf
Expand Down Expand Up @@ -137,7 +137,7 @@ def _get_session(self, url):
"""
# Use a different requests.Session per schema+hostname combination, to
# reuse connections while minimizing subtle security issues.
parsed_url = urllib.parse.urlparse(url)
parsed_url = parse.urlparse(url)

if not parsed_url.scheme or not parsed_url.hostname:
raise exceptions.URLParsingError(
Expand Down

0 comments on commit f51ac22

Please sign in to comment.