Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/local/bin/python
- # -*- coding:latin-1 -*-
- # Inserts the above encoding lines in every file it finds within the current directory
- # and its subdirectories. Tries to find similarities, in case the lines are already
- # there, but spelled incorrectly or not as per PEP...something.
- import time
- import os
- import subprocess
- from difflib import SequenceMatcher
- correct_lines = [ "#!/usr/local/bin/python", "# -*- coding:latin-1 -*-" ]
- # similarity thresholds to test the existing first two lines. If values are below 60%, then
- # the assumption is the existing lines are not encoding lines, and new ones get prepended.
- replace_thres = 75 # similarity >= 75%, replace line.
- warning_thres = 70 # >= 70 < 75, replace but warn user of low similarity.
- noreplace_thres = 60 # >= 60 < 70, querry the user.
- def cls(): subprocess.call( 'cls', shell=True, cwd='./' )
- def similar( a, b ): return SequenceMatcher( None, a, b ).ratio()
- def concat( a, b ): return ''.join( [ a, b ] )
- def check_for_replace( a, b ):
- percent = similar( a, b )*100
- if percent == 100: return ( -1, -1, percent ) # ignore, it's fine
- elif percent >= replace_thres: return ( 1, 0, percent ) # replace, no warning
- elif percent >= warning_thres: return ( 1, 1, percent ) # replace, w/ warning
- elif percent > noreplace_thres: return ( 1, 0, percent ) \
- if ask_user( a, b ) else ( 0, 0, percent )
- else: return ( 0, 0, percent )
- def ask_user( a, b ):
- inpt = ''
- while inpt.lower() not in ( 'y', 'n' ):
- inpt = raw_input( 'Found %s. Replace the string? (y/n) ' % ( b ) )
- return inpt in 'y'
- def prepend( fpath ):
- with open( fpath, 'r' ) as f:
- writen_something = False
- lines = list( f.read().split('\n') )
- tmp_lines_copy = lines
- for i, c in enumerate( correct_lines ):
- replace, warn, percent = check_for_replace( c, tmp_lines_copy[i] )
- if replace == -1: # ignore, already correct in the file
- continue
- elif replace == 1: # replace
- if warn == 1:
- print "- WARNING: string '%s' only %.1f similar..." % ( lines[i], percent ),
- lines[i] = c
- writen_something = True
- else: # prepend
- lines.insert( i, c )
- writen_something = True
- if writen_something:
- # add a blank line after the second one, to keep things neet
- if len( lines[2] ):
- lines[1] = concat( lines[1], '\n' )
- # a little quick hack to not add an extra \n at the end
- if not len( lines[-1] ):
- del lines[-1]
- with open( fpath, 'w' ) as fout:
- for l in lines:
- fout.write( concat( l, '\n' ) )
- return True
- return False
- def main():
- start = time.time()
- num_files = sum( [ len( files ) for r, d, files in os.walk(".\\") ] )
- cls()
- print "\n\t%d .py files found.\n\n" % ( num_files )
- for directory, d, files in os.walk('.\\'):
- # os.walk gives me paths w/ backslashes, so I use them for consistency
- for f in files:
- if f.endswith('.py'):
- if not directory.endswith('\\'):
- fpath = concat( concat( directory, '\\' ), f )
- else:
- fpath = concat( directory, f )
- print " %s" % ( fpath ),
- if prepend( fpath ):
- print "Done."
- else:
- print "is fine."
- end = time.time()
- print "\n\t %d files in %.3f seconds." % ( num_files, end-start )
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment