[oe-commits] [openembedded-core] 04/04: ftools: new function to replace strings inside a text file

git at git.openembedded.org git at git.openembedded.org
Fri Jun 9 16:12:51 UTC 2017


This is an automated email from the git hooks/post-receive script.

rpurdie pushed a commit to branch master-next
in repository openembedded-core.

commit 191c74303e0af0b466290f733321239acc500c87
Author: Leonardo Sandoval <leonardo.sandoval.gonzalez at linux.intel.com>
AuthorDate: Fri Jun 2 10:13:00 2017 -0700

    ftools: new function to replace strings inside a text file
    
    Signed-off-by: Leonardo Sandoval <leonardo.sandoval.gonzalez at linux.intel.com>
    Signed-off-by: Ross Burton <ross.burton at intel.com>
---
 meta/lib/oeqa/utils/ftools.py | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/meta/lib/oeqa/utils/ftools.py b/meta/lib/oeqa/utils/ftools.py
index a7233d4..8bf2547 100644
--- a/meta/lib/oeqa/utils/ftools.py
+++ b/meta/lib/oeqa/utils/ftools.py
@@ -44,3 +44,25 @@ def remove_from_file(path, data):
         except ValueError:
             pass
     write_file(path, "\n".join(contents))
+
+def replace_from_file(path, old, new):
+    # In case old is None, return immediately
+    if old is None:
+        return
+    try:
+        rdata = read_file(path)
+    except IOError as e:
+        # if file does not exit, just quit, otherwise raise an exception
+        if e.errno == errno.ENOENT:
+            return
+        else:
+            raise
+
+    old_contents = rdata.splitlines()
+    new_contents = []
+    for old_content in old_contents:
+        try:
+            new_contents.append(old_content.replace(old, new))
+        except ValueError:
+            pass
+    write_file(path, "\n".join(new_contents))

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the Openembedded-commits mailing list