Skip to content

Commit

Permalink
Merge branch 'heuristicsortingofchangessets' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
WtfJoke committed Mar 15, 2015
2 parents 395d0e6 + 6ca354f commit 1b87412
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 3 deletions.
8 changes: 5 additions & 3 deletions rtcFunctions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sys

import sorter
import shell
from gitFunctions import Commiter
import shouter
Expand Down Expand Up @@ -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
Expand Down
45 changes: 45 additions & 0 deletions sorter.py
Original file line number Diff line number Diff line change
@@ -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
50 changes: 50 additions & 0 deletions tests/test_sorter.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 1b87412

Please sign in to comment.