#!/usr/bin/env python

import os
import json
from subprocess import Popen, PIPE, check_call
from installsystems.printer import arrow

def get_image_list(repo):
	p = Popen(['is', '--no-color', '-q', 'list', '-j', '%s/*' % repo], stdout=PIPE)
	stdout, stderr = p.communicate()
	if p.returncode != 0:
		exit('Unable to get %s list' % repo)
	img = json.loads(stdout)
	d = {}
	for k,v in img.items():
		d[v['md5']] = k
	return d


arrow('Getting stable images list')
stable_imgs=get_image_list('stable')
arrow('Getting testing images list')
testing_imgs=get_image_list('testing')

official=set(stable_imgs)
to_remove=set(testing_imgs) & official

arrow('Removing testing images in stable')
for md5 in to_remove:
	name=testing_imgs[md5]
	arrow('%s (%s)' % (name, md5), 1)
	check_call(['is', 'del', '-f', name])

# vim:set ts=2 sw=2 ft=python noet: