Return matching tarball filename from dist/ dir (if found).

    Setuptools generates a source distribution in a ``dist/`` directory and we
    need to find the exact filename, whether .tgz or .zip.

    We expect "name + '-' + version + '.tar.gz'", but we *can* get a
    -dev.r1234.tar.gz as that can be configured in a setup.cfg.  Not pretty,
    but we don't want to force anyone to modify old tags.

def find_tarball(directory, name, version):
    """Return matching tarball filename from dist/ dir (if found).

    Setuptools generates a source distribution in a ``dist/`` directory and we
    need to find the exact filename, whether .tgz or .zip.

    We expect "name + '-' + version + '.tar.gz'", but we *can* get a
    -dev.r1234.tar.gz as that can be configured in a setup.cfg.  Not pretty,
    but we don't want to force anyone to modify old tags.

    """
    dir_contents = os.listdir(os.path.join(directory, 'dist'))
    candidates = [tarball for tarball in dir_contents
                  if tarball.endswith('.gz')
                  and tarball.startswith(name + '-' + version)]
    if not candidates:
        logger.error("No recognizable distribution found for %s, version %s",
                     name, version)
        logger.error("Contents of %s: %r", directory, dir_contents)
        return
    if len(candidates) > 1:
        # Should not happen.
        logger.warn("More than one candidate distribution found: %r",
                    candidates)
    tarball = candidates[0]
    return os.path.join(directory, 'dist', tarball)