forked from emanuelez/PySynergy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch-ccm-history.py
349 lines (291 loc) · 16.5 KB
/
fetch-ccm-history.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#!/usr/bin/env python
# encoding: utf-8
"""
fetch-ccm-history.py
Fetch ccm (Synergy, Continuus) history from a repository
Created by Emanuele Zattin and Aske Olsson on 2011-01-26.
Copyright (c) 2011 Nokia. All rights reserved.
"""
from datetime import datetime
import time
import cPickle
import os.path
import os
import sys
import SynergySession
import SynergySessions
import FileObject
import TaskObject
import SynergyObject
from SynergyUtils import ObjectHistory, TaskUtil, SynergyUtils, ObjectHistoryPool
from operator import itemgetter, attrgetter
class Timer():
def __init__(self):
self.time = []
def __enter__(self):
self.start = time.time()
def __exit__(self, *args):
end = time.time() - self.start
self.time.append(end)
if len(self.time) % 100 == 0:
print "time used processing last 100 objects:", sum(self.time[-100:])
if len(self.time) % 1000 == 0:
print "time used processing last 1000 objects:", sum(self.time[-1000:])
self.time = []
class CCMHistory(object):
"""Get History (objects and tasks) in a Synergy (ccm) database between baseline projects"""
def __init__(self, ccm, ccmpool, history, outputfile):
self.ccm = ccm
self.ccmpool = ccmpool
self.delim = self.ccm.delim()
self.history = history
self.history_created = []
self.tag = ""
self.outputfile = outputfile
self.timer = Timer()
def get_project_history(self, project):
# find latest top-level project
result = self.ccm.query("name='{0}' and create_time > time('%today_minus2months') and status='released'".format(project)).format("%objectname").format("%create_time").format('%version').format("%owner").format("%status").format("%task").run()
#objectname, delimiter, owner, status, create_time, task):
latest = datetime(1,1,1, tzinfo=None)
latestproject = []
for s in result:
time = datetime.strptime(s['create_time'], "%a %b %d %H:%M:%S %Y")
if time > latest:
latest = time
latestproject = SynergyObject.SynergyObject(s['objectname'], self.delim, s['owner'], s['status'], s['create_time'], s['task'])
self.tag = s['version']
print "Latest project:", latestproject.get_object_name(), "created:", latest
#find baseline of latestproject:
base = self.ccm.query("is_baseline_project_of('{0}')".format(latestproject.get_object_name())).format("%objectname").format("%create_time").format('%version').format("%owner").format("%status").format("%task").run()[0]
baseline_project = SynergyObject.SynergyObject(base['objectname'], self.delim, base['owner'], base['status'], base['create_time'], base['task'])
print "Baseline project:", baseline_project.get_object_name()
if self.tag not in self.history.keys():
self.history[self.tag] = {'objects': [], 'tasks': []}
self.history[self.tag]['next'] = None
while baseline_project:
print "Toplevel Project:", latestproject.get_object_name()
# do the history thing
self.create_history(latestproject.get_object_name(), baseline_project.get_object_name())
self.history[self.tag]['created'] = latestproject.get_created_time()
next = latestproject.get_version()
# Find next baseline project
latestproject = baseline_project
baseline = self.ccm.query("is_baseline_project_of('{0}')".format(latestproject.get_object_name())).format("%objectname").format("%create_time").format('%version').format("%owner").format("%status").format("%task").run()[0]
baseline_project = SynergyObject.SynergyObject(baseline['objectname'], self.delim, baseline['owner'], baseline['status'], baseline['create_time'], baseline['task'])
#Set previous project and name of current release:
self.history[self.tag]['previous'] = latestproject.get_version()
self.history[self.tag]['name'] = self.tag
#Store data
fname = self.outputfile + '_' + self.tag
self.persist_data(fname, self.history[self.tag])
self.history_created.append(fname)
# delete the _inc file if it exists
if os.path.isfile(fname + '_inc' + '.p'):
os.remove(fname + '_inc' + '.p')
#Print Info
print self.tag, "done processing, Info:"
print "Name ", self.tag
print "Previous <- ", self.history[self.tag]['previous']
print "Next -> ", self.history[self.tag]['next']
print "Number of: "
print " Tasks: ", str(len(self.history[self.tag]['tasks']))
print " Files: ", str(len(self.history[self.tag]['objects']))
print ""
#Drop entry from dict to save memory
#del self.history[self.tag]
#Finally set the new (old) tag for the release
self.tag = latestproject.get_version()
if self.tag not in self.history.keys():
self.history[self.tag] = {'objects': [], 'tasks': []}
self.history[self.tag]['next'] = next
print "baseline project version:", baseline_project.get_version()
self.history[self.tag]['previous'] = None
print "getting all objects for:", latestproject.get_version(), "..."
# Do the last project as a full project
self.find_project_diff(latestproject.get_object_name(), baseline_project, latestproject.get_object_name())
self.history[self.tag]['name'] = self.tag
self.history[self.tag]['created'] = latestproject.get_created_time()
#Print Info
self.history[self.tag]['previous'] = None
print self.tag, "done processing, Info:"
print "Name ", self.tag
print "Previous <- ", self.history[self.tag]['previous']
print "Next -> ", self.history[self.tag]['next']
print "Number of: "
print " Tasks: ", str(len(self.history[self.tag]['tasks']))
print " Files: ", str(len(self.history[self.tag]['objects']))
print ""
return self.history
def create_history(self, latestproject, baseline_project):
#clear changed objects and find all objects from this release
self.find_project_diff(latestproject, baseline_project, latestproject)
def find_project_diff(self, latestproject, baseline_project, toplevel_project):
# Find difference between latestproject and baseline_project
if baseline_project:
object_hist_pool = ObjectHistoryPool(self.ccmpool, toplevel_project, baseline_project)
objects_changed = self.ccm.query("recursive_is_member_of('{0}', 'none') and not recursive_is_member_of('{1}', 'none')".format(latestproject, baseline_project)).format("%objectname").format("%owner").format("%status").format("%create_time").format("%task").run()
else:
# root project, get ALL objects in release
object_hist_pool = ObjectHistoryPool(self.ccmpool, toplevel_project, toplevel_project)
objects_changed = self.ccm.query("recursive_is_member_of('{0}', 'none')".format(latestproject)).format("%objectname").format("%owner").format("%status").format("%create_time").format("%task").run()
print "DEBUG: instantiated an object_hist_pool, type is: " + str(type(object_hist_pool))
print "DEBUG: object_hist_pool[0]'s type is: " + str(type(object_hist_pool[0]))
# make the objects_changed dictionary into an array, to be able to walk it by index
objects_changed_indexable = []
for o in objects_changed:
objects_changed_indexable.append(o)
num_of_objects = len([o for o in objects_changed if ":project:" not in o])
print "objects to process for", latestproject, ": ", num_of_objects
objects = {}
persist = 1
if self.tag in self.history.keys():
if 'objects' in self.history[self.tag]:
#Add all existin objects
for o in self.history[self.tag]['objects']:
objects[o.get_object_name()] = o
else:
self.history[self.tag] = {'objects': [], 'tasks': []}
# Check history for all objects and add them to history
objects_changed_processing_progress_idx = 0
while True: # this is the objects_changed progress outer loop, which is broken when the last object in objects_changed has been processed in a processing pool
# build the processing pool
current_pool_fill_idx = 0
current_pool_ObjectArray = []
print "DEBUG: Populate a processing pool..."
while True:
tempobject = objects_changed_indexable[objects_changed_processing_progress_idx]
print "DEBUG: - start checking: tempobject['objectname'] = " + str(tempobject['objectname']) + " for pool inclusion"
if tempobject['objectname'] not in objects.keys():
print "DEBUG: -- tempobject['objectname'] = " + str(tempobject['objectname']) + " not already in objects.keys()"
if ':project:' not in tempobject['objectname']:
print "DEBUG: --- ':project:' not in tempobject['objectname'] = " + str(tempobject['objectname'])
print "DEBUG: objects_changed_indexable[objects_changed_processing_progress_idx] type = " + str(type(objects_changed_indexable[objects_changed_processing_progress_idx]))
current_pool_ObjectArray.append(objects_changed_indexable[objects_changed_processing_progress_idx])
print "DEBUG: current_pool_fill_idx = " + str(current_pool_fill_idx)
print "DEBUG: ---- current_pool_ObjectArray[" + str(current_pool_fill_idx) + "] = " + str(current_pool_ObjectArray[current_pool_fill_idx])
if (current_pool_fill_idx >= self.ccmpool.nr_sessions-1):
break
current_pool_fill_idx += 1
if (objects_changed_processing_progress_idx >= len(objects_changed_indexable)-1):
break
objects_changed_processing_progress_idx += 1
print "DEBUG: start processing pool"
ccm_session_idx = 0
print "DEBUG: type(object_hist_pool) = " + str(type(object_hist_pool))
print "DEBUG: type(ccm_session_idx) = " + str(type(ccm_session_idx))
for workobject in current_pool_ObjectArray:
print "DEBUG: type(workobject) = " + str(type(workobject))
# now we have a processing pool, of finite size, kick off the pool
print "DEBUG: + object_hist_pool[" + str(ccm_session_idx) + "].start_get_history("
object_hist_pool[ccm_session_idx].start_get_history(FileObject.FileObject(workobject['objectname'], self.delim, workobject['owner'], workobject['status'], workobject['create_time'], workobject['task']))
ccm_session_idx += 1
# join the processing pool
current_pool_ReturnObjectsArray = []
with self.timer:
ccm_session_idx = 0
for workobject in current_pool_ObjectArray:
print "DEBUG: Waiting for object_hist_pool[" + str(ccm_session_idx) + "]"
current_pool_ReturnObjectsArray.append(object_hist_pool[ccm_session_idx].join_get_history())
ccm_session_idx += 1
# update the objects not already in the objects array (in case the same object is effectively processed multiple times in the same pool)
for retObject in current_pool_ReturnObjectsArray:
print "retObject = " + str(retObject)
print "retObject type = " + str(type(retObject))
if (retObject not in objects.keys()):
objects.update(retObject)
# break out of the pool processing loop if all elemenets have been processed
if (objects_changed_processing_progress_idx >= len(objects_changed_indexable)-1):
break
persist += len(current_pool_ReturnObjectsArray)
if persist % 100 == 0:
self.history[self.tag]['objects'] = objects.values()
fname = self.outputfile + '_' + self.tag + '_inc'
self.persist_data(fname, self.history[self.tag])
num_of_objects -= len(current_pool_ObjectArray)
print "objects left:", num_of_objects
print "number of files:", str(len(objects.values()))
self.history[self.tag]['objects'] = objects.values()
# Create tasks from objects
self.find_tasks_from_objects(objects.values(), latestproject)
def find_tasks_from_objects(self, objects, project):
task_util = TaskUtil(self.ccm)
tasks = {}
not_used = []
if self.tag in self.history.keys():
if 'tasks' in self.history[self.tag]:
for t in self.history[self.tag]['tasks']:
print "loading old task:", t.get_display_name()
tasks[t.get_display_name()] = t
num_of_tasks = sum([len(o.get_tasks().split(',')) for o in objects])
print "Tasks with associated objects:", num_of_tasks
#Find all tasks from the objects found
for o in objects:
for task in o.get_tasks().split(','):
if task != "<void>":
if task not in tasks.keys():
if task not in not_used:
# create task object
print "Task:", task
if task_util.task_in_project(task, project):
result = self.ccm.query("name='{0}' and instance='{1}'".format('task' + task.split('#')[1], task.split('#')[0])).format("%objectname").format("%owner").format("%status").format("%create_time").format("%task_synopsis").format("%release").run()
t = result[0]
# Only use completed tasks!
if t['status'] == 'completed':
to = TaskObject.TaskObject(t['objectname'], self.delim, t['owner'], t['status'], t['create_time'], task)
to.set_synopsis(t['task_synopsis'])
to.set_release(t['release'])
print "adding", o.get_object_name(), "to", task
to.add_object(o.get_object_name())
tasks[task] = to
else:
not_used.append(task)
else:
if o.get_object_name() not in tasks[task].get_objects():
print "adding", o.get_object_name(), "to", task
tasks[task].add_object(o.get_object_name())
num_of_tasks -= 1
print "tasks left:", num_of_tasks
num_of_tasks = len(tasks.keys())
print "Tasks in release to process for info:", num_of_tasks
# Fill out all task info
for task in tasks.values():
if not task.get_attributes():
task_util.fill_task_info(task)
num_of_tasks -= 1
print "tasks left:", num_of_tasks
self.history[self.tag]['tasks'] = tasks.values()
fname = self.outputfile + '_' + self.tag + '_inc'
self.persist_data(fname, self.history[self.tag])
def persist_data(self, fname, data):
fname = fname + '.p'
print "saving..."
fh = open(fname, 'wb')
cPickle.dump(data, fh, cPickle.HIGHEST_PROTOCOL)
fh.close()
print "done..."
def main():
ccm_db = sys.argv[1]
project = sys.argv[2]
outputfile = sys.argv[3]
print "Starting Synergy session on", ccm_db, "..."
ccm = SynergySession.SynergySession(ccm_db)
ccmpool = SynergySessions.SynergySessions(database=ccm_db, nr_sessions=10)
print "session started"
delim = ccm.delim()
history = {}
fname = outputfile + '.p'
if os.path.isfile(fname):
print "Loading", fname, "..."
fh = open(fname, 'rb')
history = cPickle.load(fh)
fh.close()
print "history contains:", history.keys()
#print history
fetch_ccm = CCMHistory(ccm, ccmpool, history, outputfile)
history = fetch_ccm.get_project_history(project)
fh = open(outputfile + '.p', 'wb')
cPickle.dump(history, fh, cPickle.HIGHEST_PROTOCOL)
fh.close()
if __name__ == '__main__':
main()