Python 3 compatibility: use print as a function.

In Python 3 print is a function.
Especially for multiple string print, need to import
print_function from __future__.

Story: 2003430

Task: 24915

Signed-off: zhangyangyang <zhangyangyang@unionpay.com>

Change-Id: I40ae72b8efb7b342489ad2191ad02b0a5fb2898f
This commit is contained in:
zhangyangyang 2018-08-29 15:29:46 +08:00
parent d21026748a
commit 75b8c70d9d
10 changed files with 76 additions and 76 deletions

View File

@ -40,7 +40,7 @@ def main():
config.load(args.config) config.load(args.config)
if not config.CONF: if not config.CONF:
print "Error: configuration not available." print("Error: configuration not available.")
sys.exit(-1) sys.exit(-1)
log.configure(config.CONF) log.configure(config.CONF)
@ -50,18 +50,18 @@ def main():
wsgi.serve_forever() wsgi.serve_forever()
except ConfigParser.NoOptionError as e: except ConfigParser.NoOptionError as e:
print e print(e)
sys.exit(-2) sys.exit(-2)
except ConfigParser.NoSectionError as e: except ConfigParser.NoSectionError as e:
print e print(e)
sys.exit(-3) sys.exit(-3)
except KeyboardInterrupt: except KeyboardInterrupt:
sys.exit() sys.exit()
except Exception as e: except Exception as e:
print e print(e)
sys.exit(-4) sys.exit(-4)
main() main()

View File

@ -101,7 +101,7 @@ def generate(srcfiles):
for group, opts in opts_by_group.items(): for group, opts in opts_by_group.items():
print_group_opts(group, opts) print_group_opts(group, opts)
print "# Total option count: %d" % OPTION_COUNT print("# Total option count: %d" % OPTION_COUNT)
def _import_module(mod_str): def _import_module(mod_str):
@ -165,18 +165,18 @@ def _list_opts(obj):
def print_group_opts(group, opts_by_module): def print_group_opts(group, opts_by_module):
print "[%s]" % group print("[%s]" % group)
print print("")
global OPTION_COUNT global OPTION_COUNT
for mod, opts in opts_by_module: for mod, opts in opts_by_module:
OPTION_COUNT += len(opts) OPTION_COUNT += len(opts)
print '#' print('#')
print '# Options defined in %s' % mod print('# Options defined in %s' % mod)
print '#' print('#')
print print("")
for opt in opts: for opt in opts:
_print_opt(opt) _print_opt(opt)
print print("")
def _get_my_ip(): def _get_my_ip():
@ -216,33 +216,33 @@ def _print_opt(opt):
sys.stderr.write("%s\n" % str(err)) sys.stderr.write("%s\n" % str(err))
sys.exit(1) sys.exit(1)
opt_help += ' (' + OPT_TYPES[opt_type] + ')' opt_help += ' (' + OPT_TYPES[opt_type] + ')'
print '#', "\n# ".join(textwrap.wrap(opt_help, WORDWRAP_WIDTH)) print('#', "\n# ".join(textwrap.wrap(opt_help, WORDWRAP_WIDTH)))
try: try:
if opt_default is None: if opt_default is None:
print '#%s=<None>' % opt_name print('#%s=<None>' % opt_name)
elif opt_type == STROPT: elif opt_type == STROPT:
assert(isinstance(opt_default, basestring)) assert(isinstance(opt_default, basestring))
print '#%s=%s' % (opt_name, _sanitize_default(opt_default)) print('#%s=%s' % (opt_name, _sanitize_default(opt_default)))
elif opt_type == BOOLOPT: elif opt_type == BOOLOPT:
assert(isinstance(opt_default, bool)) assert(isinstance(opt_default, bool))
print '#%s=%s' % (opt_name, str(opt_default).lower()) print('#%s=%s' % (opt_name, str(opt_default).lower()))
elif opt_type == INTOPT: elif opt_type == INTOPT:
assert(isinstance(opt_default, int) and assert(isinstance(opt_default, int) and
not isinstance(opt_default, bool)) not isinstance(opt_default, bool))
print '#%s=%s' % (opt_name, opt_default) print('#%s=%s' % (opt_name, opt_default))
elif opt_type == FLOATOPT: elif opt_type == FLOATOPT:
assert(isinstance(opt_default, float)) assert(isinstance(opt_default, float))
print '#%s=%s' % (opt_name, opt_default) print('#%s=%s' % (opt_name, opt_default))
elif opt_type == LISTOPT: elif opt_type == LISTOPT:
assert(isinstance(opt_default, list)) assert(isinstance(opt_default, list))
print '#%s=%s' % (opt_name, ','.join(opt_default)) print('#%s=%s' % (opt_name, ','.join(opt_default)))
elif opt_type == MULTISTROPT: elif opt_type == MULTISTROPT:
assert(isinstance(opt_default, list)) assert(isinstance(opt_default, list))
if not opt_default: if not opt_default:
opt_default = [''] opt_default = ['']
for default in opt_default: for default in opt_default:
print '#%s=%s' % (opt_name, default) print('#%s=%s' % (opt_name, default))
print print("")
except Exception: except Exception:
sys.stderr.write('Error in option "%s"\n' % opt_name) sys.stderr.write('Error in option "%s"\n' % opt_name)
sys.exit(1) sys.exit(1)
@ -250,7 +250,7 @@ def _print_opt(opt):
def main(): def main():
if len(sys.argv) < 2: if len(sys.argv) < 2:
print "usage: %s [srcfile]...\n" % sys.argv[0] print("usage: %s [srcfile]...\n" % sys.argv[0])
sys.exit(0) sys.exit(0)
generate(sys.argv[1:]) generate(sys.argv[1:])

View File

@ -101,7 +101,7 @@ def generate(srcfiles):
for group, opts in opts_by_group.items(): for group, opts in opts_by_group.items():
print_group_opts(group, opts) print_group_opts(group, opts)
print "# Total option count: %d" % OPTION_COUNT print("# Total option count: %d" % OPTION_COUNT)
def _import_module(mod_str): def _import_module(mod_str):
@ -165,18 +165,18 @@ def _list_opts(obj):
def print_group_opts(group, opts_by_module): def print_group_opts(group, opts_by_module):
print "[%s]" % group print("[%s]" % group)
print print("")
global OPTION_COUNT global OPTION_COUNT
for mod, opts in opts_by_module: for mod, opts in opts_by_module:
OPTION_COUNT += len(opts) OPTION_COUNT += len(opts)
print '#' print('#')
print '# Options defined in %s' % mod print('# Options defined in %s' % mod)
print '#' print('#')
print print("")
for opt in opts: for opt in opts:
_print_opt(opt) _print_opt(opt)
print print("")
def _get_my_ip(): def _get_my_ip():
@ -216,33 +216,33 @@ def _print_opt(opt):
sys.stderr.write("%s\n" % str(err)) sys.stderr.write("%s\n" % str(err))
sys.exit(1) sys.exit(1)
opt_help += ' (' + OPT_TYPES[opt_type] + ')' opt_help += ' (' + OPT_TYPES[opt_type] + ')'
print '#', "\n# ".join(textwrap.wrap(opt_help, WORDWRAP_WIDTH)) print('#', "\n# ".join(textwrap.wrap(opt_help, WORDWRAP_WIDTH)))
try: try:
if opt_default is None: if opt_default is None:
print '#%s=<None>' % opt_name print('#%s=<None>' % opt_name)
elif opt_type == STROPT: elif opt_type == STROPT:
assert(isinstance(opt_default, basestring)) assert(isinstance(opt_default, basestring))
print '#%s=%s' % (opt_name, _sanitize_default(opt_default)) print('#%s=%s' % (opt_name, _sanitize_default(opt_default)))
elif opt_type == BOOLOPT: elif opt_type == BOOLOPT:
assert(isinstance(opt_default, bool)) assert(isinstance(opt_default, bool))
print '#%s=%s' % (opt_name, str(opt_default).lower()) print('#%s=%s' % (opt_name, str(opt_default).lower()))
elif opt_type == INTOPT: elif opt_type == INTOPT:
assert(isinstance(opt_default, int) and assert(isinstance(opt_default, int) and
not isinstance(opt_default, bool)) not isinstance(opt_default, bool))
print '#%s=%s' % (opt_name, opt_default) print('#%s=%s' % (opt_name, opt_default))
elif opt_type == FLOATOPT: elif opt_type == FLOATOPT:
assert(isinstance(opt_default, float)) assert(isinstance(opt_default, float))
print '#%s=%s' % (opt_name, opt_default) print('#%s=%s' % (opt_name, opt_default))
elif opt_type == LISTOPT: elif opt_type == LISTOPT:
assert(isinstance(opt_default, list)) assert(isinstance(opt_default, list))
print '#%s=%s' % (opt_name, ','.join(opt_default)) print('#%s=%s' % (opt_name, ','.join(opt_default)))
elif opt_type == MULTISTROPT: elif opt_type == MULTISTROPT:
assert(isinstance(opt_default, list)) assert(isinstance(opt_default, list))
if not opt_default: if not opt_default:
opt_default = [''] opt_default = ['']
for default in opt_default: for default in opt_default:
print '#%s=%s' % (opt_name, default) print('#%s=%s' % (opt_name, default))
print print("")
except Exception: except Exception:
sys.stderr.write('Error in option "%s"\n' % opt_name) sys.stderr.write('Error in option "%s"\n' % opt_name)
sys.exit(1) sys.exit(1)
@ -250,7 +250,7 @@ def _print_opt(opt):
def main(): def main():
if len(sys.argv) < 2: if len(sys.argv) < 2:
print "usage: %s [srcfile]...\n" % sys.argv[0] print("usage: %s [srcfile]...\n" % sys.argv[0])
sys.exit(0) sys.exit(0)
generate(sys.argv[1:]) generate(sys.argv[1:])

View File

@ -44,7 +44,7 @@ def main():
% args.service) % args.service)
row = cursor.fetchone() row = cursor.fetchone()
if row is None: if row is None:
print "Given service (%s) does not exist." % args.service print("Given service (%s) does not exist." % args.service)
sys.exit() sys.exit()
database.close() database.close()
@ -58,7 +58,7 @@ def main():
if os.path.isfile(unmanage_filepath + unmanage_filename): if os.path.isfile(unmanage_filepath + unmanage_filename):
os.remove(unmanage_filepath + unmanage_filename) os.remove(unmanage_filepath + unmanage_filename)
print "Service (%s) is now being managed." % args.service print("Service (%s) is now being managed." % args.service)
elif 'unmanage' == action: elif 'unmanage' == action:
if not os.path.exists(SM_VAR_RUN_SERVICES_DIR): if not os.path.exists(SM_VAR_RUN_SERVICES_DIR):
@ -67,16 +67,16 @@ def main():
if not os.path.isfile(unmanage_filepath + unmanage_filename): if not os.path.isfile(unmanage_filepath + unmanage_filename):
open(unmanage_filepath + unmanage_filename, 'w').close() open(unmanage_filepath + unmanage_filename, 'w').close()
print "Service (%s) is no longer being managed." % args.service print("Service (%s) is no longer being managed." % args.service)
elif 'restart-safe' == action: elif 'restart-safe' == action:
restart_service_safe(args.service) restart_service_safe(args.service)
print "Service (%s) is restarting." % args.service print("Service (%s) is restarting." % args.service)
else: else:
restart_service(args.service) restart_service(args.service)
print "Service (%s) is restarting." % args.service print("Service (%s) is restarting." % args.service)
sys.exit(0) sys.exit(0)
@ -84,5 +84,5 @@ def main():
sys.exit() sys.exit()
except Exception as e: except Exception as e:
print e print(e)
sys.exit(-1) sys.exit(-1)

View File

@ -40,7 +40,7 @@ def _send_msg_to_sm(sm_api_msg):
time.sleep(1) time.sleep(1)
except socket.error, e: except socket.error, e:
print "sm-api socket error: %s on %s" % (e, sm_api_msg) print("sm-api socket error: %s on %s" % (e, sm_api_msg))
def restart_service(service_name): def restart_service(service_name):

View File

@ -89,7 +89,7 @@ def main():
sys.exit() sys.exit()
except Exception as e: except Exception as e:
print e print(e)
sys.exit(-1) sys.exit(-1)

View File

@ -60,7 +60,7 @@ def main():
args = parser.parse_args() args = parser.parse_args()
if not os.path.exists(database_name): if not os.path.exists(database_name):
print "%s not available." % database_name print("%s not available." % database_name)
sys.exit(0) sys.exit(0)
database = sqlite3.connect(database_name) database = sqlite3.connect(database_name)
@ -69,7 +69,7 @@ def main():
if args.verbose: if args.verbose:
# Service-Groups Dump # Service-Groups Dump
print "\n-Service_Groups%s" % ('-' * 92) print("\n-Service_Groups%s" % ('-' * 92))
cursor.execute("SELECT name, desired_state, state, status, " cursor.execute("SELECT name, desired_state, state, status, "
"condition from service_groups WHERE " "condition from service_groups WHERE "
@ -79,11 +79,11 @@ def main():
if data is not None: if data is not None:
for row in data: for row in data:
print "%-32s %-20s %-20s %-10s %-20s" % (row[0], row[1], print("%-32s %-20s %-20s %-10s %-20s" % (row[0], row[1],
row[2], row[3], row[2], row[3],
row[4]) row[4]))
print "%s" % ('-' * 107) print("%s" % ('-' * 107))
# Services Dump # Services Dump
len = 98 len = 98
@ -96,7 +96,7 @@ def main():
if args.pid_file: if args.pid_file:
len += 28 len += 28
print "\n-Services%s" % ('-' * len) print("\n-Services%s" % ('-' * len))
cursor.execute("SELECT s.name, s.desired_state, s.state, " cursor.execute("SELECT s.name, s.desired_state, s.state, "
"s.status, s.condition, s.pid_file, " "s.status, s.condition, s.pid_file, "
@ -124,11 +124,11 @@ def main():
msg += "%-10s %20s" % (row[3], row[4]) msg += "%-10s %20s" % (row[3], row[4])
print msg print msg
print "%s" % ('-' * len) print("%s" % ('-' * len))
else: else:
# Service-Groups Dump # Service-Groups Dump
print "\n-Service_Groups%s" % ('-' * 72) print("\n-Service_Groups%s" % ('-' * 72))
cursor.execute("SELECT name, desired_state, state, status " cursor.execute("SELECT name, desired_state, state, status "
"from service_groups WHERE PROVISIONED = 'yes';") "from service_groups WHERE PROVISIONED = 'yes';")
@ -137,10 +137,10 @@ def main():
if data is not None: if data is not None:
for row in data: for row in data:
print "%-32s %-20s %-20s %-10s" % (row[0], row[1], row[2], print("%-32s %-20s %-20s %-10s" % (row[0], row[1], row[2],
row[3]) row[3]))
print "%s" % ('-' * 87) print("%s" % ('-' * 87))
len = 78 len = 78
if args.impact: if args.impact:
@ -153,7 +153,7 @@ def main():
len += 28 len += 28
# Services Dump # Services Dump
print "\n-Services%s" % ('-' * len) print("\n-Services%s" % ('-' * len))
cursor.execute("SELECT s.name, s.desired_state, s.state, s.status, " cursor.execute("SELECT s.name, s.desired_state, s.state, s.status, "
"s.pid_file, g.SERVICE_FAILURE_IMPACT " "s.pid_file, g.SERVICE_FAILURE_IMPACT "
@ -178,9 +178,9 @@ def main():
if args.pid_file: if args.pid_file:
msg += "%-25s" % (pid_file) msg += "%-25s" % (pid_file)
msg += "%-10s " % (row[3]) msg += "%-10s " % (row[3])
print msg print(msg)
print "%s" % ('-' * len) print("%s" % ('-' * len))
database.close() database.close()
@ -188,7 +188,7 @@ def main():
sys.exit() sys.exit()
except Exception as e: except Exception as e:
print e print(e)
sys.exit(-1) sys.exit(-1)
try: try:

View File

@ -28,13 +28,13 @@ def main():
if args.which == 'database': if args.which == 'database':
if args.which_database == 'master': if args.which_database == 'master':
if not os.path.exists(database_master_name): if not os.path.exists(database_master_name):
print "%s not available." % database_master_name print("%s not available." % database_master_name)
sys.exit() sys.exit()
database = sqlite3.connect(database_master_name) database = sqlite3.connect(database_master_name)
else: else:
if not os.path.exists(database_running_name): if not os.path.exists(database_running_name):
print "%s not available." % database_running_name print("%s not available." % database_running_name)
sys.exit() sys.exit()
database = sqlite3.connect(database_running_name) database = sqlite3.connect(database_running_name)
@ -55,5 +55,5 @@ def main():
sys.exit() sys.exit()
except Exception as e: except Exception as e:
print e print(e)
sys.exit(-1) sys.exit(-1)

View File

@ -156,5 +156,5 @@ def main():
sys.exit() sys.exit()
except Exception as e: except Exception as e:
print e print(e)
sys.exit(-1) sys.exit(-1)

View File

@ -14,7 +14,7 @@ database_name = "/var/run/sm/sm.db"
def main(): def main():
if not os.path.exists(database_name): if not os.path.exists(database_name):
print "%s not available." % database_name print("%s not available." % database_name)
sys.exit(0) sys.exit(0)
try: try:
@ -43,7 +43,7 @@ def main():
row = cursor.fetchone() row = cursor.fetchone()
if row is None: if row is None:
print "%s is disabled." % args.service_name print("%s is disabled." % args.service_name)
else: else:
service_name = row[0] service_name = row[0]
@ -51,9 +51,9 @@ def main():
status = row[3] status = row[3]
if status == 'none': if status == 'none':
print "%s is %s" % (service_name, state) print("%s is %s" % (service_name, state))
else: else:
print "%s is %s-%s" % (service_name, state, status) print("%s is %s-%s" % (service_name, state, status))
database.close() database.close()
@ -80,7 +80,7 @@ def main():
desired_state = row[1] desired_state = row[1]
state = row[2] state = row[2]
print fmt.format(service_group_name, state, desired_state) print(fmt.format(service_group_name, state, desired_state))
database.close() database.close()
@ -90,13 +90,13 @@ def main():
not_found_list.append(g) not_found_list.append(g)
if len(not_found_list) > 1: if len(not_found_list) > 1:
print "%s are not provisioned"%','.join( (g for g in not_found_list)) print("%s are not provisioned"%','.join( (g for g in not_found_list)))
elif len(not_found_list) == 1: elif len(not_found_list) == 1:
print "%s is not provisioned" % ','.join((g for g in not_found_list)) print("%s is not provisioned" % ','.join((g for g in not_found_list)))
except KeyboardInterrupt: except KeyboardInterrupt:
sys.exit() sys.exit()
except Exception as e: except Exception as e:
print e print(e)
sys.exit(-1) sys.exit(-1)