def cleanup()

in testsuite/driver/testlib.py [0:0]


    def cleanup():
        testdir = getTestOpts().testdir
        max_attempts = 5
        retries = max_attempts
        def on_error(function, path, excinfo):
            # At least one test (T11489) removes the write bit from a file it
            # produces. Windows refuses to delete read-only files with a
            # permission error. Try setting the write bit and try again.
            os.chmod(path, stat.S_IWRITE)
            function(path)

        # On Windows we have to retry the delete a couple of times.
        # The reason for this is that a FileDelete command just marks a
        # file for deletion. The file is really only removed when the last
        # handle to the file is closed. Unfortunately there are a lot of
        # system services that can have a file temporarily opened using a shared
        # readonly lock, such as the built in AV and search indexer.
        #
        # We can't really guarantee that these are all off, so what we can do is
        # whenever after a rmtree the folder still exists to try again and wait a bit.
        #
        # Based on what I've seen from the tests on CI server, is that this is relatively rare.
        # So overall we won't be retrying a lot. If after a reasonable amount of time the folder is
        # still locked then abort the current test by throwing an exception, this so it won't fail
        # with an even more cryptic error.
        #
        # See Trac #13162
        exception = None
        while retries > 0 and os.path.exists(testdir):
            time.sleep((max_attempts-retries)*6)
            try:
                shutil.rmtree(testdir, onerror=on_error, ignore_errors=False)
            except Exception as e:
                exception = e
            retries -= 1

        if retries == 0 and os.path.exists(testdir):
            raise Exception("Unable to remove folder '%s': %s\nUnable to start current test."
                            % (testdir, exception))