<s> """<STR_LIT>""" <EOL> from __future__ import with_statement <EOL> import sys <EOL> import os <EOL> import difflib <EOL> import logging <EOL> import shutil <EOL> import optparse <EOL> from . import refactor <EOL> def diff_texts ( a , b , filename ) : <EOL> """<STR_LIT>""" <EOL> a = a . splitlines ( ) <EOL> b = b . splitlines ( ) <EOL> return difflib . unified_diff ( a , b , filename , filename , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> lineterm = "<STR_LIT>" ) <EOL> class StdoutRefactoringTool ( refactor . MultiprocessRefactoringTool ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , fixers , options , explicit , nobackups , show_diffs , <EOL> input_base_dir = '<STR_LIT>' , output_dir = '<STR_LIT>' , append_suffix = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> self . nobackups = nobackups <EOL> self . show_diffs = show_diffs <EOL> if input_base_dir and not input_base_dir . endswith ( os . sep ) : <EOL> input_base_dir += os . sep <EOL> self . _input_base_dir = input_base_dir <EOL> self . _output_dir = output_dir <EOL> self . _append_suffix = append_suffix <EOL> super ( StdoutRefactoringTool , self ) . __init__ ( fixers , options , explicit ) <EOL> def log_error ( self , msg , * args , ** kwargs ) : <EOL> self . errors . append ( ( msg , args , kwargs ) ) <EOL> self . logger . error ( msg , * args , ** kwargs ) <EOL> def write_file ( self , new_text , filename , old_text , encoding ) : <EOL> orig_filename = filename <EOL> if self . _output_dir : <EOL> if filename . startswith ( self . _input_base_dir ) : <EOL> filename = os . path . join ( self . _output_dir , <EOL> filename [ len ( self . _input_base_dir ) : ] ) <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( <EOL> filename , self . _input_base_dir ) ) <EOL> if self . _append_suffix : <EOL> filename += self . _append_suffix <EOL> if orig_filename != filename : <EOL> output_dir = os . path . dirname ( filename ) <EOL> if not os . path . isdir ( output_dir ) : <EOL> os . makedirs ( output_dir ) <EOL> self . log_message ( '<STR_LIT>' , orig_filename , <EOL> filename ) <EOL> if not self . nobackups : <EOL> backup = filename + "<STR_LIT>" <EOL> if os . path . lexists ( backup ) : <EOL> try : <EOL> os . remove ( backup ) <EOL> except os . error , err : <EOL> self . log_message ( "<STR_LIT>" , backup ) <EOL> try : <EOL> os . rename ( filename , backup ) <EOL> except os . error , err : <EOL> self . log_message ( "<STR_LIT>" , filename , backup ) <EOL> write = super ( StdoutRefactoringTool , self ) . write_file <EOL> write ( new_text , filename , old_text , encoding ) <EOL> if not self . nobackups : <EOL> shutil . copymode ( backup , filename ) <EOL> if orig_filename != filename : <EOL> shutil . copymode ( orig_filename , filename ) <EOL> def print_output ( self , old , new , filename , equal ) : <EOL> if equal : <EOL> self . log_message ( "<STR_LIT>" , filename ) <EOL> else : <EOL> self . log_message ( "<STR_LIT>" , filename ) <EOL> if self . show_diffs : <EOL> diff_lines = diff_texts ( old , new , filename ) <EOL> try : <EOL> if self . output_lock is not None : <EOL> with self . output_lock : <EOL> for line in diff_lines : <EOL> print line <EOL> sys . stdout . flush ( ) <EOL> else : <EOL> for line in diff_lines : <EOL> print line <EOL> except UnicodeEncodeError : <EOL> warn ( "<STR_LIT>" % <EOL> ( filename , ) ) <EOL> return <EOL> def warn ( msg ) : <EOL> print >> sys . stderr , "<STR_LIT>" % ( msg , ) <EOL> def main ( fixer_pkg , args = None ) : <EOL> """<STR_LIT>""" <EOL> parser = optparse . OptionParser ( usage = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store_true>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT>" , default = [ ] , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store>" , default = <NUM_LIT:1> , <EOL> type = "<STR_LIT:int>" , help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT>" , default = [ ] , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store_true>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store_true>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store_true>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , action = "<STR_LIT:store_true>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store_true>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store_true>" , default = False , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store>" , type = "<STR_LIT:str>" , <EOL> default = "<STR_LIT>" , help = "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store_true>" , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , action = "<STR_LIT:store>" , type = "<STR_LIT:str>" , default = "<STR_LIT>" , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> refactor_stdin = False <EOL> flags = { } <EOL> options , args = parser . parse_args ( args ) <EOL> if options . write_unchanged_files : <EOL> flags [ "<STR_LIT>" ] = True <EOL> if not options . write : <EOL> warn ( "<STR_LIT>" ) <EOL> options . write = True <EOL> if options . output_dir and not options . nobackups : <EOL> parser . error ( "<STR_LIT>" ) <EOL> if options . add_suffix and not options . nobackups : <EOL> parser . error ( "<STR_LIT>" ) <EOL> if not options . write and options . no_diffs : <EOL> warn ( "<STR_LIT>" ) <EOL> if not options . write and options . nobackups : <EOL> parser . error ( "<STR_LIT>" ) <EOL> if options . list_fixes : <EOL> print "<STR_LIT>" <EOL> for fixname in refactor . get_all_fix_names ( fixer_pkg ) : <EOL> print fixname <EOL> if not args : <EOL> return <NUM_LIT:0> <EOL> if not args : <EOL> print >> sys . stderr , "<STR_LIT>" <EOL> print >> sys . stderr , "<STR_LIT>" <EOL> return <NUM_LIT:2> <EOL> if "<STR_LIT:->" in args : <EOL> refactor_stdin = True <EOL> if options . write : <EOL> print >> sys . stderr , "<STR_LIT>" <EOL> return <NUM_LIT:2> <EOL> if options . print_function : <EOL> flags [ "<STR_LIT>" ] = True <EOL> level = logging . DEBUG if options . verbose else logging . INFO <EOL> logging . basicConfig ( format = '<STR_LIT>' , level = level ) <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> avail_fixes = set ( refactor . get_fixers_from_package ( fixer_pkg ) ) <EOL> unwanted_fixes = set ( fixer_pkg + "<STR_LIT>" + fix for fix in options . nofix ) <EOL> explicit = set ( ) <EOL> if options . fix : <EOL> all_present = False <EOL> for fix in options . fix : <EOL> if fix == "<STR_LIT:all>" : <EOL> all_present = True <EOL> else : <EOL> explicit . add ( fixer_pkg + "<STR_LIT>" + fix ) <EOL> requested = avail_fixes . union ( explicit ) if all_present else explicit <EOL> else : <EOL> requested = avail_fixes . union ( explicit ) <EOL> fixer_names = requested . difference ( unwanted_fixes ) <EOL> input_base_dir = os . path . commonprefix ( args ) <EOL> if ( input_base_dir and not input_base_dir . endswith ( os . sep ) <EOL> and not os . path . isdir ( input_base_dir ) ) : <EOL> input_base_dir = os . path . dirname ( input_base_dir ) <EOL> if options . output_dir : <EOL> input_base_dir = input_base_dir . rstrip ( os . sep ) <EOL> logger . info ( '<STR_LIT>' , <EOL> options . output_dir , input_base_dir ) <EOL> rt = StdoutRefactoringTool ( <EOL> sorted ( fixer_names ) , flags , sorted ( explicit ) , <EOL> options . nobackups , not options . no_diffs , <EOL> input_base_dir = input_base_dir , <EOL> output_dir = options . output_dir , <EOL> append_suffix = options . add_suffix ) <EOL> if not rt . errors : <EOL> if refactor_stdin : <EOL> rt . refactor_stdin ( ) <EOL> else : <EOL> try : <EOL> rt . refactor ( args , options . write , options . doctests_only , <EOL> options . processes ) <EOL> except refactor . MultiprocessingUnsupported : <EOL> assert options . processes > <NUM_LIT:1> <EOL> print >> sys . stderr , "<STR_LIT>" "<STR_LIT>" <EOL> return <NUM_LIT:1> <EOL> rt . summarize ( ) <EOL> return int ( bool ( rt . errors ) ) </s>
