Skip to content

Commit

Permalink
Remove py2-only code paths
Browse files Browse the repository at this point in the history
Change-Id: Ic66b9ae89837afe31929ce07cc625dfc28314ea3
  • Loading branch information
tipabu committed Jan 13, 2025
1 parent 94d3a5d commit 128124c
Show file tree
Hide file tree
Showing 196 changed files with 1,277 additions and 2,759 deletions.
10 changes: 3 additions & 7 deletions doc/source/development_middleware.rst
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,7 @@ presented below::
from swift.common.request_helpers import get_sys_meta_prefix
from swift.proxy.controllers.base import get_container_info
from eventlet import Timeout
import six
if six.PY3:
from eventlet.green.urllib import request as urllib2
else:
from eventlet.green import urllib2
from eventlet.green.urllib import urllib_request

# x-container-sysmeta-webhook
SYSMETA_WEBHOOK = get_sys_meta_prefix('container') + 'webhook'
Expand Down Expand Up @@ -119,10 +115,10 @@ presented below::
webhook = container_info['sysmeta'].get('webhook')
if webhook:
# create a POST request with obj name as body
webhook_req = urllib2.Request(webhook, data=obj)
webhook_req = urllib_request.Request(webhook, data=obj)
with Timeout(20):
try:
urllib2.urlopen(webhook_req).read()
urllib_request.urlopen(webhook_req).read()
except (Exception, Timeout):
self.logger.exception(
'failed POST to webhook %s' % webhook)
Expand Down
1 change: 0 additions & 1 deletion lower-constraints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ PyYAML==3.12
requests==2.14.2
requests-mock==1.2.0
rfc3986==1.1.0
six==1.10.0
smmap2==2.0.3
snowballstemmer==1.2.1
stestr==2.0.0
Expand Down
1 change: 0 additions & 1 deletion py3-constraints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,6 @@ s3transfer===0.10.4;python_version>='3.8'
s3transfer===0.8.2;python_version=='3.7'
s3transfer===0.5.2;python_version=='3.6'
setuptools===75.3.0;python_version>='3.12'
six===1.16.0
smmap===5.0.1;python_version>='3.7'
smmap===5.0.0;python_version=='3.6'
stestr===4.1.0
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ greenlet>=0.3.3
PasteDeploy>=2.0.0
lxml>=4.2.3
requests>=2.14.2 # Apache-2.0
six>=1.10.0
xattr>=0.7.2;sys_platform!='win32' # MIT
PyECLib>=1.3.1,!=1.6.2,!=1.6.3 # BSD
cryptography>=2.0.2 # BSD/Apache-2.0
Expand Down
7 changes: 1 addition & 6 deletions swift/account/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@

import sqlite3

import six

from swift.common.utils import Timestamp, RESERVED_BYTE
from swift.common.db import DatabaseBroker, utf8encode, zero_like
from swift.common.db import DatabaseBroker, zero_like

DATADIR = 'accounts'

Expand Down Expand Up @@ -372,9 +370,6 @@ def list_containers_iter(self, limit, marker, end_marker, prefix,
put_timestamp, 0)
"""
delim_force_gte = False
if six.PY2:
(marker, end_marker, prefix, delimiter) = utf8encode(
marker, end_marker, prefix, delimiter)
if reverse:
# Reverse the markers if we are reversing the listing.
marker, end_marker = end_marker, marker
Expand Down
8 changes: 2 additions & 6 deletions swift/account/reaper.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import itertools

from eventlet import GreenPool, sleep, Timeout
import six

import swift.common.db
from swift.account.backend import AccountBroker, DATADIR
Expand Down Expand Up @@ -267,12 +266,9 @@ def reap_account(self, broker, partition, nodes, container_shard=None):
while containers:
try:
for (container, _junk, _junk, _junk, _junk) in containers:
if six.PY3:
container_ = container.encode('utf-8')
else:
container_ = container
this_shard = (
int(md5(container_, usedforsecurity=False)
int(md5(container.encode('utf-8'),
usedforsecurity=False)
.hexdigest(), 16) % len(nodes))
if container_shard not in (this_shard, None):
continue
Expand Down
7 changes: 2 additions & 5 deletions swift/account/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

import json

import six

from swift.common import constraints
from swift.common.middleware import listing_formats
from swift.common.swob import HTTPOk, HTTPNoContent, str_to_wsgi
Expand Down Expand Up @@ -86,12 +84,11 @@ def account_listing_response(account, req, response_content_type, broker=None,
data = []
for (name, object_count, bytes_used, put_timestamp, is_subdir) \
in account_list:
name_ = name.decode('utf8') if six.PY2 else name
if is_subdir:
data.append({'subdir': name_})
data.append({'subdir': name})
else:
data.append(
{'name': name_, 'count': object_count, 'bytes': bytes_used,
{'name': name, 'count': object_count, 'bytes': bytes_used,
'last_modified': Timestamp(put_timestamp).isoformat})
if response_content_type.endswith('/xml'):
account_list = listing_formats.account_to_xml(data, account)
Expand Down
2 changes: 1 addition & 1 deletion swift/cli/account_audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import json
from eventlet.greenpool import GreenPool
from eventlet.event import Event
from six.moves.urllib.parse import quote
from urllib.parse import quote

from swift.common.ring import Ring
from swift.common.utils import split_path
Expand Down
9 changes: 1 addition & 8 deletions swift/cli/container_deleter.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import io
import itertools
import json
import six
import time

from swift.common.internal_client import InternalClient
Expand All @@ -50,17 +49,11 @@ def make_delete_jobs(account, container, objects, timestamp):
:returns: list of dicts appropriate for an UPDATE request to an
expiring-object queue
'''
if six.PY2:
if isinstance(account, str):
account = account.decode('utf8')
if isinstance(container, str):
container = container.decode('utf8')
return [
{
'name': build_task_obj(
timestamp, account, container,
obj.decode('utf8') if six.PY2 and isinstance(obj, str)
else obj, high_precision=True),
obj, high_precision=True),
'deleted': 0,
'created_at': timestamp.internal,
'etag': MD5_OF_EMPTY_STRING,
Expand Down
6 changes: 2 additions & 4 deletions swift/cli/dispersion_populate.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@

from eventlet import GreenPool, patcher, sleep
from eventlet.pools import Pool
import six
from six.moves import range
from six.moves.configparser import ConfigParser
from configparser import ConfigParser

from swift.common.internal_client import SimpleClient
from swift.common.ring import Ring
Expand Down Expand Up @@ -53,7 +51,7 @@ def put_object(connpool, container, obj, report):
global retries_done
try:
with connpool.item() as conn:
data = io.BytesIO(obj if six.PY2 else obj.encode('utf8'))
data = io.BytesIO(obj.encode('utf8'))
conn.put_object(container, obj, data,
headers={'x-object-meta-dispersion': obj})
retries_done += conn.attempts - 1
Expand Down
2 changes: 1 addition & 1 deletion swift/cli/dispersion_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from __future__ import print_function
import json
from collections import defaultdict
from six.moves.configparser import ConfigParser
from configparser import ConfigParser
from optparse import OptionParser
from sys import exit, stdout, stderr
from time import time
Expand Down
6 changes: 2 additions & 4 deletions swift/cli/drive_audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
import sys


import six
from six.moves.configparser import ConfigParser
from configparser import ConfigParser

from swift.common.utils import backward, get_logger, dump_recon_cache, \
config_true_value
Expand Down Expand Up @@ -128,8 +127,7 @@ def get_errors(error_re, log_file_pattern, minutes, logger,
print("Unable to open " + path)
sys.exit(1)
for line in backward(f):
if not six.PY2:
line = line.decode(log_file_encoding, 'surrogateescape')
line = line.decode(log_file_encoding, 'surrogateescape')
if '[ 0.000000]' in line \
or 'KERNEL supported cpus:' in line \
or 'BIOS-provided physical RAM map:' in line:
Expand Down
9 changes: 3 additions & 6 deletions swift/cli/form_signature.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
"""
from __future__ import print_function
import hmac
import six
from hashlib import sha1
from os.path import basename
from time import time
Expand Down Expand Up @@ -95,11 +94,9 @@ def main(argv):
return 1
data = '%s\n%s\n%s\n%s\n%s' % (path, redirect, max_file_size,
max_file_count, expires)
if six.PY3:
data = data if isinstance(data, six.binary_type) else \
data.encode('utf8')
key = key if isinstance(key, six.binary_type) else \
key.encode('utf8')
data = data.encode('utf8')
key = key if isinstance(key, bytes) else \
key.encode('utf8')
sig = hmac.new(key, data,
sha1).hexdigest()
print(' Expires:', expires)
Expand Down
10 changes: 4 additions & 6 deletions swift/cli/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
import sys
from collections import defaultdict

import six
from six.moves import urllib
import urllib

from swift.common.exceptions import LockTimeout
from swift.common.utils import hash_path, storage_directory, \
Expand Down Expand Up @@ -722,10 +721,9 @@ def print_item_locations(ring, ring_name=None, account=None, container=None,


def obj_main():
if not six.PY2:
# Make stdout able to write escaped bytes
sys.stdout = codecs.getwriter("utf-8")(
sys.stdout.detach(), errors='surrogateescape')
# Make stdout able to write escaped bytes
sys.stdout = codecs.getwriter("utf-8")(
sys.stdout.detach(), errors='surrogateescape')

parser = OptionParser('%prog [options] OBJECT_FILE')
parser.add_option(
Expand Down
3 changes: 0 additions & 3 deletions swift/cli/manage_shard_ranges.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,6 @@
import time
from contextlib import contextmanager

from six.moves import input


from swift.common.utils import Timestamp, get_logger, ShardRange, readconf, \
ShardRangeList, non_negative_int, config_positive_int_value
from swift.container.backend import ContainerBroker, UNSHARDED
Expand Down
27 changes: 11 additions & 16 deletions swift/cli/recon.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
from __future__ import print_function

from eventlet.green import socket
from six import string_types
from six.moves.urllib.parse import urlparse
from urllib.parse import urlparse

from swift.common.utils import (
SWIFT_CONF_FILE, md5_hash_for_file, set_swift_dir)
Expand All @@ -30,13 +29,9 @@
import optparse
import time
import sys
import six
import os

if six.PY3:
from eventlet.green.urllib import request as urllib2
else:
from eventlet.green import urllib2
from eventlet.green.urllib import request as urllib_request


def seconds2timeunit(seconds):
Expand Down Expand Up @@ -86,19 +81,19 @@ def scout_host(self, base_url, recon_type):
"""
url = base_url + recon_type
try:
body = urllib2.urlopen(url, timeout=self.timeout).read()
if six.PY3 and isinstance(body, six.binary_type):
body = urllib_request.urlopen(url, timeout=self.timeout).read()
if isinstance(body, bytes):
body = body.decode('utf8')
content = json.loads(body)
if self.verbose:
print("-> %s: %s" % (url, content))
status = 200
except urllib2.HTTPError as err:
except urllib_request.HTTPError as err:
if not self.suppress_errors or self.verbose:
print("-> %s: %s" % (url, err))
content = err
status = err.code
except (urllib2.URLError, socket.timeout) as err:
except (urllib_request.URLError, socket.timeout) as err:
if not self.suppress_errors or self.verbose:
print("-> %s: %s" % (url, err))
content = err
Expand Down Expand Up @@ -128,19 +123,19 @@ def scout_server_type(self, host):
"""
try:
url = "http://%s:%s/" % (host[0], host[1])
req = urllib2.Request(url)
req = urllib_request.Request(url)
req.get_method = lambda: 'OPTIONS'
conn = urllib2.urlopen(req)
conn = urllib_request.urlopen(req)
header = conn.info().get('Server')
server_header = header.split('/')
content = server_header[0]
status = 200
except urllib2.HTTPError as err:
except urllib_request.HTTPError as err:
if not self.suppress_errors or self.verbose:
print("-> %s: %s" % (url, err))
content = err
status = err.code
except (urllib2.URLError, socket.timeout) as err:
except (urllib_request.URLError, socket.timeout) as err:
if not self.suppress_errors or self.verbose:
print("-> %s: %s" % (url, err))
content = err
Expand Down Expand Up @@ -1074,7 +1069,7 @@ def _get_ring_names(self, policy=None):
ring_names = [p.ring_name for p in POLICIES if (
p.name == policy or not policy or (
policy.isdigit() and int(policy) == int(p) or
(isinstance(policy, string_types)
(isinstance(policy, str)
and policy in p.aliases)))]
else:
ring_names = [self.server_type]
Expand Down
Loading

0 comments on commit 128124c

Please sign in to comment.