Added optional whitespace characters in before and/or after the content within <eleme...
[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."""
312ffece 90
91 header = XmlAppendChildElement(doc.documentElement, "FarHeader")
92
93 XmlAppendChildElement(header, "FarName", far.FarName)
94 XmlAppendChildElement(header, "GuidValue", genguid())
95 XmlAppendChildElement(header, "Version", far.Version)
96 XmlAppendChildElement(header, "Abstract", far.Abstract)
97 XmlAppendChildElement(header, "Description", far.Description)
98 XmlAppendChildElement(header, "Copyright", far.Copyright)
99 XmlAppendChildElement(header, "License", far.License)
100 XmlAppendChildElement(header, "Specification", "FRAMEWORK_BUILD_PACKAGING_SPECIFICATION 0x00000052")
10d6603f 101
102 return header
103
104def getSpdGuidVersion(spdFile):
e853a9d4 105
3b7a53b6 106 """Returns a tuple (guid, version) which is read from the given spdFile."""
107
10d6603f 108 spd = xml.dom.minidom.parse(inWorkspace(spdFile))
109
110 return (XmlElement(spd, "/PackageSurfaceArea/SpdHeader/GuidValue"),
111 XmlElement(spd, "/PackageSurfaceArea/SpdHeader/Version"))
e853a9d4 112
af2efcaf 113def makeFar(files, farname):
e853a9d4 114
3b7a53b6 115 """Make a far out of the given filelist and writes it to the file farname."""
116
69932b41 117 domImpl = xml.dom.minidom.getDOMImplementation()
118 man = domImpl.createDocument(None, "FrameworkArchiveManifest", None)
119 top_element = man.documentElement
120
10d6603f 121 top_element.appendChild(makeFarHeader(man))
d32aaa95 122
312ffece 123 packList = XmlAppendChildElement(top_element, "FarPackageList")
124 platList = XmlAppendChildElement(top_element, "FarPlatformList")
125 contents = XmlAppendChildElement(top_element, "Contents")
126 XmlAppendChildElement(top_element, "UserExtensions")
10d6603f 127
e853a9d4 128 zip = zipfile.ZipFile(farname, "w")
af2efcaf 129 for infile in set(files):
d32aaa95 130 if not os.path.exists(inWorkspace(infile)):
5b8acefd 131 print "Error: Non-existent file '%s'." % infile
132 sys.exit()
d32aaa95 133 (_, extension) = os.path.splitext(infile)
e853a9d4 134 if extension == ".spd":
d32aaa95 135 filelist = parseSpd(infile)
10d6603f 136 spdDir = os.path.dirname(infile)
137
138 (spdGuid, spdVersion) = getSpdGuidVersion(infile)
69932b41 139
312ffece 140 package = XmlAppendChildElement(packList, "FarPackage")
141 XmlAppendChildElement(package, "FarFilename", lean(infile), {"Md5Sum": Md5(inWorkspace(infile))})
10d6603f 142 zip.write(inWorkspace(infile), infile)
312ffece 143 XmlAppendChildElement(package, "GuidValue", spdGuid)
144 XmlAppendChildElement(package, "Version", spdVersion)
145 XmlAppendChildElement(package, "DefaultPath", spdDir)
146 XmlAppendChildElement(package, "FarPlatformList")
147 packContents = XmlAppendChildElement(package, "Contents")
148 XmlAppendChildElement(package, "UserExtensions")
d32aaa95 149
150 for spdfile in filelist:
312ffece 151 XmlAppendChildElement(packContents, "FarFilename", lean(spdfile), {"Md5Sum": Md5(inWorkspace(os.path.join(spdDir, spdfile)))})
24a86f9a 152 zip.write(inWorkspace(os.path.join(spdDir, spdfile)), os.path.join(spdDir,spdfile))
69932b41 153
e853a9d4 154 elif extension == ".fpd":
d32aaa95 155
312ffece 156 platform = XmlAppendChildElement(platList, "FarPlatform")
157 XmlAppendChildElement(platform, "FarFilename", lean(infile), {"Md5Sum": Md5(inWorkspace(infile))})
10d6603f 158 zip.write(inWorkspace(infile), infile)
d32aaa95 159
e853a9d4 160 else:
312ffece 161 XmlAppendChildElement(contents, "FarFilename", lean(infile), {"Md5Sum": Md5(inWorkspace(infile))})
10d6603f 162 zip.write(inWorkspace(infile), infile)
d32aaa95 163
312ffece 164 zip.writestr("FrameworkArchiveManifest.xml", man.toxml('UTF-8'))
e853a9d4 165 zip.close()
166 return
167
d0f7ef3e 168# This acts like the main() function for the script, unless it is 'import'ed
169# into another script.
e853a9d4 170if __name__ == '__main__':
171
172 # Create a pretty printer for dumping data structures in a readable form.
173 # pp = pprint.PrettyPrinter(indent=2)
174
175 # Process the command line args.
312ffece 176 optlist, args = getopt.getopt(sys.argv[1:], 'ho:t:v', [ 'template=', 'output=', 'far=', 'help', 'debug', 'verbose', 'version'])
d0f7ef3e 177
af2efcaf 178 # First pass through the options list.
d0f7ef3e 179 for o, a in optlist:
180 if o in ["-h", "--help"]:
181 print """
182Pass a list of .spd and .fpd files to be placed into a far for distribution.
183You may give the name of the far with a -f or --far option. For example:
184
5b8acefd 185 %s --template far-template --far library.far MdePkg/MdePkg.spd
d0f7ef3e 186
fb96878e 187The file paths of .spd and .fpd are treated as relative to the WORKSPACE
af2efcaf 188environment variable which must be set to a valid workspace root directory.
5b8acefd 189
190A template file may be passed in with the --template option. This template file
191is a text file that allows more contol over the contents of the far.
d0f7ef3e 192""" % os.path.basename(sys.argv[0])
193
194 sys.exit()
af2efcaf 195 optlist.remove((o,a))
fb96878e 196 if o in ["-t", "--template"]:
197 # The template file is processed first, so that command line options can
198 # override it.
199 templateName = a
200 execfile(templateName)
af2efcaf 201 optlist.remove((o,a))
202
203 # Second pass through the options list. These can override the first pass.
204 for o, a in optlist:
205 print o, a
312ffece 206 if o in ["-o", "--far", "--output"]:
fb96878e 207 far.FileName = a
e853a9d4 208
af2efcaf 209 # Let's err on the side of caution and not let people blow away data
210 # accidentally.
211 if os.path.exists(far.FileName):
212 print "Error: File %s exists. Not overwriting." % far.FileName
213 sys.exit()
214
215 makeFar(far.SpdFiles + far.FpdFiles + far.ExtraFiles + args, far.FileName)