'Convert an .egg-info directory into a .dist-info directory'

def egg2dist(self, egginfo_path, distinfo_path):

    def adios(p):
        'Appropriately    delete    directory,    file    or    link.'
        if (os.path.exists(p) and (not os.path.islink(p)) and os.path.isdir(p)):
            shutil.rmtree(p)
        elif os.path.exists(p):
            os.unlink(p)
    adios(distinfo_path)
    if (not os.path.exists(egginfo_path)):
        import glob
        pat = os.path.join(os.path.dirname(egginfo_path), '*.egg-info')
        possible = glob.glob(pat)
        err = ('Egg    metadata    expected    at    %s    but    not    found' % (egginfo_path,))
        if possible:
            alt = os.path.basename(possible[0])
            err += ('    (%s    found    -    possible    misnamed    archive    file?)' % (alt,))
        raise ValueError(err)
    if os.path.isfile(egginfo_path):
        pkginfo_path = egginfo_path
        pkg_info = self._pkginfo_to_metadata(egginfo_path, egginfo_path)
        os.mkdir(distinfo_path)
    else:
        pkginfo_path = os.path.join(egginfo_path, 'PKG-INFO')
        pkg_info = self._pkginfo_to_metadata(egginfo_path, pkginfo_path)
        shutil.copytree(egginfo_path, distinfo_path, ignore=(lambda x, y: set(('PKG-INFO', 'requires.txt', 'SOURCES.txt', 'not-zip-safe'))))
        dependency_links_path = os.path.join(distinfo_path, 'dependency_links.txt')
        with open(dependency_links_path, 'r') as dependency_links_file:
            dependency_links = dependency_links_file.read().strip()
        if (not dependency_links):
            adios(dependency_links_path)
    write_pkg_info(os.path.join(distinfo_path, 'METADATA'), pkg_info)
    metadata_path = os.path.join(distinfo_path, 'METADATA')
    self.add_requirements(metadata_path)
    metadata_json_path = os.path.join(distinfo_path, 'metadata.json')
    pymeta = pkginfo_to_dict(metadata_path, distribution=self.distribution)
    if ('description' in pymeta):
        description_filename = 'DESCRIPTION.rst'
        description_text = pymeta.pop('description')
        description_path = os.path.join(distinfo_path, description_filename)
        with open(description_path, 'wb') as description_file:
            description_file.write(description_text.encode('utf-8'))
        pymeta['extensions']['python.details']['document_names']['description'] = description_filename
    license = self.license_file()
    if license:
        license_filename = 'LICENSE.txt'
        shutil.copy(license, os.path.join(self.distinfo_dir, license_filename))
        pymeta['extensions']['python.details']['document_names']['license'] = license_filename
    with open(metadata_json_path, 'w') as metadata_json:
        json.dump(pymeta, metadata_json, sort_keys=True)
    adios(egginfo_path)
