# Copyright (c) 2012-2013 Paul Tagliamonte <paultag@debian.org>
# Copyright (c) 2013 Leo Cavaille <leo@cavaille.net>
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

from debile.slave.utils import tdir, cd, dget, run_command
from debile.slave.core import config

from contextlib import contextmanager
import xmlrpclib
import os


def get_proxy():
    xml = config.get("xmlrpc", None)
    if xml is None:
        raise Exception("No xmlrpc found in slave yaml")

    proxy = xmlrpclib.ServerProxy(
        "http://{user}:{password}@{host}:{port}/".format(
            user=xml['user'],
            password=xml['password'],
            host=xml['host'],
            port=xml['port'],
        ), allow_none=True)
    return proxy


@contextmanager
def checkout(package):
    proxy = get_proxy()
    _type = package['type']
    if _type not in ['binary', 'source']:
        raise ValueError("type sucks")

    def source():
        url = proxy.get_dsc_url(package['source_id'])
        dsc = os.path.basename(url)
        dget(url)
        yield dsc

    def binary():
        url = proxy.get_deb_url(package['binary_id'])
        deb = os.path.basename(url)
        out, err, ret = run_command(['wget', url])
        if ret != 0:
            raise Exception("zomgwtf")
        yield deb

    with tdir() as where:
        with cd(where):
            for x in {"source": source, "binary": binary}[_type]():
                yield x

@@ -1735,704 +1735,4 @@
 oxy%0A
-%0A%0A@contextmanager%0Adef checkout(package):%0A    proxy = get_proxy()%0A    _type = package%5B'type'%5D%0A    if _type not in %5B'binary', 'source'%5D:%0A        raise ValueError(%22type sucks%22)%0A%0A    def source():%0A        url = proxy.get_dsc_url(package%5B'source_id'%5D)%0A        dsc = os.path.basename(url)%0A        dget(url)%0A        yield dsc%0A%0A    def binary():%0A        url = proxy.get_deb_url(package%5B'binary_id'%5D)%0A        deb = os.path.basename(url)%0A        out, err, ret = run_command(%5B'wget', url%5D)%0A        if ret != 0:%0A            raise Exception(%22zomgwtf%22)%0A        yield deb%0A%0A    with tdir() as where:%0A        with cd(where):%0A            for x in %7B%22source%22: source, %22binary%22: binary%7D%5B_type%5D():%0A                yield x%0A

Drop unused and broken function