Set the executable property on the script.
[mirror_edk2.git] / Tools / Python / EdkIIWorkspace.py
1 #!/usr/bin/env python
2
3 # This is the base class for applications that operate on an EDK II Workspace
4
5 import os, sys
6 from XmlRoutines import *
7
8 class EdkIIWorkspace:
9 def __init__(self):
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.'
13 return False
14
15 self.WorkspaceDir = os.path.realpath(os.environ.get('WORKSPACE'))
16 (Drive, Path) = os.path.splitdrive(self.WorkspaceDir)
17 if Drive == '':
18 (Drive, CwdPath) = os.path.splitdrive(os.getcwd())
19 if Drive != '':
20 self.WorkspaceDir = Drive + Path
21 else:
22 self.WorkspaceDir = Drive.upper() + Path
23
24 try:
25 self.Icon = wx.Icon(self.WorkspaceFile('tools/Python/TianoCoreOrgLogo.gif'),wx.BITMAP_TYPE_GIF)
26 except:
27 self.Icon = None
28
29 self.Verbose = False
30 for arg in sys.argv:
31 if arg.lower() == '-v':
32 self.Verbose = True
33
34 return True
35
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:
40 return ''
41 return FileName.replace (self.WorkspaceDir, '').strip('\\').strip('/')
42
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))
46
47 def XmlParseFile (self, FileName):
48 """Parse an XML file into a DOM and return the DOM."""
49 if self.Verbose:
50 print FileName
51 return XmlParseFile (self.WorkspaceFile(FileName))
52
53 def XmlParseFileSection (self, FileName, SectionTag):
54 """Parse a section of an XML file into a DOM(Document Object Model) and return the DOM."""
55 if self.Verbose:
56 print FileName
57 return XmlParseFileSection (self.WorkspaceFile(FileName), SectionTag)
58
59 def XmlSaveFile (self, Dom, FileName):
60 """Save a DOM(Document Object Model) into an XML file."""
61 if self.Verbose:
62 print FileName
63 return XmlSaveFile (Dom, self.WorkspaceFile(FileName))
64
65 def ConvertTextFileToDictionary(self, FileName, Dictionary, CommentCharacter, KeySplitCharacter, ValueSplitFlag, ValueSplitCharacter):
66 """Convert a workspace relative text file to a dictionary of (name:value) pairs."""
67 if self.Verbose:
68 print FileName
69 return ConvertTextFileToDictionary(self.WorkspaceFile(FileName), Dictionary, CommentCharacter, KeySplitCharacter, ValueSplitFlag, ValueSplitCharacter)
70
71 def ConvertDictionaryToTextFile(self, FileName, Dictionary, CommentCharacter, KeySplitCharacter, ValueSplitFlag, ValueSplitCharacter):
72 """Convert a dictionary of (name:value) pairs to a workspace relative text file."""
73 if self.Verbose:
74 print FileName
75 return ConvertDictionaryToTextFile(self.WorkspaceFile(FileName), Dictionary, CommentCharacter, KeySplitCharacter, ValueSplitFlag, ValueSplitCharacter)
76
77 #
78 # Convert a text file to a dictionary
79 #
80 def ConvertTextFileToDictionary(FileName, Dictionary, CommentCharacter, KeySplitCharacter, ValueSplitFlag, ValueSplitCharacter):
81 """Convert a text file to a dictionary of (name:value) pairs."""
82 try:
83 f = open(FileName,'r')
84 except:
85 return False
86 Keys = []
87 for Line in f:
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:
92 if ValueSplitFlag:
93 Dictionary[Key[0]] = LineList[1].replace('\\','/').split(ValueSplitCharacter)
94 else:
95 Dictionary[Key[0]] = LineList[1].strip().replace('\\','/')
96 Keys += [Key[0]]
97 f.close()
98 return True
99
100 def ConvertDictionaryToTextFile(FileName, Dictionary, CommentCharacter, KeySplitCharacter, ValueSplitFlag, ValueSplitCharacter):
101 """Convert a dictionary of (name:value) pairs to a text file."""
102 try:
103 f = open(FileName,'r')
104 Lines = []
105 Lines = f.readlines()
106 f.close()
107 except:
108 Lines = []
109 Keys = Dictionary.keys()
110 MaxLength = 0
111 for Key in Keys:
112 if len(Key) > MaxLength:
113 MaxLength = len(Key)
114 Index = 0
115 for Line in Lines:
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:
120 if ValueSplitFlag:
121 Line = '%-*s %c %s\n' % (MaxLength, Key[0], KeySplitCharacter, ' '.join(Dictionary[Key[0]]))
122 else:
123 Line = '%-*s %c %s\n' % (MaxLength, Key[0], KeySplitCharacter, Dictionary[Key[0]])
124 Lines.pop(Index)
125 if Key[0] in Keys:
126 Lines.insert(Index,Line)
127 Keys.remove(Key[0])
128 Index += 1
129 for RemainingKey in Keys:
130 if ValueSplitFlag:
131 Line = '%-*s %c %s\n' % (MaxLength, RemainingKey, KeySplitCharacter,' '.join(Dictionary[RemainingKey]))
132 else:
133 Line = '%-*s %c %s\n' % (MaxLength, RemainingKey, KeySplitCharacter, Dictionary[RemainingKey])
134 Lines.append(Line)
135 try:
136 f = open(FileName,'w')
137 except:
138 return False
139 f.writelines(Lines)
140 f.close()
141 return True
142
143 # This acts like the main() function for the script, unless it is 'import'ed into another
144 # script.
145 if __name__ == '__main__':
146
147 # Nothing to do here. Could do some unit tests.
148 pass