From a4f5bf6ab974c0d3fa1e0780298d6008589b14a8 Mon Sep 17 00:00:00 2001 From: Erno Kuvaja Date: Mon, 26 Jan 2015 14:09:44 +0000 Subject: [PATCH] Unify using six.moves.range rename everywhere Mainly to improve consistency, use range() from six.moves renames across glance. Behaves consistently like py2 xrange() and py3 range(). Removes unnecessary range() from glace/api/v2/images.py Change-Id: Id21f923d05600b902f2239e25ef01716c07e74a3 --- glance/api/v2/images.py | 2 +- glance/cmd/control.py | 5 +++-- glance/common/auth.py | 2 ++ glance/common/client.py | 5 +++-- glance/common/crypt.py | 2 ++ glance/db/sqlalchemy/api.py | 7 ++++--- glance/tests/functional/__init__.py | 2 ++ glance/tests/functional/db/base.py | 2 ++ .../functional/test_bin_glance_cache_manage.py | 9 +++++---- glance/tests/functional/test_cache_middleware.py | 13 +++++++------ glance/tests/functional/test_scrubber.py | 5 +++-- glance/tests/functional/v1/test_api.py | 2 ++ glance/tests/functional/v1/test_copy_to_file.py | 7 ++++--- glance/tests/functional/v1/test_multiprocessing.py | 2 ++ glance/tests/functional/v2/test_images.py | 2 ++ .../v2/test_property_quota_violations.py | 2 ++ glance/tests/unit/common/test_property_utils.py | 5 +++-- glance/tests/unit/test_cache_middleware.py | 2 ++ glance/tests/unit/test_context.py | 3 +++ glance/tests/unit/test_domain_proxy.py | 5 +++-- glance/tests/unit/test_image_cache.py | 13 +++++++------ glance/tests/unit/test_migrations.py | 2 ++ glance/tests/unit/test_misc.py | 3 +++ glance/tests/unit/test_quota.py | 2 ++ glance/tests/unit/test_scrubber.py | 2 ++ glance/tests/unit/v2/test_images_resource.py | 2 ++ glance/tests/unit/v2/test_tasks_resource.py | 2 ++ 27 files changed, 77 insertions(+), 33 deletions(-) diff --git a/glance/api/v2/images.py b/glance/api/v2/images.py index 2b02228b..bf8df39c 100644 --- a/glance/api/v2/images.py +++ b/glance/api/v2/images.py @@ -238,7 +238,7 @@ class ImagesController(object): pos = int(path_pos) elif path_pos != '-': return None - if (not allow_max) and (pos not in range(max_pos)): + if not (allow_max or 0 <= pos < max_pos): return None return pos diff --git a/glance/cmd/control.py b/glance/cmd/control.py index fe3464a7..16fd8876 100644 --- a/glance/cmd/control.py +++ b/glance/cmd/control.py @@ -41,7 +41,8 @@ if os.path.exists(os.path.join(possible_topdir, 'glance', '__init__.py')): from oslo_config import cfg from oslo_utils import units -from six.moves import xrange +# NOTE(jokke): simplified transition to py3, behaves like py2 xrange +from six.moves import range from glance.common import config from glance import i18n @@ -262,7 +263,7 @@ def do_stop(server, args, graceful=False): except OSError: print(_("Process %d not running") % pid) for pid_file, pid in pfiles: - for _junk in xrange(150): # 15 seconds + for _junk in range(150): # 15 seconds if not os.path.exists('/proc/%s' % pid): break time.sleep(0.1) diff --git a/glance/common/auth.py b/glance/common/auth.py index 5010a262..1de20294 100644 --- a/glance/common/auth.py +++ b/glance/common/auth.py @@ -30,6 +30,8 @@ Keystone (an identity management system). """ import httplib2 from oslo.serialization import jsonutils +# NOTE(jokke): simplified transition to py3, behaves like py2 xrange +from six.moves import range import six.moves.urllib.parse as urlparse from glance.common import exception diff --git a/glance/common/client.py b/glance/common/client.py index 7bc5ff47..62294fb6 100644 --- a/glance/common/client.py +++ b/glance/common/client.py @@ -42,8 +42,9 @@ except ImportError: from oslo_utils import encodeutils import six +# NOTE(jokke): simplified transition to py3, behaves like py2 xrange +from six.moves import range import six.moves.urllib.parse as urlparse -from six.moves import xrange from glance.common import auth from glance.common import exception @@ -82,7 +83,7 @@ def handle_redirects(func): @functools.wraps(func) def wrapped(self, method, url, body, headers): - for _ in xrange(MAX_REDIRECTS): + for _ in range(MAX_REDIRECTS): try: return func(self, method, url, body, headers) except exception.RedirectException as redirect: diff --git a/glance/common/crypt.py b/glance/common/crypt.py index f719d118..ae5160b0 100644 --- a/glance/common/crypt.py +++ b/glance/common/crypt.py @@ -24,6 +24,8 @@ import base64 from Crypto.Cipher import AES from Crypto import Random from Crypto.Random import random +# NOTE(jokke): simplified transition to py3, behaves like py2 xrange +from six.moves import range def urlsafe_encrypt(key, plaintext, blocksize=16): diff --git a/glance/db/sqlalchemy/api.py b/glance/db/sqlalchemy/api.py index ab4175f7..410d11eb 100644 --- a/glance/db/sqlalchemy/api.py +++ b/glance/db/sqlalchemy/api.py @@ -29,7 +29,8 @@ from oslo_utils import timeutils import osprofiler.sqlalchemy from retrying import retry import six -from six.moves import xrange +# NOTE(jokke): simplified transition to py3, behaves like py2 xrange +from six.moves import range import sqlalchemy import sqlalchemy.orm as sa_orm import sqlalchemy.sql as sa_sql @@ -361,9 +362,9 @@ def _paginate_query(query, model, limit, sort_keys, marker=None, # Build up an array of sort criteria as in the docstring criteria_list = [] - for i in xrange(len(sort_keys)): + for i in range(len(sort_keys)): crit_attrs = [] - for j in xrange(i): + for j in range(i): model_attr = getattr(model, sort_keys[j]) default = None if isinstance( model_attr.property.columns[0].type, diff --git a/glance/tests/functional/__init__.py b/glance/tests/functional/__init__.py index 78b42b23..81e2d1a0 100644 --- a/glance/tests/functional/__init__.py +++ b/glance/tests/functional/__init__.py @@ -34,6 +34,8 @@ import time import fixtures from oslo.serialization import jsonutils +# NOTE(jokke): simplified transition to py3, behaves like py2 xrange +from six.moves import range import six.moves.urllib.parse as urlparse import testtools diff --git a/glance/tests/functional/db/base.py b/glance/tests/functional/db/base.py index 495667c9..3618c257 100644 --- a/glance/tests/functional/db/base.py +++ b/glance/tests/functional/db/base.py @@ -21,6 +21,8 @@ import uuid import mock from oslo_utils import timeutils +# NOTE(jokke): simplified transition to py3, behaves like py2 xrange +from six.moves import range from glance.common import exception from glance import context diff --git a/glance/tests/functional/test_bin_glance_cache_manage.py b/glance/tests/functional/test_bin_glance_cache_manage.py index 3cd702bf..3bef8f7c 100644 --- a/glance/tests/functional/test_bin_glance_cache_manage.py +++ b/glance/tests/functional/test_bin_glance_cache_manage.py @@ -23,7 +23,8 @@ import sys import httplib2 from oslo.serialization import jsonutils from oslo_utils import units -from six.moves import xrange +# NOTE(jokke): simplified transition to py3, behaves like py2 xrange +from six.moves import range from glance.tests import functional from glance.tests.utils import execute @@ -136,7 +137,7 @@ class TestBinGlanceCacheManage(functional.FunctionalTest): # Add a few images and cache the second one of them # by GETing the image... - for x in xrange(4): + for x in range(4): ids[x] = self.add_image("Image%s" % x) path = "http://%s:%d/v1/images/%s" % ("127.0.0.1", api_port, @@ -183,7 +184,7 @@ class TestBinGlanceCacheManage(functional.FunctionalTest): # Add a few images and cache the second one of them # by GETing the image... - for x in xrange(4): + for x in range(4): ids[x] = self.add_image("Image%s" % x) # Queue second image and then cache it @@ -281,7 +282,7 @@ log_file = %(log_file)s self.assertTrue('No queued images' in out.strip()) # Queue all images - for x in xrange(4): + for x in range(4): cmd = ("%s --port=%d --force " "queue-image %s") % (exe_cmd, api_port, ids[x]) diff --git a/glance/tests/functional/test_cache_middleware.py b/glance/tests/functional/test_cache_middleware.py index 5d1e162e..7413d115 100644 --- a/glance/tests/functional/test_cache_middleware.py +++ b/glance/tests/functional/test_cache_middleware.py @@ -29,7 +29,8 @@ import time import httplib2 from oslo.serialization import jsonutils from oslo_utils import units -from six.moves import xrange +# NOTE(jokke): simplified transition to py3, behaves like py2 xrange +from six.moves import range from glance.tests import functional from glance.tests.functional.store_utils import get_http_uri @@ -528,7 +529,7 @@ class BaseCacheManageMiddlewareTest(object): ids = {} # Add a bunch of images... - for x in xrange(4): + for x in range(4): ids[x] = self.add_image("Image%s" % str(x)) # Verify no images in cached_images because no image has been hit @@ -536,7 +537,7 @@ class BaseCacheManageMiddlewareTest(object): self.verify_no_cached_images() # Grab the images, essentially caching them... - for x in xrange(4): + for x in range(4): path = "http://%s:%d/v1/images/%s" % ("127.0.0.1", self.api_port, ids[x]) http = httplib2.Http() @@ -556,7 +557,7 @@ class BaseCacheManageMiddlewareTest(object): cached_images = data['cached_images'] self.assertEqual(4, len(cached_images)) - for x in xrange(4, 0): # Cached images returned last modified order + for x in range(4, 0): # Cached images returned last modified order self.assertEqual(ids[x], cached_images[x]['image_id']) self.assertEqual(0, cached_images[x]['hits']) @@ -612,7 +613,7 @@ class BaseCacheManageMiddlewareTest(object): NUM_IMAGES = 4 # Add and then queue some images - for x in xrange(NUM_IMAGES): + for x in range(NUM_IMAGES): ids[x] = self.add_image("Image%s" % str(x)) path = "http://%s:%d/v1/queued_images/%s" % ("127.0.0.1", self.api_port, ids[x]) @@ -676,7 +677,7 @@ log_file = %(log_file)s ids = {} # Add a bunch of images... - for x in xrange(4): + for x in range(4): ids[x] = self.add_image("Image%s" % str(x)) # Queue the first image, verify no images still in cache after queueing diff --git a/glance/tests/functional/test_scrubber.py b/glance/tests/functional/test_scrubber.py index 78646e97..c0fa9ec3 100644 --- a/glance/tests/functional/test_scrubber.py +++ b/glance/tests/functional/test_scrubber.py @@ -21,7 +21,8 @@ import glance_store.location import httplib2 from oslo.serialization import jsonutils from oslo_utils import units -from six.moves import xrange +# NOTE(jokke): simplified transition to py3, behaves like py2 xrange +from six.moves import range from glance.common import crypt from glance.tests import functional @@ -271,7 +272,7 @@ class TestScrubber(functional.FunctionalTest): http = httplib2.Http() wait_for = 300 # seconds check_every = 15 # seconds - for _ in xrange(wait_for / check_every): + for _ in range(wait_for / check_every): time.sleep(check_every) response, content = http.request(path, 'HEAD') diff --git a/glance/tests/functional/v1/test_api.py b/glance/tests/functional/v1/test_api.py index 9c9c9477..9fba3bb5 100644 --- a/glance/tests/functional/v1/test_api.py +++ b/glance/tests/functional/v1/test_api.py @@ -20,6 +20,8 @@ import hashlib import httplib2 from oslo.serialization import jsonutils from oslo_utils import units +# NOTE(jokke): simplified transition to py3, behaves like py2 xrange +from six.moves import range from glance.tests import functional from glance.tests.utils import minimal_headers diff --git a/glance/tests/functional/v1/test_copy_to_file.py b/glance/tests/functional/v1/test_copy_to_file.py index ae37772e..45ff8ca0 100644 --- a/glance/tests/functional/v1/test_copy_to_file.py +++ b/glance/tests/functional/v1/test_copy_to_file.py @@ -26,7 +26,8 @@ import time import httplib2 from oslo.serialization import jsonutils from oslo_utils import units -from six.moves import xrange +# NOTE(jokke): simplified transition to py3, behaves like py2 xrange +from six.moves import range from glance.tests import functional from glance.tests.functional.store_utils import get_http_uri @@ -92,7 +93,7 @@ class TestCopyToFile(functional.FunctionalTest): copy_image_id) def _await_status(expected_status): - for i in xrange(100): + for i in range(100): time.sleep(0.01) http = httplib2.Http() response, content = http.request(path, 'HEAD') @@ -187,7 +188,7 @@ class TestCopyToFile(functional.FunctionalTest): copy_image_id) def _await_status(expected_status): - for i in xrange(100): + for i in range(100): time.sleep(0.01) http = httplib2.Http() response, content = http.request(path, 'HEAD') diff --git a/glance/tests/functional/v1/test_multiprocessing.py b/glance/tests/functional/v1/test_multiprocessing.py index bfbb88b6..2cad2087 100644 --- a/glance/tests/functional/v1/test_multiprocessing.py +++ b/glance/tests/functional/v1/test_multiprocessing.py @@ -17,6 +17,8 @@ import time import httplib2 import psutil +# NOTE(jokke): simplified transition to py3, behaves like py2 xrange +from six.moves import range from glance.tests import functional from glance.tests.utils import execute diff --git a/glance/tests/functional/v2/test_images.py b/glance/tests/functional/v2/test_images.py index 0f3447f9..7ca37c66 100644 --- a/glance/tests/functional/v2/test_images.py +++ b/glance/tests/functional/v2/test_images.py @@ -20,6 +20,8 @@ import uuid from oslo.serialization import jsonutils import requests import six +# NOTE(jokke): simplified transition to py3, behaves like py2 xrange +from six.moves import range from glance.tests import functional from glance.tests import utils as test_utils diff --git a/glance/tests/integration/v2/test_property_quota_violations.py b/glance/tests/integration/v2/test_property_quota_violations.py index 38022f0a..b772ab1f 100644 --- a/glance/tests/integration/v2/test_property_quota_violations.py +++ b/glance/tests/integration/v2/test_property_quota_violations.py @@ -15,6 +15,8 @@ from oslo.serialization import jsonutils from oslo_config import cfg +# NOTE(jokke): simplified transition to py3, behaves like py2 xrange +from six.moves import range from glance.tests.integration.v2 import base diff --git a/glance/tests/unit/common/test_property_utils.py b/glance/tests/unit/common/test_property_utils.py index e53b6cbe..a16dbcb9 100644 --- a/glance/tests/unit/common/test_property_utils.py +++ b/glance/tests/unit/common/test_property_utils.py @@ -13,7 +13,8 @@ # License for the specific language governing permissions and limitations # under the License. -from six.moves import xrange +# NOTE(jokke): simplified transition to py3, behaves like py2 xrange +from six.moves import range from glance.api import policy from glance.common import exception @@ -205,7 +206,7 @@ class TestPropertyRulesWithRoles(base.IsolatedUnitTest): file """ self.rules_checker = property_utils.PropertyRules(self.policy) - for i in xrange(len(property_utils.CONFIG.sections())): + for i in range(len(property_utils.CONFIG.sections())): self.assertEqual(property_utils.CONFIG.sections()[i], self.rules_checker.rules[i][0].pattern) diff --git a/glance/tests/unit/test_cache_middleware.py b/glance/tests/unit/test_cache_middleware.py index 40bff882..c6bc92ac 100644 --- a/glance/tests/unit/test_cache_middleware.py +++ b/glance/tests/unit/test_cache_middleware.py @@ -13,6 +13,8 @@ # License for the specific language governing permissions and limitations # under the License. +# NOTE(jokke): simplified transition to py3, behaves like py2 xrange +from six.moves import range import testtools import webob diff --git a/glance/tests/unit/test_context.py b/glance/tests/unit/test_context.py index 82c837bc..2372e303 100644 --- a/glance/tests/unit/test_context.py +++ b/glance/tests/unit/test_context.py @@ -13,6 +13,9 @@ # License for the specific language governing permissions and limitations # under the License. +# NOTE(jokke): simplified transition to py3, behaves like py2 xrange +from six.moves import range + from glance import context from glance.tests.unit import utils as unit_utils from glance.tests import utils diff --git a/glance/tests/unit/test_domain_proxy.py b/glance/tests/unit/test_domain_proxy.py index 911a745c..39b72bec 100644 --- a/glance/tests/unit/test_domain_proxy.py +++ b/glance/tests/unit/test_domain_proxy.py @@ -15,7 +15,8 @@ # under the License. import mock -from six.moves import xrange +# NOTE(jokke): simplified transition to py3, behaves like py2 xrange +from six.moves import range from glance.domain import proxy import glance.tests.utils as test_utils @@ -115,7 +116,7 @@ class TestProxyRepoWrapping(test_utils.BaseTestCase): self.assertEqual((2,), self.fake_repo.args) self.assertEqual({'prefix': 's'}, self.fake_repo.kwargs) self.assertEqual(2, len(results)) - for i in xrange(2): + for i in range(2): self.assertIsInstance(results[i], FakeProxy) self.assertEqual(self.fake_repo.result[i], results[i].base) self.assertEqual(tuple(), results[i].args) diff --git a/glance/tests/unit/test_image_cache.py b/glance/tests/unit/test_image_cache.py index 7b92142f..2730d244 100644 --- a/glance/tests/unit/test_image_cache.py +++ b/glance/tests/unit/test_image_cache.py @@ -23,7 +23,8 @@ import fixtures from oslo_utils import units from oslotest import moxstubout import six -from six.moves import xrange +# NOTE(jokke): simplified transition to py3, behaves like py2 xrange +from six.moves import range from glance.common import exception from glance import image_cache @@ -179,7 +180,7 @@ class ImageCacheTestCase(object): # prune it. We should see only 5 images left after # pruning, and the images that are least recently accessed # should be the ones pruned... - for x in xrange(10): + for x in range(10): FIXTURE_FILE = six.StringIO(FIXTURE_DATA) self.assertTrue(self.cache.cache_image_file(x, FIXTURE_FILE)) @@ -187,7 +188,7 @@ class ImageCacheTestCase(object): self.assertEqual(10 * units.Ki, self.cache.get_cache_size()) # OK, hit the images that are now cached... - for x in xrange(10): + for x in range(10): buff = six.StringIO() with self.cache.open_for_read(x) as cache_file: for chunk in cache_file: @@ -197,11 +198,11 @@ class ImageCacheTestCase(object): self.assertEqual(5 * units.Ki, self.cache.get_cache_size()) - for x in xrange(0, 5): + for x in range(0, 5): self.assertFalse(self.cache.is_cached(x), "Image %s was cached!" % x) - for x in xrange(5, 10): + for x in range(5, 10): self.assertTrue(self.cache.is_cached(x), "Image %s was not cached!" % x) @@ -264,7 +265,7 @@ class ImageCacheTestCase(object): self.cache.delete_cached_image(1) - for x in xrange(3): + for x in range(3): self.assertTrue(self.cache.queue_image(x)) self.assertEqual(['0', '1', '2'], diff --git a/glance/tests/unit/test_migrations.py b/glance/tests/unit/test_migrations.py index c8211d4c..952468ca 100644 --- a/glance/tests/unit/test_migrations.py +++ b/glance/tests/unit/test_migrations.py @@ -39,6 +39,8 @@ from oslo_db.sqlalchemy import test_base from oslo_db.sqlalchemy import test_migrations from oslo_db.sqlalchemy import utils as db_utils from oslo_utils import timeutils +# NOTE(jokke): simplified transition to py3, behaves like py2 xrange +from six.moves import range import sqlalchemy from glance.common import crypt diff --git a/glance/tests/unit/test_misc.py b/glance/tests/unit/test_misc.py index be769c25..744c00a2 100644 --- a/glance/tests/unit/test_misc.py +++ b/glance/tests/unit/test_misc.py @@ -15,6 +15,9 @@ import os +# NOTE(jokke): simplified transition to py3, behaves like py2 xrange +from six.moves import range + from glance.common import crypt from glance.common import utils from glance.tests import utils as test_utils diff --git a/glance/tests/unit/test_quota.py b/glance/tests/unit/test_quota.py index 152bbc5a..1fdebee3 100644 --- a/glance/tests/unit/test_quota.py +++ b/glance/tests/unit/test_quota.py @@ -18,6 +18,8 @@ import mock from mock import patch from oslo_utils import units import six +# NOTE(jokke): simplified transition to py3, behaves like py2 xrange +from six.moves import range from glance.common import exception from glance.common import store_utils diff --git a/glance/tests/unit/test_scrubber.py b/glance/tests/unit/test_scrubber.py index 4feeefc5..f04746dc 100644 --- a/glance/tests/unit/test_scrubber.py +++ b/glance/tests/unit/test_scrubber.py @@ -23,6 +23,8 @@ import glance_store from mock import patch from mox3 import mox from oslo_config import cfg +# NOTE(jokke): simplified transition to py3, behaves like py2 xrange +from six.moves import range from glance.common import exception from glance import scrubber diff --git a/glance/tests/unit/v2/test_images_resource.py b/glance/tests/unit/v2/test_images_resource.py index 553eb6af..7e5d550e 100644 --- a/glance/tests/unit/v2/test_images_resource.py +++ b/glance/tests/unit/v2/test_images_resource.py @@ -22,6 +22,8 @@ import mock from oslo.serialization import jsonutils from oslo_config import cfg import six +# NOTE(jokke): simplified transition to py3, behaves like py2 xrange +from six.moves import range import testtools import webob diff --git a/glance/tests/unit/v2/test_tasks_resource.py b/glance/tests/unit/v2/test_tasks_resource.py index 43812417..8a0a45d9 100644 --- a/glance/tests/unit/v2/test_tasks_resource.py +++ b/glance/tests/unit/v2/test_tasks_resource.py @@ -20,6 +20,8 @@ import uuid import mock from oslo.serialization import jsonutils from oslo_utils import timeutils +# NOTE(jokke): simplified transition to py3, behaves like py2 xrange +from six.moves import range import webob import glance.api.v2.tasks