Skip to content

Instantly share code, notes, and snippets.

@tkhduracell
Created June 14, 2014 21:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tkhduracell/85fa7f1f7f5597a9d7cd to your computer and use it in GitHub Desktop.
Save tkhduracell/85fa7f1f7f5597a9d7cd to your computer and use it in GitHub Desktop.
Compare two php.ini files using Python
#!/usr/bin/python
import re
import argparse
def main():
parser = argparse.ArgumentParser(description='Compare two php.ini files')
parser.add_argument('file1', metavar='first_php_ini', type=str, help='First file to compare')
parser.add_argument('file2', metavar='second_php_ini', type=str, help='Second file to compare')
args = parser.parse_args()
print(" ========================================================= ")
print(" Comparing PHP ini files ")
print(" ========================================================= ")
print(" ")
file1_path = args.file1
file1_map = dict()
filter_regex = re.compile(r"\[.*\]|(;.*)")
with open(file1_path) as f:
print("Reading file "+file1_path)
for line in f:
if not line.isspace() and not filter_regex.match(line):
splitted = line.strip().split("=",1)
key = splitted[0]
value = splitted[1]
if key in file1_map:
if file1_map[key].__class__.__name__ == [].__class__.__name__:
file1_map[key].append(value)
else:
file1_map[key] = [file1_map[key], value]
else:
file1_map[key] = value
file2_path = args.file2
file2_map = dict()
filter_regex = re.compile(r"\[.*\]|(;.*)")
with open(file2_path) as f:
print("Reading file "+file2_path)
for line in f:
if not line.isspace() and not filter_regex.match(line):
splitted = line.strip().split("=",1)
key = splitted[0]
value = splitted[1]
if key in file2_map:
if file2_map[key].__class__.__name__ == [].__class__.__name__:
file2_map[key].append(value)
else:
file2_map[key] = [file2_map[key], value]
else:
file2_map[key] = value
file1_keys = set(file1_map.keys())
file2_keys = set(file2_map.keys())
print(" ")
print(" ========================================================= ")
print(" Common keys ")
print(" ========================================================= ")
print(" ")
for key in (file1_keys & file2_keys):
if file1_map[key] != file2_map[key]:
print(" {}".format(key))
print(" {}".format(file1_map[key]))
print(" {}".format(file2_map[key]))
print(" ")
print(" ========================================================= ")
print(" Keys unique to "+file1_path)
print(" ========================================================= ")
print(" ")
for key in (file1_keys - file2_keys):
print(" {} = {}".format(key,str(file1_map[key])))
print(" ")
print(" ========================================================= ")
print(" Keys unique to "+file2_path)
print(" ========================================================= ")
print(" ")
for key in (file2_keys - file1_keys):
print(" {} = {}".format(key,str(file2_map[key])))
print(" ")
print(" ========================================================= ")
print(" Summary ")
print(" ========================================================= ")
print(" ")
print(" Commons keys: "+str(len(file1_keys & file2_keys)))
print(" Extra in file1: "+str(len(file1_keys - file2_keys)))
print(" Extra in file2: "+str(len(file2_keys - file1_keys)))
print(" ")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment