-
Notifications
You must be signed in to change notification settings - Fork 1
/
validate-changes.py
119 lines (98 loc) · 4.17 KB
/
validate-changes.py
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
# This script compares ../json-ld/locations.json (or other named files) in
# the current branch to that in `master`
#
# Depends on Python 3
#
# Install dependencies:
# pip install -r requirements.txt
#
# Usage:
# git checkout [feature-branch]
# python validate-changes.py [which]
#
# Params:
# - [which]: Name of json-ld file to compare
# (e.g. 'recapCustomerCodes', 'organizations').
# Default 'locations'
from deepdiff import DeepDiff
from git import Git
import json
import sys
import re
# Provides a comparison on the serialized named JSON-LD object in the
# current working branch
def main():
# Determine what json-ld file to compare
which = 'locations'
if len(sys.argv) >= 2:
which = sys.argv[1]
branchToCompare = 'master'
if len(sys.argv) == 3:
branchToCompare = sys.argv[2]
# Initialize git client and store current branch name
git = Git()
currentBranch = None
for branch in git.branch().split('\n'):
if branch[0] == '*':
currentBranch = branch[2:]
break
print(f'Comparing {which} in {currentBranch} to {branchToCompare}')
# Get current working copy of the JSON-LD file
newFile = openJsonFile(which)
try:
# Checkout master and get the current version
git.checkout(branchToCompare)
masterFile = openJsonFile(which)
# Compare the two objects
for item in newFile['@graph']:
if not 'skos:notation' in item:
print('Item found without a skos:notation!', item)
newLocDict = {item['@id']: item for item in newFile['@graph']}
masterLocDict = {item['@id']: item for item in masterFile['@graph']}
newKeys = newLocDict.keys() - masterLocDict.keys()
deletedKeys = masterLocDict.keys() - newLocDict.keys()
alteredKeys = list(filter(lambda x: x[1], [
(key, DeepDiff(masterLocDict[key], newLocDict[key], ignore_order=True))
for key in set(newLocDict.keys()) & set(masterLocDict.keys())
]))
# Output comparison results
print('Keys Added: {}: {}'.format(len(newKeys), newKeys))
print('Keys Deleted: {}'.format(len(deletedKeys)))
print('Keys Altered: {}'.format(len(alteredKeys)))
displayAlterations(alteredKeys, masterLocDict) # Provide details on altered mapping objects
except Exception as e:
template = "An exception of type {0} occurred. Arguments:\n{1!r}"
message = template.format(type(e).__name__, e.args)
print (message)
# Reset to current working branch
git.checkout(currentBranch)
def openJsonFile(which):
with open(f'./../json-ld/{which}.json') as locFile:
return json.load(locFile)
def displayAlterations(alteredKeys, masterLocDict):
for key, diff in alteredKeys:
print('\nALTERED KEY: {}'.format(key))
for change in ['type_changes', 'values_changed']:
if change in diff.keys():
for label, changes in diff[change].items():
print(' Attribute: {}'.format(label))
print(' Old Value: {}'.format(changes['old_value']))
print(' New Value: {}'.format(changes['new_value']))
if 'iterable_item_removed' in diff.keys():
for label, removal in diff['iterable_item_removed'].items():
print(' Attribute(Pos): {}'.format(label))
print(' Removed Value: {}'.format(removal))
if 'iterable_item_added' in diff.keys():
for label, addition in diff['iterable_item_added'].items():
print(' Attribute(Pos): {}'.format(label))
print(' Added Value: {}'.format(addition))
if 'dictionary_item_added' in diff.keys():
for addition in diff['dictionary_item_added']:
print(' Added Item: {}'.format(addition))
if 'dictionary_item_removed' in diff.keys():
for prop in diff['dictionary_item_removed']:
prop = re.sub(r'(^root\[\'|\'\]$)', '', prop)
old_value = masterLocDict[key][prop]
print(' Removed Item: {} (Old value: "{}")'.format(prop, old_value))
if __name__ == '__main__':
main()