From 74f18d55714b93b4dd8fdd0f180ee7a92ce89105 Mon Sep 17 00:00:00 2001 From: Jarrod Johnson Date: Fri, 4 Oct 2019 10:37:48 -0400 Subject: [PATCH] Client side py3 changes --- confluent_client/bin/confetty | 24 ++++++++++++++---------- confluent_client/bin/nodersync | 10 ++++++---- confluent_client/bin/noderun | 4 +++- confluent_client/bin/nodesensors | 13 ++++++++++--- confluent_client/bin/nodeshell | 4 +++- confluent_client/confluent/client.py | 9 +++++++++ 6 files changed, 45 insertions(+), 19 deletions(-) diff --git a/confluent_client/bin/confetty b/confluent_client/bin/confetty index 522fbb22..d8f88d7a 100755 --- a/confluent_client/bin/confetty +++ b/confluent_client/bin/confetty @@ -89,6 +89,10 @@ except NameError: netserver = None laststate = {} +try: + input = raw_input +except NameError: + pass class BailOut(Exception): def __init__(self, errorcode=0): @@ -195,9 +199,9 @@ def prompt(): if os.environ.get('TERM', '') not in ('linux'): sys.stdout.write('\x1b]0;confetty: %s\x07' % target) try: - return raw_input(target + ' -> ') + return input(target + ' -> ') except KeyboardInterrupt: - print "" + print("") return "" except EOFError: # ctrl-d print("exit") @@ -296,7 +300,7 @@ currchildren = None def print_result(res): if 'errorcode' in res or 'error' in res: - print res['error'] + print(res['error']) return if 'databynode' in res: print_result(res['databynode']) @@ -309,9 +313,9 @@ def print_result(res): attrstr = '%s=%s' % (key, recurse_format(res[key])) elif not isinstance(res[key], dict): try: - print '{0}: {1}'.format(key, res[key]) + print('{0}: {1}'.format(key, res[key])) except UnicodeEncodeError: - print '{0}: {1}'.format(key, repr(res[key])) + print('{0}: {1}'.format(key, repr(res[key]))) continue elif 'value' in res[key] and res[key]['value'] is not None: attrstr = '%s="%s"' % (key, res[key]['value']) @@ -324,7 +328,7 @@ def print_result(res): else: sys.stdout.write('{0}: '.format(key)) if isinstance(res[key], str) or isinstance(res[key], unicode): - print res[key] + print(res[key]) else: print_result(res[key]) continue @@ -423,10 +427,10 @@ def do_command(command, server): for res in session.read(targpath): if 'item' in res: # a link relation if type(res['item']) == dict: - print res['item']["href"] + print(res['item']["href"]) else: for item in res['item']: - print item["href"] + print(item["href"]) else: # generic attributes to list if 'error' in res: sys.stderr.write(res['error'] + '\n') @@ -851,7 +855,7 @@ def server_connect(): passphrase = os.environ['CONFLUENT_PASSPHRASE'] session.authenticate(username, passphrase) while not session.authenticated: - username = raw_input("Name: ") + username = input("Name: ") passphrase = getpass.getpass("Passphrase: ") session.authenticate(username, passphrase) @@ -871,7 +875,7 @@ def main(): global inconsole try: server_connect() - except EOFError, KeyboardInterrupt: + except (EOFError, KeyboardInterrupt) as _: raise BailOut(0) except socket.gaierror: sys.stderr.write('Could not connect to confluent\n') diff --git a/confluent_client/bin/nodersync b/confluent_client/bin/nodersync index 2d6e91cf..42179cf8 100755 --- a/confluent_client/bin/nodersync +++ b/confluent_client/bin/nodersync @@ -55,7 +55,7 @@ def run(): noderange, targpath = args[-1].split(':', 1) client.check_globbing(noderange) c = client.Command() - cmdstr = " ".join(args[:-1]) + cmdstr = ' '.join(args[:-1]) cmdstr = 'rsync -av --info=progress2 ' + cmdstr cmdstr += ' {node}:' + targpath @@ -66,13 +66,15 @@ def run(): exitcode = 0 for exp in c.create('/noderange/{0}/attributes/expression'.format(noderange), - {'expression': cmdstr}): + {'expression': cmdstr}): if 'error' in exp: sys.stderr.write(exp['error'] + '\n') exitcode |= exp.get('errorcode', 1) ex = exp.get('databynode', ()) for node in ex: - cmd = ex[node]['value'].encode('utf-8') + cmd = ex[node]['value'] + if not isinstance(cmd, bytes) and not isinstance(cmd, str): + cmd = cmd.encode('utf-8') cmdv = shlex.split(cmd) if currprocs < concurrentprocs: currprocs += 1 @@ -117,7 +119,7 @@ def run(): output.set_output(node, 'error!') if node not in nodeerrs: nodeerrs[node] = '' - nodeerrs[node] += data + nodeerrs[node] += client.stringify(data) else: pop = desc['popen'] ret = pop.poll() diff --git a/confluent_client/bin/noderun b/confluent_client/bin/noderun index b199135b..10ee3975 100755 --- a/confluent_client/bin/noderun +++ b/confluent_client/bin/noderun @@ -71,7 +71,9 @@ def run(): exitcode |= exp.get('errorcode', 1) ex = exp.get('databynode', ()) for node in ex: - cmd = ex[node]['value'].encode('utf-8') + cmd = ex[node]['value'] + if not isinstance(cmd, bytes) and not isinstance(cmd, str): + cmd = cmd.encode('utf-8') cmdv = shlex.split(cmd) if currprocs < concurrentprocs: currprocs += 1 diff --git a/confluent_client/bin/nodesensors b/confluent_client/bin/nodesensors index 5e5b1746..f9c544c6 100755 --- a/confluent_client/bin/nodesensors +++ b/confluent_client/bin/nodesensors @@ -149,8 +149,11 @@ def sensorpass(showout=True, appendtime=False): if appendtime: showval += ' @' + time.strftime( '%Y-%m-%dT%H:%M:%S') - print(u'{0}: {1}:{2}'.format( - node, sensedata['name'], showval).encode('utf8')) + printval = u'{0}: {1}:{2}'.format( + node, sensedata['name'], showval) + if not isinstance(printval, str): + printval = printval.encode('utf-8') + print(printval) sys.stdout.flush() return resultdata @@ -199,7 +202,11 @@ def main(): orderedsensors.append(name) orderedsensors.sort() for name in orderedsensors: - headernames.append(sensorheaders[name].encode('utf-8')) + headername = sensorheaders[name] + if (not isinstance(headername, str) and + not isinstance(headername, bytes)): + headername = headername.encode('utf-8') + headernames.append(headername) if options.csv: linebyline = False csvwriter = csv.writer(sys.stdout) diff --git a/confluent_client/bin/nodeshell b/confluent_client/bin/nodeshell index 27ca5ad8..d0619f70 100755 --- a/confluent_client/bin/nodeshell +++ b/confluent_client/bin/nodeshell @@ -72,7 +72,9 @@ def run(): exitcode |= exp.get('errorcode', 1) ex = exp.get('databynode', ()) for node in ex: - cmd = ex[node]['value'].encode('utf-8') + cmd = ex[node]['value'] + if not isinstance(str) and not isinstance(bytes): + cmd = cmd.encode('utf-8') cmdv = ['ssh', node, cmd] if currprocs < concurrentprocs: currprocs += 1 diff --git a/confluent_client/confluent/client.py b/confluent_client/confluent/client.py index 15524b4a..c70662ec 100644 --- a/confluent_client/confluent/client.py +++ b/confluent_client/confluent/client.py @@ -40,6 +40,15 @@ _attraliases = { } +def stringify(instr): + # Normalize unicode and bytes to 'str', correcting for + # current python version + if isinstance(instr, bytes) and not isinstance(instr, str): + return instr.decode('utf-8') + elif not isinstance(instr, bytes) and not isinstance(instr, str): + return instr.encode('utf-8') + return instr + class Tabulator(object): def __init__(self, headers): self.headers = headers