FIX: Remove Hardcoded Offset in Compression
authorAnkit Pati <redacted>
Fri, 16 Feb 2018 04:47:22 +0000 (10:17 +0530)
committerAnkit Pati <redacted>
Fri, 16 Feb 2018 18:15:10 +0000 (23:45 +0530)
An offset of 7 was hardcoded in the function compress_file, presumably
to skip over the default target IP address of 0.0.0.0 in a hosts file.
However, this causes problems when the default is overridden using the
--ip or -i flag, causing visibly garbled output in the generated hosts
file.

Fix is to calculate the length of target IP at runtime.

updateHostsFile.py

index 81a6ef2efce97f0a40bfaf0e597f930d1216add4..0c5404e02ebb17d5515710df14343617df087f24 100644 (file)
@@ -667,6 +667,7 @@ def compress_file(input_file, target_ip, output_file):
     input_file.seek(0)  # reset file pointer
     write_data(output_file, '\n')
 
+    target_ip_len = len(target_ip)
     lines = [target_ip]
     lines_index = 0
     for line in input_file.readlines():
@@ -674,7 +675,8 @@ def compress_file(input_file, target_ip, output_file):
 
         if line.startswith(target_ip):
             if lines[lines_index].count(' ') < 9:
-                lines[lines_index] += ' ' + line[7:line.find('#')].strip()
+                lines[lines_index] += ' ' \
+                    + line[target_ip_len:line.find('#')].strip()
             else:
                 lines[lines_index] += '\n'
                 lines.append(line[:line.find('#')].strip())
git clone https://git.99rst.org/PROJECT