def test_writable_dir(path):
    if os.name == 'posix':
        return os.access(path, os.W_OK)

    # os.access doesn't work on Windows: http://bugs.python.org/issue2528
    # and we can't use tempfile: http://bugs.python.org/issue22107
    basename = 'accesstest_deleteme_fishfingers_custard_'
    alphabet = 'abcdefghijklmnopqrstuvwxyz0123456789'
    for i in range(10):
        name = basename + ''.join(random.choice(alphabet) for _ in range(6))
        file = os.path.join(path, name)
        try:
            with open(file, mode='xb'):
                pass
        except FileExistsError:
            continue
        except PermissionError:
            # This could be because there's a directory with the same name.
            # But it's highly unlikely there's a directory called that,
            # so we'll assume it's because the parent directory is not writable.
            return False
        else:
            os.unlink(file)
            return True

    # This should never be reached
    msg = ('Unexpected condition testing for writable directory {!r}. '
           'Please open an issue on flit to debug why this occurred.')
    raise EnvironmentError(msg.format(path))
Check if a directory is writable.
dataset/ETHPy150Open takluyver/flit/flit/install.py/test_writable_dir