#!/usr/bin/env python
import os
import logging
import shutil
from argparse import ArgumentParser

from pushsource import Source

log = logging.getLogger("list-push-items")


# This is an example of registering a source which delegates to another.
Source.register_backend(
    "fedkoji", Source.get_partial("koji:https://koji.fedoraproject.org/kojihub")
)
Source.register_backend(
    "brew",
    Source.get_partial(
        "koji:https://brewhub.engineering.redhat.com/brewhub/?basedir=/mnt/brew"
    ),
)

# Here's another example - note that it's even possible to overwrite an existing backend!
Source.register_backend(
    "errata",
    Source.get_partial("errata:https://errata.devel.redhat.com?koji_source=brew:"),
)


def run(args):
    source = Source.get(args.src_url)
    log.info("Loaded source %s", args.src_url)
    itemcount = 0
    for pushitem in source:
        log.info("\nItem: %s", pushitem)
        for dest in pushitem.dest:
            log.info("  To: %s", dest)
        itemcount += 1
    log.info("%s item(s) found in source", itemcount)


def main():
    log.setLevel(logging.INFO)
    logging.basicConfig(format="%(threadName)s %(message)s")

    parser = ArgumentParser(
        description="Report on all push items(s) available from a given source"
    )
    parser.add_argument("--debug", action="store_true")
    parser.add_argument("src_url", help="Push source URL")

    p = parser.parse_args()

    if p.debug:
        log.setLevel(logging.DEBUG)
        logging.getLogger("pushsource").setLevel(logging.DEBUG)

    run(p)


if __name__ == "__main__":
    main()
