import os
import tempfile

# The folder to place all temporary files into.
TEMP_DIR = os.environ.get('XDG_RUNTIME_DIR')
if TEMP_DIR:
    # If available use the per user XDS_RUNTIME_DIR on unix based OSs.
    TEMP_DIR = os.path.join(TEMP_DIR, 'GitGutter')
else:
    # Otherwise fallback to the default TEMP folder.
    TEMP_DIR = os.path.join(tempfile.gettempdir(), 'GitGutter')
    if hasattr(os, 'getuid'):
        TEMP_DIR += '.%s' % os.getuid()


class TempFile(object):
    """A temporary file object which allows shared reading.

    All the temporary files created by the `tempfile` module's functions are
    accessible by the calling process only on some operating systems.
    Therefore this class represents a file which is deleted as soon as the
    object is destroyed but without keeping the file open all the time.
    """

    def __init__(self, mode='r'):
        """Initialize TempFile object."""
        try:
            os.mkdir(TEMP_DIR)
        except FileExistsError:
            pass
        self.name = tempfile.mktemp(dir=TEMP_DIR)
        self._mode = mode
        self._file = None
        # Cache unlink to keep it available even though the 'os' module is
        # already None'd out whenever __del__() is called.
        # See python stdlib's tempfile.py for details.
        self._unlink = os.unlink

    def __del__(self):
        """Destroy the TempFile object and remove the file from disk."""
        self.close()
        try:
            self._unlink(self.name)
        except OSError:
            pass

    def __enter__(self):
        """`With` statement support."""
        return self.open()

    def __exit__(self, exc, value, tb):
        """`With` statement support."""
        self.close()

    def open(self):
        """Open temporary file."""
        if self._file is None:
            self._file = open(self.name, mode=self._mode)
        return self._file

    def close(self):
        """Close temporary file."""
        if self._file is not None:
            self._file.close()
            self._file = None

@@ -18,16 +18,28 @@
 tempfile
+%0Aimport time
 %0A%0A# The 
@@ -455,16 +455,648 @@
 uid()%0A%0A%0A
+def plugin_loaded():%0A    %22%22%22Sublime Text plugin loaded callback.%0A%0A    Remove all temporary files older than 2 days. Looks like in some cases some%0A    temporary files are not deleted, if ST is closed. So try to delete too old%0A    ones upon startup. Wait 2 days to reduce the chance of deleting temporary%0A    files of another open ST instance.%0A    %22%22%22%0A    now = time.time()%0A    max_age = 48 * 60 * 60%0A    for name in os.listdir(TEMP_DIR):%0A        try:%0A            path = os.path.join(TEMP_DIR, name)%0A            if now - os.path.getatime(path) %3E max_age:%0A                    os.remove(path)%0A        except OSError:%0A            pass%0A%0A%0A
 class Te

Delete old temporary files upon startup