]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Python/MkFar.py
ffe27a870aa7483bf8de06c4c0605e59cbc2c8a2
[mirror_edk2.git] / Tools / Python / MkFar.py
1 #!/usr/bin/env python
2
3 import os, sys, getopt, string, xml.dom.minidom, zipfile, md5
4 from XmlRoutines import *
5 from WorkspaceRoutines import *
6
7 def parseMsa(msaFile, spdDir):
8
9 filelist = [msaFile]
10
11 msaDir = os.path.dirname(msaFile)
12
13 msa = xml.dom.minidom.parse(inWorkspace(os.path.join(spdDir, msaFile)))
14
15 xmlPaths = [
16 "/ModuleSurfaceArea/SourceFiles/Filename",
17 "/ModuleSurfaceArea/NonProcessedFiles/Filename" ]
18
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
26 def parseSpd(spdFile):
27
28 files = []
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",
37 "/PackageSurfaceArea/PackageHeaders/IncludePkgHeader" ]
38
39 for xmlPath in xmlPaths:
40 for f in XmlList(spd, xmlPath):
41 files.append(str(XmlElementData(f)))
42
43 for f in XmlList(spd, "/PackageSurfaceArea/MsaFiles/Filename"):
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
59 def 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
89 def getSpdGuidVersion(spdFile):
90
91 spd = xml.dom.minidom.parse(inWorkspace(spdFile))
92
93 return (XmlElement(spd, "/PackageSurfaceArea/SpdHeader/GuidValue"),
94 XmlElement(spd, "/PackageSurfaceArea/SpdHeader/Version"))
95
96 def makeFar(filelist, farname):
97
98 domImpl = xml.dom.minidom.getDOMImplementation()
99 man = domImpl.createDocument(None, "FrameworkArchiveManifest", None)
100 top_element = man.documentElement
101
102 top_element.appendChild(makeFarHeader(man))
103
104 packList = man.createElement("FarPackageList")
105 top_element.appendChild(packList)
106
107 platList = man.createElement("FarPlatformList")
108 top_element.appendChild(platList)
109
110 contents = man.createElement("Contents")
111 top_element.appendChild(contents)
112
113 exts = man.createElement("UserExtensions")
114 top_element.appendChild(exts)
115
116 zip = zipfile.ZipFile(farname, "w")
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)
121 if extension == ".spd":
122 filelist = parseSpd(infile)
123 spdDir = os.path.dirname(infile)
124
125 (spdGuid, spdVersion) = getSpdGuidVersion(infile)
126
127 package = man.createElement("FarPackage")
128 packList.appendChild(package)
129
130 spdfilename = farFileNode(man, inWorkspace(infile))
131 zip.write(inWorkspace(infile), infile)
132 spdfilename.appendChild(man.createTextNode(infile))
133 package.appendChild(spdfilename)
134
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)
155
156 for spdfile in filelist:
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)
161
162 elif extension == ".fpd":
163
164 platform = man.createElement("FarPlatform")
165 platList.appendChild(platform)
166
167 fpdfilename = farFileNode(man, inWorkspace(infile))
168 zip.write(inWorkspace(infile), infile)
169 platform.appendChild(fpdfilename)
170 fpdfilename.appendChild( man.createTextNode(infile) )
171
172 else:
173 content = farFileNode(man, inWorkspace(infile))
174 zip.write(inWorkspace(infile), infile)
175 content.appendChild(man.createTextNode(infile))
176 contents.appendChild(content)
177
178 zip.writestr("FrameworkArchiveManifest.xml", man.toprettyxml(2*" "))
179 zip.close()
180 return
181
182 def 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
189 # This acts like the main() function for the script, unless it is 'import'ed
190 # into another script.
191 if __name__ == '__main__':
192
193 # Create a pretty printer for dumping data structures in a readable form.
194 # pp = pprint.PrettyPrinter(indent=2)
195
196 # Default name for far file.
197 farName = "output.far"
198
199 # Process the command line args.
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 """
205 Pass a list of .spd and .fpd files to be placed into a far for distribution.
206 You 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
210 The file paths of .spd and .fpd are relative to the WORKSPACE envirnonment
211 which 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
217
218 makeFar(args, farName)