-
Notifications
You must be signed in to change notification settings - Fork 9
/
mergeIPAMetaData.py
executable file
·82 lines (61 loc) · 2.33 KB
/
mergeIPAMetaData.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
#!/usr/bin/env python
from pprint import pprint
import json
import sys
import optparse
def key_for_bundle(bundle, primary="item_id"):
return ("%s#%s" % (bundle[primary], bundle.get("CFBundleVersion",""))).upper()
def print_stats(bundles):
all_schemes = []
all_app_ids = []
all_bundles = set()
for bundle in bundles:
all_app_ids.append(bundle["item_id"])
all_schemes.extend(bundle["url_schemes"])
all_bundles.add(key_for_bundle(bundle))
all_app_ids = set(all_app_ids)
all_schemes = set(all_schemes)
print "schemes: %d, apps: %d, bundles: %d" % (len(all_schemes), len(all_app_ids), len(all_bundles))
def loadMappings(filename):
print "loading %s" % filename
bundles = json.load(open(filename, 'r'))
print_stats(bundles)
return bundles
def mergeMappings(bundles_list):
print "merging %d bundle lists" % len(bundles_list)
items = {}
for bundles in bundles_list:
for bundle in bundles:
bundle_key = key_for_bundle(bundle)
if items.has_key(bundle_key):
existing = items.get(bundle_key)
def sanityCheck(key):
if existing.get(key, None) != bundle.get(key, None):
print "inconsistency: %s has different value for key %s" % (bundle_key, key)
for key in ["CFBundleIdentifier", "name", "url_schemes"]:
sanityCheck("")
else:
items[bundle_key] = bundle
merged = items.values()
merged.sort(key=lambda bundle:("%s#%s" % (bundle["CFBundleIdentifier"], bundle.get("CFBundleVersion",""))).upper())
print_stats(merged)
return merged
def get_options():
optp = optparse.OptionParser('usage: %prog [options] file1 file2 .. fileN"')
optp.add_option('-o', '--outputfile', action='store', dest='output_file', default=sys.stdout,
help='location to write the JSON to (default: stdout)')
args = optp.parse_args()
if len(args[1]) <= 0:
optp.print_help()
sys.exit(1)
return args
def main():
args = get_options()
bundles_list = [loadMappings(f) for f in args[1]]
merged = mergeMappings(bundles_list)
fo = args[0].output_file
if isinstance(fo, str):
fo = open(fo, 'w')
json.dump(merged, fo, indent=2, sort_keys=True)
if __name__ == '__main__':
main()