def replace_test(self): f = tempfile.NamedTemporaryFile(mode="w+t", delete=False) f.write("A few words to apply @AARDVARKS@ testing\n") f.close() replace(f.name, "@AARDVARKS@", "ant eaters") self.assertEqual(open(f.name).readline(), "A few words to apply ant eaters testing\n") os.unlink(f.name)
def replace(self, pat, repl, *fileglobs): ''' replace PATTERN REPLACEMENT FILEGLOB [FILEGLOB ...] Find-and-replace the given PATTERN (Python-style regex) with the given REPLACEMENT string for each of the files listed. Example: replace @VERSION@ ${product.version} /boot/grub.conf /boot/isolinux.cfg ''' match = False for g in fileglobs: for f in rglob(self._out(g)): match = True replace(f, pat, repl) if not match: raise IOError("no files matched %s" % " ".join(fileglobs))