#!/usr/bin/sh
#
# Copyright (c) 2006 Red Hat, Inc.
#
# This program is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 2 of
# the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see http://www.gnu.org/licenses/.
#
# Author: Greg Nichols
# Updated by: Bill Peck <bpeck@redhat.com>


# Check the correct Python executable to run this file.
# Python3 will be preferred.
""":"

cmd=$(python-check.sh)

exec -- $cmd $0 "$@";

":"""
from __future__ import print_function

# Use a hardcoded path for RHTS python modules for now:
import sys
import os
from six.moves import xmlrpc_client as xmlrpclib
import rhts
from rhts.rhts_workflow import Scheduler

USAGE_TEXT = """
Usage:
    rhts-mk-test-import <scheduler> <rpmname> <version>
"""
def usage():
    print(USAGE_TEXT)
    sys.exit(-1)

def read_rpm(filename):
    FH = open(filename, "rb")
    data = FH.read()
    FH.close()
    return data

args = sys.argv[1:]
if len(args) != 3:
  print("ERROR: %s args provided" % (len(args)))
  usage()

testrpmname = sys.argv[2]
testversion = sys.argv[3]

try:
    sched = Scheduler(sys.argv[1], "dummyrepo", type='https')
except:
    sched = Scheduler(sys.argv[1], "dummyrepo", type='http')
rhts.ensure_connection(sched)

data = read_rpm(testrpmname)
binarydata = xmlrpclib.Binary(data)
basetestrpmname = os.path.basename(testrpmname)
(ret, msg) = sched.test.import_testrpm(basetestrpmname,binarydata,testversion)
print(msg)
if ret:
    sys.exit(0)
else:
    sys.exit(1)
