Skip to content

[3.6] bpo-32885: Tools/scripts/pathfix.py: Add -n option for no backup~ (GH-5772) #6104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,7 @@ Ken Howard
Brad Howes
Mike Hoy
Ben Hoyt
Miro Hrončok
Chiu-Hsiang Hsu
Chih-Hao Huang
Christian Hudon
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add an ``-n`` flag for ``Tools/scripts/pathfix.py`` to disbale automatic
backup creation (files with ``~`` suffix).
28 changes: 20 additions & 8 deletions Tools/scripts/pathfix.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
# Directories are searched recursively for files whose name looks
# like a python module.
# Symbolic links are always ignored (except as explicit directory
# arguments). Of course, the original file is kept as a back-up
# (with a "~" attached to its name).
# arguments).
# 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
# a nice example of Python code that recurses down a directory tree
Expand All @@ -31,14 +32,17 @@

new_interpreter = None
preserve_timestamps = False
create_backup = True


def main():
global new_interpreter
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])
try:
opts, args = getopt.getopt(sys.argv[1:], 'i:p')
opts, args = getopt.getopt(sys.argv[1:], 'i:pn')
except getopt.error as msg:
err(str(msg) + '\n')
err(usage)
Expand All @@ -48,6 +52,8 @@ def main():
new_interpreter = a.encode()
if o == '-p':
preserve_timestamps = True
if o == '-n':
create_backup = False
if not new_interpreter or not new_interpreter.startswith(b'/') or \
not args:
err('-i option or file-or-directory missing\n')
Expand Down Expand Up @@ -134,10 +140,16 @@ def fix(filename):
except OSError as msg:
err('%s: warning: chmod failed (%r)\n' % (tempname, msg))
# Then make a backup of the original file as filename~
try:
os.rename(filename, filename + '~')
except OSError as msg:
err('%s: warning: backup failed (%r)\n' % (filename, msg))
if create_backup:
try:
os.rename(filename, filename + '~')
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
try:
os.rename(tempname, filename)
Expand Down