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