2 # This is the base class for applications that operate on an EDK II Workspace
4 # Copyright (c) 2007, Intel Corporation. All rights reserved.<BR>
5 # This program and the accompanying materials
6 # are licensed and made available under the terms and conditions of the BSD License
7 # which accompanies this distribution. The full text of the license may be found at
8 # http://opensource.org/licenses/bsd-license.php
10 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 from DataType
import *
22 # Collect WorkspaceDir from the environment, the Verbose command line flag, and detect an icon bitmap file.
24 # @var StartTime: Time of build system starting
25 # @var PrintRunTime: Printable time of build system running
26 # @var PrintRunStatus: Printable status of build system running
27 # @var RunStatus: Status of build system running
31 self
.StartTime
= time
.time()
32 self
.PrintRunTime
= False
33 self
.PrintRunStatus
= False
37 # Check environment valiable 'WORKSPACE'
39 if os
.environ
.get('WORKSPACE') == None:
40 print 'ERROR: WORKSPACE not defined. Please run EdkSetup from the EDK II install directory.'
43 self
.CurrentWorkingDir
= os
.getcwd()
45 self
.WorkspaceDir
= os
.path
.realpath(os
.environ
.get('WORKSPACE'))
46 (Drive
, Path
) = os
.path
.splitdrive(self
.WorkspaceDir
)
48 (Drive
, CwdPath
) = os
.path
.splitdrive(self
.CurrentWorkingDir
)
50 self
.WorkspaceDir
= Drive
+ Path
52 self
.WorkspaceDir
= Drive
.upper() + Path
54 self
.WorkspaceRelativeWorkingDir
= self
.WorkspaceRelativePath (self
.CurrentWorkingDir
)
58 # Load TianoCoreOrgLogo, used for GUI tool
60 self
.Icon
= wx
.Icon(self
.WorkspaceFile('tools/Python/TianoCoreOrgLogo.gif'),wx
.BITMAP_TYPE_GIF
)
66 if Arg
.lower() == '-v':
71 # Close build system and print running time and status
75 Seconds
= int(time
.time() - self
.StartTime
)
77 print 'Run Time: %d seconds' % (Seconds
)
79 Minutes
= Seconds
/ 60
80 Seconds
= Seconds
% 60
82 print 'Run Time: %d minutes %d seconds' % (Minutes
, Seconds
)
85 Minutes
= Minutes
% 60
86 print 'Run Time: %d hours %d minutes %d seconds' % (Hours
, Minutes
, Seconds
)
87 if self
.RunStatus
!= '':
90 ## Convert to a workspace relative filename
92 # Convert a full path filename to a workspace relative filename.
94 # @param FileName: The filename to be Converted
96 # @retval None Workspace dir is not found in the full path
97 # @retval string The relative filename
99 def WorkspaceRelativePath(self
, FileName
):
100 FileName
= os
.path
.realpath(FileName
)
101 if FileName
.find(self
.WorkspaceDir
) != 0:
103 return FileName
.replace (self
.WorkspaceDir
, '').strip('\\').strip('/')
105 ## Convert to a full path filename
107 # Convert a workspace relative filename to a full path filename.
109 # @param FileName: The filename to be Converted
111 # @retval string The full path filename
113 def WorkspaceFile(self
, FileName
):
114 return os
.path
.realpath(os
.path
.join(self
.WorkspaceDir
,FileName
))
116 ## Convert to a real path filename
118 # Convert ${WORKSPACE} to real path
120 # @param FileName: The filename to be Converted
122 # @retval string The full path filename
124 def WorkspacePathConvert(self
, FileName
):
125 return os
.path
.realpath(FileName
.replace(TAB_WORKSPACE
, self
.WorkspaceDir
))
127 ## Convert XML into a DOM
129 # Parse an XML file into a DOM and return the DOM.
131 # @param FileName: The filename to be parsed
133 # @retval XmlParseFile (self.WorkspaceFile(FileName))
135 def XmlParseFile (self
, FileName
):
138 return XmlParseFile (self
.WorkspaceFile(FileName
))
140 ## Convert a XML section
142 # Parse a section of an XML file into a DOM(Document Object Model) and return the DOM.
144 # @param FileName: The filename to be parsed
145 # @param SectionTag: The tag name of the section to be parsed
147 # @retval XmlParseFileSection (self.WorkspaceFile(FileName), SectionTag)
149 def XmlParseFileSection (self
, FileName
, SectionTag
):
152 return XmlParseFileSection (self
.WorkspaceFile(FileName
), SectionTag
)
156 # Save a DOM(Document Object Model) into an XML file.
158 # @param Dom: The Dom to be saved
159 # @param FileName: The filename
161 # @retval XmlSaveFile (Dom, self.WorkspaceFile(FileName))
163 def XmlSaveFile (self
, Dom
, FileName
):
166 return XmlSaveFile (Dom
, self
.WorkspaceFile(FileName
))
168 ## Convert Text File To Dictionary
170 # Convert a workspace relative text file to a dictionary of (name:value) pairs.
172 # @param FileName: Text filename
173 # @param Dictionary: Dictionary to store data
174 # @param CommentCharacter: Comment char, be used to ignore comment content
175 # @param KeySplitCharacter: Key split char, between key name and key value. Key1 = Value1, '=' is the key split char
176 # @param ValueSplitFlag: Value split flag, be used to decide if has multiple values
177 # @param ValueSplitCharacter: Value split char, be used to split multiple values. Key1 = Value1|Value2, '|' is the value split char
179 # @retval ConvertTextFileToDictionary(self.WorkspaceFile(FileName), Dictionary, CommentCharacter, KeySplitCharacter, ValueSplitFlag, ValueSplitCharacter)
181 def ConvertTextFileToDictionary(self
, FileName
, Dictionary
, CommentCharacter
, KeySplitCharacter
, ValueSplitFlag
, ValueSplitCharacter
):
184 return ConvertTextFileToDictionary(self
.WorkspaceFile(FileName
), Dictionary
, CommentCharacter
, KeySplitCharacter
, ValueSplitFlag
, ValueSplitCharacter
)
186 ## Convert Dictionary To Text File
188 # Convert a dictionary of (name:value) pairs to a workspace relative text file.
190 # @param FileName: Text filename
191 # @param Dictionary: Dictionary to store data
192 # @param CommentCharacter: Comment char, be used to ignore comment content
193 # @param KeySplitCharacter: Key split char, between key name and key value. Key1 = Value1, '=' is the key split char
194 # @param ValueSplitFlag: Value split flag, be used to decide if has multiple values
195 # @param ValueSplitCharacter: Value split char, be used to split multiple values. Key1 = Value1|Value2, '|' is the value split char
197 # @retval ConvertDictionaryToTextFile(self.WorkspaceFile(FileName), Dictionary, CommentCharacter, KeySplitCharacter, ValueSplitFlag, ValueSplitCharacter)
199 def ConvertDictionaryToTextFile(self
, FileName
, Dictionary
, CommentCharacter
, KeySplitCharacter
, ValueSplitFlag
, ValueSplitCharacter
):
202 return ConvertDictionaryToTextFile(self
.WorkspaceFile(FileName
), Dictionary
, CommentCharacter
, KeySplitCharacter
, ValueSplitFlag
, ValueSplitCharacter
)
204 ## Convert Text File To Dictionary
206 # Convert a text file to a dictionary of (name:value) pairs.
208 # @param FileName: Text filename
209 # @param Dictionary: Dictionary to store data
210 # @param CommentCharacter: Comment char, be used to ignore comment content
211 # @param KeySplitCharacter: Key split char, between key name and key value. Key1 = Value1, '=' is the key split char
212 # @param ValueSplitFlag: Value split flag, be used to decide if has multiple values
213 # @param ValueSplitCharacter: Value split char, be used to split multiple values. Key1 = Value1|Value2, '|' is the value split char
215 # @retval True Convert successfully
216 # @retval False Open file failed
218 def ConvertTextFileToDictionary(FileName
, Dictionary
, CommentCharacter
, KeySplitCharacter
, ValueSplitFlag
, ValueSplitCharacter
):
220 F
= open(FileName
,'r')
225 LineList
= Line
.split(KeySplitCharacter
,1)
226 if len(LineList
) >= 2:
227 Key
= LineList
[0].split()
228 if len(Key
) == 1 and Key
[0][0] != CommentCharacter
and Key
[0] not in Keys
:
230 Dictionary
[Key
[0]] = LineList
[1].replace('\\','/').split(ValueSplitCharacter
)
232 Dictionary
[Key
[0]] = LineList
[1].strip().replace('\\','/')
237 ## Convert Dictionary To Text File
239 # Convert a dictionary of (name:value) pairs to a text file.
241 # @param FileName: Text filename
242 # @param Dictionary: Dictionary to store data
243 # @param CommentCharacter: Comment char, be used to ignore comment content
244 # @param KeySplitCharacter: Key split char, between key name and key value. Key1 = Value1, '=' is the key split char
245 # @param ValueSplitFlag: Value split flag, be used to decide if has multiple values
246 # @param ValueSplitCharacter: Value split char, be used to split multiple values. Key1 = Value1|Value2, '|' is the value split char
248 # @retval True Convert successfully
249 # @retval False Open file failed
251 def ConvertDictionaryToTextFile(FileName
, Dictionary
, CommentCharacter
, KeySplitCharacter
, ValueSplitFlag
, ValueSplitCharacter
):
253 F
= open(FileName
,'r')
255 Lines
= F
.readlines()
259 Keys
= Dictionary
.keys()
262 if len(Key
) > MaxLength
:
266 LineList
= Line
.split(KeySplitCharacter
,1)
267 if len(LineList
) >= 2:
268 Key
= LineList
[0].split()
269 if len(Key
) == 1 and Key
[0][0] != CommentCharacter
and Key
[0] in Dictionary
:
271 Line
= '%-*s %c %s\n' % (MaxLength
, Key
[0], KeySplitCharacter
, ' '.join(Dictionary
[Key
[0]]))
273 Line
= '%-*s %c %s\n' % (MaxLength
, Key
[0], KeySplitCharacter
, Dictionary
[Key
[0]])
276 Lines
.insert(Index
,Line
)
279 for RemainingKey
in Keys
:
281 Line
= '%-*s %c %s\n' % (MaxLength
, RemainingKey
, KeySplitCharacter
,' '.join(Dictionary
[RemainingKey
]))
283 Line
= '%-*s %c %s\n' % (MaxLength
, RemainingKey
, KeySplitCharacter
, Dictionary
[RemainingKey
])
286 F
= open(FileName
,'w')
293 ## Create a new directory
295 # @param Directory: Directory to be created
297 def CreateDirectory(Directory
):
298 if not os
.access(Directory
, os
.F_OK
):
299 os
.makedirs (Directory
)
303 # @param Directory: Directory to be created
304 # @param FileName: Filename to be created
305 # @param Mode: The mode of open file, defautl is 'w'
307 def CreateFile(Directory
, FileName
, Mode
='w'):
308 CreateDirectory (Directory
)
309 return open(os
.path
.join(Directory
, FileName
), Mode
)
313 # This acts like the main() function for the script, unless it is 'import'ed into another
316 if __name__
== '__main__':
317 # Nothing to do here. Could do some unit tests