churchyard / rpms / python3

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