
\[\f-                 @   s   d  Z  d d l m Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l	 m
 Z
 d d   Z Gd d	   d	 e
 j  Z d
 d   Z d d d  Z d S)z
Main program for 2to3.
    )with_statementN   )refactorc          	   C   s:   |  j    }  | j    } t j |  | | | d d d d S)z%Return a unified diff of two strings.z
(original)z(refactored)Zlineterm )
splitlinesdifflibZunified_diff)abfilename r   "/usr/lib/python3.4/lib2to3/main.py
diff_texts   s
    	r   c                   s[   e  Z d  Z d Z d d d   f d d  Z d d   Z   f d d   Z d	 d
   Z   S)StdoutRefactoringToola2  
    A refactoring tool that can avoid overwriting its input files.
    Prints output to stdout.

    Output files can optionally be written to a different directory and or
    have an extra file suffix appended to their name for use in situations
    where you do not want to replace the input files.
    r   c	       	         sv   | |  _  | |  _ | r; | j t j  r; | t j 7} n  | |  _ | |  _ | |  _ t t	 |   j
 | | |  d S)aF  
        Args:
            fixers: A list of fixers to import.
            options: A dict with RefactoringTool configuration.
            explicit: A list of fixers to run even if they are explicit.
            nobackups: If true no backup '.bak' files will be created for those
                files that are being refactored.
            show_diffs: Should diffs of the refactoring be printed to stdout?
            input_base_dir: The base directory for all input files.  This class
                will strip this path prefix off of filenames before substituting
                it with output_dir.  Only meaningful if output_dir is supplied.
                All files processed by refactor() must start with this path.
            output_dir: If supplied, all converted files will be written into
                this directory tree instead of input_base_dir.
            append_suffix: If supplied, all files output by this tool will have
                this appended to their filename.  Useful for changing .py to
                .py3 for example by passing append_suffix='3'.
        N)	nobackups
show_diffsendswithossep_input_base_dir_output_dir_append_suffixsuperr   __init__)	selfZfixersoptionsexplicitr   r   input_base_dir
output_dirappend_suffix)	__class__r   r   r   $   s    					zStdoutRefactoringTool.__init__c             O   s3   |  j  j | | | f  |  j j | | |  d  S)N)errorsappendloggererror)r   msgargskwargsr   r   r   	log_errorA   s    zStdoutRefactoringTool.log_errorc       
   !      s  | } |  j  rk | j |  j  rO t j j |  j  | t |  j  d    } qk t d | |  j f   n  |  j r | |  j 7} n  | | k r t j j	 |  } t j j
 |  s t j |  n  |  j d | |  n  |  j s| d } t j j |  rKy t j |  WqKt k
 rG} z |  j d |  WYd  d  } ~ XqKXn  y t j | |  Wqt k
 r} z |  j d | |  WYd  d  } ~ XqXn  t t |   j }	 |	 | | | |  |  j st j | |  n  | | k rt j | |  n  d  S)Nz5filename %s does not start with the input_base_dir %szWriting converted %s to %s.z.bakzCan't remove backup %szCan't rename %s to %s)r   
startswithr   r   pathjoinlen
ValueErrorr   dirnameisdirmakedirslog_messager   lexistsremoveOSErrorrenamer   r   
write_fileshutilZcopymode)
r   Znew_textr
   Zold_textencodingZorig_filenamer   Zbackuperrwrite)r   r   r   r5   E   s@    		
	
&)	z StdoutRefactoringTool.write_filec             C   s   | r |  j  d |  n |  j  d |  |  j r t | | |  } yi |  j d  k	 r |  j - x | D] } t |  qg Wt j j   Wd  QXn x | D] } t |  q WWq t k
 r t	 d | f  d  SYq Xn  d  S)NzNo changes to %szRefactored %sz+couldn't encode %s's diff for your terminal)
r0   r   r   Zoutput_lockprintsysstdoutflushUnicodeEncodeErrorwarn)r   oldnewr
   ZequalZ
diff_linesliner   r   r   print_outputl   s"    	
z"StdoutRefactoringTool.print_output)__name__
__module____qualname____doc__r   r'   r5   rC   r   r   )r   r   r      s
   'r   c             C   s   t  d |  f d t j d  S)NzWARNING: %sfile)r:   r;   stderr)r$   r   r   r   r?      s    r?   c                s  t  j d d  } | j d d d d d d | j d	 d
 d d d g  d d | j d d d d d d d d d d | j d d d d d g  d d | j d d d d d d | j d d d d d d | j d d d d d d  | j d! d d d d" | j d# d$ d d d d% | j d& d' d d d d( d d) | j d* d+ d d d d, d d- d d. | j d/ d0 d d d d1 | j d2 d d d d, d d- d d3 d( } i  } | j |  \ } } | j rd4 | d5 <| j st d6  n  d4 | _ n  | j r'| j r'| j	 d7  n  | j
 rJ| j rJ| j	 d8  n  | j rj| j rjt d9  n  | j r| j r| j	 d:  n  | j rt d;  x! t j    D] } t |  qW| sd< Sn  | st d= d> t j t d? d> t j d@ SdA | k r6d4 } | j r6t dB d> t j d@ Sn  | j rLd4 | dC <n  | j r^t j n t j } t j dD dE dF |  t j dG  } t t j     }	 t   f dH dI   | j D  }
 t   } | j r7d( } x; | j D]0 } | dJ k r d4 } q| j   dK |  qW| r.|	 j |  n | } n |	 j |  } | j |
  } t  j! j" |  } | r| j# t  j$  rt  j! j% |  rt  j! j& |  } n  | j r| j' t  j$  } | j( dL | j |  n  t) t* |  | t* |  | j | j dM | dN | j dO | j
 } | j+ s| r;| j,   ng y# | j | | j | j- | j.  WnA t j/ k
 r| j. d k st0  t dP d> t j d SYn X| j1   n  t2 t3 | j+   S)QzMain program.

    Args:
        fixer_pkg: the name of a package where the fixers are located.
        args: optional; a list of command line arguments. If omitted,
              sys.argv[1:] is used.

    Returns a suggested exit status (0, 1, 2).
    Zusagez2to3 [options] file|dir ...z-dz--doctests_onlyaction
store_truehelpzFix up doctests onlyz-fz--fixr!   defaultz1Each FIX specifies a transformation; default: allz-jz--processesZstorer   typeintzRun 2to3 concurrentlyz-xz--nofixz'Prevent a transformation from being runz-lz--list-fixeszList available transformationsz-pz--print-functionz0Modify the grammar so that print() is a functionz-vz	--verbosezMore verbose loggingz
--no-diffsz#Don't show diffs of the refactoringz-wz--writezWrite back modified filesz-nz--nobackupsFz&Don't write backups for modified filesz-oz--output-dirstrr   zXPut output files in this directory instead of overwriting the input files.  Requires -n.z-Wz--write-unchanged-fileszYAlso write files even if no changes were required (useful with --output-dir); implies -w.z--add-suffixzuAppend this string to all output filenames. Requires -n if non-empty.  ex: --add-suffix='3' will generate .py3 files.Twrite_unchanged_filesz&--write-unchanged-files/-W implies -w.z%Can't use --output-dir/-o without -n.z"Can't use --add-suffix without -n.z@not writing files and not printing diffs; that's not very usefulzCan't use -n without -wz2Available transformations for the -f/--fix option:r   z1At least one file or directory argument required.rH   zUse --help to show usage.   -zCan't write to stdin.print_functionformatz%(name)s: %(message)slevelzlib2to3.mainc             3   s   |  ] }   d  | Vq d S)z.fix_Nr   ).0fix)	fixer_pkgr   r   	<genexpr>   s    zmain.<locals>.<genexpr>allz.fix_z7Output in %r will mirror the input directory %r layout.r   r   r   z+Sorry, -j isn't supported on this platform.)4optparseZOptionParserZ
add_option
parse_argsrQ   r9   r?   r   r   r#   Z
add_suffixZno_diffsZ
list_fixesr:   r   Zget_all_fix_namesr;   rI   rT   verboseloggingDEBUGINFOZbasicConfigZ	getLoggersetZget_fixers_from_packageZnofixrX   addunion
differencer   r)   commonprefixr   r   r.   r-   rstripinfor   sortedr    refactor_stdinZdoctests_onlyZ	processesZMultiprocessingUnsupportedAssertionErrorZ	summarizerO   bool)rY   r%   parserrj   flagsr   ZfixnamerV   r"   Zavail_fixesZunwanted_fixesr   Zall_presentrX   Z	requestedZfixer_namesr   Zrtr   )rY   r   main   s    	
		
		"								
	ro   )rG   Z
__future__r   r;   r   r   r_   r6   r\   r   r   r   ZMultiprocessRefactoringToolr   r?   ro   r   r   r   r   <module>   s   	g