gnulib-tool.py: Don't leave temporary directories on exit.
authorCollin Funk <collin.funk1@gmail.com>
Thu, 2 May 2024 07:49:58 +0000 (00:49 -0700)
committerCollin Funk <collin.funk1@gmail.com>
Thu, 2 May 2024 07:49:58 +0000 (00:49 -0700)
* pygnulib/main.py (main_with_exception_handling): Use
tempfile.TemporaryDirectory as a context manager so it is removed before
the program exits.
(main): Expect a temporary directory to be passed as an argument.
* pygnulib/GLConfig.py (GLConfig.__init__): Accept an optional temporary
directory parameter instead of creating one.
* pygnulib/GLImport.py (GLImport.__init__): Don't remove the cache's
temporary directory since it doesn't create one anymore.
(GLImport.execute): Don't remove the temporary directory explicitly. It
is handled by the usage of a context manager.
* pygnulib/GLTestDir.py (GLTestDir.execute, GLMegaTestDir.execute):
Likewise.

ChangeLog
pygnulib/GLConfig.py
pygnulib/GLImport.py
pygnulib/GLTestDir.py
pygnulib/main.py

index bd2c45d9afe22fad60e5a7efdac3400a019624de..22578578471174b33c90ab89fd46b0042b1b818d 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,19 @@
+2024-05-02  Collin Funk  <collin.funk1@gmail.com>
+
+       gnulib-tool.py: Don't leave temporary directories on exit.
+       * pygnulib/main.py (main_with_exception_handling): Use
+       tempfile.TemporaryDirectory as a context manager so it is removed before
+       the program exits.
+       (main): Expect a temporary directory to be passed as an argument.
+       * pygnulib/GLConfig.py (GLConfig.__init__): Accept an optional temporary
+       directory parameter instead of creating one.
+       * pygnulib/GLImport.py (GLImport.__init__): Don't remove the cache's
+       temporary directory since it doesn't create one anymore.
+       (GLImport.execute): Don't remove the temporary directory explicitly. It
+       is handled by the usage of a context manager.
+       * pygnulib/GLTestDir.py (GLTestDir.execute, GLMegaTestDir.execute):
+       Likewise.
+
 2024-05-01  Collin Funk  <collin.funk1@gmail.com>
 
        gnulib-tool.py: Quote file names passed to 'patch'.
index 92aa49d700ddbc6a62609858fa12e9621e994859..b8a7fc5b0baa0feb07286fe1023dca6d3a4f38b8 100644 (file)
@@ -20,7 +20,6 @@ from __future__ import annotations
 #===============================================================================
 import os
 import copy
-import tempfile
 from typing import Any
 from .constants import (
     MODES,
@@ -44,6 +43,7 @@ class GLConfig:
     table: dict[str, Any]
 
     def __init__(self,
+                 tempdir: str | None = None,
                  destdir: str | None = None,
                  localpath: list[str] | None = None,
                  auxdir: str | None = None,
@@ -82,7 +82,7 @@ class GLConfig:
                  errors: bool | None = None) -> None:
         '''Create new GLConfig instance.'''
         self.table = dict()
-        self.table['tempdir'] = tempfile.mkdtemp(prefix='glpy')
+        self.table['tempdir'] = tempdir
         # Check and store the attributes.
         # Remove trailing slashes from the directory names. This is necessary
         # for m4base (to avoid an error in func_import) and optional for the
index 833e186b8f4075ca208078ca3fae0a927cae8496..a11da0e63d6b9110268d3aa3fca74b8a7388c7e7 100644 (file)
@@ -92,7 +92,6 @@ class GLImport:
         # self.cache is the configuration extracted from some files on the
         # file system: configure.{ac,in}, gnulib-cache.m4, gnulib-comp.m4.
         self.cache = GLConfig()
-        os.rmdir(self.cache['tempdir'])
 
         # Read configure.{ac,in}.
         with open(self.config.getAutoconfFile(), mode='r', newline='\n', encoding='utf-8') as file:
@@ -1390,4 +1389,3 @@ 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))
-        rmtree(self.config['tempdir'])
index 2d4c684248a2420271edbb5028500553abe602e5..d3b819bd0db0e1493d16c8e4a11d3f4be02e5c0c 100644 (file)
@@ -840,7 +840,6 @@ class GLTestDir:
         if os.path.isfile(joinpath('build-aux', 'test-driver')):
             _patch_test_driver()
         os.chdir(DIRS['cwd'])
-        rmtree(self.config['tempdir'])
 
 
 #===============================================================================
@@ -1003,4 +1002,3 @@ class GLMegaTestDir:
         if os.path.isfile(joinpath('build-aux', 'test-driver')):
             _patch_test_driver()
         os.chdir(DIRS['cwd'])
-        rmtree(self.config['tempdir'])
index 3230c45a3187db466b39017ee680262622ba70ce..d429a2c47e3dfae3c18a3db0a4c11e9711540511 100644 (file)
@@ -84,7 +84,7 @@ import random
 import argparse
 import subprocess as sp
 import shlex
-from tempfile import mktemp
+import tempfile
 from pygnulib.constants import (
     APP,
     DIRS,
@@ -117,7 +117,7 @@ from pygnulib.GLTestDir import GLMegaTestDir
 #===============================================================================
 # Define main part
 #===============================================================================
-def main() -> None:
+def main(temp_directory: str) -> None:
     info = GLInfo()
     parser = argparse.ArgumentParser(
         prog=APP['name'],
@@ -815,6 +815,7 @@ def main() -> None:
 
     # Create pygnulib configuration.
     config = GLConfig(
+        tempdir=temp_directory,
         destdir=destdir,
         localpath=localpath,
         m4base=m4base,
@@ -853,7 +854,6 @@ def main() -> None:
         modulesystem = GLModuleSystem(config)
         listing = modulesystem.list()
         result = lines_to_multiline(listing)
-        os.rmdir(config['tempdir'])
         print(result, end='')
 
     elif mode == 'find':
@@ -1364,7 +1364,8 @@ def main() -> None:
 
 def main_with_exception_handling() -> None:
     try:  # Try to execute
-        main()
+        with tempfile.TemporaryDirectory() as temporary_directory:
+            main(temporary_directory)
     except GLError as error:
         errmode = 0  # gnulib-style errors
         errno = error.errno