]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Python/MkFar.py
Correct TeImage file format and Clean up PeiRebase tool to remove unused code and...
[mirror_edk2.git] / Tools / Python / MkFar.py
1 #!/usr/bin/env python
2
3 """This is a python script that takes user input from the command line and
4 creates a far (Framework Archive Manifest) file for distribution."""
5
6 import os, sys, getopt, string, xml.dom.minidom, zipfile, md5
7 from XmlRoutines import *
8 from WorkspaceRoutines import *
9
10 class Far:
11 """This class is used to collect arbitrarty data from the template file."""
12 def __init__(far):
13 """Assign the default values for the far fields."""
14 far.FileName = "output.far"
15 far.FarName=""
16 far.Version=""
17 far.License=""
18 far.Abstract=""
19 far.Description=""
20 far.Copyright=""
21 far.SpdFiles=[]
22 far.FpdFiles=[]
23 far.ExtraFiles=[]
24
25 far = Far()
26 """The far object is constructed from the template file the user passed in."""
27
28 def parseMsa(msaFile, spdDir):
29
30 """ XXX Parse an msa file and return a list of all the files that this msa
31 includes."""
32
33 filelist = [msaFile]
34
35 msaDir = os.path.dirname(msaFile)
36
37 msa = xml.dom.minidom.parse(inWorkspace(os.path.join(spdDir, msaFile)))
38
39 xmlPaths = [
40 "/ModuleSurfaceArea/SourceFiles/Filename",
41 "/ModuleSurfaceArea/NonProcessedFiles/Filename" ]
42
43 for xmlPath in xmlPaths:
44 for f in XmlList(msa, xmlPath):
45 filelist.append(str(os.path.join(msaDir, XmlElementData(f))))
46
47 return filelist
48
49 def parseSpd(spdFile):
50
51 """Parse an spd file and return a list of all the files that this spd
52 includes."""
53
54 files = []
55
56 spdDir = os.path.dirname(spdFile)
57
58 spd = xml.dom.minidom.parse(inWorkspace(spdFile))
59
60 xmlPaths = [
61 "/PackageSurfaceArea/LibraryClassDeclarations/LibraryClass/IncludeHeader",
62 "/PackageSurfaceArea/IndustryStdIncludes/IndustryStdHeader/IncludeHeader",
63 "/PackageSurfaceArea/PackageHeaders/IncludePkgHeader" ]
64
65 for xmlPath in xmlPaths:
66 for f in XmlList(spd, xmlPath):
67 files.append(str(XmlElementData(f)))
68
69 for f in XmlList(spd, "/PackageSurfaceArea/MsaFiles/Filename"):
70 msaFile = str(XmlElementData(f))
71 files += parseMsa(msaFile, spdDir)
72
73 cwd = os.getcwd()
74 os.chdir(inWorkspace(spdDir))
75 for root, dirs, entries in os.walk("Include"):
76 # Some files need to be skipped.
77 for r in ["CVS", ".svn"]:
78 if r in dirs:
79 dirs.remove(r)
80 for entry in entries:
81 files.append(os.path.join(os.path.normpath(root), entry))
82 os.chdir(cwd)
83
84 return files
85
86 def makeFarHeader(doc):
87
88 """Create a dom tree for the Far Header. It will use information from the
89 template file passed on the command line, if present."""
90
91 header = doc.createElement("FarHeader")
92 name = doc.createElement("FarName")
93 name.appendChild(doc.createTextNode(far.FarName))
94 header.appendChild(name)
95 guidVal = doc.createElement("GuidValue")
96 guidVal.appendChild(doc.createTextNode(genguid()))
97 header.appendChild(guidVal)
98 ver = doc.createElement("Version")
99 ver.appendChild(doc.createTextNode(far.Version))
100 header.appendChild(ver)
101 abstract = doc.createElement("Abstract")
102 abstract.appendChild(doc.createTextNode(far.Abstract))
103 header.appendChild(abstract)
104 desc = doc.createElement("Description")
105 desc.appendChild(doc.createTextNode(far.Description))
106 header.appendChild(desc)
107 copy = doc.createElement("Copyright")
108 copy.appendChild(doc.createTextNode(far.Copyright))
109 header.appendChild(copy)
110 lic = doc.createElement("License")
111 lic.appendChild(doc.createTextNode(far.License))
112 header.appendChild(lic)
113 spec = doc.createElement("Specification")
114 spec.appendChild(doc.createTextNode("FRAMEWORK_BUILD_PACKAGING_SPECIFICATION 0x00000052"))
115 header.appendChild(spec)
116
117 return header
118
119 def getSpdGuidVersion(spdFile):
120
121 """Returns a tuple (guid, version) which is read from the given spdFile."""
122
123 spd = xml.dom.minidom.parse(inWorkspace(spdFile))
124
125 return (XmlElement(spd, "/PackageSurfaceArea/SpdHeader/GuidValue"),
126 XmlElement(spd, "/PackageSurfaceArea/SpdHeader/Version"))
127
128 def makeFar(files, farname):
129
130 """Make a far out of the given filelist and writes it to the file farname."""
131
132 domImpl = xml.dom.minidom.getDOMImplementation()
133 man = domImpl.createDocument(None, "FrameworkArchiveManifest", None)
134 top_element = man.documentElement
135
136 top_element.appendChild(makeFarHeader(man))
137
138 packList = man.createElement("FarPackageList")
139 top_element.appendChild(packList)
140
141 platList = man.createElement("FarPlatformList")
142 top_element.appendChild(platList)
143
144 contents = man.createElement("Contents")
145 top_element.appendChild(contents)
146
147 exts = man.createElement("UserExtensions")
148 top_element.appendChild(exts)
149
150 zip = zipfile.ZipFile(farname, "w")
151 for infile in set(files):
152 if not os.path.exists(inWorkspace(infile)):
153 print "Skipping non-existent file '%s'." % infile
154 (_, extension) = os.path.splitext(infile)
155 if extension == ".spd":
156 filelist = parseSpd(infile)
157 spdDir = os.path.dirname(infile)
158
159 (spdGuid, spdVersion) = getSpdGuidVersion(infile)
160
161 package = man.createElement("FarPackage")
162 packList.appendChild(package)
163
164 spdfilename = farFileNode(man, inWorkspace(infile))
165 zip.write(inWorkspace(infile), infile)
166 spdfilename.appendChild(man.createTextNode(lean(infile)))
167 package.appendChild(spdfilename)
168
169 guidValue = man.createElement("GuidValue")
170 guidValue.appendChild(man.createTextNode(spdGuid))
171 package.appendChild(guidValue)
172
173 version = man.createElement("Version")
174 version.appendChild(man.createTextNode(spdVersion))
175 package.appendChild(version)
176
177 defaultPath = man.createElement("DefaultPath")
178 defaultPath.appendChild(man.createTextNode(spdDir))
179 package.appendChild(defaultPath)
180
181 farPlatformList = man.createElement("FarPlatformList")
182 package.appendChild(farPlatformList)
183
184 packContents = man.createElement("Contents")
185 package.appendChild(packContents)
186
187 ue = man.createElement("UserExtensions")
188 package.appendChild(ue)
189
190 for spdfile in filelist:
191 content = farFileNode(man, inWorkspace(os.path.join(spdDir, spdfile)))
192 zip.write(inWorkspace(os.path.join(spdDir, spdfile)), spdfile)
193 content.appendChild(man.createTextNode(lean(spdfile)))
194 packContents.appendChild(content)
195
196 elif extension == ".fpd":
197
198 platform = man.createElement("FarPlatform")
199 platList.appendChild(platform)
200
201 fpdfilename = farFileNode(man, inWorkspace(infile))
202 zip.write(inWorkspace(infile), infile)
203 platform.appendChild(fpdfilename)
204 fpdfilename.appendChild(man.createTextNode(lean(infile)))
205
206 else:
207 content = farFileNode(man, inWorkspace(infile))
208 zip.write(inWorkspace(infile), infile)
209 content.appendChild(man.createTextNode(lean(infile)))
210 contents.appendChild(content)
211
212 zip.writestr("FrameworkArchiveManifest.xml", man.toprettyxml(2*" "))
213 zip.close()
214 return
215
216 def farFileNode(doc, filename):
217
218 """This is a function that returns a dom tree for a given file that is
219 included in the far. An md5sum is calculated for that file."""
220
221 content = doc.createElement("FarFilename")
222 f=open(filename, "rb")
223 content.setAttribute("Md5sum", md5.md5(f.read()).hexdigest())
224 f.close()
225 return content
226
227 # This acts like the main() function for the script, unless it is 'import'ed
228 # into another script.
229 if __name__ == '__main__':
230
231 # Create a pretty printer for dumping data structures in a readable form.
232 # pp = pprint.PrettyPrinter(indent=2)
233
234 # Process the command line args.
235 optlist, args = getopt.getopt(sys.argv[1:], 'hf:t:', [ 'template=', 'far=', 'help'])
236
237 # First pass through the options list.
238 for o, a in optlist:
239 if o in ["-h", "--help"]:
240 print """
241 Pass a list of .spd and .fpd files to be placed into a far for distribution.
242 You may give the name of the far with a -f or --far option. For example:
243
244 %s --far library.far MdePkg/MdePkg.spd
245
246 The file paths of .spd and .fpd are treated as relative to the WORKSPACE
247 environment variable which must be set to a valid workspace root directory.
248 """ % os.path.basename(sys.argv[0])
249
250 sys.exit()
251 optlist.remove((o,a))
252 if o in ["-t", "--template"]:
253 # The template file is processed first, so that command line options can
254 # override it.
255 templateName = a
256 execfile(templateName)
257 optlist.remove((o,a))
258
259 # Second pass through the options list. These can override the first pass.
260 for o, a in optlist:
261 print o, a
262 if o in ["-f", "--far"]:
263 far.FileName = a
264
265 # Let's err on the side of caution and not let people blow away data
266 # accidentally.
267 if os.path.exists(far.FileName):
268 print "Error: File %s exists. Not overwriting." % far.FileName
269 sys.exit()
270
271 makeFar(far.SpdFiles + far.FpdFiles + far.ExtraFiles + args, far.FileName)