Set 0xff to both bridge & non-bridge device in case of unknown device.
[mirror_edk2.git] / Tools / Python / MkFar.py
CommitLineData
e853a9d4 1#!/usr/bin/env python
2
10d6603f 3import os, sys, getopt, string, xml.dom.minidom, zipfile, md5
e853a9d4 4from XmlRoutines import *
5from WorkspaceRoutines import *
6
7def parseMsa(msaFile, spdDir):
8
69932b41 9 filelist = [msaFile]
e853a9d4 10
11 msaDir = os.path.dirname(msaFile)
12
10d6603f 13 msa = xml.dom.minidom.parse(inWorkspace(os.path.join(spdDir, msaFile)))
e853a9d4 14
15 xmlPaths = [
10d6603f 16 "/ModuleSurfaceArea/SourceFiles/Filename",
17 "/ModuleSurfaceArea/NonProcessedFiles/Filename" ]
18
e853a9d4 19
20 for xmlPath in xmlPaths:
21 for f in XmlList(msa, xmlPath):
22 filelist.append(str(os.path.join(msaDir, XmlElementData(f))))
23
24 return filelist
25
26def parseSpd(spdFile):
27
10d6603f 28 files = []
e853a9d4 29
30 spdDir = os.path.dirname(spdFile)
31
32 spd = xml.dom.minidom.parse(inWorkspace(spdFile))
33
34 xmlPaths = [
35 "/PackageSurfaceArea/LibraryClassDeclarations/LibraryClass/IncludeHeader",
36 "/PackageSurfaceArea/IndustryStdIncludes/IndustryStdHeader/IncludeHeader",
3ff56e5e 37 "/PackageSurfaceArea/PackageHeaders/IncludePkgHeader" ]
e853a9d4 38
39 for xmlPath in xmlPaths:
40 for f in XmlList(spd, xmlPath):
10d6603f 41 files.append(str(XmlElementData(f)))
e853a9d4 42
d32aaa95 43 for f in XmlList(spd, "/PackageSurfaceArea/MsaFiles/Filename"):
10d6603f 44 msaFile = str(XmlElementData(f))
45 files += parseMsa(msaFile, spdDir)
46
47 cwd = os.getcwd()
48 os.chdir(inWorkspace(spdDir))
49 for root, dirs, entries in os.walk("Include"):
50 for r in ["CVS", ".svn"]:
51 if r in dirs:
52 dirs.remove(r)
53 for entry in entries:
54 files.append(os.path.join(os.path.normpath(root), entry))
55 os.chdir(cwd)
56
57 return files
58
59def makeFarHeader(doc):
60
61 header = doc.createElement("FarHeader")
62 name = doc.createElement("FarName")
63 name.appendChild(doc.createTextNode("My New Far"))
64 header.appendChild(name)
65 guidVal = doc.createElement("GuidValue")
66 guidVal.appendChild(doc.createTextNode(genguid()))
67 header.appendChild(guidVal)
68 ver = doc.createElement("Version")
69 ver.appendChild(doc.createTextNode("1.0"))
70 header.appendChild(ver)
71 abstract = doc.createElement("Abstract")
72 abstract.appendChild(doc.createTextNode("This is a cool new far."))
73 header.appendChild(abstract)
74 desc = doc.createElement("Description")
75 desc.appendChild(doc.createTextNode("This is a cool new far. It can do great things."))
76 header.appendChild(desc)
77 copy = doc.createElement("Copyright")
78 copy.appendChild(doc.createTextNode("Copyright (c) Intel Corporation 2006."))
79 header.appendChild(copy)
80 lic = doc.createElement("License")
81 lic.appendChild(doc.createTextNode("BSD Compatible."))
82 header.appendChild(lic)
83 spec = doc.createElement("Specification")
84 spec.appendChild(doc.createTextNode("FRAMEWORK_BUILD_PACKAGING_SPECIFICATION 0x00000052"))
85 header.appendChild(spec)
86
87 return header
88
89def getSpdGuidVersion(spdFile):
e853a9d4 90
10d6603f 91 spd = xml.dom.minidom.parse(inWorkspace(spdFile))
92
93 return (XmlElement(spd, "/PackageSurfaceArea/SpdHeader/GuidValue"),
94 XmlElement(spd, "/PackageSurfaceArea/SpdHeader/Version"))
e853a9d4 95
96def makeFar(filelist, farname):
97
69932b41 98 domImpl = xml.dom.minidom.getDOMImplementation()
99 man = domImpl.createDocument(None, "FrameworkArchiveManifest", None)
100 top_element = man.documentElement
101
10d6603f 102 top_element.appendChild(makeFarHeader(man))
d32aaa95 103
69932b41 104 packList = man.createElement("FarPackageList")
105 top_element.appendChild(packList)
d32aaa95 106
69932b41 107 platList = man.createElement("FarPlatformList")
108 top_element.appendChild(platList)
d32aaa95 109
69932b41 110 contents = man.createElement("Contents")
111 top_element.appendChild(contents)
d32aaa95 112
10d6603f 113 exts = man.createElement("UserExtensions")
114 top_element.appendChild(exts)
115
e853a9d4 116 zip = zipfile.ZipFile(farname, "w")
d32aaa95 117 for infile in filelist:
118 if not os.path.exists(inWorkspace(infile)):
119 print "Skipping non-existent file '%s'." % infile
120 (_, extension) = os.path.splitext(infile)
e853a9d4 121 if extension == ".spd":
d32aaa95 122 filelist = parseSpd(infile)
10d6603f 123 spdDir = os.path.dirname(infile)
124
125 (spdGuid, spdVersion) = getSpdGuidVersion(infile)
69932b41 126
d32aaa95 127 package = man.createElement("FarPackage")
128 packList.appendChild(package)
69932b41 129
10d6603f 130 spdfilename = farFileNode(man, inWorkspace(infile))
131 zip.write(inWorkspace(infile), infile)
132 spdfilename.appendChild(man.createTextNode(infile))
d32aaa95 133 package.appendChild(spdfilename)
69932b41 134
10d6603f 135 guidValue = man.createElement("GuidValue")
136 guidValue.appendChild(man.createTextNode(spdGuid))
137 package.appendChild(guidValue)
138
139 version = man.createElement("Version")
140 version.appendChild(man.createTextNode(spdVersion))
141 package.appendChild(version)
142
143 defaultPath = man.createElement("DefaultPath")
144 defaultPath.appendChild(man.createTextNode(spdDir))
145 package.appendChild(defaultPath)
146
147 farPlatformList = man.createElement("FarPlatformList")
148 package.appendChild(farPlatformList)
149
150 packContents = man.createElement("Contents")
151 package.appendChild(packContents)
152
153 ue = man.createElement("UserExtensions")
154 package.appendChild(ue)
d32aaa95 155
156 for spdfile in filelist:
10d6603f 157 content = farFileNode(man, inWorkspace(os.path.join(spdDir, spdfile)))
158 zip.write(inWorkspace(os.path.join(spdDir, spdfile)), spdfile)
159 content.appendChild(man.createTextNode(spdfile))
160 packContents.appendChild(content)
69932b41 161
e853a9d4 162 elif extension == ".fpd":
d32aaa95 163
164 platform = man.createElement("FarPlatform")
165 platList.appendChild(platform)
166
10d6603f 167 fpdfilename = farFileNode(man, inWorkspace(infile))
168 zip.write(inWorkspace(infile), infile)
d32aaa95 169 platform.appendChild(fpdfilename)
d32aaa95 170 fpdfilename.appendChild( man.createTextNode(infile) )
171
e853a9d4 172 else:
10d6603f 173 content = farFileNode(man, inWorkspace(infile))
174 zip.write(inWorkspace(infile), infile)
175 content.appendChild(man.createTextNode(infile))
176 contents.appendChild(content)
d32aaa95 177
d0f7ef3e 178 zip.writestr("FrameworkArchiveManifest.xml", man.toprettyxml(2*" "))
e853a9d4 179 zip.close()
180 return
181
10d6603f 182def farFileNode(doc, filename):
183 content = doc.createElement("FarFilename")
184 f=open(filename, "rb")
185 content.setAttribute("Md5sum", md5.md5(f.read()).hexdigest())
186 f.close()
187 return content
188
d0f7ef3e 189# This acts like the main() function for the script, unless it is 'import'ed
190# into another script.
e853a9d4 191if __name__ == '__main__':
192
193 # Create a pretty printer for dumping data structures in a readable form.
194 # pp = pprint.PrettyPrinter(indent=2)
195
d0f7ef3e 196 # Default name for far file.
197 farName = "output.far"
198
e853a9d4 199 # Process the command line args.
d0f7ef3e 200 optlist, args = getopt.getopt(sys.argv[1:], 'hf:', [ 'far=', 'help'])
201
202 for o, a in optlist:
203 if o in ["-h", "--help"]:
204 print """
205Pass a list of .spd and .fpd files to be placed into a far for distribution.
206You may give the name of the far with a -f or --far option. For example:
207
208 %s --far library.far MdePkg/MdePkg.spd
209
210The file paths of .spd and .fpd are relative to the WORKSPACE envirnonment
211which must be set to a valid workspace root directory.
212""" % os.path.basename(sys.argv[0])
213
214 sys.exit()
215 if o in ["-f", "--far"]:
216 farName = a
e853a9d4 217
d0f7ef3e 218 makeFar(args, farName)