-
Notifications
You must be signed in to change notification settings - Fork 0
/
launchpadCrawler.py
269 lines (203 loc) · 10.3 KB
/
launchpadCrawler.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
__author__ = 'Yijun Pan'
from launchpadlib.launchpad import Launchpad
from xmlWriter import xmlWriter
from csvWriter import csvWriter
import os
import sys
import dataStruct
# TODO: Make launchpadCrawler faster with multiprocessing
# TODO: Add a routine to check reviews
class LaunchpadCrawler:
# Directory to store files from Launchpad
__cachedir = ""
__launchpad = None
def __set_cache_dir(self, cachedir):
self.__cachedir = cachedir
if not os.path.isdir(self.__cachedir):
os.makedirs(self.__cachedir)
def set_authorization(self, application=None, server=None):
if application is not None and server is not None:
self.launchpad = Launchpad.login_with(application, server, self.__cachedir,
credential_save_failed=self.__no_credential, version='devel')
else:
self.launchpad = Launchpad.login_anonymously('just testing', 'production', self.__cachedir, version='devel')
@staticmethod
def __no_credential(self):
print "Can't proceed without Launchpad credential."
sys.exit()
def __init__(self):
self.__set_cache_dir("cache")
self.set_authorization()
def __init__(self, application=None, server=None, cachedir="cache"):
self.__set_cache_dir(cachedir)
self.set_authorization(application, server)
# Get the bug info
def crawl_project_bugs(self, project_names=[],
attributes=['date_created', 'date_assigned', 'date_closed',
'web_link',
'bug_target_name',
'status', 'is_complete', 'importance'
'owner', 'assignee'],
output_type='print',
output_file="output_bugs.xml",
bug_amount=100,
write_through=False,
trunk_size=1000,
pretty_print=False):
for name in project_names:
project = self.launchpad.projects(name)
bugs = project.searchTasks(status=['New', 'Incomplete', 'Triaged', 'Opinion', 'Expired',
'Invalid', 'Won\'t Fix', 'Confirmed', 'In Progress',
'Fix Committed', 'Fix Released'])
bugs_data = []
print str(bugs.total_size) + " bugs found."
entry_count = 0
file_count = 0
for idx, bug in enumerate(bugs):
if bug_amount > 0:
if idx >= bug_amount:
break
entry_count += 1
bug_data = Bug(bug.title)
for attribute in attributes:
# Creator of the bug event
if attribute == 'owner':
# We should always have an owner
owner = bug.owner
setattr(bug_data, 'owner_name', owner.display_name)
setattr(bug_data, 'owner_link', owner.web_link)
elif attribute == "assignee":
# Sometimes we don't have an assignee
assignee = bug.assignee
if not assignee:
setattr(bug_data, 'assignee_name', None)
setattr(bug_data, 'assignee_link', None)
else:
setattr(bug_data, 'assignee_name', assignee.display_name)
setattr(bug_data, 'assignee_link', assignee.web_link)
else:
value = getattr(bug, attribute)
setattr(bug_data, attribute, value)
bugs_data.append(bug_data)
# Save large size data into multiple files if necessary
if entry_count == trunk_size and write_through:
entry_count = 0
file_count += 1
out_file_name, out_file_ext = os.path.splitext(output_file)
out_file = out_file_name + '_' + str(file_count) + out_file_ext
if output_type == "print":
for bug_data in bugs_data:
print bug_data.__dict__
elif output_type == "xml":
writer = xmlWriter(bugs_data, "bugs", "All bugs belong to project OpenStack")
writer.write_to_file(out_file, pretty=pretty_print)
elif output_type == "csv":
writer = csvWriter(bugs_data)
writer.write_to_file(out_file, delimiter=',')
else:
print "Unsupported output format"
sys.exit(-1)
bugs_data = []
if not bug_data:
return
# write or print out bugs
if output_type == "print":
for bug_data in bugs_data:
print bug_data.__dict__
elif output_type == "xml":
writer = xmlWriter(bugs_data, "bugs", "All bugs belong to project OpenStack")
writer.write_to_file(output_file, pretty=pretty_print)
elif output_type == "csv":
writer = csvWriter(bugs_data)
writer.write_to_file(output_file, delimiter=',')
else:
print "Unsupported output format."
# Get blueprint of projects
def crawl_project_blueprints(self, project_names=[],
attributes=['date_created', 'date_started', 'date_completed',
'web_link',
'title',
'dependencies',
'implementation_status', 'priority',
'owner', 'assignee'],
output_type='print',
output_file="output_blueprints.xml",
amount=100,
write_through=False,
trunk_size=10,
pretty_print=False):
for name in project_names:
project = self.launchpad.projects(name)
blueprints = project.all_specifications
blueprints_data = []
print str(blueprints.total_size) + " blueprints found."
entry_count = 0
trunk_count = 0
for idx, blueprint in enumerate(blueprints):
if amount > 0:
if idx >= amount:
break
entry_count += 1
blueprint_data = Blueprint(blueprint.name+"("+blueprint.target.name+")")
for attribute in attributes:
# Creator of the bug event
if attribute == 'owner':
# We should always have an owner
owner = blueprint.owner
setattr(blueprint_data, 'owner_name', owner.display_name)
setattr(blueprint_data, 'owner_link', owner.web_link)
elif attribute == "assignee":
# Sometimes we don't have an assignee
assignee = blueprint.assignee
if not assignee:
setattr(blueprint_data, 'assignee_name', None)
setattr(blueprint_data, 'assignee_link', None)
else:
setattr(blueprint_data, 'assignee_name', assignee.display_name)
setattr(blueprint_data, 'assignee_link', assignee.web_link)
elif attribute == "dependencies":
dependencies = blueprint.dependencies
if not dependencies:
setattr(blueprint_data, 'dependencies', None)
else:
dep_names = []
for dep in dependencies:
dep_names.append(dep.name+"("+dep.target.name+")")
setattr(blueprint_data, 'dependencies', dep_names)
else:
value = getattr(blueprint, attribute)
setattr(blueprint_data, attribute, value)
blueprints_data.append(blueprint_data)
# Save large size data into multiple files if necessary
if entry_count == trunk_size and write_through:
entry_count = 0
trunk_count += 1
out_file_name, out_file_ext = os.path.splitext(output_file)
out_file = out_file_name + '_' + str(trunk_count) + out_file_ext
if output_type == "print":
for bug_data in blueprints_data:
print bug_data.__dict__
elif output_type == "xml":
writer = xmlWriter(blueprints_data, "bugs", "All bugs belong to project OpenStack")
writer.write_to_file(out_file, pretty=pretty_print)
elif output_type == "csv":
writer = csvWriter(blueprints_data)
writer.write_to_file(out_file, delimiter=',')
else:
print "Unsupported output format"
sys.exit(-1)
blueprints_data = []
if not blueprints_data:
return
# write or print out bugs
if output_type == "print":
for blueprint_data in blueprints_data:
print bug_data.__dict__
elif output_type == "xml":
writer = xmlWriter(blueprints_data, "blueprints", "All blueprints belong to project OpenStack")
writer.write_to_file(output_file, pretty=pretty_print)
elif output_type == "csv":
writer = csvWriter(blueprints_data)
writer.write_to_file(output_file, delimiter=',')
else:
print "Unsupported output format."