diff --git a/rtcFunctions.py b/rtcFunctions.py index 44ad0d6..e4f78f9 100644 --- a/rtcFunctions.py +++ b/rtcFunctions.py @@ -1,5 +1,6 @@ import sys +import sorter import shell from gitFunctions import Commiter import shouter @@ -137,10 +138,11 @@ def acceptchangesintoworkspace(self, changeentries): def getchangeentriesofstreamcomponents(self, componentbaselineentries): shouter.shout("Start collecting changeentries") - changeentries = [] + changeentriesbycomponentbaselineentry = {} for componentBaseLineEntry in componentbaselineentries: - changeentries.extend(self.getchangeentriesofbaseline(componentBaseLineEntry.baseline)) - changeentries.sort(key=lambda change: change.date) + changeentriesbycomponentbaselineentry[componentBaseLineEntry.componentname] = \ + self.getchangeentriesofbaseline(componentBaseLineEntry.baseline) + changeentries = sorter.tosortedlist(changeentriesbycomponentbaselineentry) return changeentries @staticmethod diff --git a/sorter.py b/sorter.py new file mode 100644 index 0000000..dac43bb --- /dev/null +++ b/sorter.py @@ -0,0 +1,45 @@ +def tosortedlist(changeentrymap): + sortedlist = [] + expectedlistsize = len(__aslist(changeentrymap)) + + while len(sortedlist) < expectedlistsize: + firstentryfromeachkey = __getfirstentryfromeachkeyasmap(changeentrymap) + changesetwithearliestdate = __getchangeentrywithearliestdate(firstentryfromeachkey) + __deleteentry(changeentrymap, changesetwithearliestdate) + sortedlist.append(changesetwithearliestdate) + + return sortedlist + + +def __getfirstentryfromeachkeyasmap(changeentrymap): + firstentries = {} + for key in changeentrymap.keys(): + changeentries = changeentrymap.get(key) + if changeentries: + firstentries[key] = changeentries[0] + return firstentries + + +def __deleteentry(changeentrymap, changeentrytodelete): + for key in changeentrymap.keys(): + changeentries = changeentrymap.get(key) + if changeentries and changeentrytodelete.revision is changeentries[0].revision: + changeentries.remove(changeentrytodelete) + break + + +def __getchangeentrywithearliestdate(changeentries): + changeentrywithearliestdate = None + for key in changeentries.keys(): + changeentry = changeentries.get(key) + if not changeentrywithearliestdate or changeentry.date < changeentrywithearliestdate.date: + changeentrywithearliestdate = changeentry + return changeentrywithearliestdate + + +def __aslist(anymap): + resultlist = [] + for key in anymap.keys(): + for changeentry in anymap.get(key): + resultlist.append(changeentry) + return resultlist \ No newline at end of file diff --git a/tests/test_sorter.py b/tests/test_sorter.py new file mode 100644 index 0000000..dc229b7 --- /dev/null +++ b/tests/test_sorter.py @@ -0,0 +1,50 @@ +import unittest + +from rtcFunctions import ChangeEntry +import sorter + + +class SorterTest(unittest.TestCase): + def setUp(self): + self.changeentriesmap = {} + self.changesetcounter = 0 + + def testSortingTwoComponents_ExpectKeepOrderingInsideSameKey(self): + firstkey = "anyKey" + secondkey = "anyOtherKey" + self.put(firstkey, self.createchangeentry("1991-03-24")) + self.put(firstkey, self.createchangeentry("1991-05-24")) + self.put(firstkey, self.createchangeentry("1991-05-22")) + self.put(secondkey, self.createchangeentry("1991-05-23")) + self.put(secondkey, self.createchangeentry("1991-07-22")) + sortedchangesets = sorter.tosortedlist(self.changeentriesmap) + + self.assertEqual("1991-03-24", sortedchangesets[0].date) # earliestEntry firstkey + self.assertEqual("1991-05-23", sortedchangesets[1].date) # earliestEntry secondKey + self.assertEqual("1991-05-24", sortedchangesets[2].date) # secondEarliestEntry firstKey + self.assertEqual("1991-05-22", sortedchangesets[3].date) # lastEntry firstKey + self.assertEqual("1991-07-22", sortedchangesets[4].date) # lastEntry secondKey + + def testSortingThreeComponents_ExpectDatesInOrder(self): + earliestdate = "2015-01-30 09:00:15" + dateinbetween = "2015-01-30 09:00:16" + latestdate = "2015-02-30 09:00:00" + self.put("justAnotherKey", self.createchangeentry(latestdate)) + self.put("anyKey", self.createchangeentry(earliestdate)) + self.put("anyOtherKey", self.createchangeentry(dateinbetween)) + + sortedchangesets = sorter.tosortedlist(self.changeentriesmap) + self.assertEqual(earliestdate, sortedchangesets[0].date) + self.assertEqual(dateinbetween, sortedchangesets[1].date) + self.assertEqual(latestdate, sortedchangesets[2].date) + + def createchangeentry(self, date): + self.changesetcounter += 1 + return ChangeEntry(self.changesetcounter, "anyAuthor", "anyEmail", date, "") + + def put(self, key, changeentry): + changeentries = self.changeentriesmap.get(key) + if not changeentries: + changeentries = [] + changeentries.append(changeentry) + self.changeentriesmap[key] = changeentries