blob: 4ff64ceb22e6e75183729c12e8401eb69c1ab38d [file] [log] [blame]
niklase@google.com47bdc462011-05-30 11:42:351import fnmatch
2import os
3import stringmanipulation
4
5def fileexist( file_name ):
6 return os.path.isfile(file_name)
7
8def pathexist( path ):
9 return os.path.exists(path)
10
11def fixpath( path ):
12 return_value = path
13 if( return_value[len(return_value) - 1] != '/'):
14 return_value = return_value + '/'
15 return return_value
16
17def 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
25def 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
32def 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
38def 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
44def 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
59def 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