| 86 | ) |
| 87 | |
| 88 | def doOperation( self, operands ) : |
| 89 | |
| 90 | source = operands["source"].value |
| 91 | destination = operands["destination"].value |
| 92 | |
| 93 | searchFor = operands["searchFor"].value |
| 94 | if not operands["regexpSearch"] : |
| 95 | searchFor = re.escape( searchFor ) |
| 96 | |
| 97 | replaceWith = operands["replaceWith"].value |
| 98 | |
| 99 | inFileStat = os.stat( source ).st_mode |
| 100 | |
| 101 | inFile = open( source, "r" ) |
| 102 | |
| 103 | tmpDestination = None |
| 104 | if source == destination : |
| 105 | fd, tmpDestination = tempfile.mkstemp() |
| 106 | outFile = os.fdopen( fd, "w" ) |
| 107 | else : |
| 108 | outFile = open( destination, "w" ) |
| 109 | |
| 110 | inLine = inFile.readline() |
| 111 | while inLine : |
| 112 | |
| 113 | outLine = re.sub( searchFor, replaceWith, inLine ) |
| 114 | outFile.write( outLine ) |
| 115 | |
| 116 | inLine = inFile.readline() |
| 117 | |
| 118 | inFile.close() |
| 119 | outFile.close() |
| 120 | |
| 121 | if tmpDestination : |
| 122 | |
| 123 | shutil.move( destination, destination + ".bak" ) |
| 124 | shutil.move( tmpDestination, destination ) |
| 125 | |
| 126 | os.chmod( destination, inFileStat ) |
| 127 | |
| 128 | return IECore.StringData( destination ) |
| 129 | |
| 130 | IECore.registerRunTimeTyped( SearchReplaceOp ) |