mirror of
				https://github.com/python/cpython.git
				synced 2025-11-03 23:21:29 +00:00 
			
		
		
		
	Now we handle different the backup copy, because of security
issues regarding user/group and permissions. Fixes 1050828.
This commit is contained in:
		
							parent
							
								
									27cca3cca7
								
							
						
					
					
						commit
						f88a077f69
					
				
					 2 changed files with 29 additions and 16 deletions
				
			
		| 
						 | 
				
			
			@ -4,10 +4,11 @@
 | 
			
		|||
 | 
			
		||||
"""reindent [-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.
 | 
			
		||||
-v (--verbose) Verbose.  Print informative msgs; else no output.
 | 
			
		||||
-h (--help)    Help.     Print this usage information and exit.
 | 
			
		||||
-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
 | 
			
		||||
| 
						 | 
				
			
			@ -31,17 +32,23 @@
 | 
			
		|||
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 that
 | 
			
		||||
you'd prefer. You can always use the --nobackup option to prevent this.
 | 
			
		||||
"""
 | 
			
		||||
 | 
			
		||||
__version__ = "1"
 | 
			
		||||
 | 
			
		||||
import tokenize
 | 
			
		||||
import os
 | 
			
		||||
import os, shutil
 | 
			
		||||
import sys
 | 
			
		||||
 | 
			
		||||
verbose = 0
 | 
			
		||||
recurse = 0
 | 
			
		||||
dryrun  = 0
 | 
			
		||||
verbose    = 0
 | 
			
		||||
recurse    = 0
 | 
			
		||||
dryrun     = 0
 | 
			
		||||
makebackup = True
 | 
			
		||||
 | 
			
		||||
def usage(msg=None):
 | 
			
		||||
    if msg is not None:
 | 
			
		||||
| 
						 | 
				
			
			@ -57,10 +64,10 @@ def errprint(*args):
 | 
			
		|||
 | 
			
		||||
def main():
 | 
			
		||||
    import getopt
 | 
			
		||||
    global verbose, recurse, dryrun
 | 
			
		||||
    global verbose, recurse, dryrun, makebackup
 | 
			
		||||
    try:
 | 
			
		||||
        opts, args = getopt.getopt(sys.argv[1:], "drvh",
 | 
			
		||||
                                   ["dryrun", "recurse", "verbose", "help"])
 | 
			
		||||
        opts, args = getopt.getopt(sys.argv[1:], "drnvh",
 | 
			
		||||
                        ["dryrun", "recurse", "nobackup", "verbose", "help"])
 | 
			
		||||
    except getopt.error, msg:
 | 
			
		||||
        usage(msg)
 | 
			
		||||
        return
 | 
			
		||||
| 
						 | 
				
			
			@ -69,6 +76,8 @@ def main():
 | 
			
		|||
            dryrun += 1
 | 
			
		||||
        elif o in ('-r', '--recurse'):
 | 
			
		||||
            recurse += 1
 | 
			
		||||
        elif o in ('-n', '--nobackup'):
 | 
			
		||||
            makebackup = False
 | 
			
		||||
        elif o in ('-v', '--verbose'):
 | 
			
		||||
            verbose += 1
 | 
			
		||||
        elif o in ('-h', '--help'):
 | 
			
		||||
| 
						 | 
				
			
			@ -112,11 +121,10 @@ def check(file):
 | 
			
		|||
                print "But this is a dry run, so leaving it alone."
 | 
			
		||||
        if not dryrun:
 | 
			
		||||
            bak = file + ".bak"
 | 
			
		||||
            if os.path.exists(bak):
 | 
			
		||||
                os.remove(bak)
 | 
			
		||||
            os.rename(file, bak)
 | 
			
		||||
            if verbose:
 | 
			
		||||
                print "renamed", file, "to", bak
 | 
			
		||||
            if makebackup:
 | 
			
		||||
                shutil.copyfile(file, bak)
 | 
			
		||||
                if verbose:
 | 
			
		||||
                    print "backed up", file, "to", bak
 | 
			
		||||
            f = open(file, "w")
 | 
			
		||||
            r.write(f)
 | 
			
		||||
            f.close()
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue