File size: 4,290 Bytes
15f5485 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | """Useful functions to compare inventory files.
This module provides functions to compare dictionaries or inventory files. It
can also be used as a script to compare inventory files via the command line.
Example:
$ python compare_tools.py file1 file2
"""
import json
import sys
import filecmp
import os
import logging
from pprint import pprint
def hash_dict(dictionary):
"""
Return the hashcode of the input dictionary. Useful for comparisons.
"""
return hash(frozenset(dictionary))
def compare_map(old_map, new_map):
"""Compare two maps and return two maps, where the first map is contains
all of the removed entries and the second contains all of the added
entries.
If no entries then empty maps will be returned or if the input maps' hashes
are equal.
"""
removed_entries = {}
new_entries = {}
#todo evauluate python sets
#https://docs.python.org/2/library/sets.html
#https://wiki.python.org/moin/TimeComplexity
# check data hash, if equal no changes
# frozenset is immutable, so can generate hash
if hash_dict(old_map) == hash_dict(new_map):
return removed_entries, new_entries
for entry in new_map.keys():
# check if in oldMap
if entry not in old_map:
new_entries[entry] = new_map[entry]
for entry in old_map.keys():
# check if in newMap
if entry not in new_map:
removed_entries[entry] = old_map[entry]
return removed_entries, new_entries
def inventory_file_to_dictionary(inventory_file):
"""Convert an inventory file to a map. The inventory file must be JSON
and have the following keys: name and id. An example is given as"
[
{"name": "Example Inventory Name", "id": 123456789},
...
]
"""
dictionary = {}
if not os.path.isfile(inventory_file):
logging.error(str(inventory_file) + ' not found!')
return dictionary
if os.stat(inventory_file).st_size == 0:
logging.error(str(inventory_file) + ' is empty!')
return dictionary
with open(inventory_file) as json_file:
data = json.load(json_file)
for line in data:
# todo check that the line contains ID and NAME
# if not then skip
dictionary[line['id']] = line['name'].encode("utf8")
return dictionary
def compare_inventory_files(inventory_file, new_file):
"""Compare two inventory files. Returns the removed and added entries
by comparison via the compare_map function. Empty maps are returned in
error cases (file not found, file is empty) for more graceful handling
upstream, rather than try / except."""
removed = {}
new = {}
if not os.path.isfile(inventory_file):
logging.error(str(inventory_file) + ' not found!')
return removed, new
if os.stat(inventory_file).st_size == 0:
logging.error(str(inventory_file) + ' is empty!')
return removed, new
if not os.path.isfile(new_file):
logging.error(str(new_file) + ' not found!')
return removed, new
if os.stat(new_file).st_size == 0:
logging.error(str(new_file) + ' is empty!')
return removed, new
hash_map1 = inventory_file_to_dictionary(inventory_file)
hash_map2 = inventory_file_to_dictionary(new_file)
removed, new = compare_map(hash_map1, hash_map2)
return removed, new
def dprint(removed, added):
"""Used for debug pretty printing two maps."""
logging.info('')
logging.info('')
logging.info('Added: ' + str(len(added)))
logging.info('')
logging.info(pprint(added))
logging.info(
'==================================================================')
logging.info('Removed: ' + str(len(removed)))
logging.info('')
logging.info(pprint(removed))
logging.info('')
logging.info('')
def main():
"""Compare two inventory files provided as command line arguments. The
output of the comparison is pretty printed."""
# compare two inventory files
if len(sys.argv) < 3:
print 'must input two file names'
return
removed, new = compare_inventory_files(sys.argv[1], sys.argv[2])
dprint(removed, new)
if __name__ == '__main__':
main()
|