From: Sarthak Munshi Date: Sat, 5 Mar 2016 17:03:45 +0000 (+0530) Subject: ceph.in: Minor python3 specific changes X-Git-Tag: v10.1.0~119^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=refs%2Fpull%2F7947%2Fhead;p=ceph.git ceph.in: Minor python3 specific changes Signed-off-by: Sarthak Munshi --- diff --git a/src/ceph.in b/src/ceph.in index 603440afd1a3..79c226351253 100755 --- a/src/ceph.in +++ b/src/ceph.in @@ -19,11 +19,17 @@ License version 2, as published by the Free Software Foundation. See file COPYING. """ +from __future__ import print_function import codecs import os import sys import platform +try: + input = raw_input +except NameError: + pass + CEPH_GIT_VER="@CEPH_GIT_VER@" CEPH_GIT_NICE_VER="@CEPH_GIT_NICE_VER@" @@ -51,11 +57,11 @@ def respawn_in_path(lib_path, pybind_path, pythonlib_path): if lib_path_var in os.environ: if lib_path not in os.environ[lib_path_var]: os.environ[lib_path_var] += ':' + lib_path - print >> sys.stderr, DEVMODEMSG + print(DEVMODEMSG, file=sys.stderr) os.execvp(py_binary, execv_cmd + sys.argv) else: os.environ[lib_path_var] = lib_path - print >> sys.stderr, DEVMODEMSG + print(DEVMODEMSG, file=sys.stderr) os.execvp(py_binary, execv_cmd + sys.argv) sys.path.insert(0, os.path.join(MYDIR, pybind_path)) sys.path.insert(0, os.path.join(MYDIR, pythonlib_path)) @@ -242,7 +248,7 @@ def parse_cmdargs(args=None, target=''): def hdr(s): - print '\n', s, '\n', '=' * len(s) + print('\n', s, '\n', '=' * len(s)) def do_basic_help(parser, args): """ @@ -262,9 +268,8 @@ def do_extended_help(parser, args): prefix='get_command_descriptions', timeout=10) if ret: - print >> sys.stderr, \ - "couldn't get command descriptions for {0}: {1}".\ - format(target, outs) + print("couldn't get command descriptions for {0}: {1}".\ + format(target, outs), file=sys.stderr) else: help_for_sigs(outbuf, partial) @@ -384,7 +389,7 @@ PROMPT = 'ceph> ' if sys.stdin.isatty(): def read_input(): while True: - line = raw_input(PROMPT).rstrip() + line = input(PROMPT).rstrip() if line in ['q', 'quit', 'Q', 'exit']: return None if line: @@ -412,7 +417,7 @@ def new_style_command(parsed_args, cmdargs, target, sigdict, inbuf, verbose): for cmdtag in sorted(sigdict.keys()): cmd = sigdict[cmdtag] sig = cmd['sig'] - print '{0}: {1}'.format(cmdtag, concise_sig(sig)) + print('{0}: {1}'.format(cmdtag, concise_sig(sig))) if True: if cmdargs: @@ -426,7 +431,7 @@ def new_style_command(parsed_args, cmdargs, target, sigdict, inbuf, verbose): else: if sys.stdin.isatty(): # do the command-interpreter looping - # for raw_input to do readline cmd editing + # for input to do readline cmd editing import readline # noqa while True: @@ -437,35 +442,35 @@ def new_style_command(parsed_args, cmdargs, target, sigdict, inbuf, verbose): try: target = find_cmd_target(cmdargs) except Exception as e: - print >> sys.stderr, \ - 'error handling command target: {0}'.format(e) + print('error handling command target: {0}'.format(e), + file=sys.stderr) continue if len(cmdargs) and cmdargs[0] == 'tell': - print >> sys.stderr, \ - 'Can not use \'tell\' in interactive mode.' + print('Can not use \'tell\' in interactive mode.', + file=sys.stderr) continue valid_dict = validate_command(sigdict, cmdargs, verbose) if valid_dict: if parsed_args.output_format: valid_dict['format'] = parsed_args.output_format if verbose: - print >> sys.stderr, "Submitting command ", valid_dict + print("Submitting command ", valid_dict, file=sys.stderr) ret, outbuf, outs = json_command(cluster_handle, target=target, argdict=valid_dict) if ret: ret = abs(ret) - print >> sys.stderr, \ - 'Error: {0} {1}'.format(ret, errno.errorcode.get(ret, 'Unknown')) + print('Error: {0} {1}'.format(ret, errno.errorcode.get(ret, 'Unknown')), + file=sys.stderr) if outbuf: - print outbuf + print(outbuf) if outs: - print >> sys.stderr, 'Status:\n', outs + print('Status:\n', outs, file=sys.stderr) else: - print >> sys.stderr, "Invalid command" + print("Invalid command", file=sys.stderr) if verbose: - print >> sys.stderr, "Submitting command ", valid_dict + print("Submitting command ", valid_dict, file=sys.stderr) return json_command(cluster_handle, target=target, argdict=valid_dict, inbuf=inbuf) @@ -493,16 +498,14 @@ def complete(sigdict, args, target): matched = matchnum(args, sig, partial=True) if (matched > best_match_cnt): if complete_verbose: - print >> sys.stderr, \ - "better match: {0} > {1}: {2}:{3} ".format(matched, - best_match_cnt, cmdtag, concise_sig(sig)) + print("better match: {0} > {1}: {2}:{3} ".format(matched, + best_match_cnt, cmdtag, concise_sig(sig)), file=sys.stderr) best_match_cnt = matched bestcmds = [{cmdtag:cmd}] elif matched == best_match_cnt: if complete_verbose: - print >> sys.stderr, \ - "equal match: {0} > {1}: {2}:{3} ".format(matched, - best_match_cnt, cmdtag, concise_sig(sig)) + print("equal match: {0} > {1}: {2}:{3} ".format(matched, + best_match_cnt, cmdtag, concise_sig(sig)), file=sys.stderr) bestcmds.append({cmdtag:cmd}) # look through all matching sigs @@ -516,15 +519,15 @@ def complete(sigdict, args, target): fullindex = matchnum(args, sig, partial=False) - 1 partindex = matchnum(args, sig, partial=True) - 1 if complete_verbose: - print >> sys.stderr, '{}: f {} p {} len {}'.format(sig, fullindex, partindex, len(sig)) + print('{}: f {} p {} len {}'.format(sig, fullindex, partindex, len(sig)), file=sys.stderr) if fullindex == partindex and fullindex + 1 < len(sig): d = sig[fullindex + 1] else: d = sig[partindex] comps.append(str(d)) if complete_verbose: - print >> sys.stderr, '\n'.join(comps) - print '\n'.join(comps) + print('\n'.join(comps), file=sys.stderr) + print('\n'.join(comps)) return 0 @@ -533,7 +536,7 @@ def complete(sigdict, args, target): ### def ping_monitor(cluster_handle, name, timeout): if 'mon.' not in name: - print >> sys.stderr, '"ping" expects a monitor to ping; try "ping mon."' + print('"ping" expects a monitor to ping; try "ping mon."', file=sys.stderr) return 1 mon_id = name[len('mon.'):] @@ -541,10 +544,10 @@ def ping_monitor(cluster_handle, name, timeout): run_in_thread(cluster_handle.connect, timeout=timeout) for m in monids() : s = run_in_thread(cluster_handle.ping_monitor, m) - print "mon.{0}".format(m) + '\n' + s + print("mon.{0}".format(m) + '\n' + s) else : s = run_in_thread(cluster_handle.ping_monitor, mon_id) - print s + print(s) return 0 ### @@ -562,18 +565,18 @@ def main(): parser, parsed_args, childargs = parse_cmdargs() if parsed_args.version: - print 'ceph version {0} ({1})'.format(CEPH_GIT_NICE_VER, CEPH_GIT_VER) # noqa + print('ceph version {0} ({1})'.format(CEPH_GIT_NICE_VER, CEPH_GIT_VER)) # noqa return 0 global verbose verbose = parsed_args.verbose if verbose: - print >> sys.stderr, "parsed_args: {0}, childargs: {1}".format(parsed_args, childargs) + print("parsed_args: {0}, childargs: {1}".format(parsed_args, childargs), file=sys.stderr) if parsed_args.admin_socket_nope: - print >> sys.stderr, '--admin-socket is used by daemons; '\ - 'you probably mean --admin-daemon/daemon' + print('--admin-socket is used by daemons; '\ + 'you probably mean --admin-daemon/daemon', file=sys.stderr) return 1 # pass on --id, --name, --conf @@ -610,14 +613,13 @@ def main(): sockpath = ceph_conf(parsed_args, 'admin_socket', childargs[1]) except Exception as e: - print >> sys.stderr, \ - 'Can\'t get admin socket path: ' + str(e) + print('Can\'t get admin socket path: ' + str(e), file=sys.stderr) return errno.EINVAL # for both: childargs = childargs[2:] else: - print >> sys.stderr, '{0} requires at least {1} arguments'.format( - childargs[0], require_args) + print('{0} requires at least {1} arguments'.format(childargs[0], require_args), + file=sys.stderr) return errno.EINVAL if sockpath and daemon_perf: @@ -629,20 +631,21 @@ def main(): if interval < 0: raise ValueError except ValueError: - print >> sys.stderr, 'daemonperf: interval should be a positive number' + print('daemonperf: interval should be a positive number', file=sys.stderr) return errno.EINVAL if len(childargs) > 1: if not childargs[1].isdigit(): - print >> sys.stderr, 'daemonperf: count should be a positive integer' + print('daemonperf: count should be a positive integer', file=sys.stderr) return errno.EINVAL count = int(childargs[1]) DaemonWatcher(sockpath).run(interval, count) return 0 elif sockpath: try: - print admin_socket(sockpath, childargs, format) + print(admin_socket(sockpath, childargs, format)) except Exception as e: - print >> sys.stderr, 'admin_socket: {0}'.format(e) + print('admin_socket: {0}'.format(e)) + print('admin_socket: {0}'.format(e), file=sys.stderr) return errno.EINVAL return 0 @@ -671,8 +674,8 @@ def main(): injectargs = childargs[position:] childargs = childargs[:position] if verbose: - print >> sys.stderr, 'Separate childargs {0} from injectargs {1}'.\ - format(childargs, injectargs) + print('Separate childargs {0} from injectargs {1}'.format(childargs, injectargs), + file=sys.stderr) else: injectargs = None @@ -687,8 +690,7 @@ def main(): conffile=conffile) retargs = run_in_thread(cluster_handle.conf_parse_argv, childargs) except rados.Error as e: - print >> sys.stderr, 'Error initializing cluster client: {0}'.\ - format(repr(e)) + print('Error initializing cluster client: {0}'.format(repr(e)), file=sys.stderr) return 1 childargs = retargs @@ -706,7 +708,8 @@ def main(): if len(childargs) >= 2 and \ childargs[0] in ['mon', 'osd'] and \ childargs[1] == 'tell': - print >> sys.stderr, '"{0} tell" is deprecated; try "tell {0}. [options...]" instead (id can be "*") '.format(childargs[0]) + print('"{0} tell" is deprecated; try "tell {0}. [options...]" instead (id can be "*") '.format(childargs[0]), + file=sys.stderr) return 1 if childargs in [['mon'], ['osd']]: @@ -718,11 +721,11 @@ def main(): timeout = 5 hdr('Monitor commands:') - print '[Contacting monitor, timeout after %d seconds]' % timeout + print('[Contacting monitor, timeout after %d seconds]' % timeout) if childargs and childargs[0] == 'ping': if len(childargs) < 2: - print >> sys.stderr, '"ping" requires a monitor name as argument: "ping mon."' + print('"ping" requires a monitor name as argument: "ping mon."', file=sys.stderr) return 1 try: @@ -730,15 +733,13 @@ def main(): return ping_monitor(cluster_handle, childargs[1], timeout) run_in_thread(cluster_handle.connect, timeout=timeout) except KeyboardInterrupt: - print >> sys.stderr, 'Cluster connection aborted' + print('Cluster connection aborted', file=sys.stderr) return 1 except rados.PermissionDeniedError as e: - print >> sys.stderr, 'Error connecting to cluster: {0}'.\ - format(e.__class__.__name__) + print('Error connecting to cluster: {0}'.format(e.__class__.__name__), file=sys.stderr) return errno.EACCES except Exception as e: - print >> sys.stderr, 'Error connecting to cluster: {0}'.\ - format(e.__class__.__name__) + print('Error connecting to cluster: {0}'.format(e.__class__.__name__), file=sys.stderr) return 1 if parsed_args.help: @@ -757,7 +758,7 @@ def main(): # an awfully simple callback def watch_cb(arg, line, who, stamp_sec, stamp_nsec, seq, level, msg): - print line + print(line) sys.stdout.flush() # first do a ceph status @@ -769,9 +770,9 @@ def main(): if ret == 0: outbuf += outs if ret: - print >> sys.stderr, "status query failed: ", outs + print("status query failed: ", outs, file=sys.stderr) return ret - print outbuf + print(outbuf) # this instance keeps the watch connection alive, but is # otherwise unused @@ -791,7 +792,7 @@ def main(): with open(parsed_args.input_file, 'r') as f: inbuf = f.read() except Exception as e: - print >> sys.stderr, 'Can\'t open input file {0}: {1}'.format(parsed_args.input_file, e) + print('Can\'t open input file {0}: {1}'.format(parsed_args.input_file, e), file=sys.stderr) return 1 # prepare output file, if any @@ -799,9 +800,7 @@ def main(): try: outf = open(parsed_args.output_file, 'w') except Exception as e: - print >> sys.stderr, \ - 'Can\'t open output file {0}: {1}'.\ - format(parsed_args.output_file, e) + print('Can\'t open output file {0}: {1}'.format(parsed_args.output_file, e), file=sys.stderr) return 1 # -s behaves like a command (ceph status). @@ -811,8 +810,7 @@ def main(): try: target = find_cmd_target(childargs) except Exception as e: - print >> sys.stderr, \ - 'error handling command target: {0}'.format(e) + print('error handling command target: {0}'.format(e), file=sys.stderr) return 1 # Repulsive hack to handle tell: lop off 'tell' and target @@ -827,9 +825,9 @@ def main(): if injectargs: childargs = injectargs if not len(childargs): - print >> sys.stderr, \ - '"{0} tell" requires additional arguments.'.format(sys.argv[0]), \ - 'Try "{0} tell [options...]" instead.'.format(sys.argv[0]) + print('"{0} tell" requires additional arguments.'.format(sys.argv[0]), + 'Try "{0} tell [options...]" instead.'.format(sys.argv[0]), + file=sys.stderr) return errno.EINVAL # fetch JSON sigs from command @@ -860,7 +858,7 @@ def main(): if ret == -errno.EINVAL: # send command to old monitor or OSD if verbose: - print prefix + '{0} to old {1}'.format(' '.join(childargs), target[0]) + print(prefix + '{0} to old {1}'.format(' '.join(childargs), target[0])) compat = True if parsed_args.output_format: childargs.extend(['--format', parsed_args.output_format]) @@ -893,11 +891,12 @@ def main(): sigdict, inbuf, verbose) if ret < 0: ret = -ret - print >> sys.stderr, prefix + 'Second attempt of previously successful command failed with {0}: {1}'.format(errno.errorcode.get(ret, 'Unknown'), outs) + print(prefix + 'Second attempt of previously successful command failed with {0}: {1}'.format(errno.errorcode.get(ret, 'Unknown'), outs), + file=sys.stderr) if ret < 0: ret = -ret - print >> sys.stderr, prefix + 'Error {0}: {1}'.format(errno.errorcode.get(ret, 'Unknown'), outs) + print('Error {0}: {1}'.format(errno.errorcode.get(ret, 'Unknown'), outs), file=sys.stderr) if len(targets) > 1: final_ret = ret else: @@ -907,10 +906,10 @@ def main(): if compat: if ret == 0: # old cli/mon would send status string to stdout on non-error - print outs + print(outs) else: if outs: - print >> sys.stderr, prefix + outs + print(prefix + outs, file=sys.stderr) sys.stdout.flush()