-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.py
167 lines (149 loc) · 5.33 KB
/
generate.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import json
import os
import requests
import glob
import string
import csv
import operator
GET_BUGS = True
schema_locations = [
'./schemas/nightly/',
'./schemas/aurora/',
'./schemas/beta/',
'./schemas/release/',
]
schema_skip = [
'context_menus_internal.json',
]
usage_file = 'apiusage.csv'
versioned_schemas = {}
def bugs(whiteboard):
# print "search bugs for %s" % whiteboard
res = requests.get(
'https://bugzilla.mozilla.org/rest/bug',
params={
'product': 'Toolkit',
'component': ['WebExtensions',
'WebExtensions: Android', 'WebExtensions: Compatibility', 'WebExtensions: Untriaged',
'WebExtensions: Developer tools', 'WebExtensions: Experiments', 'WebExtensions: Frontend',
'WebExtensions: General', 'WebExtensions: Request Handling'],
'whiteboard': '[%s]' % whiteboard,
'summary': whiteboard,
'include_fields': 'summary,status,resolution,id',
'status': ['NEW', 'ASSIGNED', 'UNCONFIRMED', 'REOPENED']
}
)
# print res.request.url
return res.json()
def parse_usage():
res = {}
with open(usage_file) as csvfile:
reader = csv.DictReader(csvfile)
for k, row in enumerate(reader):
api = row["API"]
count = len(api.split("."))
if count < 3:
continue
if count > 3:
api = ".".join(api.split(".")[:3])
res[api] = k + 1
return res
def get_status(schema, type_, api):
if not schema:
return None
res = []
for key, value in schema.items():
rank = parsed_usage.get(value['usage'], None)
if not rank:
continue
if value['deprecated']:
status = "D"
else:
status = '' if value['supported'] else 'N'
res.append((status, rank, value['full'], value['deprecated']))
return res
def process_schemas(directories):
for directory in directories:
version = os.path.basename(directory[:-1])
for fname in glob.glob(directory + '*.json'):
if os.path.basename(fname) in schema_skip:
# print 'Skipping:', fname
continue
lines = open(fname, 'r').readlines()
# Strip out stupid comments.
newlines = []
for line in lines:
if not line.startswith('//'):
newlines.append(line)
process_json(version, json.loads('\n'.join(newlines)))
def process_json(version, data):
versioned_schemas.setdefault(version, {})
parsed_schema = versioned_schemas.get(version)
for element in data:
for k, v in element.items():
if k == 'namespace' and v != 'manifest':
parsed_schema['__current__'] = v
for element in data:
for k, v in element.items():
if k == 'functions':
for function in v:
process_type(parsed_schema, 'functions', function)
if k == 'events':
for event in v:
process_type(parsed_schema, 'events', event)
def process_type(parsed_schema, type_, data):
namespace = parsed_schema['__current__']
parsed_schema.setdefault(namespace, {})
parsed_schema[namespace].setdefault(type_, {})
full = 'chrome.%s.%s' % (namespace, data['name'])
mdn = full[:]
parsed_schema[namespace][type_][data['name']] = {
'usage': full,
'full': mdn,
'supported': not(data.get('unsupported')),
'deprecated': data.get('deprecated'),
'data': data
}
def print_usage():
print "N A B R Rank API"
apis = {}
for key, value in sorted(parsed_usage.items(), key=operator.itemgetter(0)):
apis[key.split('.')[1]]=1
v = {}
for api in sorted(apis):
print "\n======= chrome.%s" % (api,)
r = v[api] = {}
# scan oldest to newest to catch deprecation data
for version in ['release', 'beta', 'aurora', 'nightly']:
parsed_schema = versioned_schemas[version]
schemas = parsed_schema.get(api, {})
# print version, api
res = get_status(schemas.get('functions', []), 'functions', api) or \
get_status(schemas.get('events', []), 'events', api)
if not res:
continue
for a in res:
e = r.setdefault(a[2], {})
e.setdefault('release', 'N')
e.setdefault('beta', 'N')
e.setdefault('aurora', 'N')
e.setdefault('nightly', 'N')
e["full"] = a[2]
e["rank"] = a[1]
e["deprecated"] = a[3] or ''
e[version] = a[0]
for key, value in sorted(parsed_usage.items(), key=operator.itemgetter(0)):
d = r.get(key, None)
if d:
print "{nightly:1} {aurora:1} {beta:1} {release:1} {rank:4} {full} {deprecated}".format(**d)
elif key.startswith("chrome.%s" % (api,)):
print "N N N N %04s %s" % (value, key,)
pile_of_bugs = None
if GET_BUGS:
pile_of_bugs = bugs(api)
for bug in pile_of_bugs['bugs']:
print "Bug %s: %s" % (bug['id'], bug['summary'],)
if __name__=='__main__':
parsed_usage = parse_usage()
process_schemas(schema_locations)
print_usage()