summaryrefslogtreecommitdiffstats
path: root/meta/lib/oeqa/utils/ftools.py
diff options
context:
space:
mode:
Diffstat (limited to 'meta/lib/oeqa/utils/ftools.py')
-rw-r--r--meta/lib/oeqa/utils/ftools.py37
1 files changed, 30 insertions, 7 deletions
diff --git a/meta/lib/oeqa/utils/ftools.py b/meta/lib/oeqa/utils/ftools.py
index 64ebe3d217..3093419cc7 100644
--- a/meta/lib/oeqa/utils/ftools.py
+++ b/meta/lib/oeqa/utils/ftools.py
@@ -1,12 +1,23 @@
+#
+# SPDX-License-Identifier: MIT
+#
+
import os
import re
+import errno
def write_file(path, data):
+ # In case data is None, return immediately
+ if data is None:
+ return
wdata = data.rstrip() + "\n"
with open(path, "w") as f:
f.write(wdata)
def append_file(path, data):
+ # In case data is None, return immediately
+ if data is None:
+ return
wdata = data.rstrip() + "\n"
with open(path, "a") as f:
f.write(wdata)
@@ -18,10 +29,22 @@ def read_file(path):
return data
def remove_from_file(path, data):
- lines = read_file(path).splitlines()
- rmdata = data.strip().splitlines()
- for l in rmdata:
- for c in range(0, lines.count(l)):
- i = lines.index(l)
- del(lines[i])
- write_file(path, "\n".join(lines))
+ # In case data is None, return immediately
+ if data 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
+
+ contents = rdata.strip().splitlines()
+ for r in data.strip().splitlines():
+ try:
+ contents.remove(r)
+ except ValueError:
+ pass
+ write_file(path, "\n".join(contents))