[Treepkg-commits] r36 - in trunk: enterprise test treepkg

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Wed Mar 19 20:50:32 CET 2008


Author: bh
Date: 2008-03-19 20:50:32 +0100 (Wed, 19 Mar 2008)
New Revision: 36

Added:
   trunk/test/test_util.py
Modified:
   trunk/enterprise/kdepim.py
   trunk/test/filesupport.py
   trunk/treepkg/util.py
Log:
Move the sed-like replacement code from enterprise/kdepim.py to treepkg/util.py

This creates a new function treepkg.util.replace_in_file with some tests
in test/test_util.py and new test support code in test/filesupport.py.
Also, adapt enterprise/kdepim.py to use the new function.


Modified: trunk/enterprise/kdepim.py
===================================================================
--- trunk/enterprise/kdepim.py	2008-03-19 15:00:54 UTC (rev 35)
+++ trunk/enterprise/kdepim.py	2008-03-19 19:50:32 UTC (rev 36)
@@ -1,4 +1,4 @@
-# Copyright (C) 2007 by Intevation GmbH
+# Copyright (C) 2007, 2008 by Intevation GmbH
 # Authors:
 # Bernhard Herzog <bh at intevation.de>
 #
@@ -44,12 +44,9 @@
         versionstring = "(enterprise %s)" % self.enterprise_version
         for versionfile in ["kmail/kmversion.h", "kontact/src/main.cpp",
                             "korganizer/version.h"]:
-            filename = os.path.join(pkgbasedir, versionfile)
-            patched = re.sub("\(enterprise ([^)]*)\)", versionstring,
-                              open(filename).read())
-            f = open(filename, "w")
-            f.write(patched)
-            f.close()
+            treepkg.util.replace_in_file(os.path.join(pkgbasedir, versionfile),
+                                         "\(enterprise ([^)]*)\)",
+                                         versionstring)
 
     def do_package(self):
         pkgbaseversion, pkgbasedir = self.export_sources()

Modified: trunk/test/filesupport.py
===================================================================
--- trunk/test/filesupport.py	2008-03-19 15:00:54 UTC (rev 35)
+++ trunk/test/filesupport.py	2008-03-19 19:50:32 UTC (rev 36)
@@ -90,3 +90,7 @@
                 self.create_files(newdir, contents)
             else:
                 writefile(os.path.join(directory, name), contents)
+
+    def checkFileContents(self, filename, contents):
+        """check the contents of a file"""
+        self.assertEquals(open(filename).read(), contents)

Added: trunk/test/test_util.py
===================================================================
--- trunk/test/test_util.py	2008-03-19 15:00:54 UTC (rev 35)
+++ trunk/test/test_util.py	2008-03-19 19:50:32 UTC (rev 36)
@@ -0,0 +1,35 @@
+# Copyright (C) 2007, 2008 by Intevation GmbH
+# Authors:
+# Bernhard Herzog <bh at intevation.de>
+#
+# This program is free software under the GPL (>=v2)
+# Read the file COPYING coming with the software for details.
+
+"""Tests for the treepkg.util module"""
+
+import unittest
+
+from filesupport import FileTestMixin
+
+from treepkg.util import replace_in_file
+
+
+class TestReplaceInFile(unittest.TestCase, FileTestMixin):
+
+    def runtest(self, orig_contents, expected_contents, pattern, replacement):
+        filename = self.create_temp_file(self.id(), orig_contents)
+        replace_in_file(filename, pattern, replacement)
+        self.checkFileContents(filename, expected_contents)
+
+    def test_version_replacement(self):
+        template = ("project foo version 1.0-svn%(rev)d"
+                    "Some filler"
+                    "text that sometimes"
+                    "looks similar to the pattern"
+                    "1.0-"
+                    "foo 1.2-svn2"
+                    "echo foo version 1.0-svn%(rev)d"
+                    ""
+                    "and more filler")
+        self.runtest(template % dict(rev=0), template % dict(rev=321),
+                     r"1\.0-svn0", "1.0-svn321")


Property changes on: trunk/test/test_util.py
___________________________________________________________________
Name: svn:keywords
   + Id Revision
Name: svn:eol-style
   + native

Modified: trunk/treepkg/util.py
===================================================================
--- trunk/treepkg/util.py	2008-03-19 15:00:54 UTC (rev 35)
+++ trunk/treepkg/util.py	2008-03-19 19:50:32 UTC (rev 36)
@@ -8,6 +8,7 @@
 """Collection of functions that didn't fit elsewhere"""
 
 import os
+import re
 import tempfile
 import shutil
 
@@ -114,3 +115,17 @@
     finally:
         if os.path.exists(tempname):
             os.remove(tempname)
+
+
+def replace_in_file(filename, pattern, replacement):
+    """Replace all occurrences of pattern in a file with replacement.
+    The file is modified in place.  The search and replace is done with
+    the re.sub function.  The pattern and replacement parameter are passed
+    through to re.sub unmodified, so their semantics are determined by
+    re.sub.
+    """
+    contents = open(filename).read()
+    modified = re.sub(pattern, replacement, contents)
+    f = open(filename, "w")
+    f.write(modified)
+    f.close()



More information about the Treepkg-commits mailing list