Fix hosts converter issues with inline comment

This commit is contained in:
Minoplhy 2021-07-13 15:33:10 +07:00
parent 793b4fb24b
commit 9134267e44

View File

@ -1,12 +1,12 @@
import os
import sys
from re import sub
import re
from shutil import copyfile
infile = sys.argv[1]
outfile = sys.argv[2]
a = ['0.0.0.0','::1','127.0.0.1','0','::']
a = ['0.0.0.0 ','::1 ','127.0.0.1 ','0 ',':: ']
lst = []
with open(infile, 'r') as f:
@ -26,7 +26,7 @@ f.close()
with open(infile) as f:
file = f.read().split('\n')
for i in range(len(file)):
file[i] = sub(r'#', ';', file[i])
file[i] = re.sub(r'#', ';', file[i])
#print(file)
with open(infile, 'w') as f1:
f1.writelines(["%s\n" % item for item in file])
@ -36,14 +36,16 @@ with open(infile, 'r') as f: # load file
lines = f.read().splitlines() # read lines
with open(infile, 'w') as f: # load file in write mode
for line in lines:
if line.startswith(';'):
f.write('\n'.join([line + '\n']))
elif not line.strip():
f.write('\n'.join([line + '\n']))
elif line.startswith('-'):
f.write('\n'.join([line + '\n']))
elif not line.startswith(';') and not line.startswith('-') and line.strip():
f.write('\n'.join([line + ' CNAME .\n'])) # add CNAME . if file does not start with ;
if line.startswith(';'):
f.write('\n'.join([line + '\n']))
elif not line.strip():
f.write('\n'.join([line + '\n']))
elif line.startswith('-'):
f.write('\n'.join([line + '\n']))
elif not line.startswith(';') and not line.startswith('-') and line.strip() and not ';' in line:
f.write('\n'.join([line + ' CNAME .\n'])) # add CNAME . if file does not start with ;
elif ';' in line and not line.startswith(';'):
f.write('\n'.join([line + '\n']))
f.close()
remove_words = ['localhost','localhost.localdomain','local','broadcasthost','loopback','ip6-localnet','ip6-mcastprefix','ip6-allnodes','ip6-allrouters','ip6-allhosts','ip6-loopback']
@ -55,6 +57,24 @@ with open(infile, 'w') as f:
if not any(remove_word in line for remove_word in remove_words):
f.write('\n'.join([line + '\n']))
with open(infile) as f:
file = f.read().split('\n')
for i in range(len(file)):
file[i] = re.sub('\s\s+', ' ', file[i])
#print(file)
with open(infile, 'w') as f1:
f1.writelines(["%s\n" % item for item in file])
f.close()
with open(infile) as f:
file = f.read().split('\n')
for i in range(len(file)):
file[i] = re.sub('\s;', ' CNAME . ;', file[i])
#print(file)
with open(infile, 'w') as f1:
f1.writelines(["%s\n" % item for item in file])
f.close()
copyfile(infile, outfile) # copy input file to output file
os.remove(infile) # remove input file