forked from emanuelez/PySynergy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ccm_fast_export.py
316 lines (272 loc) · 11.7 KB
/
ccm_fast_export.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
#!/usr/bin/env python
# encoding: utf-8
"""
ccm_fast_export.py
Output Synergy data as git fast import/export format
Created by Aske Olsson 2011-02-23.
Copyright (c) 2011 Nokia. All rights reserved.
"""
import logging as logger
import time
from datetime import datetime
from copy import copy
from operator import itemgetter, attrgetter
from collections import deque
from pygraph.classes.digraph import digraph
def ccm_fast_export(releases, graphs):
logger.basicConfig(filename='ccm_fast_export.log',level=logger.DEBUG)
commit_lookup = {}
#Start at initial release
for k, v in releases.iteritems():
if v['previous'] is None:
release = k
break
logger.info("Starting at %s as initial release" %(release))
initial_release_time = time.mktime(releases[release]['created'].timetuple())
mark = 0
files = []
#Create the initial release
for o in releases[release]['objects']:
if o.get_type() != 'dir':
mark = create_blob(o, get_mark(mark), release)
files.append('M 100644 :'+str(mark) + ' ' + o.get_path())
mark = get_mark(mark)
commit_info = []
commit_info.append('reset refs/tags/' + release)
commit_info.append('commit refs/tags/' + release)
commit_info.append('mark :' + str(mark))
commit_info.append('author Nokia <[email protected]> ' + str(int(initial_release_time)) + " +0000")
commit_info.append('committer Nokia <[email protected]> ' + str(int(initial_release_time)) + " +0000")
commit_info.append('data 15')
commit_info.append('Initial commit')
commit_info.append('\n'.join(files))
commit_info.append('')
print '\n'.join(commit_info)
logger.info("git-fast-import:\n%s" %('\n'.join(commit_info)))
commit_lookup[release] = mark
# do the following releases (graphs)
release = releases[release]['next']
while release:
logger.info("Next release: %s" %(release))
commit_graph = graphs[release]['commit']
commit_graph = fix_orphan_nodes(commit_graph, releases[release]['previous'])
neighbors = deque(commit_graph.neighbors(releases[release]['previous']))
neighbors_left = len(neighbors)
while neighbors_left > 1:
n = neighbors.popleft()
logger.info("Neighbor: %s" %(n))
# Check is all incident objects are processed
if not set(commit_graph.incidents(n)).issubset(set(commit_lookup.keys())):
#print "Processing", n
#print "objects left:", neighbors
#print "missing", set(commit_graph.incidents(n)) - set(commit_lookup.keys())
#print "lookup", commit_lookup.keys()
neighbors.append(n)
continue
reference = [commit_lookup[i] for i in commit_graph.incidents(n)]
# create blobs and commit message for task/object
mark = create_commit(n, release, releases, mark, reference, graphs)
commit_lookup[n] = mark
# Get neighbors for this node
nbs = commit_graph.neighbors(n)
neighbors.extend(set(nbs) - set(neighbors))
neighbors_left = len(neighbors)
reference = [commit_lookup[i] for i in commit_graph.incidents(release)]
mark, merge_commit = create_release_merge_commit(releases, release, get_mark(mark), reference)
print '\n'.join(merge_commit)
commit_lookup[release] = mark
release = releases[release]['next']
#release = None
#reset to master
reset = ['reset refs/heads/master']
reset.append('from :' + str(mark))
logger.info("git-fast-import:\n%s" %('\n'.join(reset)))
print '\n'.join(reset)
def create_release_merge_commit(releases, release, mark, reference):
msg = []
msg.append('commit refs/tags/' + release)
msg.append('mark :' + str(mark))
msg.append('author %s <%[email protected]> ' % (releases[release]['author'], releases[release]['author']) + str(int(time.mktime(releases[release]['created'].timetuple()))) + " +0000")
msg.append('committer %s <%[email protected]> ' % (releases[release]['author'], releases[release]['author']) + str(int(time.mktime(releases[release]['created'].timetuple()))) + " +0000")
commit_msg = "Release " + release
msg.append('data ' + str(len(commit_msg)))
msg.append(commit_msg)
msg.append('from :' + str(reference[0]))
if len(reference) > 1:
merge = ['merge :' + str(i) for i in reference[1:]]
msg.append('\n'.join(merge))
msg.append('')
logger.info("git-fast-import MERGE-COMMIT:\n%s" %('\n'.join(msg)))
return mark, msg
def create_commit(n, release, releases, mark, reference, graphs):
logger.info("Creating commit for %s" %(n))
object_lookup = {}
# Find n in release
if ':task:' in n:
# It's a task
logger.info("Task: %s" %(n))
objects = get_objects_from_graph(n, graphs[release]['task'], releases[release]['objects'])
# Get the correct task name so commit message can be filled
task_name = get_task_object_from_splitted_task_name(n)
task = find_task_in_release(task_name, releases[release]['tasks'])
# sort objects to get correct commit order, if multiple versions of one file is in in the task
objects = reduce_objects_for_commit(objects)
for o in objects:
if not o.get_type() == 'dir':
mark = create_blob(o, get_mark(mark), release)
object_lookup[o.get_object_name()] = mark
file_list = create_file_list(objects, object_lookup)
mark, commit = make_commit_from_task(task, get_mark(mark), reference, release, file_list)
print '\n'.join(commit)
return mark
else:
# It's a single object
logger.info("Single Object: %s" %(n))
o = get_object(n, releases[release]['objects'])
if not o.get_type() == 'dir':
mark = create_blob(o, get_mark(mark), release)
object_lookup[o.get_object_name()] = mark
file_list = create_file_list([o], object_lookup)
mark, commit = make_commit_from_object(o, get_mark(mark), reference, release, file_list)
print '\n'.join(commit)
return mark
def make_commit_from_task(task, mark, reference, release, file_list):
commit_info = []
commit_info.append('commit refs/tags/' + release)
commit_info.append('mark :' + str(mark))
commit_info.append('author %s <%[email protected]> ' % (task.get_author(), task.get_author()) + str(int(time.mktime(task.get_complete_time().timetuple()))) + " +0000")
commit_info.append('committer %s <%[email protected]> ' % (task.get_author(), task.get_author()) + str(int(time.mktime(task.get_complete_time().timetuple()))) + " +0000")
commit_msg = create_commit_msg_from_task(task)
commit_info.append('data ' + str(len(commit_msg)))
commit_info.append(commit_msg)
commit_info.append('from :' + str(reference[0]))
if len(reference) > 1:
merge = ['merge :' + str(i) for i in reference[1:]]
commit_info.append('\n'.join(merge))
commit_info.append(file_list)
commit_info.append('')
logger.info("git-fast-import COMMIT:\n%s" %('\n'.join(commit_info)))
return mark, commit_info
def make_commit_from_object(o, mark, reference, release, file_list):
commit_info = []
commit_info.append('commit refs/tags/' + release)
commit_info.append('mark :' + str(mark))
commit_info.append('author %s <%[email protected]> ' % (o.get_author(), o.get_author()) + str(int(time.mktime(o.get_integrate_time().timetuple()))) + " +0000")
commit_info.append('committer %s <%[email protected]> ' % (o.get_author(), o.get_author()) + str(int(time.mktime(o.get_integrate_time().timetuple()))) + " +0000")
commit_msg = "Object not associated to task in release: " + o.get_object_name()
commit_info.append('data ' + str(len(commit_msg)))
commit_info.append(commit_msg)
commit_info.append('from :' + str(reference[0]))
if len(reference) > 1:
merge = ['merge :' + str(i) for i in reference[1:]]
commit_info.append('\n'.join(merge))
commit_info.append(file_list)
commit_info.append('')
logger.info("git-fast-import COMMIT:\n%s" %('\n'.join(commit_info)))
return mark, commit_info
def create_file_list(objects, lookup):
l = []
for o in objects:
if o.get_type() != 'dir':
#exe = '100755' if o.is_executable() else '100644'
exe = '100644'
l.append('M ' + exe + ' :' + str(lookup[o.get_object_name()]) + ' ' + o.get_path())
else:
#Get deleted items:
deleted = o.get_dir_changes()['deleted']
for d in deleted:
l.append('D ' + o.get_path() + '/' + d)
return '\n'.join(l)
def create_commit_msg_from_task(task):
msg = []
attr = task.get_attributes()
msg.append(attr['task_synopsis'])
msg.append('')
msg.append(attr['task_description'])
msg.append('')
insp = None
for k, v in attr.iteritems():
if k == 'task_synopsis':
continue
if k == 'task_description':
continue
if k == 'inspection_task':
insp = v.copy()
continue
if k == 'status_log':
continue
if len(v.strip()) == 0:
continue
msg.append('Synergy-'+k.replace('_', '-')+': '+v.strip().replace("\n", " "))
if insp:
for k, v in insp.iteritems():
if k == 'status_log':
continue
if len(v.strip()) == 0:
continue
k = k.replace('task_', '').replace('insp_', '').replace('_', '-')
for line in v.splitlines():
msg.append('Synergy-insp-'+k+': '+line.strip())
return '\n'.join(msg)
def find_task_in_release(task, tasks):
for t in tasks:
if t.get_object_name() == task:
return t
def get_objects_from_task(task, objects):
objs = []
for o in task.get_objects():
for obj in objects:
if obj.get_object_name() == o:
objs.append(obj)
break
return objs
def get_objects_from_graph(task, graph, objects):
objs = []
for o in graph.links(task):
for obj in objects:
if obj.get_object_name() == o:
objs.append(obj)
break
return objs
def get_task_object_from_splitted_task_name(task):
return task.rsplit('_')[0]
def reduce_objects_for_commit(objects):
ret_val = []
objs = {}
for o in objects:
key = o.get_name() + ':' + o.get_type() + ':' + o.get_instance()
if key in objs.keys():
objs[key].append(o)
else:
objs[key] = [o]
for v in objs.values():
o = sort_objects_by_integrate_time(v)
ret_val.append(o.pop())
return ret_val
def sort_objects_by_integrate_time(objects):
#Sort first by integrate time, but also by version as several objects may be checked in at the same time (checkpoint->integrate).
sorted_objects = sorted(objects, key=attrgetter('integrate_time', 'version'))
return sorted_objects
def get_object(o, objects):
for obj in objects:
if obj.get_object_name() == o:
return obj
def create_blob(obj, mark, release):
blob =['blob']
blob.append('mark :'+str(mark))
fname = 'data/' + release + '/' + obj.get_object_name()
#f = open(fname, 'rb')
#content = f.read()
#f.close()
#length = len(content)
#blob.append('data '+ str(length))
#blob.append(content)
print '\n'.join(blob)
return mark
def fix_orphan_nodes(commit_graph, release):
orphan_nodes = [node for node in commit_graph.nodes() if commit_graph.incidents(node) == []]
[commit_graph.add_edge((release, node)) for node in orphan_nodes if node != release]
return commit_graph
def get_mark(mark):
mark +=1
return mark