#!/usr/bin/python3
# Copyright 2010 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).
#
# This is a script to do end-to-end testing of the buildd with a bzr-builder
# recipe, without involving the BuilderBehaviour.

from __future__ import print_function

import sys

from six.moves.xmlrpc_client import ServerProxy

country_code = 'us'
apt_cacher_ng_host = 'stumpy'
distroseries_name = 'maverick'
recipe_text = """# bzr-builder format 0.2 deb-version {debupstream}-0~{revno}
http://bazaar.launchpad.dev/~ppa-user/+junk/wakeonlan"""


def deb_line(host, suites):
    prefix = 'deb http://'
    if apt_cacher_ng_host is not None:
        prefix += '%s:3142/' % apt_cacher_ng_host
    return '%s%s %s %s' % (prefix, host, distroseries_name, suites)


proxy = ServerProxy('http://localhost:8221/rpc')
print(proxy.echo('Hello World'))
print(proxy.info())
status = proxy.status()
print(status)
if status[0] != 'BuilderStatus.IDLE':
    print("Aborting due to non-IDLE builder.")
    sys.exit(1)
print(proxy.build(
    '1-2', 'sourcepackagerecipe', '1ef177161c3cb073e66bf1550931c6fbaa0a94b0',
    {}, {'author_name': u'Steve\u1234',
         'author_email': 'stevea@example.org',
         'suite': distroseries_name,
         'distroseries_name': distroseries_name,
         'ogrecomponent': 'universe',
         'archive_purpose': 'puppies',
         'recipe_text': recipe_text,
         'archives': [
            deb_line('%s.archive.ubuntu.com/ubuntu' % country_code,
                     'main universe'),
            deb_line('ppa.launchpad.net/launchpad/bzr-builder-dev/ubuntu',
                     'main'),
            ]}))
