-
Notifications
You must be signed in to change notification settings - Fork 1
/
searchReplace.py
584 lines (435 loc) · 17.8 KB
/
searchReplace.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
#
# Author: Fredrik Averpil, fredrik.averpil@gmail.com, http://fredrikaverpil.tumblr.com
#
''' Imports regardless of Qt type '''
''' --------------------------------------------------------------------------------------------------------------------------------------------------------- '''
import os, sys
import xml.etree.ElementTree as xml
from cStringIO import StringIO
import datetime, socket, fnmatch
''' CONFIGURATION '''
''' --------------------------------------------------------------------------------------------------------------------------------------------------------- '''
# General
QtType = 'PySide' # Edit this to switch between PySide and PyQt
sys.dont_write_bytecode = True # Do not generate .pyc files
uiFile = os.path.join(os.path.dirname(__file__), "searchReplace.ui") # The .ui file to load
windowTitle = 'Search and Replace' # The visible title of the window
windowObject = 'searchReplace' # The name of the window object
# Standalone settings
darkorange = False # Use the 'darkorange' stylesheet
# Maya settings
launchAsDockedWindow = False # False = opens as free floating window, True = docks window to Maya UI
# Nuke settings
launchAsPanel = False # False = opens as regular window, True = opens as panel
# Site-packages location:
site_packages_Win = '' # Location of site-packages containing PySide and pysideuic and/or PyQt and SIP
site_packages_Linux = '' # Location of site-packages containing PySide and pysideuic and/or PyQt and SIP
site_packages_OSX = '' # Location of site-packages containing PySide and pysideuic and/or PyQt and SIP
#site_packages_Win = 'C:/Python26/Lib/site-packages' # Example: Windows 7
#site_packages_Linux = '/usr/lib/python2.6/site-packages' # Example: Linux CentOS 6.4
#site_packages_OSX = '/Library/Python/2.7/site-packages' # Example: Mac OS X 10.8 Mountain Lion
''' Run mode '''
''' --------------------------------------------------------------------------------------------------------------------------------------------------------- '''
runMode = 'standalone'
try:
import maya.cmds as cmds
import maya.OpenMayaUI as omui
import shiboken
runMode = 'maya'
except:
pass
try:
import nuke
from nukescripts import panels
runMode = 'nuke'
except:
pass
''' PySide or PyQt '''
''' --------------------------------------------------------------------------------------------------------------------------------------------------------- '''
if (site_packages_Win != '') and ('win' in sys.platform): sys.path.append( site_packages_Win )
if (site_packages_Linux != '') and ('linux' in sys.platform): sys.path.append( site_packages_Linux )
if (site_packages_OSX != '') and ('darwin' in sys.platform): sys.path.append( site_packages_OSX )
if QtType == 'PySide':
from PySide import QtCore, QtGui, QtUiTools
import pysideuic
elif QtType == 'PyQt':
from PyQt4 import QtCore, QtGui, uic
import sip
print 'This app is now using ' + QtType
''' Auto-setup classes and functions '''
''' --------------------------------------------------------------------------------------------------------------------------------------------------------- '''
class PyQtFixer(QtGui.QMainWindow):
def __init__(self, parent=None):
"""Super, loadUi, signal connections"""
super(PyQtFixer, self).__init__(parent)
print 'Making a detour (hack), necessary for when using PyQt'
def loadUiType(uiFile):
"""
Pyside lacks the "loadUiType" command, so we have to convert the ui file to py code in-memory first
and then execute it in a special frame to retrieve the form_class.
"""
parsed = xml.parse(uiFile)
widget_class = parsed.find('widget').get('class')
form_class = parsed.find('class').text
with open(uiFile, 'r') as f:
o = StringIO()
frame = {}
if QtType == 'PySide':
pysideuic.compileUi(f, o, indent=0)
pyc = compile(o.getvalue(), '<string>', 'exec')
exec pyc in frame
#Fetch the base_class and form class based on their type in the xml from designer
form_class = frame['Ui_%s'%form_class]
base_class = eval('QtGui.%s'%widget_class)
elif QtType == 'PyQt':
form_class = PyQtFixer
base_class = QtGui.QMainWindow
return form_class, base_class
form, base = loadUiType(uiFile)
def wrapinstance(ptr, base=None):
"""
Utility to convert a pointer to a Qt class instance (PySide/PyQt compatible)
:param ptr: Pointer to QObject in memory
:type ptr: long or Swig instance
:param base: (Optional) Base class to wrap with (Defaults to QObject, which should handle anything)
:type base: QtGui.QWidget
:return: QWidget or subclass instance
:rtype: QtGui.QWidget
"""
if ptr is None:
return None
ptr = long(ptr) #Ensure type
if globals().has_key('shiboken'):
if base is None:
qObj = shiboken.wrapInstance(long(ptr), QtCore.QObject)
metaObj = qObj.metaObject()
cls = metaObj.className()
superCls = metaObj.superClass().className()
if hasattr(QtGui, cls):
base = getattr(QtGui, cls)
elif hasattr(QtGui, superCls):
base = getattr(QtGui, superCls)
else:
base = QtGui.QWidget
return shiboken.wrapInstance(long(ptr), base)
elif globals().has_key('sip'):
base = QtCore.QObject
return sip.wrapinstance(long(ptr), base)
else:
return None
def maya_main_window():
main_window_ptr = omui.MQtUtil.mainWindow()
return wrapinstance( long( main_window_ptr ), QtGui.QWidget ) # Works with both PyQt and PySide
''' Main class '''
''' --------------------------------------------------------------------------------------------------------------------------------------------------------- '''
class SearchReplace(form, base):
def __init__(self, parent=None):
"""Super, loadUi, signal connections"""
super(SearchReplace, self).__init__(parent)
if QtType == 'PySide':
print 'Loading UI using PySide'
self.setupUi(self)
elif QtType == 'PyQt':
print 'Loading UI using PyQt'
uic.loadUi(uiFile, self)
self.setObjectName(windowObject)
self.setWindowTitle(windowTitle)
self.statusBar()
self.statusBar().showMessage('I\'m ready for action')
self.buttonTitleJustSearch = 'Just search'
self.buttonTitleSearchReplace = 'Search and replace'
self.buttonTitleStop = 'Stop'
self.running = False
self.abort = False
#self.foundFiles = {} # Main dictionary
self.unCheckableFiles = [] # Store all files that we were unable to read
self.lineEdit_startDir.setText( os.path.dirname(__file__) )
self.pushButton_browse.clicked.connect( self.browseStartingDir ) # Click on browse button
self.pushButton_find.clicked.connect( self.pushButtonJustSearch ) # Click on search button
self.pushButton_replace.clicked.connect( self.pushButtonSearchReplace ) # Click on replace button
self.listWidget_files.currentItemChanged.connect( self.showStrings ) # Click on filepath in listWidget
def reset(self):
print self.sender().text()
# Change button to default value
if self.sender().text() == self.buttonTitleStop:
self.pushButton_find.setText( self.buttonTitleJustSearch )
self.pushButton_replace.setText( self.buttonTitleSearchReplace )
self.pushButton_replace.setEnabled(True)
self.pushButton_find.setEnabled(True)
def pushButtonJustSearch(self):
if self.checkForErrorsJustSearch():
pass
else:
sender = self.sender().text()
self.pushButton_find.setText( self.buttonTitleStop )
self.pushButton_replace.setEnabled(False)
if sender == self.buttonTitleJustSearch:
self.justSearch()
self.reset()
self.abort = False
elif sender == self.buttonTitleStop:
QtGui.QApplication.processEvents()
self.abort = True
QtGui.QApplication.processEvents()
msg = 'User interrupt!'
print msg
self.log( msg )
def pushButtonSearchReplace(self):
if self.checkForErrorsJustSearch() or self.checkForErrorsSearchReplace():
pass
else:
sender = self.sender().text()
if sender == self.buttonTitleSearchReplace:
# TO DO - ASK BEFORE!
msg = 'Make sure you have made a backup of the directory you are running this script on!\n\nYou are about to start the search and replace process.\n\nContinue?'
reply = QtGui.QMessageBox.question(self, 'Message', msg, QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, QtGui.QMessageBox.No)
if reply == QtGui.QMessageBox.Yes:
self.pushButton_replace.setText( self.buttonTitleStop )
self.pushButton_find.setEnabled(False)
self.performReplace()
self.reset()
self.abort = False
elif sender == self.buttonTitleStop:
QtGui.QApplication.processEvents()
self.abort = True
QtGui.QApplication.processEvents()
msg = 'User interrupt!'
print msg
self.log( msg )
def checkForErrorsJustSearch(self):
error = False
if self.lineEdit_find.text() == '':
self.statusBar().showMessage('Error: No search phrase!')
error = True
elif self.lineEdit_startDir.text() == '':
self.statusBar().showMessage('Error: No start dir!')
error = True
elif not os.path.isdir( self.lineEdit_startDir.text() ):
self.statusBar().showMessage('Error: Start dir does not exist!')
error = True
return error
def checkForErrorsSearchReplace(self):
error = False
if self.lineEdit_replace.text() == '':
self.statusBar().showMessage('Error: No replace phrase!')
error = True
return error
def parseFiletypes(self, filetypesString):
filetypesStringModified = filetypesString.replace(' ', '') # Remove space
filetypesList = filetypesStringModified.split(',') # Split to list on comma
return filetypesList
def preProcess(self):
# Create and clear lists and resets
self.filesToSearch = []
self.foundFiles = {}
self.listWidget_files.clear()
if self.lineEdit_filetypes.text() == '':
self.lineEdit_filetypes.setText('*.*')
QtGui.QApplication.processEvents()
# Binary check inits
textchars = ''.join(map(chr, [7,8,9,10,12,13,27] + range(0x20, 0x100)))
is_binary_string = lambda bytes: bool(bytes.translate(None, textchars))
# Starting directory
startDirectory = self.lineEdit_startDir.text()
# Status message
msg = 'Starting indexing of ' + startDirectory
self.statusBar().showMessage( msg )
self.log( msg )
QtGui.QApplication.processEvents()
# If filetype filter is active, make it clear which files to include
filetypes = self.parseFiletypes( self.lineEdit_filetypes.text() )
msg = 'Limiting search to filetypes ' + self.lineEdit_filetypes.text()
self.log( msg )
for root, dirs, files in os.walk( startDirectory ):
if not self.abort:
# Status message
msg = 'Indexing ' + root
self.statusBar().showMessage( msg )
QtGui.QApplication.processEvents()
for extension in ( tuple(filetypes) ):
for filename in fnmatch.filter(files, extension):
filepath = os.path.join(root, filename)
# Are we excluding binary files?
if self.checkBox_skipBinary.isChecked():
if os.path.isfile( filepath ):
if is_binary_string(open( filepath ).read(1024)):
# File is binary, do not do anything
pass
else:
if not self.abort:
self.filesToSearch.append( filepath )
else:
self.filesToSearch.append( filepath )
# Status message
msg = 'Indexing completed'
self.statusBar().showMessage( msg )
self.log( msg )
QtGui.QApplication.processEvents()
fileCount = 1
for filepath in self.filesToSearch:
if not self.abort:
self.statusBar().showMessage( 'Searching file (' + str(fileCount) + '/' + str(len(self.filesToSearch)) + '): ' + filepath )
QtGui.QApplication.processEvents() # Force update UI
self.searchFile( filepath )
fileCount += 1
if len(self.unCheckableFiles) == 0:
msg ='Done Searching ' + str(len(self.filesToSearch)) + ' files. Found ' + str(len( self.foundFiles.keys() )) + ' files with search phrase.'
self.log( msg )
self.statusBar().showMessage( msg )
else:
msg = 'Done Searching ' + str(len(self.filesToSearch)) + ' files. Found ' + str(len( self.foundFiles.keys() )) + ' files with search phrase, but some files could not be read (maybe they were open/locked or of certain unicode type).'
self.log( msg )
self.statusBar().showMessage( msg )
def searchFile(self, filepath):
if not self.abort:
# Read contents of file
with open(filepath, "r") as f:
contents = f.read()
searchString = str( self.lineEdit_find.text() ) # Get search string from UI
# Add file to list if search string was found in file
try:
if searchString in contents:
self.listWidget_files.addItem( filepath ) # Add file to listWidget
QtGui.QApplication.processEvents() # Force update UI
lines = ''
for line in contents.split('\n'):
if searchString in line:
lines += line + '\n'
if not self.checkBox_noRecording.isChecked():
self.foundFiles[ filepath ] = lines # Create entry, record line
else:
self.foundFiles[ filepath ] = '' # Create entry, do not record line
except:
self.unCheckableFiles.append( filepath )
msg = 'Error: Unable to read ' + filepath
self.log( msg )
self.statusBar().showMessage( msg )
def justSearch(self):
if self.lineEdit_find.text() == '':
self.statusBar().showMessage('Error: No search phrase!')
return False
elif self.lineEdit_startDir.text() == '':
self.statusBar().showMessage('Error: No start dir!')
return False
elif not os.path.isdir( self.lineEdit_startDir.text() ):
self.statusBar().showMessage('Error: Start dir does not exist!')
return False
else:
msg = 'Start search for: ' + str(self.lineEdit_find.text()) + ' in ' + str(self.lineEdit_startDir.text())
self.log( msg )
self.preProcess()
return True
def performReplace(self):
if self.lineEdit_replace.text() == '':
self.statusBar().showMessage('Error: No replace phrase!')
else:
if self.justSearch():
msg = 'Start replace with: ' + str(self.lineEdit_replace.text())
self.log( msg )
fileProcessedCount = 1
for filepath in self.foundFiles.keys():
if not self.abort:
self.statusBar().showMessage('Reading (' + str(fileProcessedCount) + '/' + str(len( self.foundFiles.keys() )) + '): ' + filepath )
QtGui.QApplication.processEvents()
# Open the file as readable
with open(filepath, 'r') as fr:
originalContents = fr.read()
if str(self.lineEdit_find.text()) in originalContents:
newContents = originalContents.replace( str(self.lineEdit_find.text()) , str(self.lineEdit_replace.text()) )
if not self.abort:
with open(filepath, 'w') as fw:
writtenFile = False
try:
self.statusBar().showMessage('Writing (' + str(fileProcessedCount) + '/' + str(len( self.foundFiles.keys() )) + '): ' + filepath )
QtGui.QApplication.processEvents()
fw.write(newContents)
writtenFile = True
except:
msg = 'Error: unable to write ' + filepath
self.log( msg )
self.statusBar().showMessage( msg )
# In the case where the file is locked or cannot be written - Attempt to write to a back up dump file
if not writtenFile:
try:
dumpfilepath = filepath+'_DUMP'
with open(dumpfilepath, 'w') as dumpfile:
dumpfile.write(originalContents)
msg = 'Dump file was written: ' + dumpfilepath
self.log( msg )
self.statusBar().showMessage( msg )
except:
msg = 'Severe error: Possible loss of data!'
self.log( msg )
self.statusBar().showMessage( msg )
fileProcessedCount += 1
msg = 'Done writing ' + str(fileProcessedCount-1) + ' files'
self.log( msg )
self.statusBar().showMessage( msg )
def browseStartingDir(self):
try:
startingDir = os.path.dirname(__file__)
except:
startingDir = ''
destDir = QtGui.QFileDialog.getExistingDirectory(None, 'Open working directory', startingDir, QtGui.QFileDialog.ShowDirsOnly)
self.lineEdit_startDir.setText( destDir )
def showStrings(self):
self.listWidget_strings.clear()
try:
filepath = self.listWidget_files.currentItem().text()
lines = self.foundFiles[ filepath ].split('\n')
for line in lines:
self.listWidget_strings.addItem( line )
except:
pass
def log(self, message):
timeStamp = str(datetime.datetime.now())
with open( os.path.join( os.path.dirname(__file__), 'searchReplace.log'), 'a' ) as myfile:
myfile.write( timeStamp + '\t\t' + str(socket.gethostname()) + '\t\t' + message + '\n')
''' Run functions '''
''' --------------------------------------------------------------------------------------------------------------------------------------------------------- '''
def runStandalone():
app = QtGui.QApplication(sys.argv)
global gui
gui = SearchReplace()
gui.show()
if darkorange:
themePath = os.path.join( os.path.dirname(__file__), 'theme' )
sys.path.append( themePath )
import darkorangeResource
stylesheetFilepath = os.path.join( themePath, 'darkorange.stylesheet' )
with open( stylesheetFilepath , 'r' ) as shfp:
gui.setStyleSheet( shfp.read() )
app.setStyle("plastique")
sys.exit(app.exec_())
def runMaya():
if cmds.window(windowObject, q=True, exists=True):
cmds.deleteUI(windowObject)
global gui
gui = SearchReplace( maya_main_window() )
if launchAsDockedWindow:
allowedAreas = ['right', 'left']
cmds.dockControl( label=windowTitle, area='left', content=windowObject, allowedArea=allowedAreas )
else:
gui.show()
def runNuke():
moduleName = __name__
if moduleName == '__main__':
moduleName = ''
else:
moduleName = moduleName + '.'
global gui
if launchAsPanel:
pane = nuke.getPaneFor('Properties.1')
panel = panels.registerWidgetAsPanel( moduleName + 'SearchReplace' , windowTitle, ('uk.co.thefoundry.'+windowObject+'Window'), True).addToPane(pane) # View pane and add it to panes menu
gui = panel.customKnob.getObject().widget
else:
gui = SearchReplace()
gui.show()
if runMode == 'standalone':
runStandalone()
'''
elif runMode == 'maya':
runMaya()
elif runMode == 'nuke':
runNuke()
'''