commsfwutils/commsbufs/mbufgobblerlayer/updatecontentandfilenames.py
branchRCL_3
changeset 84 486e9e9c45a7
parent 76 576874e13a2c
child 85 7c25be0307fe
equal deleted inserted replaced
76:576874e13a2c 84:486e9e9c45a7
     1 import ConfigParser
       
     2 ##import io
       
     3 import sys
       
     4 import os
       
     5 import re
       
     6 import string
       
     7 import shutil
       
     8 
       
     9 active=1
       
    10 iniFileName='updatecontentandfilenames.ini'
       
    11 
       
    12 def AddSearchReplacePair(src,dst):
       
    13 	print "Will replace\t"+src+"  with\t"+dst
       
    14 	search_replace[src] = dst
       
    15 
       
    16 def DieAndWait(string):
       
    17 	print string
       
    18 	sys.exit()
       
    19 	
       
    20 def TranslateString(string):
       
    21 #	out = string
       
    22 #	print "\ntranslating: "+string
       
    23 	for src in search_replace.keys():
       
    24 		dest = search_replace[src]
       
    25 #		print "src: "+src+" dest: "+dest
       
    26 		string = string.replace(src,dest)
       
    27 #		print "out: "+out
       
    28 	return string
       
    29 
       
    30 	
       
    31 def RenameIfNecessary(root,file):
       
    32 	thisdest = TranslateString(file)
       
    33 	if(thisdest != file):
       
    34 		print "Renaming ["+file+"] to ["+thisdest+"]"
       
    35 		if active:
       
    36 			os.rename(os.path.join(root,file),os.path.join(root,thisdest))
       
    37 
       
    38 def CorrectFile(file):
       
    39 	# ini file is special
       
    40 	if(file.upper() == iniFileName.upper()):
       
    41 		CorrectIniFile(file)
       
    42 	else:
       
    43 		if(FileContainsSrc(file)):
       
    44 			print "Modifying ["+file+"]"
       
    45 			TranslateFile(file);
       
    46 
       
    47 def FileContainsSrc(file):
       
    48 #	print "looking in ["+file+"]"
       
    49 	f = open(file)
       
    50 	found = 0
       
    51 	for line in f:
       
    52 #		print "[[["+line
       
    53 		for src in search_replace.keys():
       
    54 			if(-1 != string.find(line,src)):
       
    55 				found = 1
       
    56 #				print "\n'"+src+"' found in "+line+".. so.."
       
    57 				break
       
    58 		if(found):
       
    59 			break
       
    60 	f.close()
       
    61 	return found
       
    62 
       
    63 def TranslateFile(file):
       
    64 	if (active == 0):
       
    65 		return
       
    66 	shutil.copyfile(file,file+".bak")
       
    67 	i = open(file+".bak")
       
    68 	o = open(file,"w+")
       
    69 	for line in i:
       
    70 		out=TranslateString(line)
       
    71 		o.write(out)
       
    72 	i.close()
       
    73 	o.close()
       
    74 
       
    75 def CorrectIniFile(file):
       
    76 	if (active == 0):
       
    77 		return
       
    78 	print "Correcting ini file "+file+" .."
       
    79 	shutil.copyfile(file,file+".bak")
       
    80 	i = open(file+".bak")
       
    81 	o = open(file,"w+")
       
    82 	searchfield=''
       
    83 	for line in i:
       
    84 		ms=re.match('^\s*Search\s*=\s*(.+)',line,flags=re.IGNORECASE)
       
    85 		mr=re.match('^\s*Replace\s*=\s*(.+)',line,flags=re.IGNORECASE)
       
    86 		if(ms):
       
    87 			if(searchfield != ''):
       
    88 				print "Multiple search sections found before a replace. fix config file manually"
       
    89 				sys.exit()
       
    90 			searchfield = ms.group(1)
       
    91 		elif(mr):
       
    92 			if(searchfield == ''):
       
    93 				print "Replace section found before search section. fix config file manually"
       
    94 				sys.exit()
       
    95 			o.write("Search="+mr.group(1)+"\nReplace=CHANGEME\n")
       
    96 			searchfield=''
       
    97 		else:
       
    98 			o.write(line)
       
    99 	i.close()
       
   100 	o.close()
       
   101 
       
   102 
       
   103 
       
   104 
       
   105 config = ConfigParser.ConfigParser()
       
   106 config.readfp(open(iniFileName))
       
   107 
       
   108 print "1. Figuring out search/replace pairs.."
       
   109 
       
   110 search_replace = {} # empty map
       
   111 
       
   112 for section in config.sections():
       
   113 	src = config.get(section,'Search')
       
   114 	if(src == ""):
       
   115 		DieAndWait("Error in ini file section '"+section+"': Must specify Search field")
       
   116 	if(src in search_replace):
       
   117 		DieAndWait("Error in ini file section '"+section+"': Search field '"+src+"' specified more than once")
       
   118 	dest = config.get(section,'Replace')
       
   119 	if(dest == ""):
       
   120 		DieAndWait("Error in ini file section '"+section+"': Must specify Replace field")
       
   121 	if(dest == "CHANGEME"):
       
   122 		DieAndWait("Error in ini file section '"+section+"': Must specify value to replace '"+src+"' with")
       
   123 	
       
   124 	AddSearchReplacePair(src,dest)
       
   125 	
       
   126 	# deal with upper / lower case variants
       
   127 	ucsrc = src.upper()
       
   128 	lcsrc = src.lower()
       
   129 	lcapsrc = ucsrc[:1]+lcsrc[1:]
       
   130 	ucdest = dest.upper()
       
   131 	lcdest = dest.lower()
       
   132 	lcapdest = ucdest[:1]+lcdest[1:]
       
   133 
       
   134 	if(ucsrc != src):
       
   135 		AddSearchReplacePair(ucsrc,ucdest)
       
   136 	if(lcsrc != src):
       
   137 		AddSearchReplacePair(lcsrc,lcdest)
       
   138 	if(lcapsrc != src and lcapsrc != lcsrc):
       
   139 		AddSearchReplacePair(lcapsrc,lcapdest)
       
   140 
       
   141 print "-----"
       
   142 print "2. Scanning for read-only files.."
       
   143   
       
   144 readOnlyFiles=0;
       
   145 for root, dirs, files in os.walk("",topdown=False):
       
   146 	for file in files:
       
   147 #		print "Testing readable  : "+root+file
       
   148 		try:
       
   149 			fh = open(os.path.join(root,file),"a+")
       
   150 			fh.close()
       
   151 		except:
       
   152 			print "Read-only file detected: "+os.path.join(root,file)
       
   153 			readOnlyFiles=1
       
   154 
       
   155 if(readOnlyFiles):
       
   156 	print "Can't operate with read-only files present."
       
   157 	sys.exit()
       
   158 else:
       
   159 	print "No read-only files present. Proceeding to search/replace.."
       
   160 
       
   161 
       
   162 print "-----"
       
   163 print "3. Renaming files and folders.."
       
   164 
       
   165 for root, dirs, files in os.walk("",topdown=False):
       
   166 #	print "ROOT: "+root
       
   167 	for dir in dirs:
       
   168 		RenameIfNecessary(root,dir)
       
   169 	for file in files:
       
   170 		RenameIfNecessary(root,file)
       
   171 
       
   172 
       
   173 print "-----"
       
   174 print "4. Search/replacing in files.."
       
   175 
       
   176 for root, dirs, files in os.walk("",topdown=False):
       
   177 	for file in files:
       
   178 		CorrectFile(os.path.join(root,file))
       
   179 
       
   180 print "-----"
       
   181 print "Done."