Merge "Replace python print operator with print function (pep H233, py33)"

This commit is contained in:
Jenkins 2015-07-18 04:15:52 +00:00 committed by Gerrit Code Review
commit f3f1f1cab9
5 changed files with 329 additions and 322 deletions

View File

@ -15,6 +15,7 @@
""" """
Script for generating a form signature for use with FormPost middleware. Script for generating a form signature for use with FormPost middleware.
""" """
from __future__ import print_function
import hmac import hmac
from hashlib import sha1 from hashlib import sha1
from os.path import basename from os.path import basename
@ -24,41 +25,41 @@ from time import time
def main(argv): def main(argv):
if len(argv) != 7: if len(argv) != 7:
prog = basename(argv[0]) prog = basename(argv[0])
print 'Syntax: %s <path> <redirect> <max_file_size> ' \ print('Syntax: %s <path> <redirect> <max_file_size> '
'<max_file_count> <seconds> <key>' % prog '<max_file_count> <seconds> <key>' % prog)
print print()
print 'Where:' print('Where:')
print ' <path> The prefix to use for form uploaded' print(' <path> The prefix to use for form uploaded')
print ' objects. For example:' print(' objects. For example:')
print ' /v1/account/container/object_prefix_ would' print(' /v1/account/container/object_prefix_ would')
print ' ensure all form uploads have that path' print(' ensure all form uploads have that path')
print ' prepended to the browser-given file name.' print(' prepended to the browser-given file name.')
print ' <redirect> The URL to redirect the browser to after' print(' <redirect> The URL to redirect the browser to after')
print ' the uploads have completed.' print(' the uploads have completed.')
print ' <max_file_size> The maximum file size per file uploaded.' print(' <max_file_size> The maximum file size per file uploaded.')
print ' <max_file_count> The maximum number of uploaded files' print(' <max_file_count> The maximum number of uploaded files')
print ' allowed.' print(' allowed.')
print ' <seconds> The number of seconds from now to allow' print(' <seconds> The number of seconds from now to allow')
print ' the form post to begin.' print(' the form post to begin.')
print ' <key> The X-Account-Meta-Temp-URL-Key for the' print(' <key> The X-Account-Meta-Temp-URL-Key for the')
print ' account.' print(' account.')
print print()
print 'Example output:' print('Example output:')
print ' Expires: 1323842228' print(' Expires: 1323842228')
print ' Signature: 18de97e47345a82c4dbfb3b06a640dbb' print(' Signature: 18de97e47345a82c4dbfb3b06a640dbb')
print print()
print 'Sample form:' print('Sample form:')
print print()
print('NOTE: the <form> tag\'s "action" attribute does not contain ' print('NOTE: the <form> tag\'s "action" attribute does not contain '
'the Swift cluster\'s hostname.') 'the Swift cluster\'s hostname.')
print 'You should manually add it before using the form.' print('You should manually add it before using the form.')
print print()
print('<form action="/v1/a/c/o" method="POST" ' print('<form action="/v1/a/c/o" method="POST" '
'enctype="multipart/form-data">') 'enctype="multipart/form-data">')
print ' <input type="hidden" name="max_file_size" value="123" />' print(' <input type="hidden" name="max_file_size" value="123" />')
print ' ... more HTML ...' print(' ... more HTML ...')
print ' <input type="submit" />' print(' <input type="submit" />')
print '</form>' print('</form>')
return 1 return 1
path, redirect, max_file_size, max_file_count, seconds, key = argv[1:] path, redirect, max_file_size, max_file_count, seconds, key = argv[1:]
try: try:
@ -66,37 +67,37 @@ def main(argv):
except ValueError: except ValueError:
max_file_size = -1 max_file_size = -1
if max_file_size < 0: if max_file_size < 0:
print 'Please use a <max_file_size> value greater than or equal to 0.' print('Please use a <max_file_size> value greater than or equal to 0.')
return 1 return 1
try: try:
max_file_count = int(max_file_count) max_file_count = int(max_file_count)
except ValueError: except ValueError:
max_file_count = 0 max_file_count = 0
if max_file_count < 1: if max_file_count < 1:
print 'Please use a positive <max_file_count> value.' print('Please use a positive <max_file_count> value.')
return 1 return 1
try: try:
expires = int(time() + int(seconds)) expires = int(time() + int(seconds))
except ValueError: except ValueError:
expires = 0 expires = 0
if expires < 1: if expires < 1:
print 'Please use a positive <seconds> value.' print('Please use a positive <seconds> value.')
return 1 return 1
parts = path.split('/', 4) parts = path.split('/', 4)
# Must be four parts, ['', 'v1', 'a', 'c'], must be a v1 request, have # Must be four parts, ['', 'v1', 'a', 'c'], must be a v1 request, have
# account and container values, and optionally have an object prefix. # account and container values, and optionally have an object prefix.
if len(parts) < 4 or parts[0] or parts[1] != 'v1' or not parts[2] or \ if len(parts) < 4 or parts[0] or parts[1] != 'v1' or not parts[2] or \
not parts[3]: not parts[3]:
print '<path> must point to a container at least.' print('<path> must point to a container at least.')
print 'For example: /v1/account/container' print('For example: /v1/account/container')
print ' Or: /v1/account/container/object_prefix' print(' Or: /v1/account/container/object_prefix')
return 1 return 1
sig = hmac.new(key, '%s\n%s\n%s\n%s\n%s' % (path, redirect, max_file_size, sig = hmac.new(key, '%s\n%s\n%s\n%s\n%s' % (path, redirect, max_file_size,
max_file_count, expires), max_file_count, expires),
sha1).hexdigest() sha1).hexdigest()
print ' Expires:', expires print(' Expires:', expires)
print 'Signature:', sig print('Signature:', sig)
print '' print('')
print('Sample form:\n') print('Sample form:\n')

View File

@ -10,6 +10,7 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
from __future__ import print_function
import itertools import itertools
import os import os
import sqlite3 import sqlite3
@ -84,17 +85,17 @@ def print_ring_locations(ring, datadir, account, container=None, obj=None,
path_hash = hash_path(account, container, obj) path_hash = hash_path(account, container, obj)
else: else:
path_hash = None path_hash = None
print 'Partition\t%s' % part print('Partition\t%s' % part)
print 'Hash \t%s\n' % path_hash print('Hash \t%s\n' % path_hash)
for node in primary_nodes: for node in primary_nodes:
print 'Server:Port Device\t%s:%s %s' % (node['ip'], node['port'], print('Server:Port Device\t%s:%s %s' % (node['ip'], node['port'],
node['device']) node['device']))
for node in handoff_nodes: for node in handoff_nodes:
print 'Server:Port Device\t%s:%s %s\t [Handoff]' % ( print('Server:Port Device\t%s:%s %s\t [Handoff]' % (
node['ip'], node['port'], node['device']) node['ip'], node['port'], node['device']))
print "\n" print("\n")
for node in primary_nodes: for node in primary_nodes:
cmd = 'curl -I -XHEAD "http://%s:%s/%s/%s/%s"' \ cmd = 'curl -I -XHEAD "http://%s:%s/%s/%s/%s"' \
@ -103,7 +104,7 @@ def print_ring_locations(ring, datadir, account, container=None, obj=None,
if policy_index is not None: if policy_index is not None:
cmd += ' -H "%s: %s"' % ('X-Backend-Storage-Policy-Index', cmd += ' -H "%s: %s"' % ('X-Backend-Storage-Policy-Index',
policy_index) policy_index)
print cmd print(cmd)
for node in handoff_nodes: for node in handoff_nodes:
cmd = 'curl -I -XHEAD "http://%s:%s/%s/%s/%s"' \ cmd = 'curl -I -XHEAD "http://%s:%s/%s/%s/%s"' \
% (node['ip'], node['port'], node['device'], part, % (node['ip'], node['port'], node['device'], part,
@ -112,30 +113,30 @@ def print_ring_locations(ring, datadir, account, container=None, obj=None,
cmd += ' -H "%s: %s"' % ('X-Backend-Storage-Policy-Index', cmd += ' -H "%s: %s"' % ('X-Backend-Storage-Policy-Index',
policy_index) policy_index)
cmd += ' # [Handoff]' cmd += ' # [Handoff]'
print cmd print(cmd)
print "\n\nUse your own device location of servers:" print("\n\nUse your own device location of servers:")
print "such as \"export DEVICE=/srv/node\"" print("such as \"export DEVICE=/srv/node\"")
if path_hash: if path_hash:
for node in primary_nodes: for node in primary_nodes:
print ('ssh %s "ls -lah ${DEVICE:-/srv/node*}/%s/%s"' % print('ssh %s "ls -lah ${DEVICE:-/srv/node*}/%s/%s"' %
(node['ip'], node['device'], (node['ip'], node['device'],
storage_directory(datadir, part, path_hash))) storage_directory(datadir, part, path_hash)))
for node in handoff_nodes: for node in handoff_nodes:
print ('ssh %s "ls -lah ${DEVICE:-/srv/node*}/%s/%s" # [Handoff]' % print('ssh %s "ls -lah ${DEVICE:-/srv/node*}/%s/%s" # [Handoff]' %
(node['ip'], node['device'], (node['ip'], node['device'],
storage_directory(datadir, part, path_hash))) storage_directory(datadir, part, path_hash)))
else: else:
for node in primary_nodes: for node in primary_nodes:
print ('ssh %s "ls -lah ${DEVICE:-/srv/node*}/%s/%s/%d"' % print('ssh %s "ls -lah ${DEVICE:-/srv/node*}/%s/%s/%d"' %
(node['ip'], node['device'], datadir, part)) (node['ip'], node['device'], datadir, part))
for node in handoff_nodes: for node in handoff_nodes:
print ('ssh %s "ls -lah ${DEVICE:-/srv/node*}/%s/%s/%d"' print('ssh %s "ls -lah ${DEVICE:-/srv/node*}/%s/%s/%d"'
' # [Handoff]' % ' # [Handoff]' %
(node['ip'], node['device'], datadir, part)) (node['ip'], node['device'], datadir, part))
print '\nnote: `/srv/node*` is used as default value of `devices`, the ' \ print('\nnote: `/srv/node*` is used as default value of `devices`, the '
'real value is set in the config file on each storage node.' 'real value is set in the config file on each storage node.')
def print_db_info_metadata(db_type, info, metadata): def print_db_info_metadata(db_type, info, metadata):
@ -162,52 +163,53 @@ def print_db_info_metadata(db_type, info, metadata):
else: else:
path = '/%s' % account path = '/%s' % account
print 'Path: %s' % path print('Path: %s' % path)
print ' Account: %s' % account print(' Account: %s' % account)
if db_type == 'container': if db_type == 'container':
print ' Container: %s' % container print(' Container: %s' % container)
path_hash = hash_path(account, container) path_hash = hash_path(account, container)
if db_type == 'container': if db_type == 'container':
print ' Container Hash: %s' % path_hash print(' Container Hash: %s' % path_hash)
else: else:
print ' Account Hash: %s' % path_hash print(' Account Hash: %s' % path_hash)
print 'Metadata:' print('Metadata:')
print (' Created at: %s (%s)' % print(' Created at: %s (%s)' %
(Timestamp(info['created_at']).isoformat, (Timestamp(info['created_at']).isoformat,
info['created_at'])) info['created_at']))
print (' Put Timestamp: %s (%s)' % print(' Put Timestamp: %s (%s)' %
(Timestamp(info['put_timestamp']).isoformat, (Timestamp(info['put_timestamp']).isoformat,
info['put_timestamp'])) info['put_timestamp']))
print (' Delete Timestamp: %s (%s)' % print(' Delete Timestamp: %s (%s)' %
(Timestamp(info['delete_timestamp']).isoformat, (Timestamp(info['delete_timestamp']).isoformat,
info['delete_timestamp'])) info['delete_timestamp']))
print (' Status Timestamp: %s (%s)' % print(' Status Timestamp: %s (%s)' %
(Timestamp(info['status_changed_at']).isoformat, (Timestamp(info['status_changed_at']).isoformat,
info['status_changed_at'])) info['status_changed_at']))
if db_type == 'account': if db_type == 'account':
print ' Container Count: %s' % info['container_count'] print(' Container Count: %s' % info['container_count'])
print ' Object Count: %s' % info['object_count'] print(' Object Count: %s' % info['object_count'])
print ' Bytes Used: %s' % info['bytes_used'] print(' Bytes Used: %s' % info['bytes_used'])
if db_type == 'container': if db_type == 'container':
try: try:
policy_name = POLICIES[info['storage_policy_index']].name policy_name = POLICIES[info['storage_policy_index']].name
except KeyError: except KeyError:
policy_name = 'Unknown' policy_name = 'Unknown'
print (' Storage Policy: %s (%s)' % ( print(' Storage Policy: %s (%s)' % (
policy_name, info['storage_policy_index'])) policy_name, info['storage_policy_index']))
print (' Reported Put Timestamp: %s (%s)' % print(' Reported Put Timestamp: %s (%s)' %
(Timestamp(info['reported_put_timestamp']).isoformat, (Timestamp(info['reported_put_timestamp']).isoformat,
info['reported_put_timestamp'])) info['reported_put_timestamp']))
print (' Reported Delete Timestamp: %s (%s)' % print(' Reported Delete Timestamp: %s (%s)' %
(Timestamp(info['reported_delete_timestamp']).isoformat, (Timestamp(info['reported_delete_timestamp']).isoformat,
info['reported_delete_timestamp'])) info['reported_delete_timestamp']))
print ' Reported Object Count: %s' % info['reported_object_count'] print(' Reported Object Count: %s' %
print ' Reported Bytes Used: %s' % info['reported_bytes_used'] info['reported_object_count'])
print ' Chexor: %s' % info['hash'] print(' Reported Bytes Used: %s' % info['reported_bytes_used'])
print ' UUID: %s' % info['id'] print(' Chexor: %s' % info['hash'])
print(' UUID: %s' % info['id'])
except KeyError as e: except KeyError as e:
raise ValueError('Info is incomplete: %s' % e) raise ValueError('Info is incomplete: %s' % e)
@ -215,7 +217,7 @@ def print_db_info_metadata(db_type, info, metadata):
for key, value in info.items(): for key, value in info.items():
if key.lower().startswith(meta_prefix): if key.lower().startswith(meta_prefix):
title = key.replace('_', '-').title() title = key.replace('_', '-').title()
print ' %s: %s' % (title, value) print(' %s: %s' % (title, value))
user_metadata = {} user_metadata = {}
sys_metadata = {} sys_metadata = {}
for key, (value, timestamp) in metadata.items(): for key, (value, timestamp) in metadata.items():
@ -225,16 +227,16 @@ def print_db_info_metadata(db_type, info, metadata):
sys_metadata[strip_sys_meta_prefix(db_type, key)] = value sys_metadata[strip_sys_meta_prefix(db_type, key)] = value
else: else:
title = key.replace('_', '-').title() title = key.replace('_', '-').title()
print ' %s: %s' % (title, value) print(' %s: %s' % (title, value))
if sys_metadata: if sys_metadata:
print ' System Metadata: %s' % sys_metadata print(' System Metadata: %s' % sys_metadata)
else: else:
print 'No system metadata found in db file' print('No system metadata found in db file')
if user_metadata: if user_metadata:
print ' User Metadata: %s' % user_metadata print(' User Metadata: %s' % user_metadata)
else: else:
print 'No user metadata found in db file' print('No user metadata found in db file')
def print_obj_metadata(metadata): def print_obj_metadata(metadata):
@ -268,21 +270,21 @@ def print_obj_metadata(metadata):
raise ValueError('Path is invalid for object %r' % path) raise ValueError('Path is invalid for object %r' % path)
else: else:
obj_hash = hash_path(account, container, obj) obj_hash = hash_path(account, container, obj)
print 'Path: %s' % path print('Path: %s' % path)
print ' Account: %s' % account print(' Account: %s' % account)
print ' Container: %s' % container print(' Container: %s' % container)
print ' Object: %s' % obj print(' Object: %s' % obj)
print ' Object hash: %s' % obj_hash print(' Object hash: %s' % obj_hash)
else: else:
print 'Path: Not found in metadata' print('Path: Not found in metadata')
if content_type: if content_type:
print 'Content-Type: %s' % content_type print('Content-Type: %s' % content_type)
else: else:
print 'Content-Type: Not found in metadata' print('Content-Type: Not found in metadata')
if ts: if ts:
print ('Timestamp: %s (%s)' % (ts.isoformat, ts.internal)) print('Timestamp: %s (%s)' % (ts.isoformat, ts.internal))
else: else:
print 'Timestamp: Not found in metadata' print('Timestamp: Not found in metadata')
for key, value in metadata.items(): for key, value in metadata.items():
if is_user_meta('Object', key): if is_user_meta('Object', key):
@ -293,12 +295,12 @@ def print_obj_metadata(metadata):
other_metadata[key] = value other_metadata[key] = value
def print_metadata(title, items): def print_metadata(title, items):
print title print(title)
if items: if items:
for meta_key in sorted(items): for meta_key in sorted(items):
print ' %s: %s' % (meta_key, items[meta_key]) print(' %s: %s' % (meta_key, items[meta_key]))
else: else:
print ' No metadata found' print(' No metadata found')
print_metadata('System Metadata:', sys_metadata) print_metadata('System Metadata:', sys_metadata)
print_metadata('User Metadata:', user_metadata) print_metadata('User Metadata:', user_metadata)
@ -307,10 +309,10 @@ def print_obj_metadata(metadata):
def print_info(db_type, db_file, swift_dir='/etc/swift'): def print_info(db_type, db_file, swift_dir='/etc/swift'):
if db_type not in ('account', 'container'): if db_type not in ('account', 'container'):
print "Unrecognized DB type: internal error" print("Unrecognized DB type: internal error")
raise InfoSystemExit() raise InfoSystemExit()
if not os.path.exists(db_file) or not db_file.endswith('.db'): if not os.path.exists(db_file) or not db_file.endswith('.db'):
print "DB file doesn't exist" print("DB file doesn't exist")
raise InfoSystemExit() raise InfoSystemExit()
if not db_file.startswith(('/', './')): if not db_file.startswith(('/', './')):
db_file = './' + db_file # don't break if the bare db file is given db_file = './' + db_file # don't break if the bare db file is given
@ -324,8 +326,8 @@ def print_info(db_type, db_file, swift_dir='/etc/swift'):
info = broker.get_info() info = broker.get_info()
except sqlite3.OperationalError as err: except sqlite3.OperationalError as err:
if 'no such table' in str(err): if 'no such table' in str(err):
print "Does not appear to be a DB of type \"%s\": %s" % ( print("Does not appear to be a DB of type \"%s\": %s"
db_type, db_file) % (db_type, db_file))
raise InfoSystemExit() raise InfoSystemExit()
raise raise
account = info['account'] account = info['account']
@ -353,7 +355,7 @@ def print_obj(datafile, check_etag=True, swift_dir='/etc/swift',
:param policy_name: optionally the name to use when finding the ring :param policy_name: optionally the name to use when finding the ring
""" """
if not os.path.exists(datafile): if not os.path.exists(datafile):
print "Data file doesn't exist" print("Data file doesn't exist")
raise InfoSystemExit() raise InfoSystemExit()
if not datafile.startswith(('/', './')): if not datafile.startswith(('/', './')):
datafile = './' + datafile datafile = './' + datafile
@ -382,8 +384,8 @@ def print_obj(datafile, check_etag=True, swift_dir='/etc/swift',
if (policy_index is not None and if (policy_index is not None and
policy_index_for_name is not None and policy_index_for_name is not None and
policy_index != policy_index_for_name): policy_index != policy_index_for_name):
print 'Warning: Ring does not match policy!' print('Warning: Ring does not match policy!')
print 'Double check your policy name!' print('Double check your policy name!')
if not ring and policy_index_for_name: if not ring and policy_index_for_name:
ring = POLICIES.get_object_ring(policy_index_for_name, ring = POLICIES.get_object_ring(policy_index_for_name,
swift_dir) swift_dir)
@ -393,7 +395,7 @@ def print_obj(datafile, check_etag=True, swift_dir='/etc/swift',
try: try:
metadata = read_metadata(fp) metadata = read_metadata(fp)
except EOFError: except EOFError:
print "Invalid metadata" print("Invalid metadata")
raise InfoSystemExit() raise InfoSystemExit()
etag = metadata.pop('ETag', '') etag = metadata.pop('ETag', '')
@ -415,24 +417,24 @@ def print_obj(datafile, check_etag=True, swift_dir='/etc/swift',
h = h.hexdigest() h = h.hexdigest()
if etag: if etag:
if h == etag: if h == etag:
print 'ETag: %s (valid)' % etag print('ETag: %s (valid)' % etag)
else: else:
print ("ETag: %s doesn't match file hash of %s!" % print("ETag: %s doesn't match file hash of %s!" %
(etag, h)) (etag, h))
else: else:
print 'ETag: Not found in metadata' print('ETag: Not found in metadata')
else: else:
print 'ETag: %s (not checked)' % etag print('ETag: %s (not checked)' % etag)
file_len = os.fstat(fp.fileno()).st_size file_len = os.fstat(fp.fileno()).st_size
if length: if length:
if file_len == int(length): if file_len == int(length):
print 'Content-Length: %s (valid)' % length print('Content-Length: %s (valid)' % length)
else: else:
print ("Content-Length: %s doesn't match file length of %s" print("Content-Length: %s doesn't match file length of %s"
% (length, file_len)) % (length, file_len))
else: else:
print 'Content-Length: Not found in metadata' print('Content-Length: Not found in metadata')
account, container, obj = path.split('/', 3)[1:] account, container, obj = path.split('/', 3)[1:]
if ring: if ring:
@ -472,33 +474,33 @@ def print_item_locations(ring, ring_name=None, account=None, container=None,
policy = POLICIES.get_by_name(policy_name) policy = POLICIES.get_by_name(policy_name)
if policy: if policy:
if ring_name != policy.ring_name: if ring_name != policy.ring_name:
print 'Warning: mismatch between ring and policy name!' print('Warning: mismatch between ring and policy name!')
else: else:
print 'Warning: Policy %s is not valid' % policy_name print('Warning: Policy %s is not valid' % policy_name)
policy_index = None policy_index = None
if ring is None and (obj or part): if ring is None and (obj or part):
if not policy_name: if not policy_name:
print 'Need a ring or policy' print('Need a ring or policy')
raise InfoSystemExit() raise InfoSystemExit()
policy = POLICIES.get_by_name(policy_name) policy = POLICIES.get_by_name(policy_name)
if not policy: if not policy:
print 'No policy named %r' % policy_name print('No policy named %r' % policy_name)
raise InfoSystemExit() raise InfoSystemExit()
policy_index = int(policy) policy_index = int(policy)
ring = POLICIES.get_object_ring(policy_index, swift_dir) ring = POLICIES.get_object_ring(policy_index, swift_dir)
ring_name = (POLICIES.get_by_name(policy_name)).ring_name ring_name = (POLICIES.get_by_name(policy_name)).ring_name
if account is None and (container is not None or obj is not None): if account is None and (container is not None or obj is not None):
print 'No account specified' print('No account specified')
raise InfoSystemExit() raise InfoSystemExit()
if container is None and obj is not None: if container is None and obj is not None:
print 'No container specified' print('No container specified')
raise InfoSystemExit() raise InfoSystemExit()
if account is None and part is None: if account is None and part is None:
print 'No target specified' print('No target specified')
raise InfoSystemExit() raise InfoSystemExit()
loc = '<type>' loc = '<type>'
@ -518,19 +520,19 @@ def print_item_locations(ring, ring_name=None, account=None, container=None,
ring = Ring(swift_dir, ring_name='container') ring = Ring(swift_dir, ring_name='container')
else: else:
if ring_name != 'container': if ring_name != 'container':
print 'Warning: account/container specified ' + \ print('Warning: account/container specified ' +
'but ring not named "container"' 'but ring not named "container"')
if account and not container and not obj: if account and not container and not obj:
loc = 'accounts' loc = 'accounts'
if not any([ring, ring_name]): if not any([ring, ring_name]):
ring = Ring(swift_dir, ring_name='account') ring = Ring(swift_dir, ring_name='account')
else: else:
if ring_name != 'account': if ring_name != 'account':
print 'Warning: account specified ' + \ print('Warning: account specified ' +
'but ring not named "account"' 'but ring not named "account"')
print '\nAccount \t%s' % account print('\nAccount \t%s' % account)
print 'Container\t%s' % container print('Container\t%s' % container)
print 'Object \t%s\n\n' % obj print('Object \t%s\n\n' % obj)
print_ring_locations(ring, loc, account, container, obj, part, all_nodes, print_ring_locations(ring, loc, account, container, obj, part, all_nodes,
policy_index=policy_index) policy_index=policy_index)

View File

@ -14,6 +14,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
from __future__ import print_function
import logging import logging
from errno import EEXIST from errno import EEXIST
@ -71,14 +72,14 @@ def _parse_search_values(argvish):
search_values = {} search_values = {}
if len(args) > 0: if len(args) > 0:
if new_cmd_format or len(args) != 1: if new_cmd_format or len(args) != 1:
print Commands.search.__doc__.strip() print(Commands.search.__doc__.strip())
exit(EXIT_ERROR) exit(EXIT_ERROR)
search_values = parse_search_value(args[0]) search_values = parse_search_value(args[0])
else: else:
search_values = parse_search_values_from_opts(opts) search_values = parse_search_values_from_opts(opts)
return search_values return search_values
except ValueError as e: except ValueError as e:
print e print(e)
exit(EXIT_ERROR) exit(EXIT_ERROR)
@ -113,7 +114,7 @@ def _parse_list_parts_values(argvish):
devs = [] devs = []
if len(args) > 0: if len(args) > 0:
if new_cmd_format: if new_cmd_format:
print Commands.list_parts.__doc__.strip() print(Commands.list_parts.__doc__.strip())
exit(EXIT_ERROR) exit(EXIT_ERROR)
for arg in args: for arg in args:
@ -125,7 +126,7 @@ def _parse_list_parts_values(argvish):
return devs return devs
except ValueError as e: except ValueError as e:
print e print(e)
exit(EXIT_ERROR) exit(EXIT_ERROR)
@ -145,7 +146,7 @@ def _parse_add_values(argvish):
parsed_devs = [] parsed_devs = []
if len(args) > 0: if len(args) > 0:
if new_cmd_format or len(args) % 2 != 0: if new_cmd_format or len(args) % 2 != 0:
print Commands.add.__doc__.strip() print(Commands.add.__doc__.strip())
exit(EXIT_ERROR) exit(EXIT_ERROR)
devs_and_weights = izip(islice(args, 0, len(args), 2), devs_and_weights = izip(islice(args, 0, len(args), 2),
@ -184,18 +185,18 @@ def _set_weight_values(devs, weight):
exit(EXIT_ERROR) exit(EXIT_ERROR)
if len(devs) > 1: if len(devs) > 1:
print 'Matched more than one device:' print('Matched more than one device:')
for dev in devs: for dev in devs:
print ' %s' % format_device(dev) print(' %s' % format_device(dev))
if raw_input('Are you sure you want to update the weight for ' if raw_input('Are you sure you want to update the weight for '
'these %s devices? (y/N) ' % len(devs)) != 'y': 'these %s devices? (y/N) ' % len(devs)) != 'y':
print 'Aborting device modifications' print('Aborting device modifications')
exit(EXIT_ERROR) exit(EXIT_ERROR)
for dev in devs: for dev in devs:
builder.set_dev_weight(dev['id'], weight) builder.set_dev_weight(dev['id'], weight)
print '%s weight set to %s' % (format_device(dev), print('%s weight set to %s' % (format_device(dev),
dev['weight']) dev['weight']))
def _parse_set_weight_values(argvish): def _parse_set_weight_values(argvish):
@ -209,7 +210,7 @@ def _parse_set_weight_values(argvish):
devs = [] devs = []
if not new_cmd_format: if not new_cmd_format:
if len(args) % 2 != 0: if len(args) % 2 != 0:
print Commands.set_weight.__doc__.strip() print(Commands.set_weight.__doc__.strip())
exit(EXIT_ERROR) exit(EXIT_ERROR)
devs_and_weights = izip(islice(argvish, 0, len(argvish), 2), devs_and_weights = izip(islice(argvish, 0, len(argvish), 2),
@ -221,7 +222,7 @@ def _parse_set_weight_values(argvish):
_set_weight_values(devs, weight) _set_weight_values(devs, weight)
else: else:
if len(args) != 1: if len(args) != 1:
print Commands.set_weight.__doc__.strip() print(Commands.set_weight.__doc__.strip())
exit(EXIT_ERROR) exit(EXIT_ERROR)
devs.extend(builder.search_devs( devs.extend(builder.search_devs(
@ -229,7 +230,7 @@ def _parse_set_weight_values(argvish):
weight = float(args[0]) weight = float(args[0])
_set_weight_values(devs, weight) _set_weight_values(devs, weight)
except ValueError as e: except ValueError as e:
print e print(e)
exit(EXIT_ERROR) exit(EXIT_ERROR)
@ -241,12 +242,12 @@ def _set_info_values(devs, change):
exit(EXIT_ERROR) exit(EXIT_ERROR)
if len(devs) > 1: if len(devs) > 1:
print 'Matched more than one device:' print('Matched more than one device:')
for dev in devs: for dev in devs:
print ' %s' % format_device(dev) print(' %s' % format_device(dev))
if raw_input('Are you sure you want to update the info for ' if raw_input('Are you sure you want to update the info for '
'these %s devices? (y/N) ' % len(devs)) != 'y': 'these %s devices? (y/N) ' % len(devs)) != 'y':
print 'Aborting device modifications' print('Aborting device modifications')
exit(EXIT_ERROR) exit(EXIT_ERROR)
for dev in devs: for dev in devs:
@ -260,14 +261,14 @@ def _set_info_values(devs, change):
if check_dev['ip'] == test_dev['ip'] and \ if check_dev['ip'] == test_dev['ip'] and \
check_dev['port'] == test_dev['port'] and \ check_dev['port'] == test_dev['port'] and \
check_dev['device'] == test_dev['device']: check_dev['device'] == test_dev['device']:
print 'Device %d already uses %s:%d/%s.' % \ print('Device %d already uses %s:%d/%s.' %
(check_dev['id'], check_dev['ip'], (check_dev['id'], check_dev['ip'],
check_dev['port'], check_dev['device']) check_dev['port'], check_dev['device']))
exit(EXIT_ERROR) exit(EXIT_ERROR)
for key in change: for key in change:
dev[key] = change[key] dev[key] = change[key]
print 'Device %s is now %s' % (orig_dev_string, print('Device %s is now %s' % (orig_dev_string,
format_device(dev)) format_device(dev)))
def _parse_set_info_values(argvish): def _parse_set_info_values(argvish):
@ -279,7 +280,7 @@ def _parse_set_info_values(argvish):
# but not both. If both are specified, raise an error. # but not both. If both are specified, raise an error.
if not new_cmd_format: if not new_cmd_format:
if len(args) % 2 != 0: if len(args) % 2 != 0:
print Commands.search.__doc__.strip() print(Commands.search.__doc__.strip())
exit(EXIT_ERROR) exit(EXIT_ERROR)
searches_and_changes = izip(islice(argvish, 0, len(argvish), 2), searches_and_changes = izip(islice(argvish, 0, len(argvish), 2),
@ -368,7 +369,7 @@ def _parse_remove_values(argvish):
devs = [] devs = []
if len(args) > 0: if len(args) > 0:
if new_cmd_format: if new_cmd_format:
print Commands.remove.__doc__.strip() print(Commands.remove.__doc__.strip())
exit(EXIT_ERROR) exit(EXIT_ERROR)
for arg in args: for arg in args:
@ -380,14 +381,14 @@ def _parse_remove_values(argvish):
return devs return devs
except ValueError as e: except ValueError as e:
print e print(e)
exit(EXIT_ERROR) exit(EXIT_ERROR)
class Commands(object): class Commands(object):
def unknown(): def unknown():
print 'Unknown command: %s' % argv[2] print('Unknown command: %s' % argv[2])
exit(EXIT_ERROR) exit(EXIT_ERROR)
def create(): def create():
@ -399,7 +400,7 @@ swift-ring-builder <builder_file> create <part_power> <replicas>
than once. than once.
""" """
if len(argv) < 6: if len(argv) < 6:
print Commands.create.__doc__.strip() print(Commands.create.__doc__.strip())
exit(EXIT_ERROR) exit(EXIT_ERROR)
builder = RingBuilder(int(argv[3]), float(argv[4]), int(argv[5])) builder = RingBuilder(int(argv[3]), float(argv[4]), int(argv[5]))
backup_dir = pathjoin(dirname(argv[1]), 'backups') backup_dir = pathjoin(dirname(argv[1]), 'backups')
@ -417,7 +418,7 @@ swift-ring-builder <builder_file> create <part_power> <replicas>
swift-ring-builder <builder_file> swift-ring-builder <builder_file>
Shows information about the ring and the devices within. Shows information about the ring and the devices within.
""" """
print '%s, build version %d' % (argv[1], builder.version) print('%s, build version %d' % (argv[1], builder.version))
regions = 0 regions = 0
zones = 0 zones = 0
balance = 0 balance = 0
@ -432,18 +433,18 @@ swift-ring-builder <builder_file>
balance = builder.get_balance() balance = builder.get_balance()
dispersion_trailer = '' if builder.dispersion is None else ( dispersion_trailer = '' if builder.dispersion is None else (
', %.02f dispersion' % (builder.dispersion)) ', %.02f dispersion' % (builder.dispersion))
print '%d partitions, %.6f replicas, %d regions, %d zones, ' \ print('%d partitions, %.6f replicas, %d regions, %d zones, '
'%d devices, %.02f balance%s' % ( '%d devices, %.02f balance%s' % (
builder.parts, builder.replicas, regions, zones, dev_count, builder.parts, builder.replicas, regions, zones, dev_count,
balance, dispersion_trailer) balance, dispersion_trailer))
print 'The minimum number of hours before a partition can be ' \ print('The minimum number of hours before a partition can be '
'reassigned is %s' % builder.min_part_hours 'reassigned is %s' % builder.min_part_hours)
print 'The overload factor is %0.2f%% (%.6f)' % ( print('The overload factor is %0.2f%% (%.6f)' % (
builder.overload * 100, builder.overload) builder.overload * 100, builder.overload))
if builder.devs: if builder.devs:
print 'Devices: id region zone ip address port ' \ print('Devices: id region zone ip address port '
'replication ip replication port name ' \ 'replication ip replication port name '
'weight partitions balance meta' 'weight partitions balance meta')
weighted_parts = builder.parts * builder.replicas / \ weighted_parts = builder.parts * builder.replicas / \
sum(d['weight'] for d in builder.devs if d is not None) sum(d['weight'] for d in builder.devs if d is not None)
for dev in builder.devs: for dev in builder.devs:
@ -483,19 +484,19 @@ swift-ring-builder <builder_file> search
Shows information about matching devices. Shows information about matching devices.
""" """
if len(argv) < 4: if len(argv) < 4:
print Commands.search.__doc__.strip() print(Commands.search.__doc__.strip())
print print()
print parse_search_value.__doc__.strip() print(parse_search_value.__doc__.strip())
exit(EXIT_ERROR) exit(EXIT_ERROR)
devs = builder.search_devs(_parse_search_values(argv[3:])) devs = builder.search_devs(_parse_search_values(argv[3:]))
if not devs: if not devs:
print 'No matching devices found' print('No matching devices found')
exit(EXIT_ERROR) exit(EXIT_ERROR)
print 'Devices: id region zone ip address port ' \ print('Devices: id region zone ip address port '
'replication ip replication port name weight partitions ' \ 'replication ip replication port name weight partitions '
'balance meta' 'balance meta')
weighted_parts = builder.parts * builder.replicas / \ weighted_parts = builder.parts * builder.replicas / \
sum(d['weight'] for d in builder.devs if d is not None) sum(d['weight'] for d in builder.devs if d is not None)
for dev in devs: for dev in devs:
@ -538,9 +539,9 @@ swift-ring-builder <builder_file> list_parts
could take a while to run. could take a while to run.
""" """
if len(argv) < 4: if len(argv) < 4:
print Commands.list_parts.__doc__.strip() print(Commands.list_parts.__doc__.strip())
print print()
print parse_search_value.__doc__.strip() print(parse_search_value.__doc__.strip())
exit(EXIT_ERROR) exit(EXIT_ERROR)
if not builder._replica2part2dev: if not builder._replica2part2dev:
@ -550,18 +551,18 @@ swift-ring-builder <builder_file> list_parts
devs = _parse_list_parts_values(argv[3:]) devs = _parse_list_parts_values(argv[3:])
if not devs: if not devs:
print 'No matching devices found' print('No matching devices found')
exit(EXIT_ERROR) exit(EXIT_ERROR)
sorted_partition_count = _find_parts(devs) sorted_partition_count = _find_parts(devs)
if not sorted_partition_count: if not sorted_partition_count:
print 'No matching devices found' print('No matching devices found')
exit(EXIT_ERROR) exit(EXIT_ERROR)
print 'Partition Matches' print('Partition Matches')
for partition, count in sorted_partition_count: for partition, count in sorted_partition_count:
print '%9d %7d' % (partition, count) print('%9d %7d' % (partition, count))
exit(EXIT_SUCCESS) exit(EXIT_SUCCESS)
def add(): def add():
@ -587,7 +588,7 @@ swift-ring-builder <builder_file> add
can make multiple device changes and rebalance them all just once. can make multiple device changes and rebalance them all just once.
""" """
if len(argv) < 5: if len(argv) < 5:
print Commands.add.__doc__.strip() print(Commands.add.__doc__.strip())
exit(EXIT_ERROR) exit(EXIT_ERROR)
try: try:
@ -598,17 +599,17 @@ swift-ring-builder <builder_file> add
if dev['ip'] == new_dev['ip'] and \ if dev['ip'] == new_dev['ip'] and \
dev['port'] == new_dev['port'] and \ dev['port'] == new_dev['port'] and \
dev['device'] == new_dev['device']: dev['device'] == new_dev['device']:
print 'Device %d already uses %s:%d/%s.' % \ print('Device %d already uses %s:%d/%s.' %
(dev['id'], dev['ip'], (dev['id'], dev['ip'],
dev['port'], dev['device']) dev['port'], dev['device']))
print "The on-disk ring builder is unchanged.\n" print("The on-disk ring builder is unchanged.\n")
exit(EXIT_ERROR) exit(EXIT_ERROR)
dev_id = builder.add_dev(new_dev) dev_id = builder.add_dev(new_dev)
print('Device %s with %s weight got id %s' % print('Device %s with %s weight got id %s' %
(format_device(new_dev), new_dev['weight'], dev_id)) (format_device(new_dev), new_dev['weight'], dev_id))
except ValueError as err: except ValueError as err:
print err print(err)
print 'The on-disk ring builder is unchanged.' print('The on-disk ring builder is unchanged.')
exit(EXIT_ERROR) exit(EXIT_ERROR)
builder.save(argv[1]) builder.save(argv[1])
@ -636,9 +637,9 @@ swift-ring-builder <builder_file> set_weight
""" """
# if len(argv) < 5 or len(argv) % 2 != 1: # if len(argv) < 5 or len(argv) % 2 != 1:
if len(argv) < 5: if len(argv) < 5:
print Commands.set_weight.__doc__.strip() print(Commands.set_weight.__doc__.strip())
print print()
print parse_search_value.__doc__.strip() print(parse_search_value.__doc__.strip())
exit(EXIT_ERROR) exit(EXIT_ERROR)
_parse_set_weight_values(argv[3:]) _parse_set_weight_values(argv[3:])
@ -677,15 +678,15 @@ swift-ring-builder <builder_file> set_info
just update the meta data for device id 74. just update the meta data for device id 74.
""" """
if len(argv) < 5: if len(argv) < 5:
print Commands.set_info.__doc__.strip() print(Commands.set_info.__doc__.strip())
print print()
print parse_search_value.__doc__.strip() print(parse_search_value.__doc__.strip())
exit(EXIT_ERROR) exit(EXIT_ERROR)
try: try:
_parse_set_info_values(argv[3:]) _parse_set_info_values(argv[3:])
except ValueError as err: except ValueError as err:
print err print(err)
exit(EXIT_ERROR) exit(EXIT_ERROR)
builder.save(argv[1]) builder.save(argv[1])
@ -714,9 +715,9 @@ swift-ring-builder <builder_file> search
once. once.
""" """
if len(argv) < 4: if len(argv) < 4:
print Commands.remove.__doc__.strip() print(Commands.remove.__doc__.strip())
print print()
print parse_search_value.__doc__.strip() print(parse_search_value.__doc__.strip())
exit(EXIT_ERROR) exit(EXIT_ERROR)
devs = _parse_remove_values(argv[3:]) devs = _parse_remove_values(argv[3:])
@ -727,19 +728,19 @@ swift-ring-builder <builder_file> search
exit(EXIT_ERROR) exit(EXIT_ERROR)
if len(devs) > 1: if len(devs) > 1:
print 'Matched more than one device:' print('Matched more than one device:')
for dev in devs: for dev in devs:
print ' %s' % format_device(dev) print(' %s' % format_device(dev))
if raw_input('Are you sure you want to remove these %s ' if raw_input('Are you sure you want to remove these %s '
'devices? (y/N) ' % len(devs)) != 'y': 'devices? (y/N) ' % len(devs)) != 'y':
print 'Aborting device removals' print('Aborting device removals')
exit(EXIT_ERROR) exit(EXIT_ERROR)
for dev in devs: for dev in devs:
try: try:
builder.remove_dev(dev['id']) builder.remove_dev(dev['id'])
except exceptions.RingBuilderError as e: except exceptions.RingBuilderError as e:
print '-' * 79 print('-' * 79)
print( print(
'An error occurred while removing device with id %d\n' 'An error occurred while removing device with id %d\n'
'This usually means that you attempted to remove\n' 'This usually means that you attempted to remove\n'
@ -748,11 +749,11 @@ swift-ring-builder <builder_file> search
'The on-disk ring builder is unchanged.\n' 'The on-disk ring builder is unchanged.\n'
'Original exception message: %s' % 'Original exception message: %s' %
(dev['id'], e)) (dev['id'], e))
print '-' * 79 print('-' * 79)
exit(EXIT_ERROR) exit(EXIT_ERROR)
print '%s marked for removal and will ' \ print('%s marked for removal and will '
'be removed next rebalance.' % format_device(dev) 'be removed next rebalance.' % format_device(dev))
builder.save(argv[1]) builder.save(argv[1])
exit(EXIT_SUCCESS) exit(EXIT_SUCCESS)
@ -793,18 +794,18 @@ swift-ring-builder <builder_file> rebalance [options]
last_balance = builder.get_balance() last_balance = builder.get_balance()
parts, balance = builder.rebalance(seed=get_seed(3)) parts, balance = builder.rebalance(seed=get_seed(3))
except exceptions.RingBuilderError as e: except exceptions.RingBuilderError as e:
print '-' * 79 print('-' * 79)
print("An error has occurred during ring validation. Common\n" print("An error has occurred during ring validation. Common\n"
"causes of failure are rings that are empty or do not\n" "causes of failure are rings that are empty or do not\n"
"have enough devices to accommodate the replica count.\n" "have enough devices to accommodate the replica count.\n"
"Original exception message:\n %s" % "Original exception message:\n %s" %
(e,)) (e,))
print '-' * 79 print('-' * 79)
exit(EXIT_ERROR) exit(EXIT_ERROR)
if not (parts or options.force): if not (parts or options.force):
print 'No partitions could be reassigned.' print('No partitions could be reassigned.')
print 'Either none need to be or none can be due to ' \ print('Either none need to be or none can be due to '
'min_part_hours [%s].' % builder.min_part_hours 'min_part_hours [%s].' % builder.min_part_hours)
exit(EXIT_WARNING) exit(EXIT_WARNING)
# If we set device's weight to zero, currently balance will be set # If we set device's weight to zero, currently balance will be set
# special value(MAX_BALANCE) until zero weighted device return all # special value(MAX_BALANCE) until zero weighted device return all
@ -813,29 +814,29 @@ swift-ring-builder <builder_file> rebalance [options]
if not options.force and \ if not options.force and \
not devs_changed and abs(last_balance - balance) < 1 and \ not devs_changed and abs(last_balance - balance) < 1 and \
not (last_balance == MAX_BALANCE and balance == MAX_BALANCE): not (last_balance == MAX_BALANCE and balance == MAX_BALANCE):
print 'Cowardly refusing to save rebalance as it did not change ' \ print('Cowardly refusing to save rebalance as it did not change '
'at least 1%.' 'at least 1%.')
exit(EXIT_WARNING) exit(EXIT_WARNING)
try: try:
builder.validate() builder.validate()
except exceptions.RingValidationError as e: except exceptions.RingValidationError as e:
print '-' * 79 print('-' * 79)
print("An error has occurred during ring validation. Common\n" print("An error has occurred during ring validation. Common\n"
"causes of failure are rings that are empty or do not\n" "causes of failure are rings that are empty or do not\n"
"have enough devices to accommodate the replica count.\n" "have enough devices to accommodate the replica count.\n"
"Original exception message:\n %s" % "Original exception message:\n %s" %
(e,)) (e,))
print '-' * 79 print('-' * 79)
exit(EXIT_ERROR) exit(EXIT_ERROR)
print ('Reassigned %d (%.02f%%) partitions. ' print('Reassigned %d (%.02f%%) partitions. '
'Balance is now %.02f. ' 'Balance is now %.02f. '
'Dispersion is now %.02f' % ( 'Dispersion is now %.02f' % (
parts, 100.0 * parts / builder.parts, parts, 100.0 * parts / builder.parts,
balance, balance,
builder.dispersion)) builder.dispersion))
status = EXIT_SUCCESS status = EXIT_SUCCESS
if builder.dispersion > 0: if builder.dispersion > 0:
print '-' * 79 print('-' * 79)
print( print(
'NOTE: Dispersion of %.06f indicates some parts are not\n' 'NOTE: Dispersion of %.06f indicates some parts are not\n'
' optimally dispersed.\n\n' ' optimally dispersed.\n\n'
@ -843,14 +844,14 @@ swift-ring-builder <builder_file> rebalance [options]
' the overload or review the dispersion report.' % ' the overload or review the dispersion report.' %
builder.dispersion) builder.dispersion)
status = EXIT_WARNING status = EXIT_WARNING
print '-' * 79 print('-' * 79)
elif balance > 5 and balance / 100.0 > builder.overload: elif balance > 5 and balance / 100.0 > builder.overload:
print '-' * 79 print('-' * 79)
print 'NOTE: Balance of %.02f indicates you should push this ' % \ print('NOTE: Balance of %.02f indicates you should push this ' %
balance balance)
print ' ring, wait at least %d hours, and rebalance/repush.' \ print(' ring, wait at least %d hours, and rebalance/repush.'
% builder.min_part_hours % builder.min_part_hours)
print '-' * 79 print('-' * 79)
status = EXIT_WARNING status = EXIT_WARNING
ts = time() ts = time()
builder.get_ring().save( builder.get_ring().save(
@ -905,12 +906,12 @@ swift-ring-builder <builder_file> dispersion <search_filter> [options]
search_filter = None search_filter = None
report = dispersion_report(builder, search_filter=search_filter, report = dispersion_report(builder, search_filter=search_filter,
verbose=options.verbose) verbose=options.verbose)
print 'Dispersion is %.06f, Balance is %.06f, Overload is %0.2f%%' % ( print('Dispersion is %.06f, Balance is %.06f, Overload is %0.2f%%' % (
builder.dispersion, builder.get_balance(), builder.overload * 100) builder.dispersion, builder.get_balance(), builder.overload * 100))
if report['worst_tier']: if report['worst_tier']:
status = EXIT_WARNING status = EXIT_WARNING
print 'Worst tier is %.06f (%s)' % (report['max_dispersion'], print('Worst tier is %.06f (%s)' % (report['max_dispersion'],
report['worst_tier']) report['worst_tier']))
if report['graph']: if report['graph']:
replica_range = range(int(math.ceil(builder.replicas + 1))) replica_range = range(int(math.ceil(builder.replicas + 1)))
part_count_width = '%%%ds' % max(len(str(builder.parts)), 5) part_count_width = '%%%ds' % max(len(str(builder.parts)), 5)
@ -929,13 +930,13 @@ swift-ring-builder <builder_file> dispersion <search_filter> [options]
for tier_name, dispersion in report['graph']: for tier_name, dispersion in report['graph']:
replica_counts_repr = replica_counts_tmpl % tuple( replica_counts_repr = replica_counts_tmpl % tuple(
dispersion['replicas']) dispersion['replicas'])
print ('%-' + str(tier_width) + 's ' + part_count_width + print('%-' + str(tier_width) + 's ' + part_count_width +
' %6.02f %6d %s') % (tier_name, ' %6.02f %6d %s') % (tier_name,
dispersion['placed_parts'], dispersion['placed_parts'],
dispersion['dispersion'], dispersion['dispersion'],
dispersion['max_replicas'], dispersion['max_replicas'],
replica_counts_repr, replica_counts_repr,
) )
exit(status) exit(status)
def validate(): def validate():
@ -957,11 +958,11 @@ swift-ring-builder <builder_file> write_ring
ring_data = builder.get_ring() ring_data = builder.get_ring()
if not ring_data._replica2part2dev_id: if not ring_data._replica2part2dev_id:
if ring_data.devs: if ring_data.devs:
print 'Warning: Writing a ring with no partition ' \ print('Warning: Writing a ring with no partition '
'assignments but with devices; did you forget to run ' \ 'assignments but with devices; did you forget to run '
'"rebalance"?' '"rebalance"?')
else: else:
print 'Warning: Writing an empty ring' print('Warning: Writing an empty ring')
ring_data.save( ring_data.save(
pathjoin(backup_dir, '%d.' % time() + basename(ring_file))) pathjoin(backup_dir, '%d.' % time() + basename(ring_file)))
ring_data.save(ring_file) ring_data.save(ring_file)
@ -976,8 +977,8 @@ swift-ring-builder <ring_file> write_builder [min_part_hours]
you can change it with set_min_part_hours. you can change it with set_min_part_hours.
""" """
if exists(builder_file): if exists(builder_file):
print 'Cowardly refusing to overwrite existing ' \ print('Cowardly refusing to overwrite existing '
'Ring Builder file: %s' % builder_file 'Ring Builder file: %s' % builder_file)
exit(EXIT_ERROR) exit(EXIT_ERROR)
if len(argv) > 3: if len(argv) > 3:
min_part_hours = int(argv[3]) min_part_hours = int(argv[3])
@ -1025,11 +1026,11 @@ swift-ring-builder <builder_file> set_min_part_hours <hours>
to determine this more easily than scanning logs. to determine this more easily than scanning logs.
""" """
if len(argv) < 4: if len(argv) < 4:
print Commands.set_min_part_hours.__doc__.strip() print(Commands.set_min_part_hours.__doc__.strip())
exit(EXIT_ERROR) exit(EXIT_ERROR)
builder.change_min_part_hours(int(argv[3])) builder.change_min_part_hours(int(argv[3]))
print 'The minimum number of hours before a partition can be ' \ print('The minimum number of hours before a partition can be '
'reassigned is now set to %s' % argv[3] 'reassigned is now set to %s' % argv[3])
builder.save(argv[1]) builder.save(argv[1])
exit(EXIT_SUCCESS) exit(EXIT_SUCCESS)
@ -1044,24 +1045,24 @@ swift-ring-builder <builder_file> set_replicas <replicas>
A rebalance is needed to make the change take effect. A rebalance is needed to make the change take effect.
""" """
if len(argv) < 4: if len(argv) < 4:
print Commands.set_replicas.__doc__.strip() print(Commands.set_replicas.__doc__.strip())
exit(EXIT_ERROR) exit(EXIT_ERROR)
new_replicas = argv[3] new_replicas = argv[3]
try: try:
new_replicas = float(new_replicas) new_replicas = float(new_replicas)
except ValueError: except ValueError:
print Commands.set_replicas.__doc__.strip() print(Commands.set_replicas.__doc__.strip())
print "\"%s\" is not a valid number." % new_replicas print("\"%s\" is not a valid number." % new_replicas)
exit(EXIT_ERROR) exit(EXIT_ERROR)
if new_replicas < 1: if new_replicas < 1:
print "Replica count must be at least 1." print("Replica count must be at least 1.")
exit(EXIT_ERROR) exit(EXIT_ERROR)
builder.set_replicas(new_replicas) builder.set_replicas(new_replicas)
print 'The replica count is now %.6f.' % builder.replicas print('The replica count is now %.6f.' % builder.replicas)
print 'The change will take effect after the next rebalance.' print('The change will take effect after the next rebalance.')
builder.save(argv[1]) builder.save(argv[1])
exit(EXIT_SUCCESS) exit(EXIT_SUCCESS)
@ -1073,7 +1074,7 @@ swift-ring-builder <builder_file> set_overload <overload>[%]
A rebalance is needed to make the change take effect. A rebalance is needed to make the change take effect.
""" """
if len(argv) < 4: if len(argv) < 4:
print Commands.set_overload.__doc__.strip() print(Commands.set_overload.__doc__.strip())
exit(EXIT_ERROR) exit(EXIT_ERROR)
new_overload = argv[3] new_overload = argv[3]
@ -1085,26 +1086,26 @@ swift-ring-builder <builder_file> set_overload <overload>[%]
try: try:
new_overload = float(new_overload) new_overload = float(new_overload)
except ValueError: except ValueError:
print Commands.set_overload.__doc__.strip() print(Commands.set_overload.__doc__.strip())
print "%r is not a valid number." % new_overload print("%r is not a valid number." % new_overload)
exit(EXIT_ERROR) exit(EXIT_ERROR)
if percent: if percent:
new_overload *= 0.01 new_overload *= 0.01
if new_overload < 0: if new_overload < 0:
print "Overload must be non-negative." print("Overload must be non-negative.")
exit(EXIT_ERROR) exit(EXIT_ERROR)
if new_overload > 1 and not percent: if new_overload > 1 and not percent:
print "!?! Warning overload is greater than 100% !?!" print("!?! Warning overload is greater than 100% !?!")
status = EXIT_WARNING status = EXIT_WARNING
else: else:
status = EXIT_SUCCESS status = EXIT_SUCCESS
builder.set_overload(new_overload) builder.set_overload(new_overload)
print 'The overload factor is now %0.2f%% (%.6f)' % ( print('The overload factor is now %0.2f%% (%.6f)' % (
builder.overload * 100, builder.overload) builder.overload * 100, builder.overload))
print 'The change will take effect after the next rebalance.' print('The change will take effect after the next rebalance.')
builder.save(argv[1]) builder.save(argv[1])
exit(status) exit(status)
@ -1117,21 +1118,21 @@ def main(arguments=None):
argv = sys_argv argv = sys_argv
if len(argv) < 2: if len(argv) < 2:
print "swift-ring-builder %(MAJOR_VERSION)s.%(MINOR_VERSION)s\n" % \ print("swift-ring-builder %(MAJOR_VERSION)s.%(MINOR_VERSION)s\n" %
globals() globals())
print Commands.default.__doc__.strip() print(Commands.default.__doc__.strip())
print print()
cmds = [c for c, f in Commands.__dict__.items() cmds = [c for c, f in Commands.__dict__.items()
if f.__doc__ and c[0] != '_' and c != 'default'] if f.__doc__ and c[0] != '_' and c != 'default']
cmds.sort() cmds.sort()
for cmd in cmds: for cmd in cmds:
print Commands.__dict__[cmd].__doc__.strip() print(Commands.__dict__[cmd].__doc__.strip())
print print()
print parse_search_value.__doc__.strip() print(parse_search_value.__doc__.strip())
print print()
for line in wrap(' '.join(cmds), 79, initial_indent='Quick list: ', for line in wrap(' '.join(cmds), 79, initial_indent='Quick list: ',
subsequent_indent=' '): subsequent_indent=' '):
print line print(line)
print('Exit codes: 0 = operation successful\n' print('Exit codes: 0 = operation successful\n'
' 1 = operation completed with warnings\n' ' 1 = operation completed with warnings\n'
' 2 = error') ' 2 = error')
@ -1142,11 +1143,11 @@ def main(arguments=None):
try: try:
builder = RingBuilder.load(builder_file) builder = RingBuilder.load(builder_file)
except exceptions.UnPicklingError as e: except exceptions.UnPicklingError as e:
print e print(e)
exit(EXIT_ERROR) exit(EXIT_ERROR)
except (exceptions.FileNotFoundError, exceptions.PermissionError) as e: except (exceptions.FileNotFoundError, exceptions.PermissionError) as e:
if len(argv) < 3 or argv[2] not in('create', 'write_builder'): if len(argv) < 3 or argv[2] not in('create', 'write_builder'):
print e print(e)
exit(EXIT_ERROR) exit(EXIT_ERROR)
except Exception as e: except Exception as e:
print('Problem occurred while reading builder file: %s. %s' % print('Problem occurred while reading builder file: %s. %s' %
@ -1169,7 +1170,7 @@ def main(arguments=None):
with lock_parent_directory(abspath(argv[1]), 15): with lock_parent_directory(abspath(argv[1]), 15):
Commands.__dict__.get(command, Commands.unknown.im_func)() Commands.__dict__.get(command, Commands.unknown.im_func)()
except exceptions.LockTimeout: except exceptions.LockTimeout:
print "Ring/builder dir currently locked." print("Ring/builder dir currently locked.")
exit(2) exit(2)
else: else:
Commands.__dict__.get(command, Commands.unknown.im_func)() Commands.__dict__.get(command, Commands.unknown.im_func)()

View File

@ -13,6 +13,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
from __future__ import print_function
import functools import functools
import errno import errno
import os import os
@ -62,22 +63,22 @@ def setup_env():
resource.setrlimit(resource.RLIMIT_NOFILE, resource.setrlimit(resource.RLIMIT_NOFILE,
(MAX_DESCRIPTORS, MAX_DESCRIPTORS)) (MAX_DESCRIPTORS, MAX_DESCRIPTORS))
except ValueError: except ValueError:
print _("WARNING: Unable to modify file descriptor limit. " print(_("WARNING: Unable to modify file descriptor limit. "
"Running as non-root?") "Running as non-root?"))
try: try:
resource.setrlimit(resource.RLIMIT_DATA, resource.setrlimit(resource.RLIMIT_DATA,
(MAX_MEMORY, MAX_MEMORY)) (MAX_MEMORY, MAX_MEMORY))
except ValueError: except ValueError:
print _("WARNING: Unable to modify memory limit. " print(_("WARNING: Unable to modify memory limit. "
"Running as non-root?") "Running as non-root?"))
try: try:
resource.setrlimit(resource.RLIMIT_NPROC, resource.setrlimit(resource.RLIMIT_NPROC,
(MAX_PROCS, MAX_PROCS)) (MAX_PROCS, MAX_PROCS))
except ValueError: except ValueError:
print _("WARNING: Unable to modify max process limit. " print(_("WARNING: Unable to modify max process limit. "
"Running as non-root?") "Running as non-root?"))
# Set PYTHON_EGG_CACHE if it isn't already set # Set PYTHON_EGG_CACHE if it isn't already set
os.environ.setdefault('PYTHON_EGG_CACHE', '/tmp') os.environ.setdefault('PYTHON_EGG_CACHE', '/tmp')
@ -217,7 +218,7 @@ class Manager(object):
try: try:
status += server.interact(**kwargs) status += server.interact(**kwargs)
except KeyboardInterrupt: except KeyboardInterrupt:
print _('\nuser quit') print(_('\nuser quit'))
self.stop(**kwargs) self.stop(**kwargs)
break break
elif kwargs.get('wait', True): elif kwargs.get('wait', True):
@ -254,7 +255,7 @@ class Manager(object):
for server in self.servers: for server in self.servers:
signaled_pids = server.stop(**kwargs) signaled_pids = server.stop(**kwargs)
if not signaled_pids: if not signaled_pids:
print _('No %s running') % server print(_('No %s running') % server)
else: else:
server_pids[server] = signaled_pids server_pids[server] = signaled_pids
@ -267,7 +268,7 @@ class Manager(object):
for server, killed_pid in watch_server_pids(server_pids, for server, killed_pid in watch_server_pids(server_pids,
interval=kill_wait, interval=kill_wait,
**kwargs): **kwargs):
print _("%s (%s) appears to have stopped") % (server, killed_pid) print(_("%s (%s) appears to have stopped") % (server, killed_pid))
killed_pids.add(killed_pid) killed_pids.add(killed_pid)
if not killed_pids.symmetric_difference(signaled_pids): if not killed_pids.symmetric_difference(signaled_pids):
# all processes have been stopped # all processes have been stopped
@ -277,8 +278,8 @@ class Manager(object):
for server, pids in server_pids.items(): for server, pids in server_pids.items():
if not killed_pids.issuperset(pids): if not killed_pids.issuperset(pids):
# some pids of this server were not killed # some pids of this server were not killed
print _('Waited %s seconds for %s to die; giving up') % ( print(_('Waited %s seconds for %s to die; giving up') % (
kill_wait, server) kill_wait, server))
return 1 return 1
@command @command
@ -461,15 +462,15 @@ class Server(object):
# maybe there's a config file(s) out there, but I couldn't find it! # maybe there's a config file(s) out there, but I couldn't find it!
if not kwargs.get('quiet'): if not kwargs.get('quiet'):
if number: if number:
print _('Unable to locate config number %s for %s' % ( print(_('Unable to locate config number %s for %s' % (
number, self.server)) number, self.server)))
else: else:
print _('Unable to locate config for %s' % (self.server)) print(_('Unable to locate config for %s' % (self.server)))
if kwargs.get('verbose') and not kwargs.get('quiet'): if kwargs.get('verbose') and not kwargs.get('quiet'):
if found_conf_files: if found_conf_files:
print _('Found configs:') print(_('Found configs:'))
for i, conf_file in enumerate(found_conf_files): for i, conf_file in enumerate(found_conf_files):
print ' %d) %s' % (i + 1, conf_file) print(' %d) %s' % (i + 1, conf_file))
return conf_files return conf_files
@ -514,27 +515,27 @@ class Server(object):
pids = {} pids = {}
for pid_file, pid in self.iter_pid_files(**kwargs): for pid_file, pid in self.iter_pid_files(**kwargs):
if not pid: # Catches None and 0 if not pid: # Catches None and 0
print _('Removing pid file %s with invalid pid') % pid_file print (_('Removing pid file %s with invalid pid') % pid_file)
remove_file(pid_file) remove_file(pid_file)
continue continue
try: try:
if sig != signal.SIG_DFL: if sig != signal.SIG_DFL:
print _('Signal %s pid: %s signal: %s') % (self.server, print(_('Signal %s pid: %s signal: %s') % (self.server,
pid, sig) pid, sig))
safe_kill(pid, sig, 'swift-%s' % self.server) safe_kill(pid, sig, 'swift-%s' % self.server)
except InvalidPidFileException as e: except InvalidPidFileException as e:
if kwargs.get('verbose'): if kwargs.get('verbose'):
print _('Removing pid file %s with wrong pid %d') \ print(_('Removing pid file %s with wrong pid %d') % (
% (pid_file, pid) pid_file, pid))
remove_file(pid_file) remove_file(pid_file)
except OSError as e: except OSError as e:
if e.errno == errno.ESRCH: if e.errno == errno.ESRCH:
# pid does not exist # pid does not exist
if kwargs.get('verbose'): if kwargs.get('verbose'):
print _("Removing stale pid file %s") % pid_file print(_("Removing stale pid file %s") % pid_file)
remove_file(pid_file) remove_file(pid_file)
elif e.errno == errno.EPERM: elif e.errno == errno.EPERM:
print _("No permission to signal PID %d") % pid print(_("No permission to signal PID %d") % pid)
else: else:
# process exists # process exists
pids[pid] = pid_file pids[pid] = pid_file
@ -579,14 +580,14 @@ class Server(object):
kwargs['quiet'] = True kwargs['quiet'] = True
conf_files = self.conf_files(**kwargs) conf_files = self.conf_files(**kwargs)
if conf_files: if conf_files:
print _("%s #%d not running (%s)") % (self.server, number, print(_("%s #%d not running (%s)") % (self.server, number,
conf_files[0]) conf_files[0]))
else: else:
print _("No %s running") % self.server print(_("No %s running") % self.server)
return 1 return 1
for pid, pid_file in pids.items(): for pid, pid_file in pids.items():
conf_file = self.get_conf_file_name(pid_file) conf_file = self.get_conf_file_name(pid_file)
print _("%s running (%s - %s)") % (self.server, pid, conf_file) print(_("%s running (%s - %s)") % (self.server, pid, conf_file))
return 0 return 0
def spawn(self, conf_file, once=False, wait=True, daemon=True, **kwargs): def spawn(self, conf_file, once=False, wait=True, daemon=True, **kwargs):
@ -638,7 +639,7 @@ class Server(object):
# no-daemon anyway, but this is quieter # no-daemon anyway, but this is quieter
proc.wait() proc.wait()
if output: if output:
print output print(output)
start = time.time() start = time.time()
# wait for process to die (output may just be a warning) # wait for process to die (output may just be a warning)
while time.time() - start < WARNING_WAIT: while time.time() - start < WARNING_WAIT:
@ -679,13 +680,14 @@ class Server(object):
# any unstarted instances # any unstarted instances
if conf_file in conf_files: if conf_file in conf_files:
already_started = True already_started = True
print _("%s running (%s - %s)") % (self.server, pid, conf_file) print(_("%s running (%s - %s)") %
(self.server, pid, conf_file))
elif not kwargs.get('number', 0): elif not kwargs.get('number', 0):
already_started = True already_started = True
print _("%s running (%s - %s)") % (self.server, pid, pid_file) print(_("%s running (%s - %s)") % (self.server, pid, pid_file))
if already_started: if already_started:
print _("%s already started...") % self.server print(_("%s already started...") % self.server)
return {} return {}
if self.server not in START_ONCE_SERVERS: if self.server not in START_ONCE_SERVERS:
@ -697,13 +699,13 @@ class Server(object):
msg = _('Running %s once') % self.server msg = _('Running %s once') % self.server
else: else:
msg = _('Starting %s') % self.server msg = _('Starting %s') % self.server
print '%s...(%s)' % (msg, conf_file) print('%s...(%s)' % (msg, conf_file))
try: try:
pid = self.spawn(conf_file, **kwargs) pid = self.spawn(conf_file, **kwargs)
except OSError as e: except OSError as e:
if e.errno == errno.ENOENT: if e.errno == errno.ENOENT:
#TODO(clayg): should I check if self.cmd exists earlier? #TODO(clayg): should I check if self.cmd exists earlier?
print _("%s does not exist") % self.cmd print(_("%s does not exist") % self.cmd)
break break
else: else:
raise raise

View File

@ -13,6 +13,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
from __future__ import print_function
import logging import logging
import os import os
import signal import signal
@ -254,8 +255,8 @@ class ContainerUpdater(Daemon):
self.account_suppressions[info['account']] = until = \ self.account_suppressions[info['account']] = until = \
time.time() + self.account_suppression_time time.time() + self.account_suppression_time
if self.new_account_suppressions: if self.new_account_suppressions:
print >>self.new_account_suppressions, \ print(info['account'], until,
info['account'], until file=self.new_account_suppressions)
# Only track timing data for attempted updates: # Only track timing data for attempted updates:
self.logger.timing_since('timing', start_time) self.logger.timing_since('timing', start_time)
else: else: