[csw-devel] SF.net SVN: gar:[12096] csw/mgar/gar/v2

wahwah at users.sourceforge.net wahwah at users.sourceforge.net
Mon Dec 27 13:26:38 CET 2010


Revision: 12096
          http://gar.svn.sourceforge.net/gar/?rev=12096&view=rev
Author:   wahwah
Date:     2010-12-27 12:26:38 +0000 (Mon, 27 Dec 2010)

Log Message:
-----------
checkpkg: Move Pkgmap to own file

Creating two new files: pkgmap.py and struct_util.py.  The first one contains
the Pkgmap class, and the second is for generic data structure processing
functions.

Modified Paths:
--------------
    csw/mgar/gar/v2/lib/python/opencsw.py
    csw/mgar/gar/v2/lib/python/opencsw_test.py
    csw/mgar/gar/v2/lib/python/package_stats_test.py
    csw/mgar/gar/v2/tests/run_tests.py

Added Paths:
-----------
    csw/mgar/gar/v2/lib/python/pkgmap.py
    csw/mgar/gar/v2/lib/python/pkgmap_test.py
    csw/mgar/gar/v2/lib/python/struct_util.py
    csw/mgar/gar/v2/lib/python/struct_util_test.py

Modified: csw/mgar/gar/v2/lib/python/opencsw.py
===================================================================
--- csw/mgar/gar/v2/lib/python/opencsw.py	2010-12-27 12:25:56 UTC (rev 12095)
+++ csw/mgar/gar/v2/lib/python/opencsw.py	2010-12-27 12:26:38 UTC (rev 12096)
@@ -165,18 +165,6 @@
   return version_str, version_info, revision_info
 
 
-def IndexDictsBy(list_of_dicts, field_key):
-  """Creates an index of list of dictionaries by a field name.
-
-  Returns a dictionary of lists.
-  """
-  index = {}
-  for d in list_of_dicts:
-    index.setdefault(d[field_key], [])
-    index[d[field_key]].append(d)
-  return index
-
-
 class CatalogBasedOpencswPackage(object):
 
   catalog_downloaded = False
@@ -490,95 +478,3 @@
   fn_data["arch"] = pkginfo_dict["ARCH"]
   fn_data["tag"] = "SUNW"
   return SRV4_FN_TMPL % fn_data
-
-
-class Pkgmap(object):
-  """Represents the pkgmap of the package.
-
-  The plan:
-
-    entries = [
-      {
-        'path': ...,
-        'class': ...,
-        (more fields?)
-      }, ...
-    ]
-
-  + indexes
-  """
-  ENTRY_TYPES = {
-      "1": "header (?)",
-      "d": "directory",
-      "f": "file",
-      "s": "symlink",
-      "l": "link",
-      "i": "script",
-      "e": "editable file"
-  }
-
-  def __init__(self, input, permissions=False,
-               strip=None):
-    self.paths = set()
-    self.analyze_permissions = permissions
-    self.entries = []
-    self.classes = None
-    for line in input:
-      fields = re.split(r'\s+', line)
-      if strip:
-        strip_re = re.compile(r"^%s" % strip)
-        fields = [re.sub(strip_re, "", x) for x in fields]
-      line_to_add = None
-      installed_path = None
-      prototype_class = None
-      line_type = fields[1]
-      mode = None
-      user = None
-      group = None
-      if len(fields) < 2:
-        continue
-      elif line_type in ('f', 'd'):
-        # Files and directories
-        line_to_add = fields[3]
-        installed_path = fields[3]
-        prototype_class = fields[2]
-        if self.analyze_permissions:
-          line_to_add += " %s %s %s" % tuple(fields[4:7])
-        mode, user, group = fields[4:7]
-      elif line_type in ('e'):
-        # Editable files
-        line_to_add = fields[3]
-        installed_path = fields[3]
-        prototype_class = fields[2]
-      elif line_type in ('s', 'l'):
-        # soft- and hardlinks
-        link_from, link_to = fields[3].split("=")
-        installed_path = link_from
-        line_to_add = "%s --> %s" % (link_from, link_to)
-        prototype_class = fields[2]
-      if line_to_add:
-        self.paths.add(line_to_add)
-      entry = {
-          "line": line.strip(),
-          "type": line_type,
-      }
-      entry["path"] = installed_path
-      entry["class"] = prototype_class
-      entry["mode"] = mode
-      entry["user"] = user
-      entry["group"] = group
-      self.entries.append(entry)
-    self.entries_by_line = IndexDictsBy(self.entries, "line")
-    self.entries_by_type = IndexDictsBy(self.entries, "type")
-    self.entries_by_class = IndexDictsBy(self.entries, "class")
-    self.entries_by_path = IndexDictsBy(self.entries, "path")
-    self.entries = sorted(self.entries, key=lambda x: x["path"])
-
-  def GetClasses(self):
-    """The assumtion is that the set of classes never changes."""
-    if not self.classes:
-      self.classes = set()
-      for entry in self.entries:
-        if entry["class"]:  # might be None
-          self.classes.add(entry["class"])
-    return self.classes

Modified: csw/mgar/gar/v2/lib/python/opencsw_test.py
===================================================================
--- csw/mgar/gar/v2/lib/python/opencsw_test.py	2010-12-27 12:25:56 UTC (rev 12095)
+++ csw/mgar/gar/v2/lib/python/opencsw_test.py	2010-12-27 12:26:38 UTC (rev 12096)
@@ -47,28 +47,6 @@
 #FASPACD= none
 """
 
-PKGMAP_1 = """1 f cswcpsampleconf /etc/opt/csw/cups/cupsd.conf.CSW 0644 root bin 4053 20987 1264420689
-"""
-
-PKGMAP_2 = """: 1 18128
-1 d none /etc/opt/csw/cups 0755 root bin
-1 f cswcpsampleconf /etc/opt/csw/cups/cupsd.conf.CSW 0644 root bin 4053 20987 1264420689
-1 f none /etc/opt/csw/cups/cupsd.conf.default 0640 root bin 4053 20987 1264420689
-1 d none /etc/opt/csw/cups/interfaces 0755 root bin
-1 d none /etc/opt/csw/cups/ppd 0755 root bin
-1 f cswinitsmf /etc/opt/csw/init.d/cswcups 0555 root bin 4547 14118 1264420798
-1 i depend 122 11155 1264524848
-1 i pkginfo 489 41685 1264524852
-1 i postremove 151 12419 1256302505
-1 i preinstall 1488 45678 125630250
-"""
-
-PKGMAP_3 = """1 d none /opt/csw/apache2/ap2mod 0755 root bin
-1 e build /opt/csw/apache2/ap2mod/suexec ? ? ? 1472 50478 1289099700
-1 d none /opt/csw/apache2/libexec 0755 root bin
-1 f none /opt/csw/apache2/libexec/mod_suexec.so 0755 root bin 6852 52597 1289092061
-"""
-
 SUBMITPKG_DATA_1 = {
     'NEW_PACKAGE': 'new package',
     'to': u'Release Manager <somebody at example.com>',
@@ -403,72 +381,6 @@
                                                repr(result)))
 
 
-class PkgmapUnitTest(unittest.TestCase):
-
-  def test_1(self):
-    pkgmap = opencsw.Pkgmap(PKGMAP_1.splitlines())
-    expected = [
-        {
-            'group': 'bin',
-            'user':  'root',
-            'path':  '/etc/opt/csw/cups/cupsd.conf.CSW',
-            'line':  '1 f cswcpsampleconf /etc/opt/csw/cups/cupsd.conf.CSW 0644 root bin 4053 20987 1264420689',
-            'type':  'f',
-            'class': 'cswcpsampleconf',
-            'mode':  '0644'
-        }
-    ]
-    self.assertEqual(expected, pkgmap.entries)
-
-  def test_2(self):
-    pkgmap = opencsw.Pkgmap(PKGMAP_2.splitlines())
-    line = ": 1 18128"
-    self.assertTrue(line in pkgmap.entries_by_line)
-
-  def test_3(self):
-    pkgmap = opencsw.Pkgmap(PKGMAP_2.splitlines())
-    self.assertTrue("cswcpsampleconf" in pkgmap.entries_by_class)
-
-  def test_4(self):
-    pkgmap = opencsw.Pkgmap(PKGMAP_3.splitlines())
-    self.assertTrue("build" in pkgmap.entries_by_class)
-
-  def testPkgmapSortedByPaths(self):
-    pkgmap = opencsw.Pkgmap(PKGMAP_2.splitlines())
-    paths = [x["path"] for x in pkgmap.entries]
-    self.assertEquals(paths, sorted(paths))
-
-class IndexByUnitTest(unittest.TestCase):
-
-  def testIndexDictsBy_1(self):
-    list_of_dicts = [
-        {"a": 1},
-        {"a": 2},
-        {"a": 3},
-    ]
-    expected = {
-        1: [{'a': 1}],
-        2: [{'a': 2}],
-        3: [{'a': 3}],
-    }
-    self.assertEquals(expected, opencsw.IndexDictsBy(list_of_dicts, "a"))
-
-  def testIndexDictsBy_2(self):
-    list_of_dicts = [
-        {"a": 1, "b": 1},
-        {"a": 1, "b": 2},
-        {"a": 1, "b": 3},
-    ]
-    expected = {
-        1: [
-          {'a': 1, 'b': 1},
-          {'a': 1, 'b': 2},
-          {'a': 1, 'b': 3},
-        ]
-    }
-    self.assertEquals(expected, opencsw.IndexDictsBy(list_of_dicts, "a"))
-
-
 class SubmitpkgTemplateUnitTest(unittest.TestCase):
 
   def testTypicalUpgradeStrict(self):

Modified: csw/mgar/gar/v2/lib/python/package_stats_test.py
===================================================================
--- csw/mgar/gar/v2/lib/python/package_stats_test.py	2010-12-27 12:25:56 UTC (rev 12095)
+++ csw/mgar/gar/v2/lib/python/package_stats_test.py	2010-12-27 12:26:38 UTC (rev 12096)
@@ -13,6 +13,7 @@
 import inspective_package
 import opencsw
 import datetime
+import pkgmap
 
 from testdata.tree_stats import pkgstats as tree_stats
 from testdata.neon_stats import pkgstats as neon_stats
@@ -37,7 +38,7 @@
     mock_dirpkg = self.mox.CreateMock(
         inspective_package.InspectivePackage)
     mock_pkgmap = self.mox.CreateMock(
-        opencsw.Pkgmap)
+        pkgmap.Pkgmap)
     mock_srv4.GetInspectivePkg().AndReturn(mock_dirpkg)
     mock_srv4.pkg_path = "/tmp/foo-1.2,REV=1234.12.11-SunOS5.8-sparc-CSW.pkg.gz"
     mock_dirpkg.pkgname = "MOCKpkgname"

Added: csw/mgar/gar/v2/lib/python/pkgmap.py
===================================================================
--- csw/mgar/gar/v2/lib/python/pkgmap.py	                        (rev 0)
+++ csw/mgar/gar/v2/lib/python/pkgmap.py	2010-12-27 12:26:38 UTC (rev 12096)
@@ -0,0 +1,95 @@
+#!/usr/bin/env python2.6
+
+import re
+import struct_util
+
+class Pkgmap(object):
+  """Represents the pkgmap of the package.
+
+  The plan:
+
+    entries = [
+      {
+        'path': ...,
+        'class': ...,
+        (more fields?)
+      }, ...
+    ]
+
+  + indexes
+  """
+  ENTRY_TYPES = {
+      "1": "header (?)",
+      "d": "directory",
+      "f": "file",
+      "s": "symlink",
+      "l": "link",
+      "i": "script",
+      "e": "editable file"
+  }
+
+  def __init__(self, input, permissions=False,
+               strip=None):
+    self.paths = set()
+    self.analyze_permissions = permissions
+    self.entries = []
+    self.classes = None
+    for line in input:
+      fields = re.split(r'\s+', line)
+      if strip:
+        strip_re = re.compile(r"^%s" % strip)
+        fields = [re.sub(strip_re, "", x) for x in fields]
+      line_to_add = None
+      installed_path = None
+      prototype_class = None
+      line_type = fields[1]
+      mode = None
+      user = None
+      group = None
+      if len(fields) < 2:
+        continue
+      elif line_type in ('f', 'd'):
+        # Files and directories
+        line_to_add = fields[3]
+        installed_path = fields[3]
+        prototype_class = fields[2]
+        if self.analyze_permissions:
+          line_to_add += " %s %s %s" % tuple(fields[4:7])
+        mode, user, group = fields[4:7]
+      elif line_type in ('e'):
+        # Editable files
+        line_to_add = fields[3]
+        installed_path = fields[3]
+        prototype_class = fields[2]
+      elif line_type in ('s', 'l'):
+        # soft- and hardlinks
+        link_from, link_to = fields[3].split("=")
+        installed_path = link_from
+        line_to_add = "%s --> %s" % (link_from, link_to)
+        prototype_class = fields[2]
+      if line_to_add:
+        self.paths.add(line_to_add)
+      entry = {
+          "line": line.strip(),
+          "type": line_type,
+      }
+      entry["path"] = installed_path
+      entry["class"] = prototype_class
+      entry["mode"] = mode
+      entry["user"] = user
+      entry["group"] = group
+      self.entries.append(entry)
+    self.entries_by_line = struct_util.IndexDictsBy(self.entries, "line")
+    self.entries_by_type = struct_util.IndexDictsBy(self.entries, "type")
+    self.entries_by_class = struct_util.IndexDictsBy(self.entries, "class")
+    self.entries_by_path = struct_util.IndexDictsBy(self.entries, "path")
+    self.entries = sorted(self.entries, key=lambda x: x["path"])
+
+  def GetClasses(self):
+    """The assumtion is that the set of classes never changes."""
+    if not self.classes:
+      self.classes = set()
+      for entry in self.entries:
+        if entry["class"]:  # might be None
+          self.classes.add(entry["class"])
+    return self.classes

Added: csw/mgar/gar/v2/lib/python/pkgmap_test.py
===================================================================
--- csw/mgar/gar/v2/lib/python/pkgmap_test.py	                        (rev 0)
+++ csw/mgar/gar/v2/lib/python/pkgmap_test.py	2010-12-27 12:26:38 UTC (rev 12096)
@@ -0,0 +1,65 @@
+#!/usr/bin/env python2.6
+
+import unittest
+import pkgmap
+
+PKGMAP_1 = """1 f cswcpsampleconf /etc/opt/csw/cups/cupsd.conf.CSW 0644 root bin 4053 20987 1264420689
+"""
+
+PKGMAP_2 = """: 1 18128
+1 d none /etc/opt/csw/cups 0755 root bin
+1 f cswcpsampleconf /etc/opt/csw/cups/cupsd.conf.CSW 0644 root bin 4053 20987 1264420689
+1 f none /etc/opt/csw/cups/cupsd.conf.default 0640 root bin 4053 20987 1264420689
+1 d none /etc/opt/csw/cups/interfaces 0755 root bin
+1 d none /etc/opt/csw/cups/ppd 0755 root bin
+1 f cswinitsmf /etc/opt/csw/init.d/cswcups 0555 root bin 4547 14118 1264420798
+1 i depend 122 11155 1264524848
+1 i pkginfo 489 41685 1264524852
+1 i postremove 151 12419 1256302505
+1 i preinstall 1488 45678 125630250
+"""
+
+PKGMAP_3 = """1 d none /opt/csw/apache2/ap2mod 0755 root bin
+1 e build /opt/csw/apache2/ap2mod/suexec ? ? ? 1472 50478 1289099700
+1 d none /opt/csw/apache2/libexec 0755 root bin
+1 f none /opt/csw/apache2/libexec/mod_suexec.so 0755 root bin 6852 52597 1289092061
+"""
+
+class PkgmapUnitTest(unittest.TestCase):
+
+  def test_1(self):
+    pm = pkgmap.Pkgmap(PKGMAP_1.splitlines())
+    expected = [
+        {
+            'group': 'bin',
+            'user':  'root',
+            'path':  '/etc/opt/csw/cups/cupsd.conf.CSW',
+            'line':  '1 f cswcpsampleconf /etc/opt/csw/cups/cupsd.conf.CSW 0644 root bin 4053 20987 1264420689',
+            'type':  'f',
+            'class': 'cswcpsampleconf',
+            'mode':  '0644'
+        }
+    ]
+    self.assertEqual(expected, pm.entries)
+
+  def test_2(self):
+    pm = pkgmap.Pkgmap(PKGMAP_2.splitlines())
+    line = ": 1 18128"
+    self.assertTrue(line in pm.entries_by_line)
+
+  def test_3(self):
+    pm = pkgmap.Pkgmap(PKGMAP_2.splitlines())
+    self.assertTrue("cswcpsampleconf" in pm.entries_by_class)
+
+  def test_4(self):
+    pm = pkgmap.Pkgmap(PKGMAP_3.splitlines())
+    self.assertTrue("build" in pm.entries_by_class)
+
+  def testPkgmapSortedByPaths(self):
+    pm = pkgmap.Pkgmap(PKGMAP_2.splitlines())
+    paths = [x["path"] for x in pm.entries]
+    self.assertEquals(paths, sorted(paths))
+
+
+if __name__ == '__main__':
+  unittest.main()


Property changes on: csw/mgar/gar/v2/lib/python/pkgmap_test.py
___________________________________________________________________
Added: svn:executable
   + *

Added: csw/mgar/gar/v2/lib/python/struct_util.py
===================================================================
--- csw/mgar/gar/v2/lib/python/struct_util.py	                        (rev 0)
+++ csw/mgar/gar/v2/lib/python/struct_util.py	2010-12-27 12:26:38 UTC (rev 12096)
@@ -0,0 +1,13 @@
+"""A module for generic data structure processing functions.
+"""
+
+def IndexDictsBy(list_of_dicts, field_key):
+  """Creates an index of list of dictionaries by a field name.
+
+  Returns a dictionary of lists.
+  """
+  index = {}
+  for d in list_of_dicts:
+    index.setdefault(d[field_key], [])
+    index[d[field_key]].append(d)
+  return index

Added: csw/mgar/gar/v2/lib/python/struct_util_test.py
===================================================================
--- csw/mgar/gar/v2/lib/python/struct_util_test.py	                        (rev 0)
+++ csw/mgar/gar/v2/lib/python/struct_util_test.py	2010-12-27 12:26:38 UTC (rev 12096)
@@ -0,0 +1,36 @@
+import unittest
+import struct_util
+
+class IndexByUnitTest(unittest.TestCase):
+
+  def testIndexDictsBy_1(self):
+    list_of_dicts = [
+        {"a": 1},
+        {"a": 2},
+        {"a": 3},
+    ]
+    expected = {
+        1: [{'a': 1}],
+        2: [{'a': 2}],
+        3: [{'a': 3}],
+    }
+    self.assertEquals(expected, struct_util.IndexDictsBy(list_of_dicts, "a"))
+
+  def testIndexDictsBy_2(self):
+    list_of_dicts = [
+        {"a": 1, "b": 1},
+        {"a": 1, "b": 2},
+        {"a": 1, "b": 3},
+    ]
+    expected = {
+        1: [
+          {'a': 1, 'b': 1},
+          {'a': 1, 'b': 2},
+          {'a': 1, 'b': 3},
+        ]
+    }
+    self.assertEquals(expected, struct_util.IndexDictsBy(list_of_dicts, "a"))
+
+
+if __name__ == '__main__':
+	unittest.main()

Modified: csw/mgar/gar/v2/tests/run_tests.py
===================================================================
--- csw/mgar/gar/v2/tests/run_tests.py	2010-12-27 12:25:56 UTC (rev 12095)
+++ csw/mgar/gar/v2/tests/run_tests.py	2010-12-27 12:26:38 UTC (rev 12096)
@@ -23,7 +23,9 @@
 from package_stats_test      import *
 from package_test            import *
 from pkgdb_test              import *
+from pkgmap_test             import *
 from sharedlib_utils_test    import *
+from struct_util_test        import *
 from submit_to_newpkgs_test  import *
 from system_pkgmap_test      import *
 from tag_test                import *


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