From 75b8c70d9d6dd11f77845626a7302857ea8d13e0 Mon Sep 17 00:00:00 2001 From: zhangyangyang Date: Wed, 29 Aug 2018 15:29:46 +0800 Subject: [PATCH] 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 Change-Id: I40ae72b8efb7b342489ad2191ad02b0a5fb2898f --- service-mgmt-api/sm-api/sm_api/api/api.py | 8 ++--- .../openstack/common/config/generator.py | 36 +++++++++---------- .../openstack/common/config/generator.py | 36 +++++++++---------- .../sm-tools/sm_tools/sm_action.py | 14 ++++---- .../sm-tools/sm_tools/sm_api_msg_utils.py | 2 +- .../sm-tools/sm_tools/sm_configure.py | 2 +- .../sm-tools/sm_tools/sm_dump.py | 30 ++++++++-------- .../sm-tools/sm_tools/sm_patch.py | 6 ++-- .../sm-tools/sm_tools/sm_provision.py | 2 +- .../sm-tools/sm_tools/sm_query.py | 16 ++++----- 10 files changed, 76 insertions(+), 76 deletions(-) diff --git a/service-mgmt-api/sm-api/sm_api/api/api.py b/service-mgmt-api/sm-api/sm_api/api/api.py index 52a6cd17..78135846 100644 --- a/service-mgmt-api/sm-api/sm_api/api/api.py +++ b/service-mgmt-api/sm-api/sm_api/api/api.py @@ -40,7 +40,7 @@ def main(): config.load(args.config) if not config.CONF: - print "Error: configuration not available." + print("Error: configuration not available.") sys.exit(-1) log.configure(config.CONF) @@ -50,18 +50,18 @@ def main(): wsgi.serve_forever() except ConfigParser.NoOptionError as e: - print e + print(e) sys.exit(-2) except ConfigParser.NoSectionError as e: - print e + print(e) sys.exit(-3) except KeyboardInterrupt: sys.exit() except Exception as e: - print e + print(e) sys.exit(-4) main() diff --git a/service-mgmt-api/sm-api/sm_api/openstack/common/config/generator.py b/service-mgmt-api/sm-api/sm_api/openstack/common/config/generator.py index 9e555c90..375e2d36 100755 --- a/service-mgmt-api/sm-api/sm_api/openstack/common/config/generator.py +++ b/service-mgmt-api/sm-api/sm_api/openstack/common/config/generator.py @@ -101,7 +101,7 @@ def generate(srcfiles): for group, opts in opts_by_group.items(): print_group_opts(group, opts) - print "# Total option count: %d" % OPTION_COUNT + print("# Total option count: %d" % OPTION_COUNT) def _import_module(mod_str): @@ -165,18 +165,18 @@ def _list_opts(obj): def print_group_opts(group, opts_by_module): - print "[%s]" % group - print + print("[%s]" % group) + print("") global OPTION_COUNT for mod, opts in opts_by_module: OPTION_COUNT += len(opts) - print '#' - print '# Options defined in %s' % mod - print '#' - print + print('#') + print('# Options defined in %s' % mod) + print('#') + print("") for opt in opts: _print_opt(opt) - print + print("") def _get_my_ip(): @@ -216,33 +216,33 @@ def _print_opt(opt): sys.stderr.write("%s\n" % str(err)) sys.exit(1) 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: if opt_default is None: - print '#%s=' % opt_name + print('#%s=' % opt_name) elif opt_type == STROPT: 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: 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: assert(isinstance(opt_default, int) and not isinstance(opt_default, bool)) - print '#%s=%s' % (opt_name, opt_default) + print('#%s=%s' % (opt_name, opt_default)) elif opt_type == FLOATOPT: assert(isinstance(opt_default, float)) - print '#%s=%s' % (opt_name, opt_default) + print('#%s=%s' % (opt_name, opt_default)) elif opt_type == LISTOPT: 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: assert(isinstance(opt_default, list)) if not opt_default: opt_default = [''] for default in opt_default: - print '#%s=%s' % (opt_name, default) - print + print('#%s=%s' % (opt_name, default)) + print("") except Exception: sys.stderr.write('Error in option "%s"\n' % opt_name) sys.exit(1) @@ -250,7 +250,7 @@ def _print_opt(opt): def main(): if len(sys.argv) < 2: - print "usage: %s [srcfile]...\n" % sys.argv[0] + print("usage: %s [srcfile]...\n" % sys.argv[0]) sys.exit(0) generate(sys.argv[1:]) diff --git a/service-mgmt-client/sm-client/sm_client/openstack/common/config/generator.py b/service-mgmt-client/sm-client/sm_client/openstack/common/config/generator.py index 765aae24..e065adc9 100644 --- a/service-mgmt-client/sm-client/sm_client/openstack/common/config/generator.py +++ b/service-mgmt-client/sm-client/sm_client/openstack/common/config/generator.py @@ -101,7 +101,7 @@ def generate(srcfiles): for group, opts in opts_by_group.items(): print_group_opts(group, opts) - print "# Total option count: %d" % OPTION_COUNT + print("# Total option count: %d" % OPTION_COUNT) def _import_module(mod_str): @@ -165,18 +165,18 @@ def _list_opts(obj): def print_group_opts(group, opts_by_module): - print "[%s]" % group - print + print("[%s]" % group) + print("") global OPTION_COUNT for mod, opts in opts_by_module: OPTION_COUNT += len(opts) - print '#' - print '# Options defined in %s' % mod - print '#' - print + print('#') + print('# Options defined in %s' % mod) + print('#') + print("") for opt in opts: _print_opt(opt) - print + print("") def _get_my_ip(): @@ -216,33 +216,33 @@ def _print_opt(opt): sys.stderr.write("%s\n" % str(err)) sys.exit(1) 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: if opt_default is None: - print '#%s=' % opt_name + print('#%s=' % opt_name) elif opt_type == STROPT: 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: 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: assert(isinstance(opt_default, int) and not isinstance(opt_default, bool)) - print '#%s=%s' % (opt_name, opt_default) + print('#%s=%s' % (opt_name, opt_default)) elif opt_type == FLOATOPT: assert(isinstance(opt_default, float)) - print '#%s=%s' % (opt_name, opt_default) + print('#%s=%s' % (opt_name, opt_default)) elif opt_type == LISTOPT: 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: assert(isinstance(opt_default, list)) if not opt_default: opt_default = [''] for default in opt_default: - print '#%s=%s' % (opt_name, default) - print + print('#%s=%s' % (opt_name, default)) + print("") except Exception: sys.stderr.write('Error in option "%s"\n' % opt_name) sys.exit(1) @@ -250,7 +250,7 @@ def _print_opt(opt): def main(): if len(sys.argv) < 2: - print "usage: %s [srcfile]...\n" % sys.argv[0] + print("usage: %s [srcfile]...\n" % sys.argv[0]) sys.exit(0) generate(sys.argv[1:]) diff --git a/service-mgmt-tools/sm-tools/sm_tools/sm_action.py b/service-mgmt-tools/sm-tools/sm_tools/sm_action.py index cbe3ddaf..8115be65 100644 --- a/service-mgmt-tools/sm-tools/sm_tools/sm_action.py +++ b/service-mgmt-tools/sm-tools/sm_tools/sm_action.py @@ -44,7 +44,7 @@ def main(): % args.service) row = cursor.fetchone() if row is None: - print "Given service (%s) does not exist." % args.service + print("Given service (%s) does not exist." % args.service) sys.exit() database.close() @@ -58,7 +58,7 @@ def main(): if os.path.isfile(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: 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): 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: restart_service_safe(args.service) - print "Service (%s) is restarting." % args.service + print("Service (%s) is restarting." % args.service) else: restart_service(args.service) - print "Service (%s) is restarting." % args.service + print("Service (%s) is restarting." % args.service) sys.exit(0) @@ -84,5 +84,5 @@ def main(): sys.exit() except Exception as e: - print e - sys.exit(-1) \ No newline at end of file + print(e) + sys.exit(-1) diff --git a/service-mgmt-tools/sm-tools/sm_tools/sm_api_msg_utils.py b/service-mgmt-tools/sm-tools/sm_tools/sm_api_msg_utils.py index e586ca80..ef3dc79d 100644 --- a/service-mgmt-tools/sm-tools/sm_tools/sm_api_msg_utils.py +++ b/service-mgmt-tools/sm-tools/sm_tools/sm_api_msg_utils.py @@ -40,7 +40,7 @@ def _send_msg_to_sm(sm_api_msg): time.sleep(1) 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): diff --git a/service-mgmt-tools/sm-tools/sm_tools/sm_configure.py b/service-mgmt-tools/sm-tools/sm_tools/sm_configure.py index 77d3b157..ac976566 100644 --- a/service-mgmt-tools/sm-tools/sm_tools/sm_configure.py +++ b/service-mgmt-tools/sm-tools/sm_tools/sm_configure.py @@ -89,7 +89,7 @@ def main(): sys.exit() except Exception as e: - print e + print(e) sys.exit(-1) diff --git a/service-mgmt-tools/sm-tools/sm_tools/sm_dump.py b/service-mgmt-tools/sm-tools/sm_tools/sm_dump.py index cdb8b568..288b5288 100755 --- a/service-mgmt-tools/sm-tools/sm_tools/sm_dump.py +++ b/service-mgmt-tools/sm-tools/sm_tools/sm_dump.py @@ -60,7 +60,7 @@ def main(): args = parser.parse_args() if not os.path.exists(database_name): - print "%s not available." % database_name + print("%s not available." % database_name) sys.exit(0) database = sqlite3.connect(database_name) @@ -69,7 +69,7 @@ def main(): if args.verbose: # Service-Groups Dump - print "\n-Service_Groups%s" % ('-' * 92) + print("\n-Service_Groups%s" % ('-' * 92)) cursor.execute("SELECT name, desired_state, state, status, " "condition from service_groups WHERE " @@ -79,11 +79,11 @@ def main(): if data is not None: 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[4]) + row[4])) - print "%s" % ('-' * 107) + print("%s" % ('-' * 107)) # Services Dump len = 98 @@ -96,7 +96,7 @@ def main(): if args.pid_file: len += 28 - print "\n-Services%s" % ('-' * len) + print("\n-Services%s" % ('-' * len)) cursor.execute("SELECT s.name, s.desired_state, s.state, " "s.status, s.condition, s.pid_file, " @@ -124,11 +124,11 @@ def main(): msg += "%-10s %20s" % (row[3], row[4]) print msg - print "%s" % ('-' * len) + print("%s" % ('-' * len)) else: # Service-Groups Dump - print "\n-Service_Groups%s" % ('-' * 72) + print("\n-Service_Groups%s" % ('-' * 72)) cursor.execute("SELECT name, desired_state, state, status " "from service_groups WHERE PROVISIONED = 'yes';") @@ -137,10 +137,10 @@ def main(): if data is not None: for row in data: - print "%-32s %-20s %-20s %-10s" % (row[0], row[1], row[2], - row[3]) + print("%-32s %-20s %-20s %-10s" % (row[0], row[1], row[2], + row[3])) - print "%s" % ('-' * 87) + print("%s" % ('-' * 87)) len = 78 if args.impact: @@ -153,7 +153,7 @@ def main(): len += 28 # Services Dump - print "\n-Services%s" % ('-' * len) + print("\n-Services%s" % ('-' * len)) cursor.execute("SELECT s.name, s.desired_state, s.state, s.status, " "s.pid_file, g.SERVICE_FAILURE_IMPACT " @@ -178,9 +178,9 @@ def main(): if args.pid_file: msg += "%-25s" % (pid_file) msg += "%-10s " % (row[3]) - print msg + print(msg) - print "%s" % ('-' * len) + print("%s" % ('-' * len)) database.close() @@ -188,7 +188,7 @@ def main(): sys.exit() except Exception as e: - print e + print(e) sys.exit(-1) try: diff --git a/service-mgmt-tools/sm-tools/sm_tools/sm_patch.py b/service-mgmt-tools/sm-tools/sm_tools/sm_patch.py index e6a7c0c7..d8d1aa6e 100644 --- a/service-mgmt-tools/sm-tools/sm_tools/sm_patch.py +++ b/service-mgmt-tools/sm-tools/sm_tools/sm_patch.py @@ -28,13 +28,13 @@ def main(): if args.which == 'database': if args.which_database == 'master': if not os.path.exists(database_master_name): - print "%s not available." % database_master_name + print("%s not available." % database_master_name) sys.exit() database = sqlite3.connect(database_master_name) else: if not os.path.exists(database_running_name): - print "%s not available." % database_running_name + print("%s not available." % database_running_name) sys.exit() database = sqlite3.connect(database_running_name) @@ -55,5 +55,5 @@ def main(): sys.exit() except Exception as e: - print e + print(e) sys.exit(-1) diff --git a/service-mgmt-tools/sm-tools/sm_tools/sm_provision.py b/service-mgmt-tools/sm-tools/sm_tools/sm_provision.py index 42867e6d..099bc771 100755 --- a/service-mgmt-tools/sm-tools/sm_tools/sm_provision.py +++ b/service-mgmt-tools/sm-tools/sm_tools/sm_provision.py @@ -156,5 +156,5 @@ def main(): sys.exit() except Exception as e: - print e + print(e) sys.exit(-1) diff --git a/service-mgmt-tools/sm-tools/sm_tools/sm_query.py b/service-mgmt-tools/sm-tools/sm_tools/sm_query.py index dde8dd7d..99ea5af9 100644 --- a/service-mgmt-tools/sm-tools/sm_tools/sm_query.py +++ b/service-mgmt-tools/sm-tools/sm_tools/sm_query.py @@ -14,7 +14,7 @@ database_name = "/var/run/sm/sm.db" def main(): if not os.path.exists(database_name): - print "%s not available." % database_name + print("%s not available." % database_name) sys.exit(0) try: @@ -43,7 +43,7 @@ def main(): row = cursor.fetchone() if row is None: - print "%s is disabled." % args.service_name + print("%s is disabled." % args.service_name) else: service_name = row[0] @@ -51,9 +51,9 @@ def main(): status = row[3] if status == 'none': - print "%s is %s" % (service_name, state) + print("%s is %s" % (service_name, state)) else: - print "%s is %s-%s" % (service_name, state, status) + print("%s is %s-%s" % (service_name, state, status)) database.close() @@ -80,7 +80,7 @@ def main(): desired_state = row[1] state = row[2] - print fmt.format(service_group_name, state, desired_state) + print(fmt.format(service_group_name, state, desired_state)) database.close() @@ -90,13 +90,13 @@ def main(): not_found_list.append(g) 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: - 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: sys.exit() except Exception as e: - print e + print(e) sys.exit(-1)