Commit 738ffeaa authored by Iikka Eklund's avatar Iikka Eklund
Browse files

Add utility function in bldinstallercommon.py


Add string replace utility function in bldinstallercommon.py

Change-Id: I1f13fed28b34dd947d47592b9bab39158c08984a
Reviewed-by: default avatarLars Knoll <lars.knoll@digia.com>
Reviewed-by: default avatarSimo Fält <simo.falt@digia.com>
parent 4648819b
No related merge requests found
Showing with 33 additions and 0 deletions
...@@ -54,6 +54,7 @@ import tarfile ...@@ -54,6 +54,7 @@ import tarfile
import urllib2 import urllib2
import zipfile import zipfile
import string import string
import fileinput
# need to include this for win platforms as long path names # need to include this for win platforms as long path names
# cause problems # cause problems
...@@ -339,6 +340,38 @@ def replace_in_files(filelist, regexp, replacement_string): ...@@ -339,6 +340,38 @@ def replace_in_files(filelist, regexp, replacement_string):
write_file.close() write_file.close()
###############################
# function
###############################
def replace_in_text_files(root_directory, match_string, replacement_string, file_type_ignore_list):
print '------------ replace_in_text_files ----------------'
print ' root_directory: ' + root_directory
print ' match_string: ' + match_string
print ' replacement_string: ' + replacement_string
pattern = re.compile(match_string)
for root, dirs, files in os.walk(root_directory):
for name in files:
path = os.path.join(root, name)
if not os.path.isdir(path) and not os.path.islink(path):
if not (any(name.endswith(item) for item in file_type_ignore_list)):
readlines=open(path,'r').read()
if pattern.search(readlines):
print '---> Regexp match: ' + path
if is_text_file(path):
print '---> Replacing build path in: ' + path
print '---> String to match: ' + match_string
print '---> Replacement: ' + replacement_string
for line in fileinput.FileInput(path,inplace=1):
output1 = line.replace(match_string, replacement_string)
if line != output1:
# we had a match
print output1.rstrip('\n')
else:
# no match so write original line back to file
print line.rstrip('\n')
print '--------------------------------------------------------------------'
############################### ###############################
# function # function
############################### ###############################
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment