forked from rtcTo/rtc2git
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rtcFunctions.py
414 lines (358 loc) · 18.3 KB
/
rtcFunctions.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
import sys
import os
import re
import sorter
import shell
from gitFunctions import Commiter
import shouter
class RTCInitializer:
@staticmethod
def initialize(config):
RTCInitializer.loginandcollectstreamuuid(config)
workspace = WorkspaceHandler(config)
if config.useexistingworkspace:
shouter.shout("Use existing workspace to start migration")
workspace.load()
else:
workspace.createandload(config.streamuuid, config.initialcomponentbaselines)
@staticmethod
def loginandcollectstreamuuid(config):
shell.execute("%s login -r %s -u %s -P %s" % (config.scmcommand, config.repo, config.user, config.password))
config.collectstreamuuid()
class WorkspaceHandler:
def __init__(self, config):
self.config = config
self.workspace = config.workspace
self.repo = config.repo
self.scmcommand = config.scmcommand
def createandload(self, stream, componentbaselineentries=[]):
shell.execute("%s create workspace -r %s -s %s %s" % (self.scmcommand, self.repo, stream, self.workspace))
if componentbaselineentries:
self.setcomponentstobaseline(componentbaselineentries, stream)
else:
self.setcomponentstobaseline(ImportHandler(self.config).determineinitialbaseline(stream),
stream)
self.load()
def load(self):
command = "%s load -r %s %s --force" % (self.scmcommand, self.repo, self.workspace)
shouter.shout("Start (re)loading current workspace: " + command)
shell.execute(command)
shouter.shout("Load of workspace finished")
def setcomponentstobaseline(self, componentbaselineentries, streamuuid):
for entry in componentbaselineentries:
shouter.shout("Set component '%s'(%s) to baseline '%s' (%s)" % (entry.componentname, entry.component,
entry.baselinename, entry.baseline))
replacecommand = "%s set component -r %s -b %s %s stream %s %s --overwrite-uncommitted" % \
(self.scmcommand, self.repo, entry.baseline, self.workspace, streamuuid, entry.component)
shell.execute(replacecommand)
def setnewflowtargets(self, streamuuid):
shouter.shout("Set new Flowtargets")
if not self.hasflowtarget(streamuuid):
shell.execute("%s add flowtarget -r %s %s %s" % (self.scmcommand, self.repo, self.workspace, streamuuid))
command = "%s set flowtarget -r %s %s --default --current %s" % (self.scmcommand, self.repo, self.workspace, streamuuid)
shell.execute(command)
def hasflowtarget(self, streamuuid):
command = "%s --show-uuid y --show-alias n list flowtargets -r %s %s" % (self.scmcommand, self.repo, self.workspace)
flowtargetlines = shell.getoutput(command)
for flowtargetline in flowtargetlines:
splittedinformationline = flowtargetline.split("\"")
uuidpart = splittedinformationline[0].split(" ")
flowtargetuuid = uuidpart[0].strip()[1:-1]
if streamuuid in flowtargetuuid:
return True
return False
class Changes:
latest_accept_command = ""
@staticmethod
def discard(config, *changeentries):
idstodiscard = Changes._collectids(changeentries)
shell.execute(config.scmcommand + " discard -w " + config.workspace + " -r " + config.repo + " -o" + idstodiscard)
@staticmethod
def accept(config, logpath, *changeentries):
for changeEntry in changeentries:
shouter.shout("Accepting: " + changeEntry.tostring())
revisions = Changes._collectids(changeentries)
Changes.latest_accept_command = config.scmcommand + " accept -v -o -r " + config.repo + " -t " + \
config.workspace + " --changes" + revisions
return shell.execute(Changes.latest_accept_command, logpath, "a")
@staticmethod
def _collectids(changeentries):
ids = ""
for changeentry in changeentries:
ids += " " + changeentry.revision
return ids
@staticmethod
def arereleatedmergechangesets(changeentry1, changeentry2):
if changeentry1 and changeentry2:
if changeentry1.author == changeentry2.author or "merge" in changeentry2.comment.lower():
return True
return False
@staticmethod
def tostring(*changes):
logmessage = "Changes: \n"
for change in changes:
logmessage += change.tostring() + "\n"
shouter.shout(logmessage)
class ImportHandler:
def __init__(self, config):
self.config = config
self.acceptlogpath = config.getlogpath("accept.txt")
def getcomponentbaselineentriesfromstream(self, stream):
filename = self.config.getlogpath("StreamComponents_" + stream + ".txt")
command = "%s --show-alias n --show-uuid y list components -v -m 30 -r %s %s" % (self.config.scmcommand,
self.config.repo, stream)
shell.execute(command, filename)
componentbaselinesentries = []
skippedfirstrow = False
islinewithcomponent = 2
component = ""
baseline = ""
componentname = ""
baselinename = ""
with open(filename, 'r', encoding=shell.encoding) as file:
for line in file:
cleanedline = line.strip()
if cleanedline:
if not skippedfirstrow:
skippedfirstrow = True
continue
splittedinformationline = line.split("\"")
uuidpart = splittedinformationline[0].split(" ")
if islinewithcomponent % 2 is 0:
component = uuidpart[3].strip()[1:-1]
componentname = splittedinformationline[1]
else:
baseline = uuidpart[5].strip()[1:-1]
baselinename = splittedinformationline[1]
if baseline and component:
componentbaselinesentries.append(
ComponentBaseLineEntry(component, baseline, componentname, baselinename))
baseline = ""
component = ""
componentname = ""
baselinename = ""
islinewithcomponent += 1
return componentbaselinesentries
def determineinitialbaseline(self, stream):
regex = "\(_\w+\)"
pattern = re.compile(regex)
config = self.config
componentbaselinesentries = self.getcomponentbaselineentriesfromstream(stream)
for entry in componentbaselinesentries:
shouter.shout("Determine initial baseline of " + entry.componentname)
# use always scm, lscm fails when specifying maximum over 10k
command = "scm --show-alias n --show-uuid y list baselines --components %s -r %s -u %s -p %s -m 20000" % \
(entry.component, config.user, config.password, config.repo)
baselineslines = shell.getoutput(command)
baselineslines.reverse() # reverse to have earliest baseline on top
for baselineline in baselineslines:
matcher = pattern.search(baselineline)
if matcher:
matchedstring = matcher.group()
uuid = matchedstring[1:-1]
entry.baseline = uuid
entry.baselinename = "Automatically detected initial baseline"
break
return componentbaselinesentries
def acceptchangesintoworkspace(self, changeentries):
amountofchanges = len(changeentries)
shouter.shoutwithdate("Start accepting %s changesets" % amountofchanges)
amountofacceptedchanges = 0
changestoskip = 0
reloaded = False
for changeEntry in changeentries:
amountofacceptedchanges += 1
if changestoskip > 0:
shouter.shout("Skipping " + changeEntry.tostring())
changestoskip -= 1
continue
acceptedsuccesfully = Changes.accept(self.config, self.acceptlogpath,
changeEntry) is 0
if not acceptedsuccesfully:
shouter.shout("Change wasnt succesfully accepted into workspace")
changestoskip = self.retryacceptincludingnextchangesets(changeEntry, changeentries)
elif not reloaded:
if self.is_reloading_necessary():
WorkspaceHandler(self.config).load()
reloaded = True
shouter.shout("Accepted change %s/%s into working directory" % (amountofacceptedchanges, amountofchanges))
Commiter.addandcommit(changeEntry)
@staticmethod
def is_reloading_necessary():
return shell.execute("git diff --exit-code") is 0
@staticmethod
def collect_changes_to_accept_to_avoid_conflicts(changewhichcantacceptedallone, changes):
changestoaccept = [changewhichcantacceptedallone]
nextchange = ImportHandler.getnextchangeset(changewhichcantacceptedallone, changes)
while True:
if Changes.arereleatedmergechangesets(changewhichcantacceptedallone, nextchange):
changestoaccept.append(nextchange)
nextchange = ImportHandler.getnextchangeset(nextchange, changes)
else:
break
return changestoaccept
def retryacceptincludingnextchangesets(self, change, changes):
changestoskip = 0
issuccessful = False
changestoaccept = ImportHandler.collect_changes_to_accept_to_avoid_conflicts(change, changes)
amountofchangestoaccept = len(changestoaccept)
if amountofchangestoaccept > 1:
Changes.tostring(*changestoaccept)
if self.config.useautomaticconflictresolution or self.is_user_agreeing_to_accept_next_change(change):
for index in range(1, amountofchangestoaccept):
toaccept = changestoaccept[0:index + 1] # accept least possible amount of changes
if Changes.accept(self.config, self.acceptlogpath, *toaccept) is 0:
changestoskip = len(toaccept) - 1 # initialchange shouldnt be skipped
issuccessful = True
break
else:
Changes.discard(self.config, *toaccept) # revert initial state
if not issuccessful:
self.is_user_aborting(change)
return changestoskip
@staticmethod
def is_user_agreeing_to_accept_next_change(change):
messagetoask = "Press Y for accepting following changes, press N to skip"
while True:
answer = input(messagetoask).lower()
if answer == "y":
return True
elif answer == "n":
return not ImportHandler.is_user_aborting(change)
else:
shouter.shout("Please answer with Y/N, input was " + answer)
@staticmethod
def is_user_aborting(change):
shouter.shout("Last executed command: \n" + Changes.latest_accept_command)
shouter.shout("Apropriate git commit command \n" + Commiter.getcommitcommand(change))
reallycontinue = "Do you want to continue? Y for continue, any key for abort"
if input(reallycontinue).lower() == "y":
return True
else:
sys.exit("Please check the output/log and rerun program with resume")
@staticmethod
def getnextchangeset(currentchangeentry, changeentries):
nextchangeentry = None
nextindex = changeentries.index(currentchangeentry) + 1
has_next_changeset = nextindex is not len(changeentries)
if has_next_changeset:
nextchangeentry = changeentries[nextindex]
return nextchangeentry
def getchangeentriesofstreamcomponents(self, componentbaselineentries):
missingchangeentries = {}
shouter.shout("Start collecting changeentries")
changeentriesbycomponentbaselineentry = {}
for componentBaseLineEntry in componentbaselineentries:
shouter.shout("Collect changes until baseline %s of component %s" %
(componentBaseLineEntry.baselinename, componentBaseLineEntry.componentname))
changeentries = self.getchangeentriesofbaseline(componentBaseLineEntry.baseline)
for changeentry in changeentries:
missingchangeentries[changeentry.revision] = changeentry
return missingchangeentries
def readhistory(self, componentbaselineentries, streamname):
if not self.config.useprovidedhistory:
warning = "Warning - UseProvidedHistory is set to false, merge-conflicts are more likely to happen. \n " \
"For more information see https://github.com/WtfJoke/rtc2git/wiki/Getting-your-History-Files"
shouter.shout(warning)
return None
historyuuids = {}
shouter.shout("Start reading history files")
for componentBaseLineEntry in componentbaselineentries:
history = self.gethistory(componentBaseLineEntry.componentname, streamname)
historyuuids[componentBaseLineEntry.component] = history
return historyuuids
@staticmethod
def getchangeentriestoaccept(missingchangeentries, history):
changeentriestoaccept = []
if history:
historywithchangeentryobject = {}
for key in history.keys():
currentuuids = history.get(key)
changeentries = []
for uuid in currentuuids:
changeentry = missingchangeentries.get(uuid)
if changeentry:
changeentries.append(changeentry)
historywithchangeentryobject[key] = changeentries
changeentriestoaccept = sorter.tosortedlist(historywithchangeentryobject)
else:
changeentriestoaccept.extend(missingchangeentries.values())
# simple sort by date - same as returned by compare command
changeentriestoaccept.sort(key=lambda change: change.date)
return changeentriestoaccept
@staticmethod
def getchangeentriesfromfile(outputfilename):
informationseparator = "@@"
numberofexpectedinformationseparators = 5
changeentries = []
with open(outputfilename, 'r', encoding=shell.encoding) as file:
currentline = ""
currentinformationpresent = 0
for line in file:
cleanedline = line.strip()
if cleanedline:
currentinformationpresent += cleanedline.count(informationseparator)
if currentline:
currentline += os.linesep
currentline += cleanedline
if currentinformationpresent >= numberofexpectedinformationseparators:
splittedlines = currentline.split(informationseparator)
revisionwithbrackets = splittedlines[0].strip()
revision = revisionwithbrackets[1:-1]
author = splittedlines[1].strip()
email = splittedlines[2].strip()
comment = splittedlines[3].strip()
date = splittedlines[4].strip()
changeentries.append(ChangeEntry(revision, author, email, date, comment))
currentinformationpresent = 0
currentline = ""
return changeentries
@staticmethod
def getsimplehistoryfromfile(outputfilename):
revisions = []
if not os.path.isfile(outputfilename):
shouter.shout("History file not found: " + outputfilename)
shouter.shout("Skipping this part of history")
return revisions
with open(outputfilename, 'r', encoding=shell.encoding) as file:
for line in file:
revisions.append(line.strip())
revisions.reverse() # to begin by the oldest
return revisions
def getchangeentriesofbaseline(self, baselinetocompare):
return self.getchangeentriesbytypeandvalue("baseline", baselinetocompare)
def getchangeentriesofstream(self, streamtocompare):
shouter.shout("Start collecting changes since baseline creation")
missingchangeentries = {}
changeentries = self.getchangeentriesbytypeandvalue("stream", streamtocompare)
for changeentry in changeentries:
missingchangeentries[changeentry.revision] = changeentry
return missingchangeentries
def getchangeentriesbytypeandvalue(self, comparetype, value):
dateformat = "yyyy-MM-dd HH:mm:ss"
outputfilename = self.config.getlogpath("Compare_" + comparetype + "_" + value + ".txt")
comparecommand = "%s --show-alias n --show-uuid y compare ws %s %s %s -r %s -I sw -C @@{name}@@{email}@@ --flow-directions i -D @@\"%s\"@@" \
% (self.config.scmcommand, self.config.workspace, comparetype, value, self.config.repo, dateformat)
shell.execute(comparecommand, outputfilename)
return ImportHandler.getchangeentriesfromfile(outputfilename)
def gethistory(self, componentname, streamname):
outputfilename = self.config.gethistorypath("History_%s_%s.txt" % (componentname, streamname))
return ImportHandler.getsimplehistoryfromfile(outputfilename)
class ChangeEntry:
def __init__(self, revision, author, email, date, comment):
self.revision = revision
self.author = author
self.email = email
self.date = date
self.comment = comment
def getgitauthor(self):
authorrepresentation = "%s <%s>" % (self.author, self.email)
return shell.quote(authorrepresentation)
def tostring(self):
return self.comment + " (Date: " + self.date + ", Author: " + self.author + ", Revision: " + self.revision + ")"
class ComponentBaseLineEntry:
def __init__(self, component, baseline, componentname, baselinename):
self.component = component
self.baseline = baseline
self.componentname = componentname
self.baselinename = baselinename