Skip to content
genDevice.py 3.6 KiB
Newer Older
Gino Tosti's avatar
Gino Tosti committed
#! /usr/bin/env python

from Cheetah.Template import Template
from datetime import date
#from getSheets import myIcd
from GenDevice.excelIcd import excelIcd
import sys,os
from optparse import OptionParser
import subprocess
from GenDevice.genFromTemplates import genFromTemplates
from GenDevice.createEmptyCDB import *
from GenDevice.acsUtils import *
Gino Tosti's avatar
Gino Tosti committed
import pandas as pd
Gino Tosti's avatar
Gino Tosti committed
import glob

outdir="/tmp/"

def manageICDFiles(dev,outdir,smain,wb):
	query="`Device Name` =="+ "'"+dev+"'"
	main=smain.query(query)
	get=(wb.book.parse("GET")).query(query)
	sett=(wb.book.parse("SET")).query(query)
	cmd=(wb.book.parse("CMD")).query(query)
	mode=(wb.book.parse("MODE")).query(query)
	outfile=outdir+dev+".xlsx"
	with pd.ExcelWriter(outfile) as writer:
		main.to_excel(writer,sheet_name='Main', index=False)
		get.to_excel(writer,sheet_name='GET',index=False)
		sett.to_excel(writer,sheet_name='SET',index=False)
		cmd.to_excel(writer,sheet_name='CMD',index=False)
		mode.to_excel(writer,sheet_name='MODE',index=False)
	return outfile

def build(mygen):
	  introot = os.environ["INTROOT"]
	  if introot=="":
	     print("INTROOT variable is not set")
	  else:
	     os.chdir(mygen._dirs['src'])
	     print ("Make Build ")
	     command="make clean all"
	     print (command)
	     if execACSCommand(command):
	        print ("ERROR executing: ",command)

def removetmpfiles():

	filelist = glob.glob(outdir+'*.xlsx')
	for fil in filelist:
		try:
	    		os.remove(fil)
		except OSError as e:
	    		print ("Error: %s - %s." % (e.filename, e.strerror))
Gino Tosti's avatar
Gino Tosti committed

if __name__ =="__main__":
	parser = OptionParser()
	today = date.today()
	parser.add_option("-f", "--file", dest="bookfile",
                  help="ICD Exel File ", metavar="FILE")
	parser.add_option("-d", "--dir", dest="basedir", default="./tmp",
                  help="Root dir where to install the new device ", metavar="DIR")
	parser.add_option("-p", "--prefix", dest="prefix", default="astri",
                  help="pkg prefix es. astri ", metavar="PREFIX")
	parser.add_option("-m", "--module", dest="module", default="tcs",
                  help="module name es.tcs ", metavar="MODULE")
	parser.add_option("-i", "--install", action="store_true",
                  help="execute the Makefile in src dir ", metavar="INSTALL")
	(options, args) = parser.parse_args()
	print("running genDevice with the following options:\n", options)
	#exit()
	gendir=os.environ["PYGEN"]
	if gendir=="":
		print ("Please set the env variable PYGEN = the Geneartor root directory")
		exit()  
	if  not checkACS():
		print ("ACS Enviroment is not set")
		exit()
	pwd=os.getcwd()
Gino Tosti's avatar
Gino Tosti committed
	excel_file =options.bookfile
	wb=excelIcd(excel_file)
	tmp=wb.book.parse("Main")
	smain=pd.DataFrame(data=tmp.loc[1:,tmp.columns].values,columns=tmp.loc[0,tmp.columns])
	print (smain)
	#devs= wb.MainSheet['Device Name']
	devs= smain['Device Name']
	comp=smain['Component Name']
	assembly=smain['Assembly']
Gino Tosti's avatar
Gino Tosti committed
	if len(devs)>1 :
		print ("the Assembly:"+assembly[0]+" includes more than one device\n",devs)
		for dev in devs:
			print("working On device:", dev)
Gino Tosti's avatar
Gino Tosti committed
			outf=manageICDFiles(dev,outdir,smain,wb)
			outfile.append(outf)
			print("Working on files:",dev)
			mygen=genFromTemplates(outfile[-1],options.prefix,options.module,options.basedir)
			mygen.generateFileInDir()
			if options.install :
Gino Tosti's avatar
Gino Tosti committed
				build(mygen)
				os.chdir(pwd)	   
			
		print ("the Assembly:"+assembly[0]+" includes one device\n",devs)
Gino Tosti's avatar
Gino Tosti committed
		outf=manageICDFiles(devs[0],outdir,smain,wb)
		mygen=genFromTemplates(outf,options.prefix,options.module,options.basedir)
		mygen.generateFileInDir()
		if options.install :
Gino Tosti's avatar
Gino Tosti committed
			build(mygen)
			os.chdir(pwd)
	removetmpfiles()
Gino Tosti's avatar
Gino Tosti committed
	print ("ALL Done")