]> git.proxmox.com Git - mirror_edk2.git/blobdiff - Tools/Python/MkFar.py
changed the guid assignment from constant array to hi and low UINT64s (Intel compiler...
[mirror_edk2.git] / Tools / Python / MkFar.py
index bdecd2137cf34b2ddcd2963b14d4a8ba84dc8cd0..2848b1de6596093c432236a13fe9eb897f3ee653 100755 (executable)
@@ -25,9 +25,16 @@ class Far:
 far = Far()
 """The far object is constructed from the template file the user passed in."""
 
+def AddToZip(zip, infile):
+
+  """Add a file to a zip file, provided it is not already there."""
+
+  if not infile in zip.namelist():
+    zip.write(inWorkspace(infile), infile)
+
 def parseMsa(msaFile, spdDir):
 
-  """ XXX Parse an msa file and return a list of all the files that this msa
+  """Parse an msa file and return a list of all the files that this msa
   includes."""
 
   filelist = [msaFile]
@@ -39,7 +46,7 @@ def parseMsa(msaFile, spdDir):
   xmlPaths = [
     "/ModuleSurfaceArea/SourceFiles/Filename",
     "/ModuleSurfaceArea/NonProcessedFiles/Filename" ]
-    
+
   for xmlPath in xmlPaths:
     for f in XmlList(msa, xmlPath):
       filelist.append(str(os.path.join(msaDir, XmlElementData(f))))
@@ -57,10 +64,16 @@ def parseSpd(spdFile):
 
   spd = xml.dom.minidom.parse(inWorkspace(spdFile))
 
+  # We are currently ignoring these hints.
+  readonly = XmlElement(spd, "/PackageSurfaceArea/PackageDefinitions/ReadOnly") != "false"
+  repackage = XmlElement(spd, "/PackageSurfaceArea/PackageDefinitions/RePackage") != "false"
+
   xmlPaths = [
     "/PackageSurfaceArea/LibraryClassDeclarations/LibraryClass/IncludeHeader",
-    "/PackageSurfaceArea/IndustryStdIncludes/IndustryStdHeader/IncludeHeader",
-    "/PackageSurfaceArea/PackageHeaders/IncludePkgHeader" ]
+    "/PackageSurfaceArea/IndustryStdIncludes/IndustryStdHeader/IncludeHeader" ]
+
+    # These are covered by the Industry Standard Includes.
+    # "/PackageSurfaceArea/PackageHeaders/IncludePkgHeader"
 
   for xmlPath in xmlPaths:
     for f in XmlList(spd, xmlPath):
@@ -87,32 +100,17 @@ def makeFarHeader(doc):
 
   """Create a dom tree for the Far Header. It will use information from the
   template file passed on the command line, if present."""
-
-  header = doc.createElement("FarHeader")
-  name = doc.createElement("FarName")
-  name.appendChild(doc.createTextNode(far.FarName))
-  header.appendChild(name)
-  guidVal = doc.createElement("GuidValue")
-  guidVal.appendChild(doc.createTextNode(genguid()))
-  header.appendChild(guidVal)
-  ver = doc.createElement("Version")
-  ver.appendChild(doc.createTextNode(far.Version))
-  header.appendChild(ver)
-  abstract = doc.createElement("Abstract")
-  abstract.appendChild(doc.createTextNode(far.Abstract))
-  header.appendChild(abstract)
-  desc = doc.createElement("Description")
-  desc.appendChild(doc.createTextNode(far.Description))
-  header.appendChild(desc)
-  copy = doc.createElement("Copyright")
-  copy.appendChild(doc.createTextNode(far.Copyright))
-  header.appendChild(copy)
-  lic = doc.createElement("License")
-  lic.appendChild(doc.createTextNode(far.License))
-  header.appendChild(lic)
-  spec = doc.createElement("Specification")
-  spec.appendChild(doc.createTextNode("FRAMEWORK_BUILD_PACKAGING_SPECIFICATION 0x00000052"))
-  header.appendChild(spec)
+  header = XmlAppendChildElement(doc.documentElement, "FarHeader")
+
+  XmlAppendChildElement(header, "FarName", far.FarName)
+  XmlAppendChildElement(header, "GuidValue", genguid())
+  XmlAppendChildElement(header, "Version", far.Version)
+  XmlAppendChildElement(header, "Abstract", far.Abstract)
+  XmlAppendChildElement(header, "Description", far.Description)
+  XmlAppendChildElement(header, "Copyright", far.Copyright)
+  XmlAppendChildElement(header, "License", far.License)
+  XmlAppendChildElement(header, "Specification", "FRAMEWORK_BUILD_PACKAGING_SPECIFICATION 0x00000052")
 
   return header
 
@@ -135,19 +133,15 @@ def makeFar(files, farname):
 
   top_element.appendChild(makeFarHeader(man))
 
-  packList = man.createElement("FarPackageList")
-  top_element.appendChild(packList)
+  packList = XmlAppendChildElement(top_element, "FarPackageList")
+  platList = XmlAppendChildElement(top_element, "FarPlatformList")
+  contents = XmlAppendChildElement(top_element, "Contents")
+  XmlAppendChildElement(top_element, "UserExtensions")
 
-  platList = man.createElement("FarPlatformList")
-  top_element.appendChild(platList)
-
-  contents = man.createElement("Contents")
-  top_element.appendChild(contents)
-
-  exts = man.createElement("UserExtensions")
-  top_element.appendChild(exts)
-
-  zip = zipfile.ZipFile(farname, "w")
+  try:
+    zip = zipfile.ZipFile(farname, "w", zipfile.ZIP_DEFLATED)
+  except:
+    zip = zipfile.ZipFile(farname, "w", zipfile.ZIP_STORED)
   for infile in set(files):
     if not os.path.exists(inWorkspace(infile)):
       print "Error: Non-existent file '%s'." % infile
@@ -159,77 +153,34 @@ def makeFar(files, farname):
 
       (spdGuid, spdVersion) = getSpdGuidVersion(infile)
 
-      package = man.createElement("FarPackage")
-      packList.appendChild(package)
-
-      spdfilename = farFileNode(man, inWorkspace(infile))
-      zip.write(inWorkspace(infile), infile)
-      spdfilename.appendChild(man.createTextNode(lean(infile)))
-      package.appendChild(spdfilename)
-
-      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)
+      package = XmlAppendChildElement(packList, "FarPackage")
+      XmlAppendChildElement(package, "FarFilename", lean(infile), {"Md5Sum": Md5(inWorkspace(infile))})
+      AddToZip(zip, infile)
+      XmlAppendChildElement(package, "GuidValue", spdGuid)
+      XmlAppendChildElement(package, "Version", spdVersion)
+      XmlAppendChildElement(package, "DefaultPath", spdDir)
+      XmlAppendChildElement(package, "FarPlatformList")
+      packContents = XmlAppendChildElement(package, "Contents")
+      XmlAppendChildElement(package, "UserExtensions")
 
       for spdfile in filelist:
-        content = farFileNode(man, inWorkspace(os.path.join(spdDir, spdfile))) 
-        zip.write(inWorkspace(os.path.join(spdDir, spdfile)), spdfile)
-        content.appendChild(man.createTextNode(lean(spdfile)))
-        packContents.appendChild(content)
+        XmlAppendChildElement(packContents, "FarFilename", lean(spdfile), {"Md5Sum": Md5(inWorkspace(os.path.join(spdDir, spdfile)))})
+        AddToZip(zip, os.path.join(spdDir,spdfile))
 
     elif extension == ".fpd":
 
-      platform = man.createElement("FarPlatform")
-      platList.appendChild(platform)
-
-      fpdfilename = farFileNode(man, inWorkspace(infile))
-      zip.write(inWorkspace(infile), infile)
-      platform.appendChild(fpdfilename)
-      fpdfilename.appendChild(man.createTextNode(lean(infile)))
+      platform = XmlAppendChildElement(platList, "FarPlatform")
+      XmlAppendChildElement(platform, "FarFilename", lean(infile), {"Md5Sum": Md5(inWorkspace(infile))})
+      AddToZip(zip, infile)
 
     else:
-      content = farFileNode(man, inWorkspace(infile))
-      zip.write(inWorkspace(infile), infile)
-      content.appendChild(man.createTextNode(lean(infile)))
-      contents.appendChild(content)
+      XmlAppendChildElement(contents, "FarFilename", lean(infile), {"Md5Sum": Md5(inWorkspace(infile))})
+      AddToZip(zip, infile)
 
-  zip.writestr("FrameworkArchiveManifest.xml", man.toprettyxml(2*" "))
+  zip.writestr("FrameworkArchiveManifest.xml", man.toxml('UTF-8'))
   zip.close()
   return
 
-def farFileNode(doc, filename):
-
-  """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."""
-
-  content = doc.createElement("FarFilename")
-  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()
-
-  return content
-
 # This acts like the main() function for the script, unless it is 'import'ed
 # into another script.
 if __name__ == '__main__':
@@ -238,7 +189,7 @@ if __name__ == '__main__':
   # pp = pprint.PrettyPrinter(indent=2)
 
   # Process the command line args.
-  optlist, args = getopt.getopt(sys.argv[1:], 'hf:t:', [ 'template=', 'far=', 'help'])
+  optlist, args = getopt.getopt(sys.argv[1:], 'ho:t:v', [ 'template=', 'output=', 'far=', 'help', 'debug', 'verbose', 'version'])
 
   # First pass through the options list.
   for o, a in optlist:
@@ -267,8 +218,7 @@ is a text file that allows more contol over the contents of the far.
 
   # Second pass through the options list. These can override the first pass.
   for o, a in optlist:
-    print o, a
-    if o in ["-f", "--far"]:
+    if o in ["-o", "--far", "--output"]:
       far.FileName = a
 
   # Let's err on the side of caution and not let people blow away data