#!/usr/bin/python
import xmlrpclib

SATELLITE_URL = "http://localhost/rpc/api"
SATELLITE_LOGIN = "XXXXXX"
SATELLITE_PASSWORD = "XXXXXXX"

client = xmlrpclib.Server(SATELLITE_URL, verbose=0)

key = client.auth.login(SATELLITE_LOGIN, SATELLITE_PASSWORD)

# Set up some variables for source, target and dates
chan_src = "rhel-x86_64-server-6"
chan_tgt = "sc-falcon-test-rhel-x86_64-server-6"
start_date = "2013-07-01"
end_date = "2013-08-01"

# Pull all the errata that apply to the channel based on a date range
all_errata = client.channel.software.listErrata(key,chan_src,start_date,end_date)
print all_errata
print "** " + str(len(all_errata)) + " Errata to clone from source"

# Build the list of advisories to clone
clone_err = []
for errata in all_errata:
	clone_err.append(errata['advisory'])

print clone_err
print "** " + str(len(clone_err)) + " Advisories added to clone list for target"

# Clone the list of advisoriies to the target channel
push_clones = client.errata.clone(key,chan_tgt,clone_err)
print push_clones

# Build list of cloned errata advisory names
clone_ids = []
for clone in push_clones:
	clone_ids.append(clone['advisory_name'])
print clone_ids
print "** " + str(len(clone_ids)) + " Cloned advisories pushed to the target channel"

# Retrive list of all unpublished errata, 0 means that everything we cloned was # already published in the target channel 
unpub_err = client.errata.listUnpublishedErrata(key)
print unpub_err
print "** " + str(len(unpub_err)) + " Unpublished errata in queue"

# Build list of cloned errata names and unpublished errata
topub_err = clone_ids
for unpub in unpub_err:
	topub_err.append(unpub['advisory_name'])
print topub_err
print "** " + str(len(topub_err)) + "  Errata in list for target"

# Method wants an array of channels, even if we only have one channel
target_channels = [chan_tgt]

# Publish the full list of unpublished errata to our target channels
for publish in topub_err:
	published = client.errata.publish(key,publish,target_channels)
	print "Published the following errata to the target "
	print published

client.auth.logout(key)
