* 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-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.
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
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'])
import sys
import codecs
import subprocess as sp
-import shutil
from pathlib import Path
from . import constants
from .enums import CopyAction
# 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
# 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
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'])
#===============================================================================
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'])
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:
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:
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)