[csw-devel] SF.net SVN: gar:[8715] csw/mgar/gar/v2/lib/python

wahwah at users.sourceforge.net wahwah at users.sourceforge.net
Sun Feb 21 13:18:05 CET 2010


Revision: 8715
          http://gar.svn.sourceforge.net/gar/?rev=8715&view=rev
Author:   wahwah
Date:     2010-02-21 12:18:05 +0000 (Sun, 21 Feb 2010)

Log Message:
-----------
mGAR v2: submitpkg, ported to use Cheetah templates, added unit tests

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

Modified: csw/mgar/gar/v2/lib/python/opencsw.py
===================================================================
--- csw/mgar/gar/v2/lib/python/opencsw.py	2010-02-21 11:32:29 UTC (rev 8714)
+++ csw/mgar/gar/v2/lib/python/opencsw.py	2010-02-21 12:18:05 UTC (rev 8715)
@@ -25,6 +25,7 @@
 import tempfile
 import urllib2
 import checkpkg
+from Cheetah import Template
 
 ARCHITECTURES = ["i386", "sparc", "all"]
 MAJOR_VERSION = "major version"
@@ -57,18 +58,32 @@
 proxy=
 """
 
-EMAIL_TMPL = """From: %(from)s
-To: %(to)s
-%(optional_cc)sDate: %(date)s
-Subject: newpkgs %(pkgnames)s
+SUBMITPKG_TMPL = """From: $from
+To: $to
+#if $cc
+Cc: $cc
+#end if
+Subject: $subject
 
-%(body)s
+#for $pkg_group in $pkg_groups
+#if $pkg_group.upgrade_type == $NEW_PACKAGE
+* $pkg_group.name: new package
+#elif $pkg_group.upgrade_type == $NO_VERSION_CHANGE
+* WARNING: no version change of $pkg_group.name
+#else
+* $pkg_group.name: $pkg_group.upgrade_type upgrade
+  - from: $pkg_group.versions[0]
+  -   to: $pkg_group.versions[1]
+#end if
+#for pkg in $pkg_group.pkgs
+  + $pkg.basename
+#end for
 
+#end for
 -- 
-Generated by submitpkg, $Rev$.
+Generated by submitpkg
 """
 
-
 class Error(Exception):
   pass
 
@@ -278,12 +293,13 @@
       self.release_cc = unicode(release_cc)
 
   def FormatMail(self):
-    body_list = ["The following package files are ready to be released:"]
-    body_list.append("")
-    # Gathering package information, grouping packages that are upgraded
-    # together.
+    return self._FormatMail(self.paths, self.pkgnames, self.sender,
+                             self.release_mgr, self.release_cc)
+
+  def _GetPkgsData(self, paths):
+    """Gathering package info, grouping packages that are upgraded together."""
     pkgs_data = {}
-    for p in self.paths:
+    for p in paths:
       base_file_name = os.path.split(p)[1]
       catalogname = base_file_name.split("-")[0]
       pkg = CatalogBasedOpencswPackage(catalogname)
@@ -302,37 +318,38 @@
       pkg.cat_version_str = catalog_version_str
       pkg.new_version_str = new_version_str
       pkgs_data[pkgs_data_key].append(pkg)
+    return pkgs_data
+
+  def _FormatMail(self, paths, pkgnames, sender, release_mgr, release_cc):
+    pkgs_data = self._GetPkgsData(paths)
     # Formatting grouped packages:
+    pkg_groups = []
     for upgrade_type, upgrade_msg, versions in pkgs_data:
-      msg = []
+      pkg_group = {}
+      pkg_group["upgrade_type"] = upgrade_type
+      pkg_group["upgrade_msg"] = upgrade_msg
+      pkg_group["versions"] = versions
       pkgs = pkgs_data[(upgrade_type, upgrade_msg, versions)]
       group_name = CatalogNameGroupName([pkg.catalogname for pkg in pkgs])
-      if upgrade_type == NEW_PACKAGE:
-        msg.append("* %s: %s" % (upgrade_type, group_name))
-      elif upgrade_type == NO_VERSION_CHANGE:
-        msg.append("* WARNING: no version change of %s" % group_name)
-      else:
-        msg.append("* %s: %s upgrade" % (group_name, upgrade_type))
-        msg.append("  - from: %s" % versions[0])
-        msg.append("  -   to: %s" % versions[1])
-      for pkg in pkgs:
-        msg.append("  + %s" % os.path.split(pkg.srv4path)[1])
-      body_list.extend(msg)
-    body_list.append("")
-    body = "\n".join(body_list)
-    # TODO: This needs to be rewritten using Cheetah
-    d = {
-        'from': self.sender,
-        'to': self.release_mgr,
-        'optional_cc': '',
-        'pkgnames': ", ".join(self.pkgnames),
-        'body': body,
+      pkg_group["name"] = group_name
+      pkg_group["pkgs"] = [{'basename': os.path.basename(x.srv4path)} for x in pkgs]
+      pkg_groups.append(pkg_group)
+    subject = u"newpkgs %s" % (", ".join(pkgnames))
+    if len(subject) > 50:
+      subject = "%s(...)" % (subject[:45],)
+    # Cheetah
+    namespace = {
+        'from': sender,
+        'to': release_mgr,
+        'cc': release_cc,
+        'subject': subject,
         'date': datetime.datetime.now(),
+        'pkg_groups': pkg_groups,
+        'NEW_PACKAGE': NEW_PACKAGE,
+        'NO_VERSION_CHANGE': NO_VERSION_CHANGE,
     }
-    if self.release_cc:
-      d['optional_cc'] = "cc: %s\n" % self.release_cc
-    mail_text = EMAIL_TMPL % d
-    return mail_text
+    t = Template.Template(SUBMITPKG_TMPL, searchList=[namespace])
+    return unicode(t)
 
   def GetEditorName(self, env):
     editor = "/opt/csw/bin/vim"

Modified: csw/mgar/gar/v2/lib/python/opencsw_test.py
===================================================================
--- csw/mgar/gar/v2/lib/python/opencsw_test.py	2010-02-21 11:32:29 UTC (rev 8714)
+++ csw/mgar/gar/v2/lib/python/opencsw_test.py	2010-02-21 12:18:05 UTC (rev 8715)
@@ -1,10 +1,13 @@
 #!/opt/csw/bin/python2.6
 # $Id$
 
+import copy
+import datetime
 import opencsw
 import os.path
 import re
 import unittest
+from Cheetah import Template
 
 CATALOG_DATA_1 = """amavisd_new 2.6.3,REV=2009.04.23 CSWamavisdnew amavisd_new-2.6.3,REV=2009.04.23-SunOS5.8-all-CSW.pkg.gz 831f063d1ba20eb8bea0e0e60ceef3cb 813802 CSWperl|CSWcswclassutils|CSWpmunixsyslog|CSWpmiostringy|CSWpmnetserver|CSWpmmailtools|CSWpmmimetools|CSWpmcompresszlib|CSWpmarchivetar|CSWpmarchivezip|CSWspamassassin|CSWpmberkeleydb|CSWpmconverttnef|CSWpmconvertuulib|CSWpmmaildkim|CSWcommon none
 amsn 0.94 CSWamsn amsn-0.94-SunOS5.8-all-CSW.pkg.gz 99afd828dd38fb39a37cb8ffd448b098 2420919 CSWtcl|CSWtk|CSWtcltls none
@@ -60,11 +63,45 @@
 1 i preinstall 1488 45678 125630250
 """
 
+SUBMITPKG_DATA_1 = {
+    'NEW_PACKAGE': 'new package',
+    'to': u'Release Manager <somebody at example.com>',
+    'NO_VERSION_CHANGE': 'no version change',
+    'from': u'Maciej Blizinski <maintainer at example.com>',
+    'date': datetime.datetime(2010, 2, 21, 12, 52, 44, 295766),
+    'cc': None,
+    'pkg_groups': [
+      {'upgrade_msg': '1.5.2.2,REV=2009.09.04 --> 1.5.3,REV=2010.02.02',
+       'versions': ('1.5.2.2,REV=2009.09.04', '1.5.3,REV=2010.02.02'),
+       'pkgs': [
+         {'basename': 'tree-1.5.3,REV=2010.02.02-SunOS5.8-i386-CSW.pkg.gz'},
+         {'basename': 'tree-1.5.3,REV=2010.02.02-SunOS5.8-sparc-CSW.pkg.gz'}
+       ],
+       'name': 'tree',
+       'upgrade_type': opencsw.PATCHLEVEL,
+      }
+    ],
+    'subject': u'newpkgs tree'
+}
+SUBMITPKG_EXPECTED_1 = u"""From: Maciej Blizinski <maintainer at example.com>
+To: Release Manager <somebody at example.com>
+Subject: newpkgs tree
 
+* tree: patchlevel upgrade
+  - from: 1.5.2.2,REV=2009.09.04
+  -   to: 1.5.3,REV=2010.02.02
+  + tree-1.5.3,REV=2010.02.02-SunOS5.8-i386-CSW.pkg.gz
+  + tree-1.5.3,REV=2010.02.02-SunOS5.8-sparc-CSW.pkg.gz
+
+-- 
+Generated by submitpkg
+"""
+
 class ParsePackageFileNameTest(unittest.TestCase):
 
   def testParsePackageFileName1(self):
-    test_data = open(os.path.join(os.path.split(__file__)[0], "testdata/example-catalog.txt"))
+    test_data = open(os.path.join(os.path.split(__file__)[0],
+                                  "testdata/example-catalog.txt"))
     split_re = re.compile(r"\s+")
     for line in test_data:
       fields = re.split(split_re, line)
@@ -247,18 +284,18 @@
 
 
 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'
+            '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)
@@ -282,9 +319,9 @@
         {"a": 3},
     ]
     expected = {
-    	  1: [{'a': 1}],
-    	  2: [{'a': 2}],
-    	  3: [{'a': 3}],
+        1: [{'a': 1}],
+        2: [{'a': 2}],
+        3: [{'a': 3}],
     }
     self.assertEquals(expected, opencsw.IndexDictsBy(list_of_dicts, "a"))
 
@@ -304,5 +341,30 @@
     self.assertEquals(expected, opencsw.IndexDictsBy(list_of_dicts, "a"))
 
 
+class SubmitpkgTemplateUnitTest(unittest.TestCase):
+
+  def testTypicalUpgradeStrict(self):
+    """Strict testing of standard upgrade."""
+    t = Template.Template(opencsw.SUBMITPKG_TMPL,
+                          searchList=[SUBMITPKG_DATA_1])
+    self.assertEquals(SUBMITPKG_EXPECTED_1, unicode(t))
+
+  def testWarningOnNoChange(self):
+    """If there's no version change, a warning is needed."""
+    submitpkg_data = copy.deepcopy(SUBMITPKG_DATA_1)
+    submitpkg_data["pkg_groups"][0]["upgrade_type"] = opencsw.NO_VERSION_CHANGE
+    t = Template.Template(opencsw.SUBMITPKG_TMPL,
+                          searchList=[submitpkg_data])
+    self.assertTrue(re.search(r"WARNING", unicode(t)), unicode(t))
+
+  def testNewPackage(self):
+    """Tests for "new package" somewhere in the message."""
+    submitpkg_data = copy.deepcopy(SUBMITPKG_DATA_1)
+    submitpkg_data["pkg_groups"][0]["upgrade_type"] = opencsw.NEW_PACKAGE
+    t = Template.Template(opencsw.SUBMITPKG_TMPL,
+                          searchList=[submitpkg_data])
+    self.assertTrue(re.search(r"new package", unicode(t)), unicode(t))
+
+
 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