From 219ad8f56be3efa013946e9330d49fb2542bf47f Mon Sep 17 00:00:00 2001 From: Jarrod Johnson Date: Fri, 22 Oct 2021 16:07:20 -0400 Subject: [PATCH] Fixes for confluent2hosts python2 compatibility and various scenarios not handled. --- confluent_client/bin/confluent2hosts | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/confluent_client/bin/confluent2hosts b/confluent_client/bin/confluent2hosts index 39c18b2c..93b432af 100644 --- a/confluent_client/bin/confluent2hosts +++ b/confluent_client/bin/confluent2hosts @@ -23,7 +23,7 @@ def partitionhostsline(line): line = line[:cmdidx].strip() if not line.strip(): return '', [], comment - ipaddr, names = line.split(maxsplit=1) + ipaddr, names = line.split(None, 1) names = names.split() return ipaddr, names, comment @@ -66,9 +66,11 @@ class HostMerger(object): def read_target(self, targetfile): + if not os.path.exists(targetfile): + return with open(targetfile, 'r') as hfile: lines = hfile.read().split('\n') - while not lines[-1]: + while lines and not lines[-1]: lines = lines[:-1] for y in range(len(lines)): line = lines[y] @@ -96,9 +98,9 @@ class HostMerger(object): self.targlines.append(line) def write_out(self, targetfile): - while not self.targlines[-1]: + while self.targlines and not self.targlines[-1]: self.targlines = self.targlines[:-1] - while not self.sourcelines[-1]: + while self.sourcelines and not self.sourcelines[-1]: self.sourcelines = self.sourcelines[:-1] if not self.sourcelines: break @@ -135,8 +137,9 @@ def main(): merger = HostMerger() for node in ipbynode: merger.add_entry(ipbynode[node], namesbynode[node]) - merger.read_target('/etc/hosts') - os.rename('/etc/hosts', '/etc/hosts.confluentbkup') + if os.path.exists('/etc/hosts'): + merger.read_target('/etc/hosts') + os.rename('/etc/hosts', '/etc/hosts.confluentbkup') merger.write_out('/etc/hosts') @@ -154,4 +157,4 @@ def expand_expression(c, namesbynode, expurl, expression): if __name__ == '__main__': - main() \ No newline at end of file + main()