#!/usr/bin/env python
#	
#	File name: run_dataset.py
#	The basic script for running the real-life dataset
#
#	
#	Verion: 1.0
#	Date created: 	11/09/2020
#
#	Author: Charalampos Marantos
#

import sys
import os
from application import application

import signal
import subprocess

# we do not need clang parser for this operation
from transformer import transformer
import ConfigParser
from application.code_block import Code_block

if (len(sys.argv) < 2):
	print "Usage: python run_dataset.py <hotspot_name>"
	exit()

#os.system("rm ./temp/*")

configParser = ConfigParser.RawConfigParser()   
configParser.read('../config.txt')

os.system("cd ../ && "+configParser.get(sys.argv[1], 'build'))

new_application = application.Application(configParser.get(sys.argv[1], 'dir'), configParser.get(sys.argv[1], 'exec'), configParser.get(sys.argv[1], 'source'), configParser.get(sys.argv[1], 'build'))

print "Application initialized"
print "==========================================================================================="

# insert specific code block from config file
stmts = [Code_block("../"+configParser.get(sys.argv[1], 'source'), " ", configParser.get(sys.argv[1], 'line_start'), configParser.get(sys.argv[1], 'line_end'))]

#insert code to measure execution time or energy
#set energy for collecting energy on Nvidia Tegra TX1
transformed_hotspot = transformer.Transformer(stmts[0], "time")
transformed_hotspot.prepare([],[])

#re-build program
os.system("cd ../ && "+configParser.get(sys.argv[1], 'build'))

#execute program
os.system("cd ../ && "+configParser.get(sys.argv[1], 'exec'))

#restore source file
transformed_hotspot.restore()

