forked from dalbothek/Hamburglar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hamburglar.py
executable file
·139 lines (117 loc) · 3.56 KB
/
hamburglar.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env python
# -*- coding: utf8 -*-
"""
This program is free software. It comes without any warranty, to
the extent permitted by applicable law. You can redistribute it
and/or modify it under the terms of the Do What The Fuck You Want
To Public License, Version 2, as published by Sam Hocevar. See
http://sam.zoy.org/wtfpl/COPYING for more details.
"""
import os
import sys
import getopt
import json
from collections import deque
def usage():
print "Example usage:",
print "Burger/munch.py -c 1.5.jar 1.6.jar | Hamburglar/hamburglar.py"
def import_toppings():
"""
Attempts to load all available toppings.
"""
this_dir = os.path.dirname(__file__)
toppings_dir = os.path.join(this_dir, "hamburglar", "toppings")
from_list = ["topping"]
# Traverse the toppings directory and import everything.
for root, dirs, files in os.walk(toppings_dir):
for file_ in files:
if not file_.endswith(".py"):
continue
elif file_.startswith("__"):
continue
from_list.append(file_[:-3])
imports = __import__("hamburglar.toppings", fromlist=from_list)
toppings = imports.topping.Topping.__subclasses__()
subclasses = toppings
while len(subclasses) > 0:
newclasses = []
for subclass in subclasses:
newclasses += subclass.__subclasses__()
subclasses = newclasses
toppings += subclasses
return toppings
if __name__ == '__main__':
try:
opts, args = getopt.gnu_getopt(
sys.argv[1:],
"o:c",
[
"output=",
"compact"
]
)
except getopt.GetoptError, err:
print str(err)
sys.exit(1)
# Default options
output = sys.stdout
compact = False
for o, a in opts:
if o in ("-o", "--output"):
output = open(a, "ab")
elif o in ("-c", "--compact"):
compact = True
toppings = import_toppings()
if len(args) > 0:
# Load JSON objects from files
versions = []
for path in args:
try:
versions += json.load(open(path, "r"))
except:
print "Error: Can't load ", path
sys.exit(3)
else:
# Load JSON objects from stdin
if sys.stdin.isatty():
print "Error: The Hamburglar needs to be fed burgers\n"
usage()
sys.exit(3)
try:
versions = json.load(sys.stdin)
except ValueError, err:
print "Error: Invalid input (" + str(err) + ")\n"
usage()
sys.exit(5)
if len(versions) < 2:
print "Error: The Hamburglar needs more burgers\n"
usage()
sys.exit(2)
# Compare versions
aggregate = {}
for topping in toppings:
if topping.KEY == None:
continue
keys = topping.KEY.split(".")
obj1 = versions[0]
obj2 = versions[1]
target = aggregate
skip = False
for key in keys:
if not (key in obj1 and key in obj2):
skip = True
break
obj1 = obj1[key]
obj2 = obj2[key]
if skip:
continue
for key in keys[:-1]:
if not key in target:
target[key] = {}
target = target[key]
target[keys[-1]] = topping().filter(obj1, obj2)
# Output results
if not compact:
json.dump(aggregate, output, sort_keys=True, indent=4)
else:
json.dump(aggregate, output)