niklase@google.com | 47bdc46 | 2011-05-30 11:42:35 | [diff] [blame] | 1 | import fnmatch |
| 2 | import os |
| 3 | import stringmanipulation |
| 4 | |
| 5 | def fileexist( file_name ): |
| 6 | return os.path.isfile(file_name) |
| 7 | |
| 8 | def pathexist( path ): |
| 9 | return os.path.exists(path) |
| 10 | |
| 11 | def fixpath( path ): |
| 12 | return_value = path |
| 13 | if( return_value[len(return_value) - 1] != '/'): |
| 14 | return_value = return_value + '/' |
| 15 | return return_value |
| 16 | |
| 17 | def listallfilesinfolder( path, extension ): |
| 18 | matches = [] |
| 19 | signature = '*' + extension |
| 20 | for root, dirnames, filenames in os.walk(path): |
| 21 | for filename in fnmatch.filter(filenames, signature): |
| 22 | matches.append([fixpath(root), filename]) |
| 23 | return matches |
| 24 | |
| 25 | def copyfile(to_file, from_file): |
| 26 | if(not fileexist(from_file)): |
| 27 | return |
| 28 | command = 'cp -f ' + from_file + ' ' + to_file |
| 29 | os.system(command) |
| 30 | #print command |
| 31 | |
| 32 | def deletefile(file_to_delete): |
| 33 | if(not fileexist(file_to_delete)): |
| 34 | return |
| 35 | os.system('rm ' + file_to_delete) |
| 36 | |
| 37 | # very ugly but works, so keep for now |
| 38 | def findstringinfile(path,file_name,search_string): |
| 39 | command = 'grep \'' + search_string + '\' ' + path + file_name + ' > deleteme.txt' |
| 40 | return_value = os.system(command) |
| 41 | # print command |
| 42 | return (return_value == 0) |
| 43 | |
| 44 | def replacestringinfolder( path, old_string, new_string, extension ): |
| 45 | if(not stringmanipulation.isextension(extension)): |
| 46 | print 'failed to search and replace' |
| 47 | return |
| 48 | if(len(old_string) == 0): |
| 49 | print 'failed to search and replace' |
| 50 | return |
| 51 | find_command = 'ls '+ path + '/*' + extension |
| 52 | sed_command = 'sed -i \'s/' + old_string + '/' + new_string +\ |
| 53 | '/g\' *' + extension |
| 54 | command_string = find_command + ' | xargs ' + sed_command + ' 2> deleteme.txt' |
| 55 | os.system(command_string) |
| 56 | #print command_string |
| 57 | |
| 58 | #find ./ -name "*.h" -type f | xargs -P 0 sed -i 's/process_thread_wrapper.h/process_thread.h/g' *.h deleteme.txt |
| 59 | def replacestringinallsubfolders( old_string, new_string, extension): |
| 60 | if(not stringmanipulation.isextension(extension)): |
| 61 | print 'failed to search and replace' |
| 62 | return |
| 63 | if(len(old_string) == 0): |
| 64 | print 'failed to search and replace' |
| 65 | return |
| 66 | |
| 67 | find_command = 'find ./ -name \"*' + extension + '\" -type f' |
| 68 | sed_command = 'sed -i \'s/' + old_string + '/' + new_string +\ |
| 69 | '/g\' *' + extension |
| 70 | command_string = find_command + ' | xargs -P 0 ' + sed_command + ' 2> deleteme.txt' |
| 71 | os.system(command_string) |
| 72 | #print command_string |