From f221632a82283f3e2d3c564e5baa2a083b4662ae Mon Sep 17 00:00:00 2001 From: Tim Burke Date: Thu, 29 Aug 2019 11:38:59 -0700 Subject: Port to py3 Change-Id: Ib86178259f854c15dd305dd39779a203cafb0451 --- swiftbench/bench.py | 13 +++++++++---- swiftbench/utils.py | 18 ++++++++---------- 2 files changed, 17 insertions(+), 14 deletions(-) (limited to 'swiftbench') diff --git a/swiftbench/bench.py b/swiftbench/bench.py index 696b6ad..b7f35a1 100644 --- a/swiftbench/bench.py +++ b/swiftbench/bench.py @@ -29,6 +29,8 @@ import eventlet import eventlet.pools from eventlet.green.httplib import CannotSendRequest +from six.moves import range + import swiftclient as client from swiftbench.utils import config_true_value, using_http_proxy @@ -107,12 +109,12 @@ class SourceFile(object): raise StopIteration chunk_size = min(self.size - self.pos, self.chunk_size) self.pos += chunk_size - return '0' * chunk_size + return b'0' * chunk_size def read(self, desired_size): chunk_size = min(self.size - self.pos, desired_size) self.pos += chunk_size - return '0' * chunk_size + return b'0' * chunk_size class ConnectionPool(eventlet.pools.Pool): @@ -224,7 +226,10 @@ class Bench(object): self.files = [] if self.object_sources: self.object_sources = self.object_sources.split() - self.files = [file(f, 'rb').read() for f in self.object_sources] + self.files = [] + for f in self.object_sources: + with open(f, 'rb') as fp: + self.files.append(fp.read()) self.put_concurrency = int(conf.put_concurrency) self.get_concurrency = int(conf.get_concurrency) @@ -270,7 +275,7 @@ class Bench(object): self.heartbeat -= 13 # just to get the first report quicker self.failures = 0 self.complete = 0 - for i in xrange(self.total): + for i in range(self.total): if self.aborted: break pool.spawn_n(self._run, i) diff --git a/swiftbench/utils.py b/swiftbench/utils.py index 1916ac9..df39bab 100644 --- a/swiftbench/utils.py +++ b/swiftbench/utils.py @@ -13,13 +13,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +import six import sys -from ConfigParser import ConfigParser, RawConfigParser -try: - from urllib import getproxies, proxy_bypass -except ImportError: - from urllib.request import getproxies, proxy_bypass -from urlparse import urlparse +from six.moves.configparser import ConfigParser, RawConfigParser +from six.moves.urllib.parse import urlparse +from six.moves.urllib.request import getproxies, proxy_bypass # Used when reading config values TRUE_VALUES = set(('true', '1', 'yes', 'on', 't', 'y')) @@ -51,14 +49,14 @@ def readconf(conf_path, section_name=None, log_name=None, defaults=None, else: success = c.read(conf_path) if not success: - print "Unable to read config from %s" % conf_path + print("Unable to read config from %s" % conf_path) sys.exit(1) if section_name: if c.has_section(section_name): conf = dict(c.items(section_name)) else: - print "Unable to find %s config section in %s" % \ - (section_name, conf_path) + print("Unable to find %s config section in %s" % + (section_name, conf_path)) sys.exit(1) if "log_name" not in conf: if log_name is not None: @@ -81,7 +79,7 @@ def config_true_value(value): Returns False otherwise. """ return value is True or \ - (isinstance(value, basestring) and value.lower() in TRUE_VALUES) + (isinstance(value, six.string_types) and value.lower() in TRUE_VALUES) def using_http_proxy(url): -- cgit v1.2.1