Rfc@sdZdZddlZddlZddlZddlZddlZdadada e a ddZ dZdZdZd d Zd dd YZd ZedkrendS(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 Cs?tjj|rtjj| rtr7dG|GHntj|}x|D]}tjj||}trtjj|rtjj| rtjj|dj d s|j j drMt |qMqMWdStrdG|GdGnyt j|}Wn.tk r5}td|t|fdSXt|}|j|j}t|trvtd|dS|jr)trd GHtrd GHqnts%|d }trtj||trd G|Gd G|GHqnt j|dd|}|j||jtr%dG|GHq%ntStr7dGHntSdS(Nslisting directoryit.s.pytcheckings...s%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 upttotwtnewlines wrote news unchanged.(tostpathtisdirtislinkRtlistdirtjoinRtsplitt startswithtlowertendswithRtiotopentIOErrorRR Rtclosetnewlinest isinstancettupleRRRtshutiltcopyfileR tTrueR( tfiletnamestnametfullnametfRR"R(tbak((s./usr/lib64/python2.7/Tools/scripts/reindent.pyR_sZ%           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(tlen(tlinetJUNKti((s./usr/lib64/python2.7/Tools/scripts/reindent.pyt_rstrips #RcBsSeZdZdZdZdZejejej ej ej dZ RS(cCsd|_d|_|j|_g|jD]}t|jd^q+|_|jjddd|_ g|_ |j |_ dS(Niis ( t find_stmttlevelt readlinestrawRGt expandtabstlinestinsertRtindextstatsR7(tselfRARD((s./usr/lib64/python2.7/Tools/scripts/reindent.pyt__init__s  ,  cCstj|j|j|j}x$|rE|ddkrE|jq"W|j}|jt|dfi}g}|_|dd}|j |d|!xQt t|dD]9}||\}}||dd}t ||} |d} | dkr+| r"|j | d} | dkrxkt |dt|dD]I} || \} } | dkrD| t || kr| d} nPqDqDWn| dkr xgt |dddD]L} || \} } | dkr| t || dt || } PqqWn| dkr(| } q(q+d} n| || <| | }|dksW| dkrn|j |||!qx||||!D]m}|dkr|dkr|j|q|jd||q|tt || }|j||q|WqW|j|jkS(Nis iiiR(ttokenizetgetlinet tokeneaterRMtpopRPtappendRCtaftertextendtranget getlspacetgettxrangetminRK(RQRMRPt have2wantRXRFtthisstmtt thisleveltnextstmtthavetwanttjtjlinetjleveltdiffRDtremove((s./usr/lib64/python2.7/Tools/scripts/reindent.pyRs^      $             cCs|j|jdS(N(t writelinesRX(RQRA((s./usr/lib64/python2.7/Tools/scripts/reindent.pyR scCsD|jt|jkr!d}n|j|j}|jd7_|S(NRi(RORCRM(RQRD((s./usr/lib64/python2.7/Tools/scripts/reindent.pyRTs  c Cs|\} } ||kr$d|_n||krKd|_|jd7_n||krrd|_|jd8_nw|| kr|jr|jj| dfqnF|| krn7|jrd|_|r|jj| |jfqndS(Niii(RHRIRPRW( RQttypettokent.3tendRDtINDENTtDEDENTtNEWLINEtCOMMENTtNLtslinetscol((s./usr/lib64/python2.7/Tools/scripts/reindent.pyRU s$            ( t__name__t __module__RRRR RTRSRoRpRqRrRsRU(((s./usr/lib64/python2.7/Tools/scripts/reindent.pyRs  E  cCsDdt|}}x*||kr?||dkr?|d7}qW|S(NiRi(RC(RDRFtn((s./usr/lib64/python2.7/Tools/scripts/reindent.pyR[4st__main__((Rt __version__RSR)R:RR3RRRR<RRRRR#RRGRR[Rv(((s./usr/lib64/python2.7/Tools/scripts/reindent.pyt(s$       4