From 1c6e9e595458a5af2af153374454a97dbb6a0408 Mon Sep 17 00:00:00 2001 From: Lubomír Sedlář Date: Apr 25 2017 12:58:56 +0000 Subject: legacy: Avoid crash on missing paths Pungi 4.1.14 can generate composeinfo without paths (if the location does not exist). The script must be able to handle that. Fixes: #40 Signed-off-by: Lubomír Sedlář --- diff --git a/compose_utils/legacy.py b/compose_utils/legacy.py index 39b29c8..da93fca 100644 --- a/compose_utils/legacy.py +++ b/compose_utils/legacy.py @@ -56,39 +56,36 @@ def any_path(paths, options, arch): def _serialize_paths(conf, section, paths, arch): conf.add_section(section) - conf.set(section, "os_tree", paths.os_tree[arch]) - conf.set(section, "os_dir", paths.os_tree[arch]) - conf.set(section, "packages", paths.packages[arch]) - if paths.repository.get(arch, None): - conf.set(section, "repository", paths.repository[arch]) - if paths.isos.get(arch, None): - conf.set(section, "isos", paths.isos[arch]) - conf.set(section, "iso_dir", paths.isos[arch]) - if paths.jigdos.get(arch, None): - conf.set(section, "jigdos", paths.jigdos[arch]) - conf.set(section, "jigdo_dir", paths.jigdos[arch]) + def _set(key, paths, arch): + if arch in paths: + conf.set(section, key, paths[arch]) + + _set("os_tree", paths.os_tree, arch) + _set("os_dir", paths.os_tree, arch) + _set("packages", paths.packages, arch) + _set("repository", paths.repository, arch) + _set("isos", paths.isos, arch) + _set("iso_dir", paths.isos, arch) + _set("jigdos", paths.jigdos, arch) + _set("jigdo_dir", paths.jigdos, arch) if any_path(paths, ["source_tree", "source_packages", "source_repository", "source_isos", "source_jigdos"], arch): - conf.set(section, "source_tree", paths.source_tree[arch]) - conf.set(section, "source_dir", paths.source_tree[arch]) - conf.set(section, "source_packages", paths.source_packages[arch]) - if paths.source_repository.get(arch, None): - conf.set(section, "source_repository", paths.source_repository[arch]) - if paths.source_isos.get(arch, None): - conf.set(section, "source_isos", paths.source_isos[arch]) - conf.set(section, "source_iso_dir", paths.source_isos[arch]) - if paths.source_jigdos.get(arch, None): - conf.set(section, "source_jigdos", paths.source_jigdos[arch]) - conf.set(section, "source_jigdo_dir", paths.source_jigdos[arch]) + _set("source_tree", paths.source_tree, arch) + _set("source_dir", paths.source_tree, arch) + _set("source_packages", paths.source_packages, arch) + _set("source_repository", paths.source_repository, arch) + _set("source_isos", paths.source_isos, arch) + _set("source_iso_dir", paths.source_isos, arch) + _set("source_jigdos", paths.source_jigdos, arch) + _set("source_jigdo_dir", paths.source_jigdos, arch) if any_path(paths, ["debug_tree", "debug_packages", "debug_repository"], arch): - conf.set(section, "debug_tree", paths.debug_tree[arch]) - conf.set(section, "debug_dir", paths.debug_tree[arch]) - conf.set(section, "debug_packages", paths.debug_packages[arch]) - if paths.debug_repository.get(arch, None): - conf.set(section, "debug_repository", paths.debug_repository[arch]) - conf.set(section, "debuginfo", paths.debug_repository[arch]) + _set("debug_tree", paths.debug_tree, arch) + _set("debug_dir", paths.debug_tree, arch) + _set("debug_packages", paths.debug_packages, arch) + _set("debug_repository", paths.debug_repository, arch) + _set("debuginfo", paths.debug_repository, arch) def _serialize_variant(conf, variant): diff --git a/tests/composes/DP-1.0-20160720.t.8/compose/.composeinfo.empty b/tests/composes/DP-1.0-20160720.t.8/compose/.composeinfo.empty new file mode 100644 index 0000000..0d2ae3f --- /dev/null +++ b/tests/composes/DP-1.0-20160720.t.8/compose/.composeinfo.empty @@ -0,0 +1,24 @@ +[compose] +date = 20160720 +id = DP-1.0-20160720.t.8 +respin = 8 +type = test + +[product] +family = Dummy Product +name = DP-1.0-20160720.t.8 +short = DP +type = ga +variants = Server +version = 1.0 + +[variant-Server] +arches = s390x,x86_64 +id = Server +name = Server +type = variant +uid = Server + +[variant-Server.s390x] + +[variant-Server.x86_64] diff --git a/tests/test_legacy_composeinfo.py b/tests/test_legacy_composeinfo.py index eab1514..52ee839 100644 --- a/tests/test_legacy_composeinfo.py +++ b/tests/test_legacy_composeinfo.py @@ -15,16 +15,16 @@ from compose_utils.legacy import write_legacy_composeinfo BASE_PATH = os.path.join(os.path.dirname(__file__), 'composes', 'DP-1.0-20160720.t.8') -def get_expected(): +def get_expected(ext=''): import pkg_resources from distutils.version import LooseVersion try: productmd_ver = LooseVersion(pkg_resources.get_distribution('productmd').version) if productmd_ver == LooseVersion('1.1'): - return '.productmd11.composeinfo' + return '.productmd11.composeinfo' + ext except pkg_resources.DistributionNotFound: pass - return '.composeinfo' + return '.composeinfo' + ext class LegacyComposeInfoTest(unittest.TestCase): @@ -39,6 +39,20 @@ class LegacyComposeInfoTest(unittest.TestCase): expected = f.read().split() self.assertEqual(output.getvalue().split(), expected) + def test_write_legacy_file_no_paths(self): + output = StringIO.StringIO() + # Keep only Server variant and remove all paths for server + server = self.compose.info.variants['Server'] + server.variants = {} + self.compose.info.variants.variants = {'Server': server} + for type in self.compose.info.variants['Server'].paths._fields: + setattr(self.compose.info.variants['Server'].paths, type, {}) + + write_legacy_composeinfo(self.compose.info, output) + with open(os.path.join(BASE_PATH, 'compose', get_expected('.empty'))) as f: + expected = f.read().split() + self.assertEqual(output.getvalue().split(), expected) + if __name__ == '__main__': unittest.main()