Skip to content
genDevice.py 4.17 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

Gino Tosti's avatar
Gino Tosti committed
OUTDIR="/tmp/"
Gino Tosti's avatar
Gino Tosti committed

Gino Tosti's avatar
Gino Tosti committed
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()
Gino Tosti's avatar
Gino Tosti committed

def generateComponent(config):
Gino Tosti's avatar
Gino Tosti committed
	pwd=os.getcwd()
	wb=excelIcd(config['icdfile'])
Gino Tosti's avatar
Gino Tosti committed
	devs= wb.MainSheet['Device Name']
Gino Tosti's avatar
Gino Tosti committed
	if len(devs)>1 :
Gino Tosti's avatar
Gino Tosti committed
		print ("The Assembly:"+wb.MainSheet['Assembly'][0]+" includes more than one device\n",devs)
		genMulti(wb,config)	   
Gino Tosti's avatar
Gino Tosti committed
	else:
Gino Tosti's avatar
Gino Tosti committed
		print ("The Assembly:"+wb.MainSheet['Assembly'][0]+" includes one device\n",devs)
		genSingle(wb,config)
Gino Tosti's avatar
Gino Tosti committed
	os.chdir(pwd)
Gino Tosti's avatar
Gino Tosti committed

Gino Tosti's avatar
Gino Tosti committed
def genMulti(wb,config):
	for dev in wb.MainSheet['Device Name']:
		print("working On device:", dev)
		outf=manageICDFiles(dev,OUTDIR,wb)
		print("Working on files:",outf)
		wb1=excelIcd(outf)
		config['icdfile']=outf
		genSingle(wb1,config)
	removetmpfiles()

def genSingle(wb,config):
	mygen=genFromTemplates(wb,config['icdfile'],config['prefix'],config['module'],config['basedir'])
	mygen.generateFileInDir()	
	if config['install'] :
		build(mygen)
		checkCDB(mygen)

def manageICDFiles(dev,outdir,wb):
Gino Tosti's avatar
Gino Tosti committed
	query="`Device Name` =="+ "'"+dev+"'"
	main=(wb.book.parse("Main")).query(query)
Gino Tosti's avatar
Gino Tosti committed
	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:
Gino Tosti's avatar
Gino Tosti committed
	     pwd=os.getcwd()
Gino Tosti's avatar
Gino Tosti committed
	     os.chdir(mygen._dirs['src'])
	     print ("Make Build ")
Gino Tosti's avatar
Gino Tosti committed
	     command="make clean all install"
	     print ("Executing:",command,'\n',"...Please wait it will take some minutes")
Gino Tosti's avatar
Gino Tosti committed
	     if execACSCommand(command):
	        print ("ERROR executing: ",command)
Gino Tosti's avatar
Gino Tosti committed
	     os.chdir(pwd)
	     checkCDB(mygen)
	  return

def checkCDB(mygen):
	pwd=os.getcwd()
	print ("Check CDB in", os.getcwd())
	command= "source "+mygen._dirs['test']+"setCDB.sh ; cdbChecker"
	print ("Executing:",command,'\n',"...Please wait....")
	if execACSCommand(command):
		print ("ERROR executing: ",command)
		return
	
	os.chdir(pwd)
	return
Gino Tosti's avatar
Gino Tosti committed

def removetmpfiles():

Gino Tosti's avatar
Gino Tosti committed
	filelist = glob.glob(OUTDIR+'*.xlsx')
Gino Tosti's avatar
Gino Tosti committed
	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

Gino Tosti's avatar
Gino Tosti committed
def getInput():
Gino Tosti's avatar
Gino Tosti committed
	usage = "usage: %prog [options] arg"
Gino Tosti's avatar
Gino Tosti committed
	parser = OptionParser()
	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",
Gino Tosti's avatar
Gino Tosti committed
                  help="pkg prefix es.:  astri ", metavar="PREFIX")
Gino Tosti's avatar
Gino Tosti committed
	parser.add_option("-m", "--module", dest="module", default="tcs",
Gino Tosti's avatar
Gino Tosti committed
                  help="module name es.: tcs ", metavar="MODULE")
Gino Tosti's avatar
Gino Tosti committed
	parser.add_option("-i", "--install", dest="install", default=False,
Gino Tosti's avatar
Gino Tosti committed
                  help="execute the Makefile in basedir/src dir ", metavar="INSTALL")
Gino Tosti's avatar
Gino Tosti committed
	(options, args) = parser.parse_args()
Gino Tosti's avatar
Gino Tosti committed
	#print("running genDevice with the following options:\n", options)
Gino Tosti's avatar
Gino Tosti committed
	config={}
	config['icdfile']=options.bookfile
	config['prefix']=options.prefix
	config['module']=options.module
	config['basedir']=options.basedir
Gino Tosti's avatar
Gino Tosti committed
	config['install']=options.install
	return config

if __name__ =="__main__":
	pwd=os.getcwd()
	config=getInput()
Gino Tosti's avatar
Gino Tosti committed
	generateComponent(config)
Gino Tosti's avatar
Gino Tosti committed
	print ("ALL Done")
Gino Tosti's avatar
Gino Tosti committed
	os.chdir(pwd)
Gino Tosti's avatar
Gino Tosti committed