2006-12-16 07:39:33 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2007-01-05 22:08:32 +01:00
|
|
|
"""This is a python script that takes user input from the command line and
|
|
|
|
creates a far (Framework Archive Manifest) file for distribution."""
|
|
|
|
|
2006-12-19 09:02:13 +01:00
|
|
|
import os, sys, getopt, string, xml.dom.minidom, zipfile, md5
|
2006-12-16 07:39:33 +01:00
|
|
|
from XmlRoutines import *
|
|
|
|
from WorkspaceRoutines import *
|
|
|
|
|
2006-12-21 02:32:37 +01:00
|
|
|
class Far:
|
|
|
|
"""This class is used to collect arbitrarty data from the template file."""
|
|
|
|
def __init__(far):
|
|
|
|
"""Assign the default values for the far fields."""
|
|
|
|
far.FileName = "output.far"
|
|
|
|
far.FarName=""
|
|
|
|
far.Version=""
|
|
|
|
far.License=""
|
2006-12-22 01:25:11 +01:00
|
|
|
far.Abstract=""
|
2006-12-21 02:32:37 +01:00
|
|
|
far.Description=""
|
|
|
|
far.Copyright=""
|
2006-12-22 01:25:11 +01:00
|
|
|
far.SpdFiles=[]
|
|
|
|
far.FpdFiles=[]
|
|
|
|
far.ExtraFiles=[]
|
2006-12-21 02:32:37 +01:00
|
|
|
|
|
|
|
far = Far()
|
2007-01-05 22:08:32 +01:00
|
|
|
"""The far object is constructed from the template file the user passed in."""
|
2006-12-21 02:32:37 +01:00
|
|
|
|
2006-12-16 07:39:33 +01:00
|
|
|
def parseMsa(msaFile, spdDir):
|
|
|
|
|
2007-01-05 22:08:32 +01:00
|
|
|
""" XXX Parse an msa file and return a list of all the files that this msa
|
|
|
|
includes."""
|
|
|
|
|
2006-12-16 08:50:00 +01:00
|
|
|
filelist = [msaFile]
|
2006-12-16 07:39:33 +01:00
|
|
|
|
|
|
|
msaDir = os.path.dirname(msaFile)
|
|
|
|
|
2006-12-19 09:02:13 +01:00
|
|
|
msa = xml.dom.minidom.parse(inWorkspace(os.path.join(spdDir, msaFile)))
|
2006-12-16 07:39:33 +01:00
|
|
|
|
|
|
|
xmlPaths = [
|
2006-12-19 09:02:13 +01:00
|
|
|
"/ModuleSurfaceArea/SourceFiles/Filename",
|
|
|
|
"/ModuleSurfaceArea/NonProcessedFiles/Filename" ]
|
|
|
|
|
2006-12-16 07:39:33 +01:00
|
|
|
for xmlPath in xmlPaths:
|
|
|
|
for f in XmlList(msa, xmlPath):
|
|
|
|
filelist.append(str(os.path.join(msaDir, XmlElementData(f))))
|
|
|
|
|
|
|
|
return filelist
|
|
|
|
|
|
|
|
def parseSpd(spdFile):
|
|
|
|
|
2007-01-05 22:08:32 +01:00
|
|
|
"""Parse an spd file and return a list of all the files that this spd
|
|
|
|
includes."""
|
|
|
|
|
2006-12-19 09:02:13 +01:00
|
|
|
files = []
|
2006-12-16 07:39:33 +01:00
|
|
|
|
|
|
|
spdDir = os.path.dirname(spdFile)
|
|
|
|
|
|
|
|
spd = xml.dom.minidom.parse(inWorkspace(spdFile))
|
|
|
|
|
|
|
|
xmlPaths = [
|
|
|
|
"/PackageSurfaceArea/LibraryClassDeclarations/LibraryClass/IncludeHeader",
|
|
|
|
"/PackageSurfaceArea/IndustryStdIncludes/IndustryStdHeader/IncludeHeader",
|
2006-12-16 21:51:59 +01:00
|
|
|
"/PackageSurfaceArea/PackageHeaders/IncludePkgHeader" ]
|
2006-12-16 07:39:33 +01:00
|
|
|
|
|
|
|
for xmlPath in xmlPaths:
|
|
|
|
for f in XmlList(spd, xmlPath):
|
2006-12-19 09:02:13 +01:00
|
|
|
files.append(str(XmlElementData(f)))
|
2006-12-16 07:39:33 +01:00
|
|
|
|
2006-12-16 09:28:47 +01:00
|
|
|
for f in XmlList(spd, "/PackageSurfaceArea/MsaFiles/Filename"):
|
2006-12-19 09:02:13 +01:00
|
|
|
msaFile = str(XmlElementData(f))
|
|
|
|
files += parseMsa(msaFile, spdDir)
|
|
|
|
|
|
|
|
cwd = os.getcwd()
|
|
|
|
os.chdir(inWorkspace(spdDir))
|
|
|
|
for root, dirs, entries in os.walk("Include"):
|
2006-12-22 01:25:11 +01:00
|
|
|
# Some files need to be skipped.
|
|
|
|
for r in ["CVS", ".svn"]:
|
2006-12-19 09:02:13 +01:00
|
|
|
if r in dirs:
|
|
|
|
dirs.remove(r)
|
|
|
|
for entry in entries:
|
|
|
|
files.append(os.path.join(os.path.normpath(root), entry))
|
|
|
|
os.chdir(cwd)
|
|
|
|
|
|
|
|
return files
|
|
|
|
|
|
|
|
def makeFarHeader(doc):
|
|
|
|
|
2007-01-05 22:08:32 +01:00
|
|
|
"""Create a dom tree for the Far Header. It will use information from the
|
|
|
|
template file passed on the command line, if present."""
|
|
|
|
|
2006-12-19 09:02:13 +01:00
|
|
|
header = doc.createElement("FarHeader")
|
|
|
|
name = doc.createElement("FarName")
|
2006-12-21 02:32:37 +01:00
|
|
|
name.appendChild(doc.createTextNode(far.FarName))
|
2006-12-19 09:02:13 +01:00
|
|
|
header.appendChild(name)
|
|
|
|
guidVal = doc.createElement("GuidValue")
|
|
|
|
guidVal.appendChild(doc.createTextNode(genguid()))
|
|
|
|
header.appendChild(guidVal)
|
|
|
|
ver = doc.createElement("Version")
|
2006-12-21 02:32:37 +01:00
|
|
|
ver.appendChild(doc.createTextNode(far.Version))
|
2006-12-19 09:02:13 +01:00
|
|
|
header.appendChild(ver)
|
|
|
|
abstract = doc.createElement("Abstract")
|
2006-12-21 02:32:37 +01:00
|
|
|
abstract.appendChild(doc.createTextNode(far.Abstract))
|
2006-12-19 09:02:13 +01:00
|
|
|
header.appendChild(abstract)
|
|
|
|
desc = doc.createElement("Description")
|
2006-12-21 02:32:37 +01:00
|
|
|
desc.appendChild(doc.createTextNode(far.Description))
|
2006-12-19 09:02:13 +01:00
|
|
|
header.appendChild(desc)
|
|
|
|
copy = doc.createElement("Copyright")
|
2006-12-21 02:32:37 +01:00
|
|
|
copy.appendChild(doc.createTextNode(far.Copyright))
|
2006-12-19 09:02:13 +01:00
|
|
|
header.appendChild(copy)
|
|
|
|
lic = doc.createElement("License")
|
2006-12-21 02:32:37 +01:00
|
|
|
lic.appendChild(doc.createTextNode(far.License))
|
2006-12-19 09:02:13 +01:00
|
|
|
header.appendChild(lic)
|
|
|
|
spec = doc.createElement("Specification")
|
|
|
|
spec.appendChild(doc.createTextNode("FRAMEWORK_BUILD_PACKAGING_SPECIFICATION 0x00000052"))
|
|
|
|
header.appendChild(spec)
|
|
|
|
|
|
|
|
return header
|
|
|
|
|
|
|
|
def getSpdGuidVersion(spdFile):
|
2006-12-16 07:39:33 +01:00
|
|
|
|
2007-01-05 22:08:32 +01:00
|
|
|
"""Returns a tuple (guid, version) which is read from the given spdFile."""
|
|
|
|
|
2006-12-19 09:02:13 +01:00
|
|
|
spd = xml.dom.minidom.parse(inWorkspace(spdFile))
|
|
|
|
|
|
|
|
return (XmlElement(spd, "/PackageSurfaceArea/SpdHeader/GuidValue"),
|
|
|
|
XmlElement(spd, "/PackageSurfaceArea/SpdHeader/Version"))
|
2006-12-16 07:39:33 +01:00
|
|
|
|
2006-12-22 01:25:11 +01:00
|
|
|
def makeFar(files, farname):
|
2006-12-16 07:39:33 +01:00
|
|
|
|
2007-01-05 22:08:32 +01:00
|
|
|
"""Make a far out of the given filelist and writes it to the file farname."""
|
|
|
|
|
2006-12-16 08:50:00 +01:00
|
|
|
domImpl = xml.dom.minidom.getDOMImplementation()
|
|
|
|
man = domImpl.createDocument(None, "FrameworkArchiveManifest", None)
|
|
|
|
top_element = man.documentElement
|
|
|
|
|
2006-12-19 09:02:13 +01:00
|
|
|
top_element.appendChild(makeFarHeader(man))
|
2006-12-16 09:28:47 +01:00
|
|
|
|
2006-12-16 08:50:00 +01:00
|
|
|
packList = man.createElement("FarPackageList")
|
|
|
|
top_element.appendChild(packList)
|
2006-12-16 09:28:47 +01:00
|
|
|
|
2006-12-16 08:50:00 +01:00
|
|
|
platList = man.createElement("FarPlatformList")
|
|
|
|
top_element.appendChild(platList)
|
2006-12-16 09:28:47 +01:00
|
|
|
|
2006-12-16 08:50:00 +01:00
|
|
|
contents = man.createElement("Contents")
|
|
|
|
top_element.appendChild(contents)
|
2006-12-16 09:28:47 +01:00
|
|
|
|
2006-12-19 09:02:13 +01:00
|
|
|
exts = man.createElement("UserExtensions")
|
|
|
|
top_element.appendChild(exts)
|
|
|
|
|
2006-12-16 07:39:33 +01:00
|
|
|
zip = zipfile.ZipFile(farname, "w")
|
2006-12-22 01:25:11 +01:00
|
|
|
for infile in set(files):
|
2006-12-16 09:28:47 +01:00
|
|
|
if not os.path.exists(inWorkspace(infile)):
|
2007-01-17 01:02:57 +01:00
|
|
|
print "Error: Non-existent file '%s'." % infile
|
|
|
|
sys.exit()
|
2006-12-16 09:28:47 +01:00
|
|
|
(_, extension) = os.path.splitext(infile)
|
2006-12-16 07:39:33 +01:00
|
|
|
if extension == ".spd":
|
2006-12-16 09:28:47 +01:00
|
|
|
filelist = parseSpd(infile)
|
2006-12-19 09:02:13 +01:00
|
|
|
spdDir = os.path.dirname(infile)
|
|
|
|
|
|
|
|
(spdGuid, spdVersion) = getSpdGuidVersion(infile)
|
2006-12-16 08:50:00 +01:00
|
|
|
|
2006-12-16 09:28:47 +01:00
|
|
|
package = man.createElement("FarPackage")
|
|
|
|
packList.appendChild(package)
|
2006-12-16 08:50:00 +01:00
|
|
|
|
2006-12-19 09:02:13 +01:00
|
|
|
spdfilename = farFileNode(man, inWorkspace(infile))
|
|
|
|
zip.write(inWorkspace(infile), infile)
|
2006-12-22 01:25:11 +01:00
|
|
|
spdfilename.appendChild(man.createTextNode(lean(infile)))
|
2006-12-16 09:28:47 +01:00
|
|
|
package.appendChild(spdfilename)
|
2006-12-16 08:50:00 +01:00
|
|
|
|
2006-12-19 09:02:13 +01:00
|
|
|
guidValue = man.createElement("GuidValue")
|
|
|
|
guidValue.appendChild(man.createTextNode(spdGuid))
|
|
|
|
package.appendChild(guidValue)
|
|
|
|
|
|
|
|
version = man.createElement("Version")
|
|
|
|
version.appendChild(man.createTextNode(spdVersion))
|
|
|
|
package.appendChild(version)
|
|
|
|
|
|
|
|
defaultPath = man.createElement("DefaultPath")
|
|
|
|
defaultPath.appendChild(man.createTextNode(spdDir))
|
|
|
|
package.appendChild(defaultPath)
|
|
|
|
|
|
|
|
farPlatformList = man.createElement("FarPlatformList")
|
|
|
|
package.appendChild(farPlatformList)
|
|
|
|
|
|
|
|
packContents = man.createElement("Contents")
|
|
|
|
package.appendChild(packContents)
|
|
|
|
|
|
|
|
ue = man.createElement("UserExtensions")
|
|
|
|
package.appendChild(ue)
|
2006-12-16 09:28:47 +01:00
|
|
|
|
|
|
|
for spdfile in filelist:
|
2006-12-19 09:02:13 +01:00
|
|
|
content = farFileNode(man, inWorkspace(os.path.join(spdDir, spdfile)))
|
|
|
|
zip.write(inWorkspace(os.path.join(spdDir, spdfile)), spdfile)
|
2006-12-22 01:25:11 +01:00
|
|
|
content.appendChild(man.createTextNode(lean(spdfile)))
|
2006-12-19 09:02:13 +01:00
|
|
|
packContents.appendChild(content)
|
2006-12-16 08:50:00 +01:00
|
|
|
|
2006-12-16 07:39:33 +01:00
|
|
|
elif extension == ".fpd":
|
2006-12-16 09:28:47 +01:00
|
|
|
|
|
|
|
platform = man.createElement("FarPlatform")
|
|
|
|
platList.appendChild(platform)
|
|
|
|
|
2006-12-19 09:02:13 +01:00
|
|
|
fpdfilename = farFileNode(man, inWorkspace(infile))
|
|
|
|
zip.write(inWorkspace(infile), infile)
|
2006-12-16 09:28:47 +01:00
|
|
|
platform.appendChild(fpdfilename)
|
2006-12-22 01:25:11 +01:00
|
|
|
fpdfilename.appendChild(man.createTextNode(lean(infile)))
|
2006-12-16 09:28:47 +01:00
|
|
|
|
2006-12-16 07:39:33 +01:00
|
|
|
else:
|
2006-12-19 09:02:13 +01:00
|
|
|
content = farFileNode(man, inWorkspace(infile))
|
|
|
|
zip.write(inWorkspace(infile), infile)
|
2006-12-22 01:25:11 +01:00
|
|
|
content.appendChild(man.createTextNode(lean(infile)))
|
2006-12-19 09:02:13 +01:00
|
|
|
contents.appendChild(content)
|
2006-12-16 09:28:47 +01:00
|
|
|
|
2006-12-16 20:15:09 +01:00
|
|
|
zip.writestr("FrameworkArchiveManifest.xml", man.toprettyxml(2*" "))
|
2006-12-16 07:39:33 +01:00
|
|
|
zip.close()
|
|
|
|
return
|
|
|
|
|
2006-12-19 09:02:13 +01:00
|
|
|
def farFileNode(doc, filename):
|
2007-01-05 22:08:32 +01:00
|
|
|
|
|
|
|
"""This is a function that returns a dom tree for a given file that is
|
|
|
|
included in the far. An md5sum is calculated for that file."""
|
|
|
|
|
2006-12-19 09:02:13 +01:00
|
|
|
content = doc.createElement("FarFilename")
|
2007-01-17 01:02:57 +01:00
|
|
|
try:
|
|
|
|
f=open(filename, "rb")
|
|
|
|
content.setAttribute("Md5sum", md5.md5(f.read()).hexdigest())
|
|
|
|
f.close()
|
|
|
|
except IOError:
|
|
|
|
print "Error: Unable to open file: %s" % filename
|
|
|
|
sys.exit()
|
|
|
|
|
2006-12-19 09:02:13 +01:00
|
|
|
return content
|
|
|
|
|
2006-12-16 20:15:09 +01:00
|
|
|
# This acts like the main() function for the script, unless it is 'import'ed
|
|
|
|
# into another script.
|
2006-12-16 07:39:33 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
|
|
|
|
# Create a pretty printer for dumping data structures in a readable form.
|
|
|
|
# pp = pprint.PrettyPrinter(indent=2)
|
|
|
|
|
|
|
|
# Process the command line args.
|
2006-12-21 02:32:37 +01:00
|
|
|
optlist, args = getopt.getopt(sys.argv[1:], 'hf:t:', [ 'template=', 'far=', 'help'])
|
2006-12-16 20:15:09 +01:00
|
|
|
|
2006-12-22 01:25:11 +01:00
|
|
|
# First pass through the options list.
|
2006-12-16 20:15:09 +01:00
|
|
|
for o, a in optlist:
|
|
|
|
if o in ["-h", "--help"]:
|
|
|
|
print """
|
|
|
|
Pass a list of .spd and .fpd files to be placed into a far for distribution.
|
|
|
|
You may give the name of the far with a -f or --far option. For example:
|
|
|
|
|
2007-01-17 01:02:57 +01:00
|
|
|
%s --template far-template --far library.far MdePkg/MdePkg.spd
|
2006-12-16 20:15:09 +01:00
|
|
|
|
2006-12-21 02:32:37 +01:00
|
|
|
The file paths of .spd and .fpd are treated as relative to the WORKSPACE
|
2006-12-22 01:25:11 +01:00
|
|
|
environment variable which must be set to a valid workspace root directory.
|
2007-01-17 01:02:57 +01:00
|
|
|
|
|
|
|
A template file may be passed in with the --template option. This template file
|
|
|
|
is a text file that allows more contol over the contents of the far.
|
2006-12-16 20:15:09 +01:00
|
|
|
""" % os.path.basename(sys.argv[0])
|
|
|
|
|
|
|
|
sys.exit()
|
2006-12-22 01:25:11 +01:00
|
|
|
optlist.remove((o,a))
|
2006-12-21 02:32:37 +01:00
|
|
|
if o in ["-t", "--template"]:
|
|
|
|
# The template file is processed first, so that command line options can
|
|
|
|
# override it.
|
|
|
|
templateName = a
|
|
|
|
execfile(templateName)
|
2006-12-22 01:25:11 +01:00
|
|
|
optlist.remove((o,a))
|
|
|
|
|
|
|
|
# Second pass through the options list. These can override the first pass.
|
|
|
|
for o, a in optlist:
|
|
|
|
print o, a
|
2006-12-16 20:15:09 +01:00
|
|
|
if o in ["-f", "--far"]:
|
2006-12-21 02:32:37 +01:00
|
|
|
far.FileName = a
|
2006-12-16 07:39:33 +01:00
|
|
|
|
2006-12-22 01:25:11 +01:00
|
|
|
# Let's err on the side of caution and not let people blow away data
|
|
|
|
# accidentally.
|
|
|
|
if os.path.exists(far.FileName):
|
|
|
|
print "Error: File %s exists. Not overwriting." % far.FileName
|
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
makeFar(far.SpdFiles + far.FpdFiles + far.ExtraFiles + args, far.FileName)
|