f21dd0
From 5affd5c29eb1493cb31ef3cfdde15538ac134689 Mon Sep 17 00:00:00 2001
f21dd0
From: =?UTF-8?q?Miro=20Hron=C4=8Dok?= <miro@hroncok.cz>
f21dd0
Date: Tue, 13 Mar 2018 10:56:43 +0100
f21dd0
Subject: [PATCH] bpo-32885: Tools/scripts/pathfix.py: Add -n option for no
f21dd0
 backup~ (#5772)
f21dd0
f21dd0
Creating backup files with ~ suffix can be undesirable in some environment,
f21dd0
such as when building RPM packages. Instead of requiring the user to remove
f21dd0
those files manually, option -n was added, that simply disables this feature.
f21dd0
f21dd0
-n was selected because 2to3 has the same option with this behavior.
f21dd0
---
f21dd0
 Misc/ACKS                                          |  1 +
f21dd0
 .../2018-02-20-12-16-47.bpo-32885.dL5x7C.rst       |  2 ++
f21dd0
 Tools/scripts/pathfix.py                           | 28 +++++++++++++++-------
f21dd0
 3 files changed, 23 insertions(+), 8 deletions(-)
f21dd0
 create mode 100644 Misc/NEWS.d/next/Tools-Demos/2018-02-20-12-16-47.bpo-32885.dL5x7C.rst
f21dd0
f21dd0
diff --git a/Misc/ACKS b/Misc/ACKS
f21dd0
index d8179c8b03ab..d752d8a35434 100644
f21dd0
--- a/Misc/ACKS
f21dd0
+++ b/Misc/ACKS
f21dd0
@@ -687,6 +687,7 @@ Ken Howard
f21dd0
 Brad Howes
f21dd0
 Mike Hoy
f21dd0
 Ben Hoyt
f21dd0
+Miro Hrončok
f21dd0
 Chiu-Hsiang Hsu
f21dd0
 Chih-Hao Huang
f21dd0
 Christian Hudon
f21dd0
diff --git a/Misc/NEWS.d/next/Tools-Demos/2018-02-20-12-16-47.bpo-32885.dL5x7C.rst b/Misc/NEWS.d/next/Tools-Demos/2018-02-20-12-16-47.bpo-32885.dL5x7C.rst
f21dd0
new file mode 100644
f21dd0
index 000000000000..e003e1d84fd0
f21dd0
--- /dev/null
f21dd0
+++ b/Misc/NEWS.d/next/Tools-Demos/2018-02-20-12-16-47.bpo-32885.dL5x7C.rst
f21dd0
@@ -0,0 +1,2 @@
f21dd0
+Add an ``-n`` flag for ``Tools/scripts/pathfix.py`` to disbale automatic
f21dd0
+backup creation (files with ``~`` suffix).
f21dd0
diff --git a/Tools/scripts/pathfix.py b/Tools/scripts/pathfix.py
f21dd0
index 562bbc737812..c5bf984306a3 100755
f21dd0
--- a/Tools/scripts/pathfix.py
f21dd0
+++ b/Tools/scripts/pathfix.py
f21dd0
@@ -7,8 +7,9 @@
f21dd0
 # Directories are searched recursively for files whose name looks
f21dd0
 # like a python module.
f21dd0
 # Symbolic links are always ignored (except as explicit directory
f21dd0
-# arguments).  Of course, the original file is kept as a back-up
f21dd0
-# (with a "~" attached to its name).
f21dd0
+# arguments).
f21dd0
+# The original file is kept as a back-up (with a "~" attached to its name),
f21dd0
+# -n flag can be used to disable this.
f21dd0
 #
f21dd0
 # Undoubtedly you can do this using find and sed or perl, but this is
f21dd0
 # a nice example of Python code that recurses down a directory tree
f21dd0
@@ -31,14 +32,17 @@
f21dd0
 
f21dd0
 new_interpreter = None
f21dd0
 preserve_timestamps = False
f21dd0
+create_backup = True
f21dd0
+
f21dd0
 
f21dd0
 def main():
f21dd0
     global new_interpreter
f21dd0
     global preserve_timestamps
f21dd0
-    usage = ('usage: %s -i /interpreter -p file-or-directory ...\n' %
f21dd0
+    global create_backup
f21dd0
+    usage = ('usage: %s -i /interpreter -p -n file-or-directory ...\n' %
f21dd0
              sys.argv[0])
f21dd0
     try:
f21dd0
-        opts, args = getopt.getopt(sys.argv[1:], 'i:p')
f21dd0
+        opts, args = getopt.getopt(sys.argv[1:], 'i:pn')
f21dd0
     except getopt.error as msg:
f21dd0
         err(str(msg) + '\n')
f21dd0
         err(usage)
f21dd0
@@ -48,6 +52,8 @@ def main():
f21dd0
             new_interpreter = a.encode()
f21dd0
         if o == '-p':
f21dd0
             preserve_timestamps = True
f21dd0
+        if o == '-n':
f21dd0
+            create_backup = False
f21dd0
     if not new_interpreter or not new_interpreter.startswith(b'/') or \
f21dd0
            not args:
f21dd0
         err('-i option or file-or-directory missing\n')
f21dd0
@@ -134,10 +140,16 @@ def fix(filename):
f21dd0
     except OSError as msg:
f21dd0
         err('%s: warning: chmod failed (%r)\n' % (tempname, msg))
f21dd0
     # Then make a backup of the original file as filename~
f21dd0
-    try:
f21dd0
-        os.rename(filename, filename + '~')
f21dd0
-    except OSError as msg:
f21dd0
-        err('%s: warning: backup failed (%r)\n' % (filename, msg))
f21dd0
+    if create_backup:
f21dd0
+        try:
f21dd0
+            os.rename(filename, filename + '~')
f21dd0
+        except OSError as msg:
f21dd0
+            err('%s: warning: backup failed (%r)\n' % (filename, msg))
f21dd0
+    else:
f21dd0
+        try:
f21dd0
+            os.remove(filename)
f21dd0
+        except OSError as msg:
f21dd0
+            err('%s: warning: removing failed (%r)\n' % (filename, msg))
f21dd0
     # Now move the temp file to the original file
f21dd0
     try:
f21dd0
         os.rename(tempname, filename)