Simplify iterating over module defaults

There are now two places where we need to do this, so we can simplify
the logic of finding and filtering them.

Signed-off-by: Lubomír Sedlář <lsedlar@redhat.com>
This commit is contained in:
Lubomír Sedlář 2018-06-22 09:18:10 +02:00
parent 98e7106f3e
commit 6c14236562
3 changed files with 25 additions and 13 deletions

View File

@ -33,7 +33,12 @@ from kobo.shortcuts import run, relative_path
from ..wrappers.scm import get_dir_from_scm from ..wrappers.scm import get_dir_from_scm
from ..wrappers.createrepo import CreaterepoWrapper from ..wrappers.createrepo import CreaterepoWrapper
from .base import PhaseBase from .base import PhaseBase
from ..util import find_old_compose, temp_dir, get_arch_variant_data from ..util import (
find_old_compose,
get_arch_variant_data,
iter_module_defaults,
temp_dir,
)
from pungi import Modulemd from pungi import Modulemd
from pungi.arch import tree_arch_to_yum_arch from pungi.arch import tree_arch_to_yum_arch
@ -244,10 +249,9 @@ def create_variant_repo(compose, arch, variant, pkg_type, modules_metadata=None)
modules.append(repo_mmd) modules.append(repo_mmd)
module_names = set([x.get_name() for x in modules]) module_names = set([x.get_name() for x in modules])
for mmddeffile in glob.glob(os.path.join(compose.paths.work.module_defaults_dir(), "*.yaml")): for mmddef in iter_module_defaults(compose.paths.work.module_defaults_dir()):
for mmddef in Modulemd.objects_from_file(mmddeffile): if mmddef.peek_module_name() in module_names:
if isinstance(mmddef, Modulemd.Defaults) and mmddef.peek_module_name() in module_names: modules.append(mmddef)
modules.append(mmddef)
with temp_dir() as tmp_dir: with temp_dir() as tmp_dir:
modules_path = os.path.join(tmp_dir, "modules.yaml") modules_path = os.path.join(tmp_dir, "modules.yaml")

View File

@ -15,16 +15,14 @@
import collections import collections
import glob
import os import os
import shutil import shutil
from kobo.shortcuts import run from kobo.shortcuts import run
from pungi import Modulemd
from pungi.phases.base import PhaseBase from pungi.phases.base import PhaseBase
from pungi.phases.gather import write_prepopulate_file from pungi.phases.gather import write_prepopulate_file
from pungi.util import temp_dir from pungi.util import temp_dir, iter_module_defaults
from pungi.wrappers.comps import CompsWrapper from pungi.wrappers.comps import CompsWrapper
from pungi.wrappers.createrepo import CreaterepoWrapper from pungi.wrappers.createrepo import CreaterepoWrapper
from pungi.wrappers.scm import get_dir_from_scm, get_file_from_scm from pungi.wrappers.scm import get_dir_from_scm, get_file_from_scm
@ -188,11 +186,9 @@ def validate_module_defaults(path):
:param str path: directory with cloned module defaults :param str path: directory with cloned module defaults
""" """
seen_defaults = collections.defaultdict(set) seen_defaults = collections.defaultdict(set)
for file in glob.glob(os.path.join(path, "*.yaml")):
for mmddef in Modulemd.objects_from_file(file): for mmddef in iter_module_defaults(path):
if not isinstance(mmddef, Modulemd.Defaults): seen_defaults[mmddef.peek_module_name()].add(mmddef.peek_default_stream())
continue
seen_defaults[mmddef.peek_module_name()].add(mmddef.peek_default_stream())
errors = [] errors = []
for module_name, defaults in seen_defaults.items(): for module_name, defaults in seen_defaults.items():

View File

@ -15,6 +15,7 @@
import argparse import argparse
import fnmatch import fnmatch
import glob
import json import json
import subprocess import subprocess
import os import os
@ -34,6 +35,8 @@ from six.moves import urllib, range, shlex_quote
from kobo.shortcuts import run, force_list from kobo.shortcuts import run, force_list
from productmd.common import get_major_version from productmd.common import get_major_version
from pungi import Modulemd
# Patterns that match all names of debuginfo packages # Patterns that match all names of debuginfo packages
DEBUG_PATTERNS = ["*-debuginfo", "*-debuginfo-*", "*-debugsource"] DEBUG_PATTERNS = ["*-debuginfo", "*-debuginfo-*", "*-debugsource"]
@ -855,3 +858,12 @@ def parse_koji_event(event):
raise argparse.ArgumentTypeError( raise argparse.ArgumentTypeError(
"%s is not a number or path to compose with valid Koji event" % event "%s is not a number or path to compose with valid Koji event" % event
) )
def iter_module_defaults(path):
"""Given a path to a directory with yaml files, yield each module default in there.
"""
for file in glob.glob(os.path.join(path, "*.yaml")):
for mmddef in Modulemd.objects_from_file(file):
if isinstance(mmddef, Modulemd.Defaults):
yield mmddef