fc@sdZdZddlZddlZddlZddlZddlZdadada e a ddZ dZdZdZd Zd d Zd dd YZdZedkrendS(sAreindent [-d][-r][-v] [ path ... ] -d (--dryrun) Dry run. Analyze, but don't make any changes to, files. -r (--recurse) Recurse. Search for all .py files in subdirectories too. -n (--nobackup) No backup. Does not make a ".bak" file before reindenting. -v (--verbose) Verbose. Print informative msgs; else no output. -h (--help) Help. Print this usage information and exit. Change Python (.py) files to use 4-space indents and no hard tab characters. Also trim excess spaces and tabs from ends of lines, and remove empty lines at the end of files. Also ensure the last line ends with a newline. If no paths are given on the command line, reindent operates as a filter, reading a single source file from standard input and writing the transformed source to standard output. In this case, the -d, -r and -v flags are ignored. You can pass one or more file and/or directory paths. When a directory path, all .py files within the directory will be examined, and, if the -r option is given, likewise recursively for subdirectories. If output is not to standard output, reindent overwrites files in place, renaming the originals with a .bak extension. If it finds nothing to change, the file is left alone. If reindent does change a file, the changed file is a fixed-point for future runs (i.e., running reindent on the resulting .py file won't change it again). The hard part of reindenting is figuring out what to do with comment lines. So long as the input files get a clean bill of health from tabnanny.py, reindent should do a good job. The backup file is a copy of the one that is being reindented. The ".bak" file is generated with shutil.copy(), but some corner cases regarding user/group and permissions could leave the backup file more readable than you'd prefer. You can always use the --nobackup option to prevent this. t1iNicCs-|dk rtj|IJntjtIJdS(N(tNonetsyststderrt__doc__(tmsg((s./usr/lib64/python2.7/Tools/scripts/reindent.pytusage6s cGsKd}x.|D]&}tjj|t|d}q WtjjddS(Ntt s (RRtwritetstr(targstseptarg((s./usr/lib64/python2.7/Tools/scripts/reindent.pyterrprint;s   cCsEddl}y5|jtjdddddddg\}}Wn!|jk rd}t|dSXx|D]\}}|dkrtd7aql|dkrtd7aql|dkrtaql|dkrt d7a ql|dkrltdSqlW|s&t tj }|j |j tjdSx|D]}t|q-WdS(Niitdrnvhtdryruntrecursetnobackuptverbosethelps-ds--dryruns-rs --recurses-ns --nobackups-vs --verboses-hs--help(s-ds--dryrun(s-rs --recurse(s-ns --nobackup(s-vs --verbose(s-hs--help(tgetoptRtargvterrorRRRtFalset makebackupRt ReindentertstdintrunR tstdouttcheck(RtoptsR RtotatrR ((s./usr/lib64/python2.7/Tools/scripts/reindent.pytmainBs4 "            c Cs6tjj|rtjj| rtr7dG|GHntj|}x|D]}tjj||}trtjj|rtjj| rtjj|dj d s|j j drMt |qMqMWdStrdG|GdGnyt |d}Wn.tk r5}td|t|fdSXt|}|j|j}t|trvtd |dS|jr trd GHtrd GHqnts|d }trtj||trd G|GdG|GHqnt |d}|j||jtrdG|GHqntStr.dGHntSdS(Nslisting directoryit.s.pytcheckings...trbs%s: I/O Error: %ss0%s: mixed newlines detected; cannot process fileschanged.s+But this is a dry run, so leaving it alone.s.baks backed upttotwbs wrote news unchanged.(tostpathtisdirtislinkRtlistdirtjoinRtsplitt startswithtlowertendswithRtopentIOErrorRR Rtclosetnewlinest isinstancettupleRRRtshutiltcopyfileR tTrueR( tfiletnamestnametfullnametfRR"tnewlinetbak((s./usr/lib64/python2.7/Tools/scripts/reindent.pyR_sZ%           cCsWd|D}|jdtt|}|s9dSt|dkrS|dS|S(NcSsXh|]N}|ddkr"dn/|ddkr8dn|ddkrNdndqS(is is s R((t.0tline((s./usr/lib64/python2.7/Tools/scripts/reindent.pys s Rs ii(tdiscardR8tsortedtlen(tlinesR6((s./usr/lib64/python2.7/Tools/scripts/reindent.pyt_detect_newliness  s cCsEt|}x.|dkr<||d|kr<|d8}qW|| S(sReturn line stripped of trailing spaces, tabs, newlines. Note that line.rstrip() instead also strips sundry control characters, but at least one known Emacs user expects to keep junk like that, not mentioning Barry by name or anything . ii(RG(RDtJUNKti((s./usr/lib64/python2.7/Tools/scripts/reindent.pyt_rstrips #RcBsSeZdZdZdZdZejejej ej ej dZ RS(cCsd|_d|_|j|_t|j|_t|jtrX|jd|_n |j|_g|jD]}t |j |j^qn|_ |j j ddd|_g|_dS(Nii(t find_stmttlevelt readlinestrawRIR6R7R8RARLt expandtabsRHtinsertRtindextstats(tselfR@RD((s./usr/lib64/python2.7/Tools/scripts/reindent.pyt__init__s   / cCstj|j|j|j}x'|rH|d|jkrH|jq"W|j}|jt|dfi}g}|_ |dd}|j |d|!xTt t|dD]<}||\}}||dd}t ||} |d} | dkr.| r%|j | d} | dkrxkt|dt|dD]I} || \} } | dkrG| t || kr| d} nPqGqGWn| dkr xgt|dddD]L} || \} } | dkr| t || dt || } PqqWn| dkr+| } q+q.d} n| || <| | }|dksZ| dkrq|j |||!qx|||!D]p}|dkr||jkr|j|q|jd||qtt || }|j||qWqW|j|j kS(NiiiiR(ttokenizetgetlinet tokeneaterRHRAtpopRTtappendRGtaftertextendtranget getlspacetgettxrangetminRP(RURHRTt have2wantR\RKtthisstmtt thisleveltnextstmtthavetwanttjtjlinetjleveltdiffRDtremove((s./usr/lib64/python2.7/Tools/scripts/reindent.pyRs^      $            cCs|j|jdS(N(t writelinesR\(RUR@((s./usr/lib64/python2.7/Tools/scripts/reindent.pyR scCsD|jt|jkr!d}n|j|j}|jd7_|S(NRi(RSRGRH(RURD((s./usr/lib64/python2.7/Tools/scripts/reindent.pyRXs  c Cs|\} } ||kr$d|_n||krKd|_|jd7_n||krrd|_|jd8_nw|| kr|jr|jj| dfqnF|| krn7|jrd|_|r|jj| |jfqndS(Niii(RMRNRTR[( RUttypettokent.3tendRDtINDENTtDEDENTtNEWLINEtCOMMENTtNLtslinetscol((s./usr/lib64/python2.7/Tools/scripts/reindent.pyRYs$            ( t__name__t __module__RVRR RXRWRsRtRuRvRwRY(((s./usr/lib64/python2.7/Tools/scripts/reindent.pyRs  E  cCsDdt|}}x*||kr?||dkr?|d7}qW|S(NiRi(RG(RDRKtn((s./usr/lib64/python2.7/Tools/scripts/reindent.pyR_Fst__main__((Rt __version__RWR)R9RtioRRRR;RRRRR#RRIRLRR_Rz(((s./usr/lib64/python2.7/Tools/scripts/reindent.pyt(s&       4