buildframework/helium/tools/common/python/lib/buildtools.py
changeset 179 d8ac696cc51f
parent 1 be27ed110b50
equal deleted inserted replaced
1:be27ed110b50 179:d8ac696cc51f
    46         A prebuilder takes a configurationset as input and generates a build file.
    46         A prebuilder takes a configurationset as input and generates a build file.
    47     """
    47     """
    48     def __init__(self, configSet):
    48     def __init__(self, configSet):
    49         self.configSet = configSet
    49         self.configSet = configSet
    50         # Select the first configuration as a default, for referencing common properties
    50         # Select the first configuration as a default, for referencing common properties
    51         self.config = configSet.getConfigurations()[0]
    51         self.config = None
       
    52         configs = configSet.getConfigurations()
       
    53         if len(configs) > 0:
       
    54             self.config = configs[0]
    52 
    55 
    53     def writeBuildFile(self, taskList, buildFilePath, output='ant'):
    56     def writeBuildFile(self, taskList, buildFilePath, output='ant'):
    54         """ Converting a task list into output format and writing it into buildFilePath file. """
    57         """ Converting a task list into output format and writing it into buildFilePath file. """
    55         writer = None
    58         writer = None
    56         #if 'build.tool' in self.config:
       
    57             # Choose appropriate build tool
       
    58         #    print 'choosing build tool!'
       
    59         #    pass
       
    60         #else:
       
    61         # Choose Ant by default for now
       
    62         print self.config.keys()
       
    63         buildFileDir = os.path.dirname(buildFilePath)
    59         buildFileDir = os.path.dirname(buildFilePath)
    64         if not os.path.exists(buildFileDir):
    60         if len(buildFileDir) > 0 and not os.path.exists(buildFileDir):
    65             os.makedirs(buildFileDir)
    61             os.makedirs(buildFileDir)
    66         writer = get_writer(output, open(buildFilePath, 'w'))
    62         writer = get_writer(output, open(buildFilePath, 'w'))
    67         writer.write(taskList)
    63         writer.write(taskList)
    68 
    64 
       
    65 
    69 class Task(object):
    66 class Task(object):
    70     """ Abstract Task object. """
    67     """ Abstract Task object. """
    71     pass
    68     pass
       
    69 
    72         
    70         
    73 class Command(Task):
    71 class Command(Task):
    74     """
    72     """
    75         This class implements a command definition.
    73         This class implements a command definition.
    76         It handles command id and stage.
    74         It handles command id and stage.
   125 
   123 
   126     def __repr__(self):
   124     def __repr__(self):
   127         argsString = ' '.join(self._args)
   125         argsString = ' '.join(self._args)
   128         return "%s: %s: %s" % (self.name(), self.path(), argsString)
   126         return "%s: %s: %s" % (self.name(), self.path(), argsString)
   129 
   127 
       
   128 
   130 class AntTask(Task):
   129 class AntTask(Task):
   131     """ Interface that defines supports for an Ant task rendering. """
   130     """ Interface that defines supports for an Ant task rendering. """
   132     
   131     
   133     def toAntTask(self, doc):
   132     def toAntTask(self, doc):
   134         """ Override this method to convert a specific command into Ant command.
   133         """ Override this method to convert a specific command into Ant command.
   135             e.g: Delete Class will use delete task from Ant, else convert into perl ... remove filename.__getCommandByStage
   134             e.g: Delete Class will use delete task from Ant, else convert into perl ... remove filename.__getCommandByStage
   136         """ 
   135         """ 
   137         pass
   136         pass
   138     
   137 
       
   138 
   139 class Delete(AntTask, Command):
   139 class Delete(AntTask, Command):
   140     """ Implements file/directory deleletion mechanism. """
   140     """ Implements file/directory deleletion mechanism. """
   141     
   141     
   142     def __init__(self, filename=None, dirname=None):
   142     def __init__(self, filename=None, dirname=None):
   143         Command.__init__(self, "perl", "")
   143         Command.__init__(self, "perl", "")
   183         node = doc.createElementNS("", "copy")
   183         node = doc.createElementNS("", "copy")
   184         node.setAttributeNS("", "verbose", "true")
   184         node.setAttributeNS("", "verbose", "true")
   185         node.setAttributeNS("", "failonerror", "false")
   185         node.setAttributeNS("", "failonerror", "false")
   186         node.setAttributeNS("", "file", self.srcFile)
   186         node.setAttributeNS("", "file", self.srcFile)
   187         node.setAttributeNS("", "todir", self.todir)
   187         node.setAttributeNS("", "todir", self.todir)
       
   188         node.setAttributeNS("", "overwrite", "true")
   188         return node
   189         return node
   189          
   190          
   190 
   191 
   191 class CommandList(object):
   192 class CommandList(object):
   192     """
   193     """
   239         self._fileOut.close()
   240         self._fileOut.close()
   240 
   241 
   241     def __del__(self):
   242     def __del__(self):
   242         self.close()
   243         self.close()
   243 
   244 
       
   245 
   244 class StringWriter(AbstractOutputWriter):
   246 class StringWriter(AbstractOutputWriter):
   245     """ Implements a Writer which is able to directly write to the output stream. """
   247     """ Implements a Writer which is able to directly write to the output stream. """
   246     
   248     
   247     def __init__(self, fileOut):
   249     def __init__(self, fileOut):
   248         AbstractOutputWriter.__init__(self, fileOut)
   250         AbstractOutputWriter.__init__(self, fileOut)
   293     def writeTopLevel(self, config_list, spec_name, output_path, xml_file):
   295     def writeTopLevel(self, config_list, spec_name, output_path, xml_file):
   294         doc = xml.dom.minidom.Document()
   296         doc = xml.dom.minidom.Document()
   295         projectnode = doc.createElementNS("", "project")
   297         projectnode = doc.createElementNS("", "project")
   296         projectnode.setAttributeNS("", "name", '')
   298         projectnode.setAttributeNS("", "name", '')
   297         projectnode.setAttributeNS("", "default", "all")
   299         projectnode.setAttributeNS("", "default", "all")
       
   300         projectnode.setAttributeNS("", "xmlns:hlm", "http://www.nokia.com/helium")
   298         doc.appendChild(projectnode)
   301         doc.appendChild(projectnode)
   299         target = doc.createElementNS("", "target")
   302         target = doc.createElementNS("", "target")
   300         target.setAttributeNS("", "name", "all")
   303         target.setAttributeNS("", "name", "all")
   301         projectnode.appendChild(target)
   304         projectnode.appendChild(target)
   302 
   305 
   306         index = 0
   309         index = 0
   307         script_loc = os.path.normpath(os.path.join(os.environ['HELIUM_HOME'], 'tools/common/python/lib/CreateZipInput.py'))
   310         script_loc = os.path.normpath(os.path.join(os.environ['HELIUM_HOME'], 'tools/common/python/lib/CreateZipInput.py'))
   308         for config in config_list:
   311         for config in config_list:
   309             sequential = doc.createElementNS("", "sequential")
   312             sequential = doc.createElementNS("", "sequential")
   310             outputfile = os.path.normpath(os.path.join(output_path, config + ".xml"))
   313             outputfile = os.path.normpath(os.path.join(output_path, config + ".xml"))
   311             exec_element = doc.createElementNS("", "exec")
   314             exec_element = doc.createElementNS("", "hlm:exec")
   312             exec_element.setAttributeNS("", "executable", "python")
   315             exec_element.setAttributeNS("", "executable", "python")
   313             exec_element.setAttributeNS("", "failonerror", "true")
   316             exec_element.setAttributeNS("", "failonerror", "true")
   314 
   317 
   315             args = doc.createElementNS("", "arg")
   318             args = doc.createElementNS("", "arg")
   316             args.setAttributeNS("", "value", "%s" % script_loc)
   319             args.setAttributeNS("", "value", "%s" % script_loc)
   345         """ Writes the command list to Ant format. """
   348         """ Writes the command list to Ant format. """
   346         doc = xml.dom.minidom.Document()
   349         doc = xml.dom.minidom.Document()
   347         projectnode = doc.createElementNS("", "project")
   350         projectnode = doc.createElementNS("", "project")
   348         projectnode.setAttributeNS("", "name", '')
   351         projectnode.setAttributeNS("", "name", '')
   349         projectnode.setAttributeNS("", "default", "all")
   352         projectnode.setAttributeNS("", "default", "all")
       
   353         projectnode.setAttributeNS("", "xmlns:hlm", "http://www.nokia.com/helium")
   350         doc.appendChild(projectnode)
   354         doc.appendChild(projectnode)
   351 
   355 
   352         stages = self.__getCommandByStage(cmdList)
   356         stages = self.__getCommandByStage(cmdList)
   353 
   357 
   354         for stage in stages.keys():
   358         for stage in stages.keys():
   382         # does the API support Ant task conversion.
   386         # does the API support Ant task conversion.
   383         # else treat it as a cmd
   387         # else treat it as a cmd
   384         if issubclass(type(cmd), AntTask):
   388         if issubclass(type(cmd), AntTask):
   385             return cmd.toAntTask(doc)
   389             return cmd.toAntTask(doc)
   386         else:
   390         else:
   387             execnode = doc.createElementNS("", "exec")
   391             execnode = doc.createElementNS("", "hlm:exec")
   388             execnode.setAttributeNS("", "executable", cmd.executable())
   392             execnode.setAttributeNS("", "executable", cmd.executable())
   389             execnode.setAttributeNS("", "dir", cmd.path())
   393             execnode.setAttributeNS("", "dir", cmd.path())
   390             arg = doc.createElementNS("", "arg")
   394             arg = doc.createElementNS("", "arg")
   391             arg.setAttributeNS("", "line", cmd.cmd())
   395             arg.setAttributeNS("", "line", cmd.cmd())
   392             execnode.appendChild(arg)
   396             execnode.appendChild(arg)
   458             def __toId(cmd):
   462             def __toId(cmd):
   459                 """ Convert command Id into a target name. """
   463                 """ Convert command Id into a target name. """
   460                 self.__commandToTarget(cmd)
   464                 self.__commandToTarget(cmd)
   461                 return "id%s" % cmd.jobId()
   465                 return "id%s" % cmd.jobId()
   462             self._fileOut.write("stage%s : %s\n" % (stage, ' '.join([__toId(task) for task in stages[stage]])))
   466             self._fileOut.write("stage%s : %s\n" % (stage, ' '.join([__toId(task) for task in stages[stage]])))
   463         
       
   464 
   467 
   465     def __commandToTarget(self, cmd):
   468     def __commandToTarget(self, cmd):
   466         """ Converting a Command into a Makefile target. """
   469         """ Converting a Command into a Makefile target. """
   467         deps = ""
   470         deps = ""
   468         if cmd.stage() > 1:
   471         if cmd.stage() > 1:
   479 
   482 
   480 __writerConstructors = { 'ant': AntWriter,
   483 __writerConstructors = { 'ant': AntWriter,
   481                          'make': MakeWriter,
   484                          'make': MakeWriter,
   482                          'ebs': EBSWriter }
   485                          'ebs': EBSWriter }
   483 
   486 
       
   487 
   484 def convert(cmdList, filename, outputtype="ant"):
   488 def convert(cmdList, filename, outputtype="ant"):
   485     """ Helper to directly convert a command list into a specific runnable command format.
   489     """ Helper to directly convert a command list into a specific runnable command format.
   486         e.g:
   490         e.g:
   487         cmdList = CommandList()
   491         cmdList = CommandList()
   488         cmdList.addCommand(...)
   492         cmdList.addCommand(...)
   489         convert(cmdList, "echo Hello world", "ant")
   493         convert(cmdList, "echo Hello world", "ant")
   490     """
   494     """
   491     writer = __writerConstructors[outputtype](filename)
   495     writer = __writerConstructors[outputtype](filename)
   492     writer(cmdList)
   496     writer(cmdList)
   493 
   497 
       
   498 
   494 def get_writer(buildTool, fileOut):
   499 def get_writer(buildTool, fileOut):
   495     """ Get a Writer for a specific format. """
   500     """ Get a Writer for a specific format. """
   496     return __writerConstructors[buildTool](fileOut)
   501     return __writerConstructors[buildTool](fileOut)
   497 
   502 
   498 
   503