"""
Copyright (c) 2015 Red Hat, Inc
All rights reserved.

This software may be modified and distributed under the terms
of the BSD license. See the LICENSE file for details.


Pre build plugin for koji build system
"""
import os
import koji
from atomic_reactor.constants import YUM_REPOS_DIR

from atomic_reactor.plugin import PreBuildPlugin
from atomic_reactor.util import render_yum_repo


class KojiPlugin(PreBuildPlugin):
    key = "koji"
    can_fail = False

    def __init__(self, tasker, workflow, target, hub, root):
        """
        constructor

        :param tasker: DockerTasker instance
        :param workflow: DockerBuildWorkflow instance
        :param target: string, koji target to use as a source
        :param hub: string, koji hub (xmlrpc)
        :param root: string, koji root (storage)
        """
        # call parent constructor
        super(KojiPlugin, self).__init__(tasker, workflow)
        self.target = target
        self.xmlrpc = koji.ClientSession(hub)
        self.pathinfo = koji.PathInfo(topdir=root)

    def run(self):
        """
        run the plugin
        """
        target_info = self.xmlrpc.getBuildTarget(self.target)
        if target_info is None:
            self.log.error("provided target '%s' doesn't exist", self.target)
            raise RuntimeError("Provided target '%s' doesn't exist!" % self.target)
        tag_info = self.xmlrpc.getTag(target_info['build_tag_name'])
        repo_info = self.xmlrpc.getRepo(tag_info['id'])
        # to use urljoin, we would have to append '/', so let's append everything
        baseurl = self.pathinfo.repo(repo_info['id'], tag_info['name']) + "/$basearch"

        self.log.info("baseurl = '%s'", baseurl)

        repo = {
            'name': 'atomic-reactor-koji-plugin-%s' % self.target,
            'baseurl': baseurl,
            'enabled': 1,
            'gpgcheck': 0,
        }
        path = os.path.join(YUM_REPOS_DIR, self.target + ".repo")
        self.log.info("yum repo of koji target: '%s'", path)
        self.workflow.files[path] = render_yum_repo(repo, escape_dollars=False)
