#!/usr/bin/python
# Print out custom template UUIDs in CLI comma-separated minimal format
# (c) Anil Madhavapeddy, Citrix Systems Inc, 2008

import atexit
import XenAPI
import os, sys, time

def logout():
    try:
        session.xenapi.session.logout()
    except:
        pass
atexit.register(logout)

def main(argv):
    try:
        session = XenAPI.xapi_local()
        session.xenapi.login_with_password("", "")

        templates = session.xenapi.VM.get_all_records_where('field "is_a_template" = "true"' )
    except:
        print >> sys.stderr, "Error retrieving template list"
        sys.exit(1)

    output=[]
    for tmplref in templates.keys():
        tmplrec = templates[tmplref]
        try:
            if not tmplrec['other_config'].has_key('default_template'):
                output.append(tmplrec['uuid'])
            elif tmplrec['other_config']['default_template'] != true:
                output.append(tmplrec['uuid'])
        except:
            pass
    print(str.join(',', output))
    session.xenapi.logout()

if __name__ == "__main__":
    main(sys.argv[1:])


