[csw-devel] SF.net SVN: gar:[13106] csw/mgar/gar/v2/lib/python
wahwah at users.sourceforge.net
wahwah at users.sourceforge.net
Sat Jan 29 16:48:01 CET 2011
Revision: 13106
http://gar.svn.sourceforge.net/gar/?rev=13106&view=rev
Author: wahwah
Date: 2011-01-29 15:48:01 +0000 (Sat, 29 Jan 2011)
Log Message:
-----------
checkpkg: A catalog comparator class
Modified Paths:
--------------
csw/mgar/gar/v2/lib/python/catalog.py
csw/mgar/gar/v2/lib/python/catalog_test.py
Modified: csw/mgar/gar/v2/lib/python/catalog.py
===================================================================
--- csw/mgar/gar/v2/lib/python/catalog.py 2011-01-29 15:47:31 UTC (rev 13105)
+++ csw/mgar/gar/v2/lib/python/catalog.py 2011-01-29 15:48:01 UTC (rev 13106)
@@ -168,3 +168,25 @@
logging.warning("Catalog name %s is duplicated!", d["catalogname"])
self.by_catalogname[d["catalogname"]] = d
return self.by_catalogname
+
+
+class CatalogComparator(object):
+
+ def GetCatalogDiff(self, cat_a, cat_b):
+ """Returns a difference between two catalogs."""
+ bc_a = cat_a.GetDataByCatalogname()
+ bc_b = cat_b.GetDataByCatalogname()
+ cn_a = set(bc_a)
+ cn_b = set(bc_b)
+ new_catalognames = cn_b.difference(cn_a)
+ removed_catalognames = cn_a.difference(cn_b)
+ same_catalognames = cn_b.intersection(cn_a)
+ # Looking for updated catalognames
+ updated_catalognames = set()
+ for catalogname in same_catalognames:
+ if bc_a[catalogname]["version"] != bc_b[catalogname]["version"]:
+ updated_catalognames.add(catalogname)
+ new_pkgs = [bc_b[x] for x in new_catalognames]
+ removed_pkgs = [bc_a[x] for x in removed_catalognames]
+ updated_pkgs = [{"from": bc_a[x], "to": bc_b[x]} for x in updated_catalognames]
+ return new_pkgs, removed_pkgs, updated_pkgs
Modified: csw/mgar/gar/v2/lib/python/catalog_test.py
===================================================================
--- csw/mgar/gar/v2/lib/python/catalog_test.py 2011-01-29 15:47:31 UTC (rev 13105)
+++ csw/mgar/gar/v2/lib/python/catalog_test.py 2011-01-29 15:48:01 UTC (rev 13106)
@@ -12,7 +12,6 @@
"cfe40c06e994f6e8d3b191396d0365cb 137550 "
"CSWgcc4corert|CSWeventlog|CSWosslrt|CSWzlib|CSWpcrert|CSWggettextrt|"
"CSWglib2|CSWtcpwrap|CSWcswclassutils|CSWcommon none")
-
CATALOG_LINE_2 = (
"syslog_ng 3.0.4,REV=2009.10.12 "
"CSWsyslogng "
@@ -20,17 +19,19 @@
"a1e9747ac3aa04c0497d2a3a23885995 137367 "
"CSWcswclassutils|CSWgcc4corert|CSWeventlog|CSWosslrt|CSWzlib|CSWpcrert|"
"CSWggettextrt|CSWglib2|CSWtcpwrap|CSWcswclassutils|CSWcommon none")
+CATALOG_LINE_3 = (
+ 'tmux 1.2,REV=2010.05.17 CSWtmux '
+ 'tmux-1.2,REV=2010.05.17-SunOS5.9-sparc-CSW.pkg.gz '
+ '145351cf6186fdcadcd169b66387f72f 214091 '
+ 'CSWcommon|CSWlibevent none none\n')
+
+
class OpencswCatalogUnitTest(unittest.TestCase):
def test_ParseCatalogLine_1(self):
- line = (
- 'tmux 1.2,REV=2010.05.17 CSWtmux '
- 'tmux-1.2,REV=2010.05.17-SunOS5.9-sparc-CSW.pkg.gz '
- '145351cf6186fdcadcd169b66387f72f 214091 '
- 'CSWcommon|CSWlibevent none none\n')
oc = catalog.OpencswCatalog(None)
- parsed = oc._ParseCatalogLine(line)
+ parsed = oc._ParseCatalogLine(CATALOG_LINE_3)
expected = {'catalogname': 'tmux',
'deps': ('CSWcommon', 'CSWlibevent'),
'file_basename': 'tmux-1.2,REV=2010.05.17-SunOS5.9-sparc-CSW.pkg.gz',
@@ -60,5 +61,37 @@
self.assertEqual(expected, oc.GetDataByCatalogname())
+class CatalogComparatorUnitTest(unittest.TestCase):
+
+ def testUpdateOnly(self):
+ oc1 = catalog.OpencswCatalog(StringIO(CATALOG_LINE_1))
+ oc2 = catalog.OpencswCatalog(StringIO(CATALOG_LINE_2))
+ c = catalog.CatalogComparator()
+ new_pkgs, removed_pkgs, updated_pkgs = c.GetCatalogDiff(oc1, oc2)
+ self.assertFalse(new_pkgs)
+ self.assertFalse(removed_pkgs)
+ self.assertTrue("from" in updated_pkgs[0])
+
+ def testAddition(self):
+ oc1 = catalog.OpencswCatalog(StringIO(CATALOG_LINE_1))
+ oc2 = catalog.OpencswCatalog(
+ StringIO(CATALOG_LINE_1 + "\n" + CATALOG_LINE_3))
+ c = catalog.CatalogComparator()
+ new_pkgs, removed_pkgs, updated_pkgs = c.GetCatalogDiff(oc1, oc2)
+ self.assertFalse(removed_pkgs)
+ self.assertFalse(updated_pkgs)
+ self.assertEqual(1, len(new_pkgs))
+
+ def testRemoval(self):
+ oc1 = catalog.OpencswCatalog(
+ StringIO(CATALOG_LINE_1 + "\n" + CATALOG_LINE_3))
+ oc2 = catalog.OpencswCatalog(StringIO(CATALOG_LINE_1))
+ c = catalog.CatalogComparator()
+ new_pkgs, removed_pkgs, updated_pkgs = c.GetCatalogDiff(oc1, oc2)
+ self.assertFalse(new_pkgs)
+ self.assertFalse(updated_pkgs)
+ self.assertEqual(1, len(removed_pkgs))
+
+
if __name__ == '__main__':
unittest.main()
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
More information about the devel
mailing list