ZQdN.dZddlmZmZddlZddlZddlZddlZddlZddl Z ddl m Z dZ Gdde j Zd Zd d ZdS) z Main program for 2to3. )with_statementprint_functionN)refactorc |}|}tj||||dddS)z%Return a unified diff of two strings.z (original)z (refactored))lineterm) splitlinesdifflib unified_diff)abfilenames #/usr/lib/python3.11/lib2to3/main.py diff_textsrsF A A  1h ,n)+ - - --c<eZdZdZ dfd ZdZfdZdZxZS)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. rc ||_||_|r.|tjs|tjz }||_||_||_tt| |||dS)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_diffsendswithossep_input_base_dir _output_dir_append_suffixsuperr__init__) selffixersoptionsexplicitrrinput_base_dir output_dir append_suffix __class__s rrzStdoutRefactoringTool.__init__$s(#$  %."9"9"&"A"A % bf $N-%+ #T**33FGXNNNNNrcl|j|||f|jj|g|Ri|dSN)errorsappendloggererror)r msgargskwargss r log_errorzStdoutRefactoringTool.log_errorAsJ Cv./// #/////////rc|}|jrt||jr@tj|j|t |jd}ntd|d|j|jr ||jz }||krktj |}tj |s|rtj || d|||j s|dz}tj|r< tj|n&#t $r| d|YnwxYw tj||n'#t $r| d||YnwxYwt%t&|j}||||||j st+j||||krt+j||dSdS)Nz filename z( does not start with the input_base_dir zWriting converted %s to %s.z.bakzCan't remove backup %szCan't rename %s to %s)r startswithrrpathjoinlen ValueErrorrdirnameisdirmakedirs log_messagerlexistsremoveOSErrorrenamerr write_fileshutilcopymode) r new_textrold_textencoding orig_filenamer%backupwriter's rr@z StdoutRefactoringTool.write_fileEsd   J""4#788 J7<<(8(0T5I1J1J1K1K(LNN!j)143G3G"IJJJ   , + +H H $ $22J7==,, ( ( J'''   :M% ' ' '~ L&Fwv&& GGIf%%%%GGG$$%=vFFFFFG L (F++++ L L L  !8(FKKKKK L+T22= h(H555~ . OFH - - - H $ $ OM8 4 4 4 4 4 % $s$-E E%$E%)E??!F#"F#c|r|d|dS|d||jrt|||} |jU|j5|D]}t |t jdddn #1swxYwYdSdS|D]}t |dS#t$rtd|dYdSwxYwdS)NzNo changes to %sz Refactored %szcouldn't encode z's diff for your terminal) r;rr output_lockprintsysstdoutflushUnicodeEncodeErrorwarn)r oldnewrequal diff_lineslines r print_outputz"StdoutRefactoringTool.print_outputls|     / : : : : :   _h 7 7 7 'S(;;  '3!-//(2,, %d J,,.../////////////////// %/((D!$KKKK(()D"((%&&&FF  s< B<3B B<BB<BB<&B<<CC)rrr) __name__ __module__ __qualname____doc__rr1r@rV __classcell__)r's@rrrsBDOOOOOO:000%5%5%5%5%5NrrcBtd|tjdS)Nz WARNING: file)rKrLstderr)r.s rrPrPs$ E33 sz222222rc  tjd}|dddd|dd d gd |d ddddd|ddd gd |dddd|dddd|dddd|d d!dd"|d#dd$|d%d&dd'|d(d)dd*d+ |d,d-dd.d/d01|d2d3dd4|d5dd.d/d61d*}i}||\}}|jr"d7|d8<|jst d9d7|_|jr|js| d:|j r|js| d;|js|j rt d<|js|jr| d=|j r9td>tjD]}t||sd?S|s8td@t jAtdBt jAdCSdD|vr&d7}|jrtdEt jAdCS|jrd7|dF<|jrd7|dG<|jr t*jn t*j}t+jdH|It+jdJ}t5tj} t5fdK|jD} t5} |jrJd*} |jD]&} | dLkrd7} | dMz| z'| r| | n| }n| | }| | }tBj"#|}|r]|$tBj%s>tBj"&|stBj"'|}|jr;|(tBj%}|)dN|j|tUtW||tW| |j|j ||j|j O}|j,s|r|-ng |||j|j.|j/n>#tj0$r,|j/dksJtdPt jAYdSwxYw|1tetg|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). z2to3 [options] file|dir ...)usagez-dz--doctests_only store_truezFix up doctests only)actionhelpz-fz--fixr+z1Each FIX specifies a transformation; default: all)rcdefaultrdz-jz --processesstorerintzRun 2to3 concurrently)rcretyperdz-xz--nofixz'Prevent a transformation from being runz-lz --list-fixeszList available transformationsz-pz--print-functionz0Modify the grammar so that print() is a functionz-ez--exec-functionz/Modify the grammar so that exec() is a functionz-vz --verbosezMore verbose loggingz --no-diffsz#Don't show diffs of the refactoringz-wz--writezWrite back modified filesz-nz --nobackupsFz&Don't write backups for modified filesz-oz --output-dirstrrzXPut output files in this directory instead of overwriting the input files. Requires -n.)rcrhrerdz-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.Twrite_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 usefulzCan't use -n without -wz2Available transformations for the -f/--fix option:rz1At least one file or directory argument required.r]zUse --help to show usage.-zCan't write to stdin.r exec_functionz%(name)s: %(message)s)formatlevelz lib2to3.mainc3(K|] }dz|zV dS).fix_N).0fix fixer_pkgs r zmain..s-LLsW,s2LLLLLLrallrqz7Output in %r will mirror the input directory %r layout.)r$r%r&z+Sorry, -j isn't supported on this platform.)4optparse OptionParser add_option parse_argsrjrHrPr%rr- add_suffixno_diffs list_fixesrKrget_all_fix_namesrLr_rrmverboseloggingDEBUGINFO basicConfig getLoggersetget_fixers_from_packagenofixrtaddunion differencerr4 commonprefixrrr9r8rstripinforsortedr*refactor_stdin doctests_only processesMultiprocessingUnsupported summarizergbool)rur/parserrflagsr"fixnameror, avail_fixesunwanted_fixesr# all_presentrt requested fixer_namesr$rts` rmainrs! ")F G G GF d-l1333 dGHbNPPP dM'1 '>@@@ dIhDFFF dN<;=== d.|MOOO d-lLNNN dK 1333 l<@BBB dIl6888 dM,CEEE dN7 (NOOO d5lABBB nW5"GHHH N E%%d++MGT$)-%&} ; 9 : : : >'"3> <===;'"3; 9::: =QW-Q OPPP =0W.0 ./// BCCC1)<<  G 'NNNN 1  A SSSS ) ;;;;q d{{ =  ) ; ; ; ;1'"&&!%o%_ >GMM',E 6eDDDD  ~ . .Fh6yAABBKLLLLgmLLLLLNuuH{ 0 ; 8 8Ce||"  Y03677773>LK%%h///H %%h// &&~66KW))$//N9~66rv>>9 n--9 888'..rv66 M& 8 8 8  ;  x(8(8  7#33))!,  . . .B 9            D'-1F#-////6   (1,,,,C:''''qq    tBI  s;'U##7VVr))rZ __future__rrrLrr rrArxrrrMultiprocessRefactoringToolrrPrrrrrrs65555555  ---eeeeeH@eeeN333L L L L L L r