Skip to content

Commit a954919

Browse files
miss-islingtonhroncok
authored andcommitted
[3.6] bpo-32885: Tools/scripts/pathfix.py: Add -n option for no backup~ (GH-5772) (#6104)
Creating backup files with ~ suffix can be undesirable in some environment, such as when building RPM packages. Instead of requiring the user to remove those files manually, option -n was added, that simply disables this feature. -n was selected because 2to3 has the same option with this behavior. (cherry picked from commit 5affd5c) Co-authored-by: Miro Hrončok <[email protected]>
1 parent 0ec0290 commit a954919

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,7 @@ Ken Howard
672672
Brad Howes
673673
Mike Hoy
674674
Ben Hoyt
675+
Miro Hrončok
675676
Chiu-Hsiang Hsu
676677
Chih-Hao Huang
677678
Christian Hudon
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add an ``-n`` flag for ``Tools/scripts/pathfix.py`` to disbale automatic
2+
backup creation (files with ``~`` suffix).

Tools/scripts/pathfix.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
# Directories are searched recursively for files whose name looks
88
# like a python module.
99
# Symbolic links are always ignored (except as explicit directory
10-
# arguments). Of course, the original file is kept as a back-up
11-
# (with a "~" attached to its name).
10+
# arguments).
11+
# The original file is kept as a back-up (with a "~" attached to its name),
12+
# -n flag can be used to disable this.
1213
#
1314
# Undoubtedly you can do this using find and sed or perl, but this is
1415
# a nice example of Python code that recurses down a directory tree
@@ -31,14 +32,17 @@
3132

3233
new_interpreter = None
3334
preserve_timestamps = False
35+
create_backup = True
36+
3437

3538
def main():
3639
global new_interpreter
3740
global preserve_timestamps
38-
usage = ('usage: %s -i /interpreter -p file-or-directory ...\n' %
41+
global create_backup
42+
usage = ('usage: %s -i /interpreter -p -n file-or-directory ...\n' %
3943
sys.argv[0])
4044
try:
41-
opts, args = getopt.getopt(sys.argv[1:], 'i:p')
45+
opts, args = getopt.getopt(sys.argv[1:], 'i:pn')
4246
except getopt.error as msg:
4347
err(str(msg) + '\n')
4448
err(usage)
@@ -48,6 +52,8 @@ def main():
4852
new_interpreter = a.encode()
4953
if o == '-p':
5054
preserve_timestamps = True
55+
if o == '-n':
56+
create_backup = False
5157
if not new_interpreter or not new_interpreter.startswith(b'/') or \
5258
not args:
5359
err('-i option or file-or-directory missing\n')
@@ -134,10 +140,16 @@ def fix(filename):
134140
except OSError as msg:
135141
err('%s: warning: chmod failed (%r)\n' % (tempname, msg))
136142
# Then make a backup of the original file as filename~
137-
try:
138-
os.rename(filename, filename + '~')
139-
except OSError as msg:
140-
err('%s: warning: backup failed (%r)\n' % (filename, msg))
143+
if create_backup:
144+
try:
145+
os.rename(filename, filename + '~')
146+
except OSError as msg:
147+
err('%s: warning: backup failed (%r)\n' % (filename, msg))
148+
else:
149+
try:
150+
os.remove(filename)
151+
except OSError as msg:
152+
err('%s: warning: removing failed (%r)\n' % (filename, msg))
141153
# Now move the temp file to the original file
142154
try:
143155
os.rename(tempname, filename)

0 commit comments

Comments
 (0)