]> git.proxmox.com Git - mirror_edk2.git/blobdiff - Tools/Python/MkFar.py
Add better command line parameter handling.
[mirror_edk2.git] / Tools / Python / MkFar.py
index 1dba1fc4aae0ad3a7f9148fafa822e28e43c0eb1..105f1f49dee16e1a152319d477bf2a7c8727be2a 100755 (executable)
@@ -6,7 +6,7 @@ from WorkspaceRoutines import *
 
 def parseMsa(msaFile, spdDir):
 
-  filelist = []
+  filelist = [msaFile]
 
   msaDir = os.path.dirname(msaFile)
 
@@ -39,47 +39,98 @@ def parseSpd(spdFile):
     for f in XmlList(spd, xmlPath):
       filelist.append(str(os.path.join(spdDir, XmlElementData(f))))
 
-  for xmlPath in ["/PackageSurfaceArea/MsaFiles/Filename"]:
-    for f in XmlList(spd, xmlPath):
-      msaFile = str(os.path.join(spdDir, XmlElementData(f)))
-      filelist.append(msaFile)
-
-      filelist += parseMsa(msaFile, spdDir)
+  for f in XmlList(spd, "/PackageSurfaceArea/MsaFiles/Filename"):
+    msaFile = str(os.path.join(spdDir, XmlElementData(f)))
+    filelist += parseMsa(msaFile, spdDir)
 
   return filelist
 
 def makeFar(filelist, farname):
 
-  man = \
-"""<?xml version="1.0" encoding="UTF-8"?>
-<FrameworkArchiveManifest>
-</FrameworkArchiveManifest>
-"""
+  domImpl = xml.dom.minidom.getDOMImplementation()
+  man = domImpl.createDocument(None, "FrameworkArchiveManifest", None)
+  top_element = man.documentElement
+
+  header = man.createElement("FarHeader")
+  top_element.appendChild(header)
+
+  packList = man.createElement("FarPackageList")
+  top_element.appendChild(packList)
+
+  platList = man.createElement("FarPlatformList")
+  top_element.appendChild(platList)
+
+  contents = man.createElement("Contents")
+  top_element.appendChild(contents)
+
   zip = zipfile.ZipFile(farname, "w")
-  for file in args:
-    if not os.path.exists(inWorkspace(file)):
-      print "Skipping non-existent file '%s'." % file
-    (_, extension) = os.path.splitext(file)
+  for infile in filelist:
+    if not os.path.exists(inWorkspace(infile)):
+      print "Skipping non-existent file '%s'." % infile
+    (_, extension) = os.path.splitext(infile)
     if extension == ".spd":
-      filelist = parseSpd(file)
+      filelist = parseSpd(infile)
+
+      package = man.createElement("FarPackage")
+      packList.appendChild(package)
+
+      spdfilename = man.createElement("FarFilename")
+      package.appendChild(spdfilename)
+
+      spdfilename.appendChild( man.createTextNode(infile) )
+
+      for spdfile in filelist:
+        content = man.createElement("FarFilename")
+        content.appendChild( man.createTextNode(spdfile))
+        contents.appendChild(content)
+
     elif extension == ".fpd":
-      filelist = [file]
+      filelist = [infile]
+
+      platform = man.createElement("FarPlatform")
+      platList.appendChild(platform)
+
+      fpdfilename = man.createElement("FarFilename")
+      platform.appendChild(fpdfilename)
+
+      fpdfilename.appendChild( man.createTextNode(infile) )
+
     else:
       filelist = []
+
     for f in set(filelist):
       zip.write(inWorkspace(f), f)
-  zip.writestr("FrameworkArchiveManifest.xml", man)
+  zip.writestr("FrameworkArchiveManifest.xml", man.toprettyxml(2*" "))
   zip.close()
   return
 
-# This acts like the main() function for the script, unless it is 'import'ed into another
-# script.
+# This acts like the main() function for the script, unless it is 'import'ed
+# into another script.
 if __name__ == '__main__':
 
   # Create a pretty printer for dumping data structures in a readable form.
   # pp = pprint.PrettyPrinter(indent=2)
 
+  # Default name for far file.
+  farName = "output.far"
+
   # Process the command line args.
-  optlist, args = getopt.getopt(sys.argv[1:], 'h', [ 'example-long-arg=', 'testing'])
+  optlist, args = getopt.getopt(sys.argv[1:], 'hf:', [ 'far=', 'help'])
+
+  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:
+
+  %s --far library.far MdePkg/MdePkg.spd
+
+The file paths of .spd and .fpd are relative to the WORKSPACE envirnonment
+which must be set to a valid workspace root directory.
+""" % os.path.basename(sys.argv[0])
+
+      sys.exit()
+    if o in ["-f", "--far"]:
+      farName = a
 
-  makeFar(args, "test.far")
+  makeFar(args, farName)