]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/Common/EdkIIWorkspace.py
Check In tool source code based on Build tool project revision r1655.
[mirror_edk2.git] / BaseTools / Source / Python / Common / EdkIIWorkspace.py
1 ## @file
2 # This is the base class for applications that operate on an EDK II Workspace
3 #
4 # Copyright (c) 2007, Intel Corporation
5 # All rights reserved. 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
9 #
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.
12 #
13
14 ##
15 # Import Modules
16 #
17 import os, sys, time
18 from DataType import *
19
20 ## EdkIIWorkspace
21 #
22 # Collect WorkspaceDir from the environment, the Verbose command line flag, and detect an icon bitmap file.
23 #
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
28 #
29 class EdkIIWorkspace:
30 def __init__(self):
31 self.StartTime = time.time()
32 self.PrintRunTime = False
33 self.PrintRunStatus = False
34 self.RunStatus = ''
35
36 #
37 # Check environment valiable 'WORKSPACE'
38 #
39 if os.environ.get('WORKSPACE') == None:
40 print 'ERROR: WORKSPACE not defined. Please run EdkSetup from the EDK II install directory.'
41 return False
42
43 self.CurrentWorkingDir = os.getcwd()
44
45 self.WorkspaceDir = os.path.realpath(os.environ.get('WORKSPACE'))
46 (Drive, Path) = os.path.splitdrive(self.WorkspaceDir)
47 if Drive == '':
48 (Drive, CwdPath) = os.path.splitdrive(self.CurrentWorkingDir)
49 if Drive != '':
50 self.WorkspaceDir = Drive + Path
51 else:
52 self.WorkspaceDir = Drive.upper() + Path
53
54 self.WorkspaceRelativeWorkingDir = self.WorkspaceRelativePath (self.CurrentWorkingDir)
55
56 try:
57 #
58 # Load TianoCoreOrgLogo, used for GUI tool
59 #
60 self.Icon = wx.Icon(self.WorkspaceFile('tools/Python/TianoCoreOrgLogo.gif'),wx.BITMAP_TYPE_GIF)
61 except:
62 self.Icon = None
63
64 self.Verbose = False
65 for Arg in sys.argv:
66 if Arg.lower() == '-v':
67 self.Verbose = True
68
69 ## Close build system
70 #
71 # Close build system and print running time and status
72 #
73 def Close(self):
74 if self.PrintRunTime:
75 Seconds = int(time.time() - self.StartTime)
76 if Seconds < 60:
77 print 'Run Time: %d seconds' % (Seconds)
78 else:
79 Minutes = Seconds / 60
80 Seconds = Seconds % 60
81 if Minutes < 60:
82 print 'Run Time: %d minutes %d seconds' % (Minutes, Seconds)
83 else:
84 Hours = Minutes / 60
85 Minutes = Minutes % 60
86 print 'Run Time: %d hours %d minutes %d seconds' % (Hours, Minutes, Seconds)
87 if self.RunStatus != '':
88 print self.RunStatus
89
90 ## Convert to a workspace relative filename
91 #
92 # Convert a full path filename to a workspace relative filename.
93 #
94 # @param FileName: The filename to be Converted
95 #
96 # @retval None Workspace dir is not found in the full path
97 # @retval string The relative filename
98 #
99 def WorkspaceRelativePath(self, FileName):
100 FileName = os.path.realpath(FileName)
101 if FileName.find(self.WorkspaceDir) != 0:
102 return None
103 return FileName.replace (self.WorkspaceDir, '').strip('\\').strip('/')
104
105 ## Convert to a full path filename
106 #
107 # Convert a workspace relative filename to a full path filename.
108 #
109 # @param FileName: The filename to be Converted
110 #
111 # @retval string The full path filename
112 #
113 def WorkspaceFile(self, FileName):
114 return os.path.realpath(os.path.join(self.WorkspaceDir,FileName))
115
116 ## Convert to a real path filename
117 #
118 # Convert ${WORKSPACE} to real path
119 #
120 # @param FileName: The filename to be Converted
121 #
122 # @retval string The full path filename
123 #
124 def WorkspacePathConvert(self, FileName):
125 return os.path.realpath(FileName.replace(TAB_WORKSPACE, self.WorkspaceDir))
126
127 ## Convert XML into a DOM
128 #
129 # Parse an XML file into a DOM and return the DOM.
130 #
131 # @param FileName: The filename to be parsed
132 #
133 # @retval XmlParseFile (self.WorkspaceFile(FileName))
134 #
135 def XmlParseFile (self, FileName):
136 if self.Verbose:
137 print FileName
138 return XmlParseFile (self.WorkspaceFile(FileName))
139
140 ## Convert a XML section
141 #
142 # Parse a section of an XML file into a DOM(Document Object Model) and return the DOM.
143 #
144 # @param FileName: The filename to be parsed
145 # @param SectionTag: The tag name of the section to be parsed
146 #
147 # @retval XmlParseFileSection (self.WorkspaceFile(FileName), SectionTag)
148 #
149 def XmlParseFileSection (self, FileName, SectionTag):
150 if self.Verbose:
151 print FileName
152 return XmlParseFileSection (self.WorkspaceFile(FileName), SectionTag)
153
154 ## Save a XML file
155 #
156 # Save a DOM(Document Object Model) into an XML file.
157 #
158 # @param Dom: The Dom to be saved
159 # @param FileName: The filename
160 #
161 # @retval XmlSaveFile (Dom, self.WorkspaceFile(FileName))
162 #
163 def XmlSaveFile (self, Dom, FileName):
164 if self.Verbose:
165 print FileName
166 return XmlSaveFile (Dom, self.WorkspaceFile(FileName))
167
168 ## Convert Text File To Dictionary
169 #
170 # Convert a workspace relative text file to a dictionary of (name:value) pairs.
171 #
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
178 #
179 # @retval ConvertTextFileToDictionary(self.WorkspaceFile(FileName), Dictionary, CommentCharacter, KeySplitCharacter, ValueSplitFlag, ValueSplitCharacter)
180 #
181 def ConvertTextFileToDictionary(self, FileName, Dictionary, CommentCharacter, KeySplitCharacter, ValueSplitFlag, ValueSplitCharacter):
182 if self.Verbose:
183 print FileName
184 return ConvertTextFileToDictionary(self.WorkspaceFile(FileName), Dictionary, CommentCharacter, KeySplitCharacter, ValueSplitFlag, ValueSplitCharacter)
185
186 ## Convert Dictionary To Text File
187 #
188 # Convert a dictionary of (name:value) pairs to a workspace relative text file.
189 #
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
196 #
197 # @retval ConvertDictionaryToTextFile(self.WorkspaceFile(FileName), Dictionary, CommentCharacter, KeySplitCharacter, ValueSplitFlag, ValueSplitCharacter)
198 #
199 def ConvertDictionaryToTextFile(self, FileName, Dictionary, CommentCharacter, KeySplitCharacter, ValueSplitFlag, ValueSplitCharacter):
200 if self.Verbose:
201 print FileName
202 return ConvertDictionaryToTextFile(self.WorkspaceFile(FileName), Dictionary, CommentCharacter, KeySplitCharacter, ValueSplitFlag, ValueSplitCharacter)
203
204 ## Convert Text File To Dictionary
205 #
206 # Convert a text file to a dictionary of (name:value) pairs.
207 #
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
214 #
215 # @retval True Convert successfully
216 # @retval False Open file failed
217 #
218 def ConvertTextFileToDictionary(FileName, Dictionary, CommentCharacter, KeySplitCharacter, ValueSplitFlag, ValueSplitCharacter):
219 try:
220 F = open(FileName,'r')
221 except:
222 return False
223 Keys = []
224 for Line in F:
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:
229 if ValueSplitFlag:
230 Dictionary[Key[0]] = LineList[1].replace('\\','/').split(ValueSplitCharacter)
231 else:
232 Dictionary[Key[0]] = LineList[1].strip().replace('\\','/')
233 Keys += [Key[0]]
234 F.close()
235 return True
236
237 ## Convert Dictionary To Text File
238 #
239 # Convert a dictionary of (name:value) pairs to a text file.
240 #
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
247 #
248 # @retval True Convert successfully
249 # @retval False Open file failed
250 #
251 def ConvertDictionaryToTextFile(FileName, Dictionary, CommentCharacter, KeySplitCharacter, ValueSplitFlag, ValueSplitCharacter):
252 try:
253 F = open(FileName,'r')
254 Lines = []
255 Lines = F.readlines()
256 F.close()
257 except:
258 Lines = []
259 Keys = Dictionary.keys()
260 MaxLength = 0
261 for Key in Keys:
262 if len(Key) > MaxLength:
263 MaxLength = len(Key)
264 Index = 0
265 for Line in Lines:
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:
270 if ValueSplitFlag:
271 Line = '%-*s %c %s\n' % (MaxLength, Key[0], KeySplitCharacter, ' '.join(Dictionary[Key[0]]))
272 else:
273 Line = '%-*s %c %s\n' % (MaxLength, Key[0], KeySplitCharacter, Dictionary[Key[0]])
274 Lines.pop(Index)
275 if Key[0] in Keys:
276 Lines.insert(Index,Line)
277 Keys.remove(Key[0])
278 Index += 1
279 for RemainingKey in Keys:
280 if ValueSplitFlag:
281 Line = '%-*s %c %s\n' % (MaxLength, RemainingKey, KeySplitCharacter,' '.join(Dictionary[RemainingKey]))
282 else:
283 Line = '%-*s %c %s\n' % (MaxLength, RemainingKey, KeySplitCharacter, Dictionary[RemainingKey])
284 Lines.append(Line)
285 try:
286 F = open(FileName,'w')
287 except:
288 return False
289 F.writelines(Lines)
290 F.close()
291 return True
292
293 ## Create a new directory
294 #
295 # @param Directory: Directory to be created
296 #
297 def CreateDirectory(Directory):
298 if not os.access(Directory, os.F_OK):
299 os.makedirs (Directory)
300
301 ## Create a new file
302 #
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'
306 #
307 def CreateFile(Directory, FileName, Mode='w'):
308 CreateDirectory (Directory)
309 return open(os.path.join(Directory, FileName), Mode)
310
311 ##
312 #
313 # This acts like the main() function for the script, unless it is 'import'ed into another
314 # script.
315 #
316 if __name__ == '__main__':
317 # Nothing to do here. Could do some unit tests
318 pass