def test_str_no_variant(self): """ Assert __str__ works with distributions that don't have a Variant. """ d = models.Distribution(family='family', variant=None, version='version', arch='arch') self.assertEqual('distribution: ks-family--version-arch-family-None-version-arch', str(d))
def test_str(self): """ Assert __str__ works with distributions. """ d = models.Distribution(family='family', variant='variant', version='version', arch='arch') self.assertEqual('distribution: ks-family-variant-version-arch-family-' 'variant-version-arch', str(d))
def test_process_download_reports_sanitizes_checksum_type(self): """ Ensure that the process_download_reports() method calls sanitize_checksum_type correctly. """ d = models.Distribution('family', 'variant', 'version', 'arch', {}) mock_report = mock.MagicMock() # This should get altered to sha1 mock_report.data = {'checksumtype': 'sha', 'checksum': 'somesum', 'relativepath': 'some/path'} reports = [mock_report] d.process_download_reports(reports) self.assertEqual(d.metadata['files'][0]['checksumtype'], 'sha1')
def parse_treefile(path): """ The treefile seems to be approximately in INI format, which can be read by the standard library's ConfigParser. :param path: full path to the treefile :return: instance of Distribution model, and a list of dicts describing the distribution's files :rtype: (pulp_rpm.plugins.db.models.Distribution, list of dict) """ parser = ConfigParser.RawConfigParser() # the default implementation of this method makes all option names lowercase, # which we don't want. This is the suggested solution in the python.org docs. parser.optionxform = str with open(path) as open_file: try: parser.readfp(open_file) except ConfigParser.ParsingError: # wouldn't need this if ParsingError subclassed ValueError. raise ValueError('could not parse treeinfo file') # apparently the 'variant' is optional. for example, it does not appear # in the RHEL 5.9 treeinfo file. This is how the previous importer # handled that. try: variant = parser.get(SECTION_GENERAL, 'variant') except ConfigParser.NoOptionError: variant = None try: packagedir = parser.get(SECTION_GENERAL, KEY_PACKAGEDIR) except ConfigParser.NoOptionError: packagedir = None try: model = models.Distribution(parser.get(SECTION_GENERAL, 'family'), variant, parser.get(SECTION_GENERAL, 'version'), parser.get(SECTION_GENERAL, 'arch'), metadata={ KEY_PACKAGEDIR: packagedir, KEY_TIMESTAMP: float( parser.get(SECTION_GENERAL, KEY_TIMESTAMP)), }) except (ConfigParser.NoSectionError, ConfigParser.NoOptionError): raise ValueError( 'invalid treefile: could not find unit key components') files = {} # this section is likely to have all the files we care about listed with # checksums. But, it might not. Other sections checked below will only add # files to the "files" dict if they are not already present. For those cases, # there will not be checksums available. if parser.has_section(SECTION_CHECKSUMS): for item in parser.items(SECTION_CHECKSUMS): relativepath = item[0] checksumtype, checksum = item[1].split(':') checksumtype = verification.sanitize_checksum_type(checksumtype) files[relativepath] = { 'relativepath': relativepath, 'checksum': checksum, 'checksumtype': checksumtype } for section_name in parser.sections(): if section_name.startswith( 'images-') or section_name == SECTION_STAGE2: for item in parser.items(section_name): if item[1] not in files: relativepath = item[1] files[relativepath] = { 'relativepath': relativepath, 'checksum': None, 'checksumtype': None, } return model, files.values()