Blame SOURCES/ansible-generator

rdobuilder 71c769
#!/usr/bin/python3
rdobuilder 71c769
rdobuilder 71c769
# Copyright (C) 2020 Igor Raits <ignatenkobrain@fedoraproject.org>.
rdobuilder 71c769
#
rdobuilder 71c769
# Fedora-License-Identifier: GPLv3+
rdobuilder 71c769
# SPDX-2.0-License-Identifier: GPL-3.0+
rdobuilder 71c769
# SPDX-3.0-License-Identifier: GPL-3.0-or-later
rdobuilder 71c769
#
rdobuilder 71c769
# This program is free software.
rdobuilder 71c769
# For more information on the license, see COPYING.
rdobuilder 71c769
# For more information on free software, see
rdobuilder 71c769
# <https://www.gnu.org/philosophy/free-sw.en.html>.
rdobuilder 71c769
rdobuilder 71c769
import argparse
rdobuilder 71c769
import json
rdobuilder 71c769
import re
rdobuilder 71c769
import sys
rdobuilder 71c769
rdobuilder 71c769
rdobuilder 71c769
def main():
rdobuilder 71c769
    parser = argparse.ArgumentParser()
rdobuilder 71c769
    group = parser.add_mutually_exclusive_group(required=True)
rdobuilder 71c769
    group.add_argument(
rdobuilder 71c769
        "-P", "--provides", action="store_const", const="provides", dest="action"
rdobuilder 71c769
    )
rdobuilder 71c769
    group.add_argument(
rdobuilder 71c769
        "-R", "--requires", action="store_const", const="requires", dest="action"
rdobuilder 71c769
    )
rdobuilder 71c769
    args = parser.parse_args()
rdobuilder 71c769
rdobuilder 71c769
    files = sys.stdin.read().splitlines()
rdobuilder 71c769
rdobuilder 71c769
    for f in files:
rdobuilder 71c769
        with open(f, "r") as fobj:
rdobuilder 71c769
            info = json.load(fobj)["collection_info"]
rdobuilder 71c769
            if args.action == "provides":
rdobuilder 71c769
                print(
rdobuilder 71c769
                    f"ansible-collection({info['namespace']}.{info['name']}) = {info['version']}"
rdobuilder 71c769
                )
rdobuilder 71c769
            if args.action == "requires":
rdobuilder 71c769
                print("(ansible >= 2.9.0 or ansible-core >= 2.11.0)")
rdobuilder 71c769
                for dep, req in info.get("dependencies", {}).items():
rdobuilder 71c769
                    print(f"ansible-collection({dep})", end="")
rdobuilder 71c769
                    if req == "*":
rdobuilder 71c769
                        print()
rdobuilder 71c769
                        continue
rdobuilder 71c769
                    m = re.match(r"^>=(\d+\.\d+\.\d+)$", req)
rdobuilder 71c769
                    if m:
rdobuilder 71c769
                        print(f" >= {m.group(1)}")
rdobuilder 71c769
                        continue
rdobuilder 71c769
                    raise NotImplementedError(
rdobuilder 71c769
                        "Generation of dependencies different than '*' or '>=' is not supported yet"
rdobuilder 71c769
                    )
rdobuilder 71c769
rdobuilder 71c769
rdobuilder 71c769
if __name__ == "__main__":
rdobuilder 71c769
    main()