def test_xml_parser_other_repo02(self): userdata = { "pkgs": [], "pkgcb_calls": 0, "warnings": [] } def newpkgcb(pkgId, name, arch): pkg = cr.Package() userdata["pkgs"].append(pkg) return pkg def pkgcb(pkg): userdata["pkgcb_calls"] += 1 def warningcb(warn_type, msg): userdata["warnings"].append((warn_type, msg)) with open(OTHER_SNIPPET_02) as other_snip: cr.xml_parse_other_snippet(other_snip.read(), newpkgcb, pkgcb, warningcb) self.assertEqual([pkg.name for pkg in userdata["pkgs"]], ['fake_bash', 'super_kernel']) self.assertEqual(userdata["pkgcb_calls"], 2) self.assertEqual(userdata["warnings"], [])
def parse_repodata(primary_xml, filelists_xml, other_xml): """ Parse repodata to extract package info. Args: primary_xml (str): a string containing contents of primary.xml filelists_xml (str): a string containing contents of filelists.xml other_xml (str): a string containing contents of other.xml Returns: dict: createrepo_c package objects with the pkgId as a key """ package = None def pkgcb(pkg): """ A callback which is used when a whole package entry in xml is parsed. Args: pkg(preaterepo_c.Package): a parsed metadata for a package """ nonlocal package package = pkg def newpkgcb(pkgId, name, arch): """ A callback which is used when a new package entry is encountered. Only opening <package> element is parsed at that moment. This function has to return a package which parsed data will be added to or None if a package should be skipped. pkgId, name and arch of a package can be used to skip further parsing. Available only for filelists.xml and other.xml. Args: pkgId(str): pkgId of a package name(str): name of a package arch(str): arch of a package Returns: createrepo_c.Package: a package which parsed data should be added to. If None is returned, further parsing of a package will be skipped. """ return package # TODO: handle parsing errors/warnings, warningcb callback can be used below cr.xml_parse_primary_snippet(primary_xml, pkgcb=pkgcb, do_files=False) cr.xml_parse_filelists_snippet(filelists_xml, newpkgcb=newpkgcb) cr.xml_parse_other_snippet(other_xml, newpkgcb=newpkgcb) changelogs = package.changelogs # make sure the changelogs are sorted by date changelogs.sort(key=lambda t: t[1]) # keep only the last 10 changelogs package.changelogs = changelogs[-10:] return package
def test_xml_parser_other_snippet01(self): userdata = {"pkgs": [], "pkgcb_calls": 0, "warnings": []} def newpkgcb(pkgId, name, arch): pkg = cr.Package() userdata["pkgs"].append(pkg) return pkg def pkgcb(pkg): userdata["pkgcb_calls"] += 1 def warningcb(warn_type, msg): userdata["warnings"].append((warn_type, msg)) content = open(OTHER_SNIPPET_01).read() cr.xml_parse_other_snippet(content, newpkgcb, pkgcb, warningcb) self.assertEqual(userdata["warnings"], []) self.assertEqual([pkg.name for pkg in userdata["pkgs"]], ['super_kernel']) self.assertEqual(userdata["pkgcb_calls"], 1) pkg = userdata["pkgs"][0] self.assertEqual( pkg.pkgId, "152824bff2aa6d54f429d43e87a3ff3a0286505c6d93ec87692b5e3a9e3b97bf") self.assertEqual(pkg.name, "super_kernel") self.assertEqual(pkg.arch, "x86_64") self.assertEqual(pkg.version, "6.0.1") self.assertEqual(pkg.epoch, "0") self.assertEqual(pkg.release, "2") self.assertEqual(pkg.summary, None) self.assertEqual(pkg.description, None) self.assertEqual(pkg.url, None) self.assertEqual(pkg.time_file, 0) self.assertEqual(pkg.time_build, 0) self.assertEqual(pkg.rpm_license, None) self.assertEqual(pkg.rpm_vendor, None) self.assertEqual(pkg.rpm_group, None) self.assertEqual(pkg.rpm_buildhost, None) self.assertEqual(pkg.rpm_sourcerpm, None) self.assertEqual(pkg.rpm_header_start, 0) self.assertEqual(pkg.rpm_header_end, 0) self.assertEqual(pkg.rpm_packager, None) self.assertEqual(pkg.size_package, 0) self.assertEqual(pkg.size_installed, 0) self.assertEqual(pkg.size_archive, 0) self.assertEqual(pkg.location_href, None) self.assertEqual(pkg.location_base, None) self.assertEqual(pkg.checksum_type, None) self.assertEqual(pkg.requires, []) self.assertEqual(pkg.provides, []) self.assertEqual(pkg.conflicts, []) self.assertEqual(pkg.obsoletes, []) self.assertEqual(pkg.files, []) self.assertEqual(pkg.changelogs, [('Tomas Mlcoch <*****@*****.**> - 6.0.1-1', 1334664000, '- First release'), ('Tomas Mlcoch <*****@*****.**> - 6.0.1-2', 1334664001, '- Second release')])