def download_directory(url, target, insecure=False):
    def mkdir():
        if not mkdir.done:
            try:
                os.mkdir(target)
            except __HOLE__:
                pass
            mkdir.done = True
    mkdir.done = False

    opener = build_opener(insecure=insecure)
    response = opener.open(url)

    if response.info().type == 'text/html':
        contents = response.read()

        parser = ListingParser(url)
        parser.feed(contents)
        for link in parser.links:
            link = resolve_link(link, url)
            if link[-1] == '/':
                link = link[:-1]
            if not link.startswith(url):
                continue
            name = link.rsplit('/', 1)[1]
            if '?' in name:
                continue
            mkdir()
            download_directory(link, os.path.join(target, name), insecure)
        if not mkdir.done:
            # We didn't find anything to write inside this directory
            # Maybe it's a HTML file?
            if url[-1] != '/':
                end = target[-5:].lower()
                if not (end.endswith('.htm') or end.endswith('.html')):
                    target = target + '.html'
                with open(target, 'wb') as fp:
                    fp.write(contents)
    else:
        buffer_size = 4096
        with open(target, 'wb') as fp:
            chunk = response.read(buffer_size)
            while chunk:
                fp.write(chunk)
                chunk = response.read(buffer_size)


###############################################################################
dataset/ETHPy150Open VisTrails/VisTrails/vistrails/packages/URL/http_directory.py/download_directory