Skip to content

Commit c0e669a

Browse files
author
facundo.batista
committed
Now we handle different the backup copy, because of security issues regarding user/group and permissions. Fixes 1050828. git-svn-id: http://svn.python.org/projects/python/trunk@60877 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 904ec8e commit c0e669a

2 files changed

Lines changed: 29 additions & 16 deletions

File tree

Misc/NEWS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,6 +1378,11 @@ Tests
13781378
Tools
13791379
-----
13801380

1381+
- Tools/scripts/reindent.py now creates the backup file using shutil.copy
1382+
to preserve user/group and permissions. Added also a --nobackup option
1383+
to not create the backup if the user is concerned regarding this. Check
1384+
issue 1050828 for more details.
1385+
13811386
- Tools/scripts/win_add2path.py was added. The simple script modifes the
13821387
PATH environment var of the HKCU tree and adds the python bin and script
13831388
directory.

Tools/scripts/reindent.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44

55
"""reindent [-d][-r][-v] [ path ... ]
66
7-
-d (--dryrun) Dry run. Analyze, but don't make any changes to, files.
8-
-r (--recurse) Recurse. Search for all .py files in subdirectories too.
9-
-v (--verbose) Verbose. Print informative msgs; else no output.
10-
-h (--help) Help. Print this usage information and exit.
7+
-d (--dryrun) Dry run. Analyze, but don't make any changes to, files.
8+
-r (--recurse) Recurse. Search for all .py files in subdirectories too.
9+
-n (--nobackup) No backup. Does not make a ".bak" file before reindenting.
10+
-v (--verbose) Verbose. Print informative msgs; else no output.
11+
-h (--help) Help. Print this usage information and exit.
1112
1213
Change Python (.py) files to use 4-space indents and no hard tab characters.
1314
Also trim excess spaces and tabs from ends of lines, and remove empty lines
@@ -31,17 +32,23 @@
3132
The hard part of reindenting is figuring out what to do with comment
3233
lines. So long as the input files get a clean bill of health from
3334
tabnanny.py, reindent should do a good job.
35+
36+
The backup file is a copy of the one that is being reindented. The ".bak"
37+
file is generated with shutil.copy(), but some corner cases regarding
38+
user/group and permissions could leave the backup file more readable that
39+
you'd prefer. You can always use the --nobackup option to prevent this.
3440
"""
3541

3642
__version__ = "1"
3743

3844
import tokenize
39-
import os
45+
import os, shutil
4046
import sys
4147

42-
verbose = 0
43-
recurse = 0
44-
dryrun = 0
48+
verbose = 0
49+
recurse = 0
50+
dryrun = 0
51+
makebackup = True
4552

4653
def usage(msg=None):
4754
if msg is not None:
@@ -57,10 +64,10 @@ def errprint(*args):
5764

5865
def main():
5966
import getopt
60-
global verbose, recurse, dryrun
67+
global verbose, recurse, dryrun, makebackup
6168
try:
62-
opts, args = getopt.getopt(sys.argv[1:], "drvh",
63-
["dryrun", "recurse", "verbose", "help"])
69+
opts, args = getopt.getopt(sys.argv[1:], "drnvh",
70+
["dryrun", "recurse", "nobackup", "verbose", "help"])
6471
except getopt.error, msg:
6572
usage(msg)
6673
return
@@ -69,6 +76,8 @@ def main():
6976
dryrun += 1
7077
elif o in ('-r', '--recurse'):
7178
recurse += 1
79+
elif o in ('-n', '--nobackup'):
80+
makebackup = False
7281
elif o in ('-v', '--verbose'):
7382
verbose += 1
7483
elif o in ('-h', '--help'):
@@ -112,11 +121,10 @@ def check(file):
112121
print "But this is a dry run, so leaving it alone."
113122
if not dryrun:
114123
bak = file + ".bak"
115-
if os.path.exists(bak):
116-
os.remove(bak)
117-
os.rename(file, bak)
118-
if verbose:
119-
print "renamed", file, "to", bak
124+
if makebackup:
125+
shutil.copyfile(file, bak)
126+
if verbose:
127+
print "backed up", file, "to", bak
120128
f = open(file, "w")
121129
r.write(f)
122130
f.close()

0 commit comments

Comments
 (0)