Example #1
0
    def test_write_metadata(self):
        # check support of non-ASCII values
        tmp_dir = self.mkdtemp()
        my_file = os.path.join(tmp_dir, 'f')

        metadata = Metadata(mapping={'author': 'Mister Café',
                                     'name': 'my.project',
                                     'author': 'Café Junior',
                                     'summary': 'Café torréfié',
                                     'description': 'Héhéhé',
                                     'keywords': ['café', 'coffee']})
        metadata.write(my_file)

        # the file should use UTF-8
        metadata2 = Metadata()
        with open(my_file, encoding='utf-8') as fp:
            metadata2.read_file(fp)

        # XXX when keywords are not defined, metadata will have
        # 'Keywords': [] but metadata2 will have 'Keywords': ['']
        # because of a value.split(',') in Metadata.get
        self.assertEqual(metadata.items(), metadata2.items())

        # ASCII also works, it's a subset of UTF-8
        metadata = Metadata(mapping={'author': 'Mister Cafe',
                                     'name': 'my.project',
                                     'author': 'Cafe Junior',
                                     'summary': 'Cafe torrefie',
                                     'description': 'Hehehe'})
        metadata.write(my_file)

        metadata2 = Metadata()
        with open(my_file, encoding='utf-8') as fp:
            metadata2.read_file(fp)
Example #2
0
def _convert(egginfo_dir, distinfo_dir=None, handlers=egginfo_handlers):
    """Converts an .egg-info directory structure to a .dist-info structure.
    """
    pkginfo = os.path.join(egginfo_dir, PKGINFO)
    metadata = Metadata(pkginfo)
    #: Define a .dist-info directory location if one wasn't given
    if distinfo_dir is None:
        # Name the directory based on the metadata and PEP 376 naming:
        # http://www.python.org/dev/peps/pep-0376/#one-dist-info-directory-per-installed-distribution
        container = os.path.abspath(egginfo_dir).split(os.sep)[:-1]
        container = os.path.join(*container)
        dirname = "{0}-{1}.dist-info"\
                  .format(metadata['Name'], metadata['Version'])
        distinfo_dir = os.path.join(container, dirname)
    #: Create the .dist-info directory if it doesn't exits
    if not os.path.exists(distinfo_dir):
        os.makedirs(distinfo_dir)
    distinfo_metadata = os.path.join(distinfo_dir, METADATA)
    shutil.copy2(pkginfo, distinfo_metadata)
    #: Pave over the exist metadata variable and use the .dist-info one
    metadata = Metadata(distinfo_metadata)

    fields, version = up_convert(metadata._fields)
    #: Update the fileds and metadata version
    metadata.update(fields)

    if not isinstance(handlers, Registry):
        raise TypeError("Expected a Registry objects recieved a {0}"\
                        .format(type(handlers)))
    handlers.init_handlers(metadata, egginfo_dir)

    for name in handlers:
        handlers[name]()

    metadata.write(distinfo_metadata)
    return distinfo_dir