buildframework/helium/sf/python/pythoncore/lib/ats3/dropgenerator.py
changeset 628 7c4a911dc066
parent 588 c7c26511138f
child 645 b8d81fa19e7d
equal deleted inserted replaced
588:c7c26511138f 628:7c4a911dc066
    19 #Description:
    19 #Description:
    20 #===============================================================================
    20 #===============================================================================
    21 
    21 
    22 """ Generate test drop zip file for ATS3"""
    22 """ Generate test drop zip file for ATS3"""
    23 
    23 
    24 # pylint: disable-msg=W0142,R0912,R0201,R0915,R0913,R0904
       
    25 # pylint: disable-msg=C0302
       
    26 # pylint: disable-msg=W0404,W0603
       
    27 
    24 
    28 #W0142 => * and ** were used
    25 #W0142 => * and ** were used
    29 #C0302 => Too many lines
    26 #C0302 => Too many lines
    30 #R* removed when refactored
    27 #R* removed when refactored
    31 #W => use of global statement
    28 #W => use of global statement
    32 
    29 
    33 import codecs
    30 import codecs
    34 from  xml.parsers.expat import ExpatError
    31 from xml.parsers.expat import ExpatError
    35 
    32 
    36 from xml.etree import ElementTree as et
    33 from xml.etree import ElementTree as et
    37 import pkg_resources
    34 import pkg_resources
    38 from path import path # pylint: disable-msg=F0401
    35 from path import path # pylint: disable=F0401
    39 import logging
    36 import logging
    40 import os
    37 import os
    41 import re
    38 import re
    42 import zipfile
    39 import zipfile
    43 import amara
    40 import amara
    44 import atsconfigparser
    41 import ats3.atsconfigparser
    45 
    42 
    46 # pylint: disable-msg=W0404
       
    47 from ntpath import sep as atssep
    43 from ntpath import sep as atssep
    48 import ntpath as atspath
    44 import ntpath as atspath
    49 
    45 
    50 import jinja2 # pylint: disable-msg=F0401
    46 import jinja2 # pylint: disable=F0401
    51 
    47 
    52 _logger = logging.getLogger('ats')
    48 _logger = logging.getLogger('ats')
    53 
    49 
    54 # Shortcuts
    50 # Shortcuts
    55 E = et.Element
    51 E = et.Element
   104         """Generate a test drop file."""
   100         """Generate a test drop file."""
   105         xml = self.generate_xml(test_plan)
   101         xml = self.generate_xml(test_plan)
   106         
   102         
   107         if config_file:
   103         if config_file:
   108             xmltext = et.tostring(xml.getroot(), "ISO-8859-1")
   104             xmltext = et.tostring(xml.getroot(), "ISO-8859-1")
   109             xmltext = atsconfigparser.converttestxml(config_file, xmltext)
   105             xmltext = ats3.atsconfigparser.converttestxml(config_file, xmltext)
   110             xml = et.ElementTree(et.XML(xmltext))
   106             xml = et.ElementTree(et.XML(xmltext))
   111             
   107             
   112         return self.generate_drop(test_plan, xml, output_file)
   108         return self.generate_drop(test_plan, xml, output_file)
   113 
   109 
   114     def generate_drop(self, test_plan, xml, output_file):
   110     def generate_drop(self, test_plan, xml, output_file):
   302                     SE(params, "param", timeout=test_plan["test_timeout"])
   298                     SE(params, "param", timeout=test_plan["test_timeout"])
   303                     ini_name = setd["engine_ini_file"].name
   299                     ini_name = setd["engine_ini_file"].name
   304                     SE(params, "param", engineini=path(r"c:" + os.sep + "testframework") / ini_name)            
   300                     SE(params, "param", engineini=path(r"c:" + os.sep + "testframework") / ini_name)            
   305                     
   301                     
   306                 # if no inifile, but cfg files defined, use those
   302                 # if no inifile, but cfg files defined, use those
   307                 elif setd["config_files"]!=[]:
   303                 elif setd["config_files"] != []:
   308                     for config_file in setd["config_files"]:
   304                     for config_file in setd["config_files"]:
   309                         step = SE(case, "step", 
   305                         step = SE(case, "step", 
   310                                   name="Execute test: %s" % config_file.name, 
   306                                   name="Execute test: %s" % config_file.name, 
   311                                   harness=setd["test_harness"], **self.defaults)
   307                                   harness=setd["test_harness"], **self.defaults)
   312                         SE(step, "command").text = "run-cases"
   308                         SE(step, "command").text = "run-cases"
   442                 sorted_images.append(image_file)
   438                 sorted_images.append(image_file)
   443         for image_file in setd["image_files"]:
   439         for image_file in setd["image_files"]:
   444             if 'rofs3' in image_file.name:
   440             if 'rofs3' in image_file.name:
   445                 sorted_images.append(image_file)
   441                 sorted_images.append(image_file)
   446         for image_file in setd["image_files"]:
   442         for image_file in setd["image_files"]:
   447             if 'core' not in image_file.name and 'rofs2' not in image_file.name and 'rofs3' not in image_file.name:
   443             if 'core' not in image_file.name and 'rofs2' not in image_file.name and 'rofs3' not in image_file.name and 'udaerase' not in image_file.name.lower():
   448                 sorted_images.append(image_file)
   444                 sorted_images.append(image_file)
   449         if len(sorted_images) > 0 and "rofs" in sorted_images[0]:
   445         if len(sorted_images) > 0 and "rofs" in sorted_images[0]:
   450             return setd["image_files"]
   446             return setd["image_files"]
   451         return sorted_images
   447         return sorted_images
       
   448         
       
   449     def get_udaerase_image(self, setd):
       
   450         for image_file in setd["image_files"]:
       
   451             if 'udaerase' in image_file.name.lower():
       
   452                 return image_file
       
   453         return None
   452     
   454     
   453     def generate_steps(self, setd, case, test_plan):
   455     def generate_steps(self, setd, case, test_plan):
   454         """Generate the test plan <step>s."""
   456         """Generate the test plan <step>s."""
   455         # Flash images.
   457         # Flash images.
   456         images = self.drop_path_root.joinpath("images")
   458         images = self.drop_path_root.joinpath("images")
   518         for file1 in src_dst:
   520         for file1 in src_dst:
   519             if 'testscript' in file1[2]:
   521             if 'testscript' in file1[2]:
   520                 filename = file1[1]
   522                 filename = file1[1]
   521                 filename = filename[file1[1].rfind(os.sep)+1:]
   523                 filename = filename[file1[1].rfind(os.sep)+1:]
   522                 harness = "testexecute.exe"
   524                 harness = "testexecute.exe"
   523                 if file1[2] == "testscript:mtf":
   525                 if "testscript:mtf" in file1[2]:
   524                     harness = "testframework.exe"
   526                     harness = "testframework.exe"
   525                 step = SE(case, "step", 
   527                 step = SE(case, "step", 
   526                               name="Execute test: %s" %  filename, harness=setd["test_harness"], 
   528                               name="Execute test: %s" %  filename, harness=setd["test_harness"], 
   527                               **self.defaults)
   529                               **self.defaults)
   528                 SE(step, "command").text = "execute"
   530                 SE(step, "command").text = "execute"
   529                 params = SE(step, "params")
   531                 params = SE(step, "params")
   530                 SE(params, "param", file=harness)
   532                 SE(params, "param", file=harness)
   531                 SE(params, "param", parameters=file1[1])
   533                 SE(params, "param", parameters=file1[1])
   532                 
   534                 
   533                 if file1[2] == "testscript:mtf":
   535                 if "testscript:mtf" in file1[2]:
   534                     SE(params, "param", {'result-file': self.MTF_LOG_DIR + os.sep + filename.replace('.script', '.htm')})
   536                     SE(params, "param", {'result-file': self.MTF_LOG_DIR + os.sep + filename.replace('.script', '.htm')})
   535                 else:
   537                 else:
   536                     SE(params, "param", {'result-file': self.TEF_LOG_DIR + os.sep + filename.replace('.script', '.htm')})
   538                     SE(params, "param", {'result-file': self.TEF_LOG_DIR + os.sep + filename.replace('.script', '.htm')})
   537                 SE(params, "param", timeout=time_out)
   539                 SE(params, "param", timeout=time_out)
   538                 if file1[2] == "testscript:mtf":
   540                 if "testscript:mtf" in file1[2]:
   539                     SE(params, "param", parser="MTFResultParser")
   541                     SE(params, "param", parser="MTFResultParser")
   540                 else:
   542                 else:
   541                     SE(params, "param", parser="TEFTestResultParser")
   543                     SE(params, "param", parser="TEFTestResultParser")
   542             if file1[2] == 'testmodule:rtest':
   544             if file1[2] == 'testmodule:rtest':
   543                 filename = file1[1]
   545                 filename = file1[1]
   638 
   640 
   639             
   641             
   640             #for EUnit or other executables
   642             #for EUnit or other executables
   641             if sdst[2] == "testmodule":
   643             if sdst[2] == "testmodule":
   642                 eunit_exe = "EUNITEXERUNNER.EXE"
   644                 eunit_exe = "EUNITEXERUNNER.EXE"
   643                 if re_dll.search(filename):                    
   645                 # include dll file only if it has some harness defined, else skip execute step, only install                
   644                     step = SE(case, "step", name = "Execute test: %s" % filename, harness=setd["test_harness"],
   646                 if filename.lower() in setd['dll_files']:
   645                               **self.defaults)
   647                     if setd['dll_files'][filename.lower()] == "":
   646                     SE(step, "command").text = "execute"
   648                         pass 
   647                     params = SE(step, "params")
   649                     else:
   648                     SE(params, "param", file=path(r"z:" + os.sep + "sys" + os.sep + "bin") / eunit_exe)
   650                         if re_dll.search(filename):                    
   649                     elem = SE(params, "param")
   651                             step = SE(case, "step", name = "Execute test: %s" % filename, harness=setd["test_harness"],
   650                     elem.set('result-file', path(self.EUNIT_LOG_DIR) / no_dll_xml)
   652                                       **self.defaults)
   651                     SE(params, "param", parameters="%s /F %s /l xml %s" % (eunit_flags, no_dll, filename))
   653                             SE(step, "command").text = "execute"
   652                     SE(params, "param", timeout=time_out)
   654                             params = SE(step, "params")
   653             
   655                             SE(params, "param", file=path(r"z:" + os.sep + "sys" + os.sep + "bin") / eunit_exe)
       
   656                             elem = SE(params, "param")
       
   657                             elem.set('result-file', path(self.EUNIT_LOG_DIR) / no_dll_xml)
       
   658                             SE(params, "param", parameters="%s /F %s /l xml %s" % (eunit_flags, no_dll, filename))
       
   659                             SE(params, "param", timeout=time_out)
       
   660                     
   654             #for QtTest.lib executables
   661             #for QtTest.lib executables
   655             elif sdst[2] == "testmodule:qt":
   662             elif sdst[2] == "testmodule:qt":
   656                 step = SE(case, "step", name = "Execute Qt-test: %s" % filename, harness=setd["test_harness"],
   663                 # include dll file only if it has some harness defined, else skip execute step, only install
   657                           **self.defaults)
   664                 if filename.lower() in setd['dll_files']:
   658                 SE(step, "command").text = "execute"
   665                     if setd['dll_files'][filename.lower()] == "":
   659                 params = SE(step, "params")
   666                         pass 
   660                 SE(params, "param", file=path(sdst[1]))
   667                     else:
   661                 SE(params, "param", parameters=r"-lightxml -o %s\%s" % (path(self.QT_LOG_DIR),  no_dll_xml))
   668                         step = SE(case, "step", name = "Execute Qt-test: %s" % filename, harness=setd["test_harness"],
   662                 elem = SE(params, "param")
   669                                   **self.defaults)
   663                 elem.set('result-file', path(self.QT_LOG_DIR) / no_dll_xml)
   670                         SE(step, "command").text = "execute"
   664                 SE(params, "param", parser="QTestResultParser")
   671                         params = SE(step, "params")
   665                 elem = SE(params, "param")
   672                         SE(params, "param", file=path(sdst[1]))
   666                 elem.set('delete-result',"true")
   673                         SE(params, "param", parameters=r"-lightxml -o %s\%s" % (path(self.QT_LOG_DIR),  no_dll_xml))
   667                 SE(params, "param", async="false")
   674                         elem = SE(params, "param")
   668                 SE(params, "param", timeout=time_out)
   675                         elem.set('result-file', path(self.QT_LOG_DIR) / no_dll_xml)
       
   676                         SE(params, "param", parser="QTestResultParser")
       
   677                         elem = SE(params, "param")
       
   678                         elem.set('delete-result',"true")
       
   679                         SE(params, "param", async="false")
       
   680                         SE(params, "param", timeout=time_out)
   669 
   681 
   670                 
   682                 
   671 
   683 
   672     def generate_run_steps(self, case, setd, time_out, eunit_flags):
   684     def generate_run_steps(self, case, setd, time_out, eunit_flags):
   673         """Generates run-steps"""
   685         """Generates run-steps"""
   793         return files_elem
   805         return files_elem
   794         
   806         
   795     def check_mtf_harness(self, _setd_):
   807     def check_mtf_harness(self, _setd_):
   796         """check the testscript.mtf file is present"""
   808         """check the testscript.mtf file is present"""
   797         for _srcdst_ in _setd_['src_dst']:
   809         for _srcdst_ in _setd_['src_dst']:
   798             if _srcdst_[2] == "testscript:mtf":
   810             if "testscript:mtf" in _srcdst_[2]:
       
   811                 return True
       
   812         return False
       
   813         
       
   814     def check_sut_harness(self, _setd_):
       
   815         """check the testscript.sut file is present"""
       
   816         for _srcdst_ in _setd_['src_dst']:
       
   817             if "testmodule:sut" in _srcdst_[2]:
   799                 return True
   818                 return True
   800         return False
   819         return False
   801 
   820 
   802     def check_qt_harness(self, _setd_):
   821     def check_qt_harness(self, _setd_):
   803         """ check the QT harness is OK """
   822         """ check the QT harness is OK """
   869                     loop = cust
   888                     loop = cust
   870                 else:
   889                 else:
   871                 # clear the loop variable 
   890                 # clear the loop variable 
   872                     loop = ''
   891                     loop = ''
   873                 cust = unicode(custom_action_file.read(1))
   892                 cust = unicode(custom_action_file.read(1))
   874         except Exception, err:
   893         except UnicodeError, err:
   875             _logger.error("Error %s in XML when prosessing %s\n" % ( err, filename))
   894             _logger.error("Error %s in XML when prosessing %s\n" % ( err, filename))
   876             xmltree.append(et.Comment("Error in XML file when prosessing %s\n" % ( filename)))
   895             xmltree.append(et.Comment("Error in XML file when prosessing %s\n" % ( filename)))
   877 
   896 
   878         if loop != '' :
   897         if loop != '':
   879             # we should have used all the input and cleared loop variable
   898             # we should have used all the input and cleared loop variable
   880             _logger.warning("Issues in customization file %s in XML when prosessing issue %s \n Line and column refer to section:\n%s\n" % ( filename, err,  loop))
   899             _logger.warning("Issues in customization file %s in XML when prosessing issue %s \n Line and column refer to section:\n%s\n" % ( filename, err,  loop))
   881 
   900 
   882         custom_action_file.close()
   901         custom_action_file.close()
   883     except IOError, err:
   902     except IOError, err:
   910 class Ats3TemplateTestDropGenerator(Ats3TestDropGenerator):
   929 class Ats3TemplateTestDropGenerator(Ats3TestDropGenerator):
   911     """ATS3 template for test drop generator"""
   930     """ATS3 template for test drop generator"""
   912     STIF_LOG_DIR = r"c:\logs\testframework"
   931     STIF_LOG_DIR = r"c:\logs\testframework"
   913     TEF_LOG_DIR = r"c:\logs\testexecute"
   932     TEF_LOG_DIR = r"c:\logs\testexecute"
   914     MTF_LOG_DIR = r"c:\logs\testresults"
   933     MTF_LOG_DIR = r"c:\logs\testresults"
       
   934     SUT_LOG_DIR = r"c:\sut"
   915     STIFUNIT_LOG_DIR = r"c:\logs\testframework"
   935     STIFUNIT_LOG_DIR = r"c:\logs\testframework"
   916     EUNIT_LOG_DIR = r"c:\Shared\EUnit\logs"
   936     EUNIT_LOG_DIR = r"c:\Shared\EUnit\logs"
   917     #QT_LOG_DIR = r"c:\private\Qt\logs"
   937     #QT_LOG_DIR = r"c:\private\Qt\logs"
   918     QT_LOG_DIR = r"c:\shared\EUnit\logs"
   938     QT_LOG_DIR = r"c:\shared\EUnit\logs"
   919     CTC_LOG_DIR = r"c:\data\ctc"
   939     CTC_LOG_DIR = r"c:\data\ctc"
       
   940     AtsInterface_LOG_DIR = r"c:\spd_logs\xml"
   920 
   941 
   921     def stif_init_file(self, src_dst):
   942     def stif_init_file(self, src_dst):
   922         """init the STIF format file"""
   943         """init the STIF format file"""
   923         has_tf_ini = False
   944         has_tf_ini = False
   924         ini_file = None
   945         ini_file = None
   953             drop_id = temp_drop_id.rsplit(atssep, 1)[1]
   974             drop_id = temp_drop_id.rsplit(atssep, 1)[1]
   954         else:
   975         else:
   955             drop_id = temp_drop_id
   976             drop_id = temp_drop_id
   956 
   977 
   957         return atspath.join(ats_network, "ctc_helium" , diamonds_id, drop_id, setd["name"], "ctcdata")
   978         return atspath.join(ats_network, "ctc_helium" , diamonds_id, drop_id, setd["name"], "ctcdata")
   958 
   979     
   959     def getlogdir(self, setd):
   980     def stifmodulename(self, ini_file):
       
   981         modname = None
       
   982         ini = open(ini_file)
       
   983         for line in ini:
       
   984             if line.startswith('ModuleName'):
       
   985                 modname = line.split('=')[1].strip()
       
   986         ini.close()
       
   987         return modname
       
   988 
       
   989     def getlogdir(self, test_plan, setd):
   960         """ find the logger directory"""
   990         """ find the logger directory"""
       
   991         returnval = None
   961         if setd["test_harness"] == "STIF":
   992         if setd["test_harness"] == "STIF":
   962             return self.STIF_LOG_DIR
   993             if test_plan['hti'] == 'True':
       
   994                 returnval = self.STIF_LOG_DIR
       
   995             else:
       
   996                 returnval = self.AtsInterface_LOG_DIR
   963         elif setd["test_harness"] == "STIFUNIT":
   997         elif setd["test_harness"] == "STIFUNIT":
   964             return self.STIFUNIT_LOG_DIR
   998             returnval = self.STIFUNIT_LOG_DIR
   965         elif setd["test_harness"] == "GENERIC":
   999         elif setd["test_harness"] == "GENERIC":
   966             if self.check_mtf_harness(setd):
  1000             if self.check_mtf_harness(setd):
   967                 return self.MTF_LOG_DIR
  1001                 returnval = self.MTF_LOG_DIR
       
  1002             elif self.check_sut_harness(setd):
       
  1003                 returnval = self.SUT_LOG_DIR
   968             else:
  1004             else:
   969                 return self.TEF_LOG_DIR
  1005                 returnval = self.TEF_LOG_DIR
   970         elif setd["test_harness"] == "EUNIT":
  1006         elif setd["test_harness"] == "EUNIT":
   971             if self.check_qt_harness(setd):
  1007             if self.check_qt_harness(setd):
   972                 return self.QT_LOG_DIR
  1008                 returnval = self.QT_LOG_DIR
   973             else:
  1009             else:
   974                 return self.EUNIT_LOG_DIR
  1010                 returnval = self.EUNIT_LOG_DIR
       
  1011         return returnval
   975 
  1012 
   976     def generate_xml(self, test_plan):
  1013     def generate_xml(self, test_plan):
   977         """generate the XML"""
  1014         """generate the XML"""
   978         
  1015         
   979         customdirs = []
  1016         customdirs = []
   987         env = jinja2.Environment(loader=loader)
  1024         env = jinja2.Environment(loader=loader)
   988         
  1025         
   989         if hasattr(test_plan, 'custom_template'):
  1026         if hasattr(test_plan, 'custom_template'):
   990             template = env.from_string(open(test_plan.custom_template).read())
  1027             template = env.from_string(open(test_plan.custom_template).read())
   991         else:
  1028         else:
   992             template = env.from_string(pkg_resources.resource_string(__name__, 'ats4_template.xml'))# pylint: disable-msg=E1101
  1029             template = env.from_string(pkg_resources.resource_string(__name__, 'ats4_template.xml'))# pylint: disable=E1101
   993 
  1030 
   994         xmltext = template.render(test_plan=test_plan, os=os, atspath=atspath, atsself=self).encode('ISO-8859-1')
  1031         xmltext = template.render(test_plan=test_plan, os=os, atspath=atspath, atsself=self).encode('ISO-8859-1')
   995         return et.ElementTree(et.XML(xmltext))
  1032         return et.ElementTree(et.XML(xmltext))
       
  1033         
       
  1034     def get_template(self, directory, template_name):
       
  1035         if directory:
       
  1036             name = os.path.join(directory, template_name)
       
  1037             if os.path.exists(name):
       
  1038                 template_file = open(name)
       
  1039                 contents = template_file.read()
       
  1040                 template_file.close()
       
  1041                 return contents
       
  1042         return ''
       
  1043     
       
  1044