tempfiles.py 2.67 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
# -*- coding: utf-8 -*-
#
# diffoscope: in-depth comparison of files, archives, and directories
#
# Copyright © 2016 Chris Lamb <lamby@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
21
import logging
22 23
import tempfile

Chris Lamb's avatar
Chris Lamb committed
24
_DIRS, _FILES = [], []
25

26 27
logger = logging.getLogger(__name__)

28 29

def get_named_temporary_file(*args, **kwargs):
30
    kwargs['dir'] = _get_base_temporary_directory()
31
    kwargs['suffix'] = kwargs.pop('suffix', '_diffoscope')
Chris Lamb's avatar
Chris Lamb committed
32

33
    f = tempfile.NamedTemporaryFile(*args, **kwargs)
Chris Lamb's avatar
Chris Lamb committed
34
    _FILES.append(f.name)
35

Chris Lamb's avatar
Chris Lamb committed
36
    return f
37

Chris Lamb's avatar
Chris Lamb committed
38

39
def get_temporary_directory(*args, **kwargs):
40
    kwargs['dir'] = _get_base_temporary_directory()
41
    kwargs['suffix'] = kwargs.pop('suffix', '_diffoscope')
Chris Lamb's avatar
Chris Lamb committed
42

43
    d = tempfile.TemporaryDirectory(*args, **kwargs)
Chris Lamb's avatar
Chris Lamb committed
44
    _DIRS.append(d)
45

Chris Lamb's avatar
Chris Lamb committed
46
    return d
47

Chris Lamb's avatar
Chris Lamb committed
48

49
def clean_all_temp_files():
50 51
    logger.debug("Cleaning %d temp files", len(_FILES))

Chris Lamb's avatar
Chris Lamb committed
52
    for x in _FILES:
53
        try:
Chris Lamb's avatar
Chris Lamb committed
54
            os.unlink(x)
55 56 57
        except FileNotFoundError:
            pass
        except:
Chris Lamb's avatar
Chris Lamb committed
58 59
            logger.exception("Unable to delete %s", x)

60 61
    logger.debug("Cleaning %d temporary directories", len(_DIRS))

62 63
    # Reverse so we delete the top-level directory last.
    for x in reversed(_DIRS):
64
        try:
Chris Lamb's avatar
Chris Lamb committed
65
            x.cleanup()
66
        except PermissionError:
67 68 69
            # Recursively reset the permissions of temporary directories prior
            # to deletion to ensure that non-writable permissions such as 0555
            # are removed and do not cause a traceback. (#891363)
70 71 72 73 74
            for dirpath, ys, _ in os.walk(x.name):
                for y in ys:
                    os.chmod(os.path.join(dirpath, y), 0o777)
            # try removing it again now
            x.cleanup()
75 76
        except FileNotFoundError:
            pass
77
        except:
Chris Lamb's avatar
Chris Lamb committed
78
            logger.exception("Unable to delete %s", x)
79 80 81 82 83 84 85 86 87 88 89 90 91 92


def _get_base_temporary_directory():
    if not _DIRS:
        d = tempfile.TemporaryDirectory(
            dir=tempfile.gettempdir(),
            prefix='diffoscope-',
        )

        logger.debug("Created top-level temporary directory: %s", d.name)

        _DIRS.append(d)

    return _DIRS[0].name