summaryrefslogtreecommitdiffstats
path: root/scripts/lib/devtool/__init__.py
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/lib/devtool/__init__.py')
-rw-r--r--scripts/lib/devtool/__init__.py36
1 files changed, 36 insertions, 0 deletions
diff --git a/scripts/lib/devtool/__init__.py b/scripts/lib/devtool/__init__.py
index b231e46b16..14170cb69e 100644
--- a/scripts/lib/devtool/__init__.py
+++ b/scripts/lib/devtool/__init__.py
@@ -261,3 +261,39 @@ def get_bbclassextend_targets(recipefile, pn):
261 targets.append('%s-%s' % (pn, variant)) 261 targets.append('%s-%s' % (pn, variant))
262 return targets 262 return targets
263 263
264def replace_from_file(path, old, new):
265 """Replace strings on a file"""
266
267 def read_file(path):
268 data = None
269 with open(path) as f:
270 data = f.read()
271 return data
272
273 def write_file(path, data):
274 if data is None:
275 return
276 wdata = data.rstrip() + "\n"
277 with open(path, "w") as f:
278 f.write(wdata)
279
280 # In case old is None, return immediately
281 if old is None:
282 return
283 try:
284 rdata = read_file(path)
285 except IOError as e:
286 # if file does not exit, just quit, otherwise raise an exception
287 if e.errno == errno.ENOENT:
288 return
289 else:
290 raise
291
292 old_contents = rdata.splitlines()
293 new_contents = []
294 for old_content in old_contents:
295 try:
296 new_contents.append(old_content.replace(old, new))
297 except ValueError:
298 pass
299 write_file(path, "\n".join(new_contents))