# Copyright (C) 2010, 2011, 2012, 2013, 2014 Intel, 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; version 2 of the License.
#
#    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, write to the Free Software
#    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
"""A build trigger"""

import base64
import json

from common.utils import unicode_to_str

def trigger_next(job_name, data):
    """Trigger next job"""

    print "====LOG:TRIGGER_NEXT:%s =======================" % job_name.upper()
    with open("%s.env" % job_name, 'w') as info_file:
        info_file.write('TRIGGER_INFO=%s\n' % \
                base64.b64encode(json.dumps(data)))
    for key in data.keys():
        print "%s='%s'" % (key, data[key])
    print "====LOG:TRIGGER_NEXT:======================end=="

def trigger_info(_trigger_info):
    "Return a trigger info dict"""

    print "====LOG:TRIGGER_INFO:==========================="
    content_dict = unicode_to_str(json.loads(base64.b64decode(_trigger_info)))
    print json.dumps(content_dict, indent=4)
    print "====LOG:TRIGGER_INFO:======================end=="
    return content_dict
