]>
git.proxmox.com Git - mirror_edk2.git/blob - Tools/Python/EdkIIWorkspace.py
3 # This is the base class for applications that operate on an EDK II Workspace
6 from XmlRoutines
import *
10 """Collect WorkspaceDir from the environment, the Verbose command line flag, and detect an icon bitmap file."""
11 if os
.environ
.get('WORKSPACE') == None:
12 print 'ERROR: WORKSPACE not defined. Please run EdkSetup from the EDK II install directory.'
15 self
.WorkspaceDir
= os
.path
.realpath(os
.environ
.get('WORKSPACE'))
16 (Drive
, Path
) = os
.path
.splitdrive(self
.WorkspaceDir
)
18 (Drive
, CwdPath
) = os
.path
.splitdrive(os
.getcwd())
20 self
.WorkspaceDir
= Drive
+ Path
22 self
.WorkspaceDir
= Drive
.upper() + Path
25 self
.Icon
= wx
.Icon(self
.WorkspaceFile('tools/Python/TianoCoreOrgLogo.gif'),wx
.BITMAP_TYPE_GIF
)
31 if arg
.lower() == '-v':
36 def WorkspaceRelativePath(self
, FileName
):
37 """Convert a full path filename to a workspace relative filename."""
38 FileName
= os
.path
.realpath(FileName
)
39 if FileName
.find(self
.WorkspaceDir
) != 0:
41 return FileName
.replace (self
.WorkspaceDir
, '').strip('\\').strip('/')
43 def WorkspaceFile(self
, FileName
):
44 """Convert a workspace relative filename to a full path filename."""
45 return os
.path
.realpath(os
.path
.join(self
.WorkspaceDir
,FileName
))
47 def XmlParseFile (self
, FileName
):
48 """Parse an XML file into a DOM and return the DOM."""
51 return XmlParseFile (self
.WorkspaceFile(FileName
))
53 def XmlParseFileSection (self
, FileName
, SectionTag
):
54 """Parse a section of an XML file into a DOM(Document Object Model) and return the DOM."""
57 return XmlParseFileSection (self
.WorkspaceFile(FileName
), SectionTag
)
59 def XmlSaveFile (self
, Dom
, FileName
):
60 """Save a DOM(Document Object Model) into an XML file."""
63 return XmlSaveFile (Dom
, self
.WorkspaceFile(FileName
))
65 def ConvertTextFileToDictionary(self
, FileName
, Dictionary
, CommentCharacter
, KeySplitCharacter
, ValueSplitFlag
, ValueSplitCharacter
):
66 """Convert a workspace relative text file to a dictionary of (name:value) pairs."""
69 return ConvertTextFileToDictionary(self
.WorkspaceFile(FileName
), Dictionary
, CommentCharacter
, KeySplitCharacter
, ValueSplitFlag
, ValueSplitCharacter
)
71 def ConvertDictionaryToTextFile(self
, FileName
, Dictionary
, CommentCharacter
, KeySplitCharacter
, ValueSplitFlag
, ValueSplitCharacter
):
72 """Convert a dictionary of (name:value) pairs to a workspace relative text file."""
75 return ConvertDictionaryToTextFile(self
.WorkspaceFile(FileName
), Dictionary
, CommentCharacter
, KeySplitCharacter
, ValueSplitFlag
, ValueSplitCharacter
)
78 # Convert a text file to a dictionary
80 def ConvertTextFileToDictionary(FileName
, Dictionary
, CommentCharacter
, KeySplitCharacter
, ValueSplitFlag
, ValueSplitCharacter
):
81 """Convert a text file to a dictionary of (name:value) pairs."""
83 f
= open(FileName
,'r')
88 LineList
= Line
.split(KeySplitCharacter
,1)
89 if len(LineList
) >= 2:
90 Key
= LineList
[0].split()
91 if len(Key
) == 1 and Key
[0][0] != CommentCharacter
and Key
[0] not in Keys
:
93 Dictionary
[Key
[0]] = LineList
[1].replace('\\','/').split(ValueSplitCharacter
)
95 Dictionary
[Key
[0]] = LineList
[1].strip().replace('\\','/')
100 def ConvertDictionaryToTextFile(FileName
, Dictionary
, CommentCharacter
, KeySplitCharacter
, ValueSplitFlag
, ValueSplitCharacter
):
101 """Convert a dictionary of (name:value) pairs to a text file."""
103 f
= open(FileName
,'r')
105 Lines
= f
.readlines()
109 Keys
= Dictionary
.keys()
112 if len(Key
) > MaxLength
:
116 LineList
= Line
.split(KeySplitCharacter
,1)
117 if len(LineList
) >= 2:
118 Key
= LineList
[0].split()
119 if len(Key
) == 1 and Key
[0][0] != CommentCharacter
and Key
[0] in Dictionary
:
121 Line
= '%-*s %c %s\n' % (MaxLength
, Key
[0], KeySplitCharacter
, ' '.join(Dictionary
[Key
[0]]))
123 Line
= '%-*s %c %s\n' % (MaxLength
, Key
[0], KeySplitCharacter
, Dictionary
[Key
[0]])
126 Lines
.insert(Index
,Line
)
129 for RemainingKey
in Keys
:
131 Line
= '%-*s %c %s\n' % (MaxLength
, RemainingKey
, KeySplitCharacter
,' '.join(Dictionary
[RemainingKey
]))
133 Line
= '%-*s %c %s\n' % (MaxLength
, RemainingKey
, KeySplitCharacter
, Dictionary
[RemainingKey
])
136 f
= open(FileName
,'w')
143 # This acts like the main() function for the script, unless it is 'import'ed into another
145 if __name__
== '__main__':
147 # Nothing to do here. Could do some unit tests.