]>
Commit | Line | Data |
---|---|---|
30fdf114 LG |
1 | ## @file\r |
2 | # This is the base class for applications that operate on an EDK II Workspace \r | |
3 | #\r | |
40d841f6 LG |
4 | # Copyright (c) 2007, Intel Corporation. All rights reserved.<BR>\r |
5 | # This program and the accompanying materials\r | |
30fdf114 LG |
6 | # are licensed and made available under the terms and conditions of the BSD License\r |
7 | # which accompanies this distribution. The full text of the license may be found at\r | |
8 | # http://opensource.org/licenses/bsd-license.php\r | |
9 | #\r | |
10 | # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
11 | # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
12 | #\r | |
13 | \r | |
14 | ##\r | |
15 | # Import Modules\r | |
16 | #\r | |
17 | import os, sys, time\r | |
18 | from DataType import *\r | |
19 | \r | |
20 | ## EdkIIWorkspace\r | |
21 | #\r | |
22 | # Collect WorkspaceDir from the environment, the Verbose command line flag, and detect an icon bitmap file.\r | |
23 | # \r | |
24 | # @var StartTime: Time of build system starting\r | |
25 | # @var PrintRunTime: Printable time of build system running\r | |
26 | # @var PrintRunStatus: Printable status of build system running\r | |
27 | # @var RunStatus: Status of build system running\r | |
28 | #\r | |
29 | class EdkIIWorkspace:\r | |
30 | def __init__(self):\r | |
31 | self.StartTime = time.time()\r | |
32 | self.PrintRunTime = False\r | |
33 | self.PrintRunStatus = False\r | |
34 | self.RunStatus = ''\r | |
35 | \r | |
36 | #\r | |
37 | # Check environment valiable 'WORKSPACE'\r | |
38 | #\r | |
39 | if os.environ.get('WORKSPACE') == None:\r | |
40 | print 'ERROR: WORKSPACE not defined. Please run EdkSetup from the EDK II install directory.'\r | |
41 | return False\r | |
42 | \r | |
43 | self.CurrentWorkingDir = os.getcwd()\r | |
44 | \r | |
45 | self.WorkspaceDir = os.path.realpath(os.environ.get('WORKSPACE'))\r | |
46 | (Drive, Path) = os.path.splitdrive(self.WorkspaceDir)\r | |
47 | if Drive == '':\r | |
48 | (Drive, CwdPath) = os.path.splitdrive(self.CurrentWorkingDir)\r | |
49 | if Drive != '':\r | |
50 | self.WorkspaceDir = Drive + Path\r | |
51 | else:\r | |
52 | self.WorkspaceDir = Drive.upper() + Path\r | |
53 | \r | |
54 | self.WorkspaceRelativeWorkingDir = self.WorkspaceRelativePath (self.CurrentWorkingDir)\r | |
55 | \r | |
56 | try:\r | |
57 | #\r | |
58 | # Load TianoCoreOrgLogo, used for GUI tool\r | |
59 | #\r | |
60 | self.Icon = wx.Icon(self.WorkspaceFile('tools/Python/TianoCoreOrgLogo.gif'),wx.BITMAP_TYPE_GIF)\r | |
61 | except:\r | |
62 | self.Icon = None\r | |
63 | \r | |
64 | self.Verbose = False\r | |
65 | for Arg in sys.argv:\r | |
66 | if Arg.lower() == '-v':\r | |
67 | self.Verbose = True\r | |
68 | \r | |
69 | ## Close build system\r | |
70 | #\r | |
71 | # Close build system and print running time and status\r | |
72 | #\r | |
73 | def Close(self):\r | |
74 | if self.PrintRunTime:\r | |
75 | Seconds = int(time.time() - self.StartTime)\r | |
76 | if Seconds < 60:\r | |
77 | print 'Run Time: %d seconds' % (Seconds)\r | |
78 | else:\r | |
79 | Minutes = Seconds / 60\r | |
80 | Seconds = Seconds % 60\r | |
81 | if Minutes < 60:\r | |
82 | print 'Run Time: %d minutes %d seconds' % (Minutes, Seconds)\r | |
83 | else:\r | |
84 | Hours = Minutes / 60\r | |
85 | Minutes = Minutes % 60\r | |
86 | print 'Run Time: %d hours %d minutes %d seconds' % (Hours, Minutes, Seconds)\r | |
87 | if self.RunStatus != '':\r | |
88 | print self.RunStatus\r | |
89 | \r | |
90 | ## Convert to a workspace relative filename\r | |
91 | #\r | |
92 | # Convert a full path filename to a workspace relative filename.\r | |
93 | #\r | |
94 | # @param FileName: The filename to be Converted\r | |
95 | #\r | |
96 | # @retval None Workspace dir is not found in the full path\r | |
97 | # @retval string The relative filename\r | |
98 | #\r | |
99 | def WorkspaceRelativePath(self, FileName):\r | |
100 | FileName = os.path.realpath(FileName)\r | |
101 | if FileName.find(self.WorkspaceDir) != 0:\r | |
102 | return None\r | |
103 | return FileName.replace (self.WorkspaceDir, '').strip('\\').strip('/')\r | |
104 | \r | |
105 | ## Convert to a full path filename\r | |
106 | #\r | |
107 | # Convert a workspace relative filename to a full path filename.\r | |
108 | #\r | |
109 | # @param FileName: The filename to be Converted\r | |
110 | #\r | |
111 | # @retval string The full path filename\r | |
112 | #\r | |
113 | def WorkspaceFile(self, FileName):\r | |
114 | return os.path.realpath(os.path.join(self.WorkspaceDir,FileName))\r | |
115 | \r | |
116 | ## Convert to a real path filename\r | |
117 | #\r | |
118 | # Convert ${WORKSPACE} to real path\r | |
119 | #\r | |
120 | # @param FileName: The filename to be Converted\r | |
121 | #\r | |
122 | # @retval string The full path filename\r | |
123 | #\r | |
124 | def WorkspacePathConvert(self, FileName):\r | |
125 | return os.path.realpath(FileName.replace(TAB_WORKSPACE, self.WorkspaceDir))\r | |
126 | \r | |
127 | ## Convert XML into a DOM\r | |
128 | #\r | |
129 | # Parse an XML file into a DOM and return the DOM.\r | |
130 | #\r | |
131 | # @param FileName: The filename to be parsed\r | |
132 | #\r | |
133 | # @retval XmlParseFile (self.WorkspaceFile(FileName))\r | |
134 | #\r | |
135 | def XmlParseFile (self, FileName):\r | |
136 | if self.Verbose:\r | |
137 | print FileName\r | |
138 | return XmlParseFile (self.WorkspaceFile(FileName))\r | |
139 | \r | |
140 | ## Convert a XML section\r | |
141 | #\r | |
142 | # Parse a section of an XML file into a DOM(Document Object Model) and return the DOM.\r | |
143 | #\r | |
144 | # @param FileName: The filename to be parsed\r | |
145 | # @param SectionTag: The tag name of the section to be parsed\r | |
146 | #\r | |
147 | # @retval XmlParseFileSection (self.WorkspaceFile(FileName), SectionTag)\r | |
148 | #\r | |
149 | def XmlParseFileSection (self, FileName, SectionTag):\r | |
150 | if self.Verbose:\r | |
151 | print FileName\r | |
152 | return XmlParseFileSection (self.WorkspaceFile(FileName), SectionTag) \r | |
153 | \r | |
154 | ## Save a XML file\r | |
155 | #\r | |
156 | # Save a DOM(Document Object Model) into an XML file.\r | |
157 | #\r | |
158 | # @param Dom: The Dom to be saved\r | |
159 | # @param FileName: The filename\r | |
160 | #\r | |
161 | # @retval XmlSaveFile (Dom, self.WorkspaceFile(FileName))\r | |
162 | #\r | |
163 | def XmlSaveFile (self, Dom, FileName):\r | |
164 | if self.Verbose:\r | |
165 | print FileName\r | |
166 | return XmlSaveFile (Dom, self.WorkspaceFile(FileName))\r | |
167 | \r | |
168 | ## Convert Text File To Dictionary\r | |
169 | #\r | |
170 | # Convert a workspace relative text file to a dictionary of (name:value) pairs.\r | |
171 | #\r | |
172 | # @param FileName: Text filename\r | |
173 | # @param Dictionary: Dictionary to store data\r | |
174 | # @param CommentCharacter: Comment char, be used to ignore comment content\r | |
175 | # @param KeySplitCharacter: Key split char, between key name and key value. Key1 = Value1, '=' is the key split char\r | |
176 | # @param ValueSplitFlag: Value split flag, be used to decide if has multiple values\r | |
177 | # @param ValueSplitCharacter: Value split char, be used to split multiple values. Key1 = Value1|Value2, '|' is the value split char\r | |
178 | #\r | |
179 | # @retval ConvertTextFileToDictionary(self.WorkspaceFile(FileName), Dictionary, CommentCharacter, KeySplitCharacter, ValueSplitFlag, ValueSplitCharacter)\r | |
180 | #\r | |
181 | def ConvertTextFileToDictionary(self, FileName, Dictionary, CommentCharacter, KeySplitCharacter, ValueSplitFlag, ValueSplitCharacter):\r | |
182 | if self.Verbose:\r | |
183 | print FileName\r | |
184 | return ConvertTextFileToDictionary(self.WorkspaceFile(FileName), Dictionary, CommentCharacter, KeySplitCharacter, ValueSplitFlag, ValueSplitCharacter)\r | |
185 | \r | |
186 | ## Convert Dictionary To Text File\r | |
187 | #\r | |
188 | # Convert a dictionary of (name:value) pairs to a workspace relative text file.\r | |
189 | #\r | |
190 | # @param FileName: Text filename\r | |
191 | # @param Dictionary: Dictionary to store data\r | |
192 | # @param CommentCharacter: Comment char, be used to ignore comment content\r | |
193 | # @param KeySplitCharacter: Key split char, between key name and key value. Key1 = Value1, '=' is the key split char\r | |
194 | # @param ValueSplitFlag: Value split flag, be used to decide if has multiple values\r | |
195 | # @param ValueSplitCharacter: Value split char, be used to split multiple values. Key1 = Value1|Value2, '|' is the value split char\r | |
196 | #\r | |
197 | # @retval ConvertDictionaryToTextFile(self.WorkspaceFile(FileName), Dictionary, CommentCharacter, KeySplitCharacter, ValueSplitFlag, ValueSplitCharacter)\r | |
198 | #\r | |
199 | def ConvertDictionaryToTextFile(self, FileName, Dictionary, CommentCharacter, KeySplitCharacter, ValueSplitFlag, ValueSplitCharacter):\r | |
200 | if self.Verbose:\r | |
201 | print FileName\r | |
202 | return ConvertDictionaryToTextFile(self.WorkspaceFile(FileName), Dictionary, CommentCharacter, KeySplitCharacter, ValueSplitFlag, ValueSplitCharacter)\r | |
203 | \r | |
204 | ## Convert Text File To Dictionary\r | |
205 | #\r | |
206 | # Convert a text file to a dictionary of (name:value) pairs.\r | |
207 | #\r | |
208 | # @param FileName: Text filename\r | |
209 | # @param Dictionary: Dictionary to store data\r | |
210 | # @param CommentCharacter: Comment char, be used to ignore comment content\r | |
211 | # @param KeySplitCharacter: Key split char, between key name and key value. Key1 = Value1, '=' is the key split char\r | |
212 | # @param ValueSplitFlag: Value split flag, be used to decide if has multiple values\r | |
213 | # @param ValueSplitCharacter: Value split char, be used to split multiple values. Key1 = Value1|Value2, '|' is the value split char\r | |
214 | #\r | |
215 | # @retval True Convert successfully\r | |
216 | # @retval False Open file failed\r | |
217 | #\r | |
218 | def ConvertTextFileToDictionary(FileName, Dictionary, CommentCharacter, KeySplitCharacter, ValueSplitFlag, ValueSplitCharacter):\r | |
219 | try:\r | |
220 | F = open(FileName,'r')\r | |
221 | except:\r | |
222 | return False\r | |
223 | Keys = []\r | |
224 | for Line in F:\r | |
225 | LineList = Line.split(KeySplitCharacter,1)\r | |
226 | if len(LineList) >= 2:\r | |
227 | Key = LineList[0].split()\r | |
228 | if len(Key) == 1 and Key[0][0] != CommentCharacter and Key[0] not in Keys: \r | |
229 | if ValueSplitFlag:\r | |
230 | Dictionary[Key[0]] = LineList[1].replace('\\','/').split(ValueSplitCharacter)\r | |
231 | else:\r | |
232 | Dictionary[Key[0]] = LineList[1].strip().replace('\\','/')\r | |
233 | Keys += [Key[0]]\r | |
234 | F.close()\r | |
235 | return True\r | |
236 | \r | |
237 | ## Convert Dictionary To Text File\r | |
238 | #\r | |
239 | # Convert a dictionary of (name:value) pairs to a text file.\r | |
240 | #\r | |
241 | # @param FileName: Text filename\r | |
242 | # @param Dictionary: Dictionary to store data\r | |
243 | # @param CommentCharacter: Comment char, be used to ignore comment content\r | |
244 | # @param KeySplitCharacter: Key split char, between key name and key value. Key1 = Value1, '=' is the key split char\r | |
245 | # @param ValueSplitFlag: Value split flag, be used to decide if has multiple values\r | |
246 | # @param ValueSplitCharacter: Value split char, be used to split multiple values. Key1 = Value1|Value2, '|' is the value split char\r | |
247 | #\r | |
248 | # @retval True Convert successfully\r | |
249 | # @retval False Open file failed\r | |
250 | #\r | |
251 | def ConvertDictionaryToTextFile(FileName, Dictionary, CommentCharacter, KeySplitCharacter, ValueSplitFlag, ValueSplitCharacter):\r | |
252 | try:\r | |
253 | F = open(FileName,'r')\r | |
254 | Lines = []\r | |
255 | Lines = F.readlines()\r | |
256 | F.close()\r | |
257 | except:\r | |
258 | Lines = []\r | |
259 | Keys = Dictionary.keys()\r | |
260 | MaxLength = 0\r | |
261 | for Key in Keys:\r | |
262 | if len(Key) > MaxLength:\r | |
263 | MaxLength = len(Key)\r | |
264 | Index = 0\r | |
265 | for Line in Lines:\r | |
266 | LineList = Line.split(KeySplitCharacter,1)\r | |
267 | if len(LineList) >= 2:\r | |
268 | Key = LineList[0].split()\r | |
269 | if len(Key) == 1 and Key[0][0] != CommentCharacter and Key[0] in Dictionary:\r | |
270 | if ValueSplitFlag:\r | |
271 | Line = '%-*s %c %s\n' % (MaxLength, Key[0], KeySplitCharacter, ' '.join(Dictionary[Key[0]]))\r | |
272 | else:\r | |
273 | Line = '%-*s %c %s\n' % (MaxLength, Key[0], KeySplitCharacter, Dictionary[Key[0]])\r | |
274 | Lines.pop(Index)\r | |
275 | if Key[0] in Keys:\r | |
276 | Lines.insert(Index,Line)\r | |
277 | Keys.remove(Key[0])\r | |
278 | Index += 1\r | |
279 | for RemainingKey in Keys:\r | |
280 | if ValueSplitFlag:\r | |
281 | Line = '%-*s %c %s\n' % (MaxLength, RemainingKey, KeySplitCharacter,' '.join(Dictionary[RemainingKey])) \r | |
282 | else:\r | |
283 | Line = '%-*s %c %s\n' % (MaxLength, RemainingKey, KeySplitCharacter, Dictionary[RemainingKey])\r | |
284 | Lines.append(Line)\r | |
285 | try:\r | |
286 | F = open(FileName,'w')\r | |
287 | except:\r | |
288 | return False\r | |
289 | F.writelines(Lines)\r | |
290 | F.close()\r | |
291 | return True\r | |
292 | \r | |
293 | ## Create a new directory\r | |
294 | #\r | |
295 | # @param Directory: Directory to be created\r | |
296 | #\r | |
297 | def CreateDirectory(Directory):\r | |
298 | if not os.access(Directory, os.F_OK):\r | |
299 | os.makedirs (Directory)\r | |
300 | \r | |
301 | ## Create a new file\r | |
302 | #\r | |
303 | # @param Directory: Directory to be created\r | |
304 | # @param FileName: Filename to be created\r | |
305 | # @param Mode: The mode of open file, defautl is 'w'\r | |
306 | #\r | |
307 | def CreateFile(Directory, FileName, Mode='w'):\r | |
308 | CreateDirectory (Directory)\r | |
309 | return open(os.path.join(Directory, FileName), Mode)\r | |
310 | \r | |
311 | ##\r | |
312 | #\r | |
313 | # This acts like the main() function for the script, unless it is 'import'ed into another\r | |
314 | # script.\r | |
315 | #\r | |
316 | if __name__ == '__main__':\r | |
317 | # Nothing to do here. Could do some unit tests\r | |
318 | pass |