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