[diffoscope] 02/05: Move tempfile handling to its own module.

Chris Lamb chris at chris-lamb.co.uk
Wed Dec 28 13:45:09 CET 2016


This is an automated email from the git hooks/post-receive script.

lamby pushed a commit to branch master
in repository diffoscope.

commit 58fd25eae477d941e86f5aaefe72576e5e618520
Author: Chris Lamb <lamby at debian.org>
Date:   Wed Dec 28 12:37:24 2016 +0000

    Move tempfile handling to its own module.
    
    Signed-off-by: Chris Lamb <lamby at debian.org>
---
 diffoscope/__init__.py                     | 34 ------------------
 diffoscope/comparators/apk.py              |  4 +--
 diffoscope/comparators/device.py           |  2 +-
 diffoscope/comparators/elf.py              |  3 +-
 diffoscope/comparators/rpm.py              |  3 +-
 diffoscope/comparators/symlink.py          |  3 +-
 diffoscope/comparators/utils/archive.py    |  3 +-
 diffoscope/comparators/utils/libarchive.py |  3 +-
 diffoscope/main.py                         |  3 +-
 diffoscope/tempfiles.py                    | 55 ++++++++++++++++++++++++++++++
 10 files changed, 70 insertions(+), 43 deletions(-)

diff --git a/diffoscope/__init__.py b/diffoscope/__init__.py
index 5c372a1..7347202 100644
--- a/diffoscope/__init__.py
+++ b/diffoscope/__init__.py
@@ -21,7 +21,6 @@ import os
 import shutil
 import logging
 import platform
-import tempfile
 import functools
 import time
 
@@ -95,36 +94,3 @@ def set_locale():
     os.environ['LC_CTYPE'] = 'C.UTF-8'
     os.environ['TZ'] = 'UTC'
     time.tzset()
-
-
-temp_files = []
-temp_dirs = []
-
-
-def get_named_temporary_file(*args, **kwargs):
-    kwargs['suffix'] = kwargs.pop('suffix', '_diffoscope')
-    f = tempfile.NamedTemporaryFile(*args, **kwargs)
-    temp_files.append(f.name)
-    return f
-
-
-def get_temporary_directory(*args, **kwargs):
-    kwargs['suffix'] = kwargs.pop('suffix', '_diffoscope')
-    d = tempfile.TemporaryDirectory(*args, **kwargs)
-    temp_dirs.append(d)
-    return d
-
-
-def clean_all_temp_files():
-    for temp_file in temp_files:
-        try:
-            os.unlink(temp_file)
-        except FileNotFoundError:
-            pass
-        except:
-            logger.exception('Unable to delete %s', temp_file)
-    for temp_dir in temp_dirs:
-        try:
-            temp_dir.cleanup()
-        except:
-            logger.exception('Unable to delete %s', temp_dir)
diff --git a/diffoscope/comparators/apk.py b/diffoscope/comparators/apk.py
index c5e2008..786c3e1 100644
--- a/diffoscope/comparators/apk.py
+++ b/diffoscope/comparators/apk.py
@@ -21,11 +21,11 @@ import re
 import os.path
 import subprocess
 
-from diffoscope import logger, tool_required, get_temporary_directory
+from diffoscope import logger, tool_required
+from diffoscope.tempfiles import get_temporary_directory
 
 from .binary import File
 from .utils.archive import Archive
-from .utils.filenames import get_compressed_content_name
 
 class ApkContainer(Archive):
     @property
diff --git a/diffoscope/comparators/device.py b/diffoscope/comparators/device.py
index 601402e..974e4a7 100644
--- a/diffoscope/comparators/device.py
+++ b/diffoscope/comparators/device.py
@@ -20,7 +20,7 @@
 import os
 import stat
 
-from diffoscope import get_named_temporary_file
+from diffoscope.tempfiles import get_named_temporary_file
 from diffoscope.difference import Difference
 
 from .binary import File, FilesystemFile
diff --git a/diffoscope/comparators/elf.py b/diffoscope/comparators/elf.py
index 0b82993..f688933 100644
--- a/diffoscope/comparators/elf.py
+++ b/diffoscope/comparators/elf.py
@@ -22,8 +22,9 @@ import os.path
 import subprocess
 import collections
 
-from diffoscope import tool_required, logger, get_named_temporary_file
+from diffoscope import tool_required, logger
 from diffoscope.exc import OutputParsingError
+from diffoscope.tempfiles import get_named_temporary_file
 from diffoscope.difference import Difference
 
 from .deb import DebFile, get_build_id_map
diff --git a/diffoscope/comparators/rpm.py b/diffoscope/comparators/rpm.py
index dc271d8..746dd01 100644
--- a/diffoscope/comparators/rpm.py
+++ b/diffoscope/comparators/rpm.py
@@ -24,7 +24,8 @@ import os.path
 import binascii
 import subprocess
 
-from diffoscope import logger, tool_required, get_temporary_directory
+from diffoscope import logger, tool_required
+from diffoscope.tempfiles import get_temporary_directory
 from diffoscope.difference import Difference
 
 from .rpm_fallback import AbstractRpmFile
diff --git a/diffoscope/comparators/symlink.py b/diffoscope/comparators/symlink.py
index f7c4784..7528ec1 100644
--- a/diffoscope/comparators/symlink.py
+++ b/diffoscope/comparators/symlink.py
@@ -19,7 +19,8 @@
 
 import os
 
-from diffoscope import logger, get_named_temporary_file
+from diffoscope import logger
+from diffoscope.tempfiles import get_named_temporary_file
 from diffoscope.difference import Difference
 
 from .binary import File
diff --git a/diffoscope/comparators/utils/archive.py b/diffoscope/comparators/utils/archive.py
index 201962a..25dd61f 100644
--- a/diffoscope/comparators/utils/archive.py
+++ b/diffoscope/comparators/utils/archive.py
@@ -19,8 +19,9 @@
 
 import abc
 
-from diffoscope import logger, get_temporary_directory
+from diffoscope import logger
 from diffoscope.profiling import profile
+from diffoscope.tempfiles import get_temporary_directory
 
 from ..missing_file import MissingFile
 
diff --git a/diffoscope/comparators/utils/libarchive.py b/diffoscope/comparators/utils/libarchive.py
index ed48721..e9a788a 100644
--- a/diffoscope/comparators/utils/libarchive.py
+++ b/diffoscope/comparators/utils/libarchive.py
@@ -23,7 +23,8 @@ import os.path
 import ctypes
 import libarchive
 
-from diffoscope import logger, get_temporary_directory
+from diffoscope import logger
+from diffoscope.tempfiles import get_temporary_directory
 
 from ..device import Device
 from ..symlink import Symlink
diff --git a/diffoscope/main.py b/diffoscope/main.py
index 50ff83a..4dd40bd 100644
--- a/diffoscope/main.py
+++ b/diffoscope/main.py
@@ -28,10 +28,11 @@ import argparse
 import traceback
 import contextlib
 
-from diffoscope import logger, VERSION, set_locale, clean_all_temp_files
+from diffoscope import logger, VERSION, set_locale
 from diffoscope.exc import RequiredToolNotFound
 from diffoscope.config import Config
 from diffoscope.progress import ProgressManager, Progress
+from diffoscope.tempfiles import clean_all_temp_files
 from diffoscope.profiling import ProfileManager, profile
 from diffoscope.difference import Difference
 from diffoscope.presenters.html import output_html, output_html_directory, \
diff --git a/diffoscope/tempfiles.py b/diffoscope/tempfiles.py
new file mode 100644
index 0000000..bf6953f
--- /dev/null
+++ b/diffoscope/tempfiles.py
@@ -0,0 +1,55 @@
+# -*- coding: utf-8 -*-
+#
+# diffoscope: in-depth comparison of files, archives, and directories
+#
+# Copyright © 2016 Chris Lamb <lamby at debian.org>
+#
+# diffoscope is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# diffoscope is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with diffoscope.  If not, see <https://www.gnu.org/licenses/>.
+
+import os
+import tempfile
+
+from diffoscope import logger
+
+temp_dirs = []
+temp_files = []
+
+
+def get_named_temporary_file(*args, **kwargs):
+    kwargs['suffix'] = kwargs.pop('suffix', '_diffoscope')
+    f = tempfile.NamedTemporaryFile(*args, **kwargs)
+    temp_files.append(f.name)
+    return f
+
+
+def get_temporary_directory(*args, **kwargs):
+    kwargs['suffix'] = kwargs.pop('suffix', '_diffoscope')
+    d = tempfile.TemporaryDirectory(*args, **kwargs)
+    temp_dirs.append(d)
+    return d
+
+
+def clean_all_temp_files():
+    for temp_file in temp_files:
+        try:
+            os.unlink(temp_file)
+        except FileNotFoundError:
+            pass
+        except:
+            logger.exception('Unable to delete %s', temp_file)
+    for temp_dir in temp_dirs:
+        try:
+            temp_dir.cleanup()
+        except:
+            logger.exception('Unable to delete %s', temp_dir)

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/reproducible/diffoscope.git


More information about the diffoscope mailing list