Python script for generating build files for platform and modules, which uses the...
[mirror_edk2.git] / Tools / Python / buildgen / AntTasks.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2007, Intel Corporation
4 # All rights reserved. This program and the accompanying materials
5 # are licensed and made available under the terms and conditions of the BSD License
6 # which accompanies this distribution. The full text of the license may be found at
7 # http://opensource.org/licenses/bsd-license.php
8 #
9 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 """Ant Tasks Dom Element"""
13
14 import xml.dom.minidom, os
15
16 class AntTask:
17 def __init__(self, document, _name, _body=None, **_attributes):
18 """Instantiate an Ant task element
19 document, _name=task name, _body=task body, **_attributes=task attributes
20 """
21 self.Document = document
22 self.Root = ""
23
24 if _name == None or _name == "":
25 raise Exception("No Ant task name")
26
27 taskElement = self.Document.createElement(_name)
28 for name in _attributes:
29 taskElement.setAttribute(name, _attributes[name])
30
31 if _body != None:
32 if isinstance(_body, str):
33 text = self.Document.createTextNode(_body)
34 taskElement.appendChild(text)
35 elif isinstance(_body, list):
36 for subtask in _body:
37 taskElement.appendChild(subtask)
38
39 self.Root = taskElement
40
41 def SubTask(self, _name, _body=None, **_attributes):
42 """Insert a sub-task into this task"""
43 newTask = AntTask(self.Document, _name , _body, **_attributes)
44 self.Root.appendChild(newTask.Root)
45 return newTask
46
47 def AddSubTask(self, subTask):
48 """Insert a existing sub-task into this task"""
49 self.Root.appendChild(subTask.Root.cloneNode(True))
50 return subTask
51
52 def Comment(self, string):
53 """Generate a XML comment"""
54 self.Root.appendChild(self.Document.createComment(string))
55
56 def Blankline(self):
57 """Generate a blank line"""
58 self.Root.appendChild(self.Document.createTextNode(""))
59
60
61 class AntBuildFile(AntTask):
62 _FileComment = "DO NOT EDIT\nThis file is auto-generated by the build utility\n\nAbstract:\nAuto-generated ANT build file for building Framework modules and platforms\n"
63
64 def __init__(self, name, basedir=".", default="all"):
65 """Instantiate an Ant build.xml
66 name=project name, basedir=project default directory, default=default target of this project
67 """
68 self.Document = xml.dom.minidom.getDOMImplementation().createDocument(None, "project", None)
69 self.Root = self.Document.documentElement
70
71 self.Document.insertBefore(self.Document.createComment(self._FileComment), self.Root)
72 self.Root.setAttribute("name", name)
73 self.Root.setAttribute("basedir", basedir)
74 self.Root.setAttribute("default", default)
75
76 def Create(self, file):
77 """Generate the build.xml using given file path"""
78 buildFileDir = os.path.dirname(file)
79 if not os.path.exists(buildFileDir):
80 os.makedirs(buildFileDir)
81
82 f = open(file, "w")
83 f.write(self.Document.toprettyxml(2*" ", encoding="UTF-8"))
84 f.close()