churchyard / rpms / python3

Forked from rpms/python3 6 years ago
Clone

Blame 00301-pathfix-add-n-option-for-no-backup.patch

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