Blame tests/modules/specs/_createrepo_c_modularity_hack.py

Packit 6f3914
#!/usr/bin/python
Packit 6f3914
Packit 6f3914
"""
Packit 6f3914
Createrepo_c doesn't support indexing module metadata.
Packit 6f3914
This script indexes all yaml files found in a target directory,
Packit 6f3914
concatenates them and injects into repodata as "modules" mdtype.
Packit 6f3914
"""
Packit 6f3914
Packit 6f3914
import os
Packit 6f3914
import argparse
Packit 6f3914
import subprocess
Packit 6f3914
import tempfile
Packit 6f3914
Packit 6f3914
import gi
Packit 6f3914
gi.require_version('Modulemd', '1.0')
Packit 6f3914
from gi.repository import Modulemd
Packit 6f3914
Packit 6f3914
Packit 6f3914
def get_parser():
Packit 6f3914
    """
Packit 6f3914
    Construct argument parser.
Packit 6f3914
Packit 6f3914
    :returns: ArgumentParser object with arguments set up.
Packit 6f3914
    :rtype: argparse.ArgumentParser
Packit 6f3914
    """
Packit 6f3914
    parser = argparse.ArgumentParser(
Packit 6f3914
        description="Scan directory for modulemd yaml files and inject them into repodata.",
Packit 6f3914
        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
Packit 6f3914
    )
Packit 6f3914
    parser.add_argument(
Packit 6f3914
        "path",
Packit 6f3914
        metavar="directory_to_index",
Packit 6f3914
    )
Packit 6f3914
    return parser
Packit 6f3914
Packit 6f3914
Packit 6f3914
def index_modulemd_files(repo_path):
Packit 6f3914
    result = []
Packit 6f3914
    for fn in sorted(os.listdir(repo_path)):
Packit 6f3914
        if not fn.endswith(".yaml"):
Packit 6f3914
            continue
Packit 6f3914
        yaml_path = os.path.join(repo_path, fn)
Packit 6f3914
        mmd = Modulemd.objects_from_file_ext(yaml_path)
Packit 6f3914
        result.append(mmd[0][0])
Packit 6f3914
    return result
Packit 6f3914
Packit 6f3914
Packit 6f3914
def modify_repo(repo_path, modules):
Packit 6f3914
    tmp = tempfile.mkdtemp()
Packit 6f3914
    path = os.path.join(tmp, "modules.yaml")
Packit 6f3914
    for module in modules:
Packit 6f3914
        Modulemd.dump(modules, path)
Packit 6f3914
    subprocess.check_call(["modifyrepo_c", "--mdtype=modules", path,
Packit 6f3914
                           os.path.join(repo_path, "repodata")])
Packit 6f3914
    os.unlink(path)
Packit 6f3914
    os.rmdir(tmp)
Packit 6f3914
Packit 6f3914
Packit 6f3914
if __name__ == "__main__":
Packit 6f3914
    parser = get_parser()
Packit 6f3914
    args = parser.parse_args()
Packit 6f3914
Packit 6f3914
    modules = index_modulemd_files(args.path)
Packit 6f3914
    modify_repo(args.path, modules)