]> Savannah Git Hosting - gnulib.git/commitdiff
gnulib-tool.py: Refactor directory tree removals.
authorBruno Haible <bruno@clisp.org>
Sat, 13 Apr 2024 10:06:34 +0000 (12:06 +0200)
committerBruno Haible <bruno@clisp.org>
Sat, 13 Apr 2024 10:16:51 +0000 (12:16 +0200)
* pygnulib/constants.py (rmtree): New function.
* pygnulib/GLImport.py (GLImport.execute): Use it instead of calling
'rm -rf' directly or shutil.rmtree.
* pygnulib/GLTestDir.py (GLTestDir.execute, GLMegaTestDir.execute):
Likewise.
* pygnulib/main.py (main): Likewise.

ChangeLog
gnulib-tool.py.TODO
pygnulib/GLImport.py
pygnulib/GLTestDir.py
pygnulib/constants.py
pygnulib/main.py

index 80679d0743697de2c15f4c03e970b2b3f74aa0b8..1b2d51c54f3df56d68b410f512ec4c99f11d5b67 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2024-04-13  Bruno Haible  <bruno@clisp.org>
+
+       gnulib-tool.py: Refactor directory tree removals.
+       * pygnulib/constants.py (rmtree): New function.
+       * pygnulib/GLImport.py (GLImport.execute): Use it instead of calling
+       'rm -rf' directly or shutil.rmtree.
+       * pygnulib/GLTestDir.py (GLTestDir.execute, GLMegaTestDir.execute):
+       Likewise.
+       * pygnulib/main.py (main): Likewise.
+
 2024-04-12  Collin Funk  <collin.funk1@gmail.com>
 
        gnulib-tool.py: Fix --copy-file directory creation.
index 368c0addac7fd77a2885c0df1a3d8d23f3224d46..1eada154290e7f7acfd904cd4104bb8131800840 100644 (file)
@@ -10,7 +10,6 @@ Optimize:
 
 Various other refactorings, as deemed useful:
   - Use an enum for 'all', 'old', 'new', 'added', 'removed' in GLImport.py.
-  - sp.call(['rm', '-rf' ...]) versus shutil.rmtree ?
   - go through all the open() and codecs.open() calls and turn them into
         with open(file_name, 'r', newline='\n', encoding='utf-8') as file:
     or
index d45cf6b3ba14001673ce8835eaf42b50c827368b..3d694a7919ede39069b5dd812f84232c7316dd4d 100644 (file)
@@ -1466,4 +1466,4 @@ in <library>_a_LDFLAGS or <library>_la_LDFLAGS when linking a library.''')
             position_early_after = 'AC_PROG_CC'
         print('  - invoke %s_EARLY in %s, right after %s,' % (macro_prefix, configure_ac, position_early_after))
         print('  - invoke %s_INIT in %s.' % (macro_prefix, configure_ac))
-        sp.call(['rm', '-rf', self.config['tempdir']], shell=False)
+        constants.rmtree(self.config['tempdir'])
index 5fd4197e521b2362dd659c92f91d2e0428cd6e6d..be61d58b529b28f3c446b72ebef5fee94d685b19 100644 (file)
@@ -23,7 +23,6 @@ import re
 import sys
 import codecs
 import subprocess as sp
-import shutil
 from pathlib import Path
 from . import constants
 from .enums import CopyAction
@@ -709,7 +708,7 @@ class GLTestDir:
         # automake
         args = [UTILS['automake'], '--add-missing', '--copy']
         constants.execute(args, verbose)
-        shutil.rmtree('autom4te.cache')
+        constants.rmtree('autom4te.cache')
         os.chdir(DIRS['cwd'])
         if inctests and not single_configure:
             # Do not use "${AUTORECONF} --force --install", because it may invoke
@@ -744,7 +743,7 @@ class GLTestDir:
             # automake
             args = [UTILS['automake'], '--add-missing', '--copy']
             constants.execute(args, verbose)
-            shutil.rmtree('autom4te.cache')
+            constants.rmtree('autom4te.cache')
             os.chdir(DIRS['cwd'])
 
         # Need to run configure and make once, to create built files that are to be
@@ -879,7 +878,7 @@ class GLTestDir:
         if isfile(joinpath('build-aux', 'test-driver')):
             _patch_test_driver()
         os.chdir(DIRS['cwd'])
-        sp.call(['rm', '-rf', self.config['tempdir']], shell=False)
+        constants.rmtree(self.config['tempdir'])
 
 
 #===============================================================================
@@ -1036,8 +1035,8 @@ class GLMegaTestDir:
         constants.execute(args, verbose)
         args = [UTILS['automake'], '--add-missing', '--copy']
         constants.execute(args, verbose)
-        shutil.rmtree('autom4te.cache')
+        constants.rmtree('autom4te.cache')
         if isfile(joinpath('build-aux', 'test-driver')):
             _patch_test_driver()
         os.chdir(DIRS['cwd'])
-        sp.call(['rm', '-rf', self.config['tempdir']], shell=False)
+        constants.rmtree(self.config['tempdir'])
index dc1297d52995b4f7dc0c5b930443cc2415f5d360..f307ffa6b1919fa1544942fcb67de4a03898781c 100644 (file)
@@ -454,6 +454,20 @@ def hardlink(src: str, dest: str) -> None:
         ensure_writable(dest)
 
 
+def rmtree(dest: str) -> None:
+    '''Removes the file or directory tree at dest, if it exists.'''
+    # These two implementations are nearly equivalent.
+    # Speed: 'rm -rf' can be a little faster.
+    # Exceptions: shutil.rmtree raises Python exceptions, e.g. PermissionError.
+    if True:
+        sp.run(['rm', '-rf', dest], shell=False)
+    else:
+        try:
+            shutil.rmtree(dest)
+        except FileNotFoundError:
+            pass
+
+
 def filter_filelist(separator: str, filelist: str, prefix: str, suffix: str,
                     removed_prefix: str, removed_suffix: str,
                     added_prefix: str = '', added_suffix: str = '') -> str:
index 4b643050497e899e6d006b697746c535219ceca0..471f4b9fb9010428242767f87e5c47856317e4ee 100644 (file)
@@ -1120,7 +1120,7 @@ def main() -> None:
             sys.stderr.write(message)
             sys.exit(1)
         os.chdir('../..')
-        sp.call(['rm', '-rf', destdir], shell=False)
+        constants.rmtree(destdir)
 
     elif mode == 'megatest':
         if not destdir:
@@ -1150,7 +1150,7 @@ def main() -> None:
             sys.stderr.write(message)
             sys.exit(1)
         os.chdir('../..')
-        sp.call(['rm', '-rf', destdir], shell=False)
+        constants.rmtree(destdir)
 
     elif mode == 'extract-description':
         modulesystem = GLModuleSystem(config)