[3.7] bpo-32885: Tools/scripts/pathfix.py: Add -n option for no backup~ (GH-5772) (#6103)
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 5affd5c29e
)
Co-authored-by: Miro Hrončok <miro@hroncok.cz>
This commit is contained in:
parent
7d0528dd3f
commit
6e65e44626
|
@ -686,6 +686,7 @@ Ken Howard
|
||||||
Brad Howes
|
Brad Howes
|
||||||
Mike Hoy
|
Mike Hoy
|
||||||
Ben Hoyt
|
Ben Hoyt
|
||||||
|
Miro Hrončok
|
||||||
Chiu-Hsiang Hsu
|
Chiu-Hsiang Hsu
|
||||||
Chih-Hao Huang
|
Chih-Hao Huang
|
||||||
Christian Hudon
|
Christian Hudon
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Add an ``-n`` flag for ``Tools/scripts/pathfix.py`` to disbale automatic
|
||||||
|
backup creation (files with ``~`` suffix).
|
|
@ -7,8 +7,9 @@
|
||||||
# Directories are searched recursively for files whose name looks
|
# Directories are searched recursively for files whose name looks
|
||||||
# like a python module.
|
# like a python module.
|
||||||
# Symbolic links are always ignored (except as explicit directory
|
# Symbolic links are always ignored (except as explicit directory
|
||||||
# arguments). Of course, the original file is kept as a back-up
|
# arguments).
|
||||||
# (with a "~" attached to its name).
|
# The original file is kept as a back-up (with a "~" attached to its name),
|
||||||
|
# -n flag can be used to disable this.
|
||||||
#
|
#
|
||||||
# Undoubtedly you can do this using find and sed or perl, but this is
|
# Undoubtedly you can do this using find and sed or perl, but this is
|
||||||
# a nice example of Python code that recurses down a directory tree
|
# a nice example of Python code that recurses down a directory tree
|
||||||
|
@ -31,14 +32,17 @@ rep = sys.stdout.write
|
||||||
|
|
||||||
new_interpreter = None
|
new_interpreter = None
|
||||||
preserve_timestamps = False
|
preserve_timestamps = False
|
||||||
|
create_backup = True
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
global new_interpreter
|
global new_interpreter
|
||||||
global preserve_timestamps
|
global preserve_timestamps
|
||||||
usage = ('usage: %s -i /interpreter -p file-or-directory ...\n' %
|
global create_backup
|
||||||
|
usage = ('usage: %s -i /interpreter -p -n file-or-directory ...\n' %
|
||||||
sys.argv[0])
|
sys.argv[0])
|
||||||
try:
|
try:
|
||||||
opts, args = getopt.getopt(sys.argv[1:], 'i:p')
|
opts, args = getopt.getopt(sys.argv[1:], 'i:pn')
|
||||||
except getopt.error as msg:
|
except getopt.error as msg:
|
||||||
err(str(msg) + '\n')
|
err(str(msg) + '\n')
|
||||||
err(usage)
|
err(usage)
|
||||||
|
@ -48,6 +52,8 @@ def main():
|
||||||
new_interpreter = a.encode()
|
new_interpreter = a.encode()
|
||||||
if o == '-p':
|
if o == '-p':
|
||||||
preserve_timestamps = True
|
preserve_timestamps = True
|
||||||
|
if o == '-n':
|
||||||
|
create_backup = False
|
||||||
if not new_interpreter or not new_interpreter.startswith(b'/') or \
|
if not new_interpreter or not new_interpreter.startswith(b'/') or \
|
||||||
not args:
|
not args:
|
||||||
err('-i option or file-or-directory missing\n')
|
err('-i option or file-or-directory missing\n')
|
||||||
|
@ -134,10 +140,16 @@ def fix(filename):
|
||||||
except OSError as msg:
|
except OSError as msg:
|
||||||
err('%s: warning: chmod failed (%r)\n' % (tempname, msg))
|
err('%s: warning: chmod failed (%r)\n' % (tempname, msg))
|
||||||
# Then make a backup of the original file as filename~
|
# Then make a backup of the original file as filename~
|
||||||
try:
|
if create_backup:
|
||||||
os.rename(filename, filename + '~')
|
try:
|
||||||
except OSError as msg:
|
os.rename(filename, filename + '~')
|
||||||
err('%s: warning: backup failed (%r)\n' % (filename, msg))
|
except OSError as msg:
|
||||||
|
err('%s: warning: backup failed (%r)\n' % (filename, msg))
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
os.remove(filename)
|
||||||
|
except OSError as msg:
|
||||||
|
err('%s: warning: removing failed (%r)\n' % (filename, msg))
|
||||||
# Now move the temp file to the original file
|
# Now move the temp file to the original file
|
||||||
try:
|
try:
|
||||||
os.rename(tempname, filename)
|
os.rename(tempname, filename)
|
||||||
|
|
Loading…
Reference in New Issue