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