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
This commit is contained in:
Erno Kuvaja 2015-01-26 14:09:44 +00:00
parent b3edc09221
commit a4f5bf6ab9
27 changed files with 77 additions and 33 deletions

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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:

View File

@ -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):

View File

@ -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,

View File

@ -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

View File

@ -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

View File

@ -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])

View File

@ -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

View File

@ -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')

View File

@ -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

View File

@ -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')

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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'],

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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