]> git.proxmox.com Git - mirror_edk2.git/blobdiff - Tools/Python/MkFar.py
1. Fix EDKT413: EnumerationData.java should use defined final static string
[mirror_edk2.git] / Tools / Python / MkFar.py
index ffe27a870aa7483bf8de06c4c0605e59cbc2c8a2..327c81a94a28e3033128fbb91ab7f5ed5ce64f43 100755 (executable)
@@ -4,6 +4,23 @@ import os, sys, getopt, string, xml.dom.minidom, zipfile, md5
 from XmlRoutines import *
 from WorkspaceRoutines import *
 
+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=""
+    far.Abstract=""
+    far.Description=""
+    far.Copyright=""
+    far.SpdFiles=[]
+    far.FpdFiles=[]
+    far.ExtraFiles=[]
+
+far = Far()
+
 def parseMsa(msaFile, spdDir):
 
   filelist = [msaFile]
@@ -16,7 +33,6 @@ def parseMsa(msaFile, spdDir):
     "/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))))
@@ -47,7 +63,8 @@ def parseSpd(spdFile):
   cwd = os.getcwd()
   os.chdir(inWorkspace(spdDir))
   for root, dirs, entries in os.walk("Include"):
-    for r  in ["CVS", ".svn"]:
+    # Some files need to be skipped.
+    for r in ["CVS", ".svn"]:
       if r in dirs:
         dirs.remove(r)
     for entry in entries:
@@ -60,25 +77,25 @@ def makeFarHeader(doc):
 
   header = doc.createElement("FarHeader")
   name = doc.createElement("FarName")
-  name.appendChild(doc.createTextNode("My New Far"))
+  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("1.0"))
+  ver.appendChild(doc.createTextNode(far.Version))
   header.appendChild(ver)
   abstract = doc.createElement("Abstract")
-  abstract.appendChild(doc.createTextNode("This is a cool new far."))
+  abstract.appendChild(doc.createTextNode(far.Abstract))
   header.appendChild(abstract)
   desc = doc.createElement("Description")
-  desc.appendChild(doc.createTextNode("This is a cool new far. It can do great things."))
+  desc.appendChild(doc.createTextNode(far.Description))
   header.appendChild(desc)
   copy = doc.createElement("Copyright")
-  copy.appendChild(doc.createTextNode("Copyright (c) Intel Corporation 2006."))
+  copy.appendChild(doc.createTextNode(far.Copyright))
   header.appendChild(copy)
   lic = doc.createElement("License")
-  lic.appendChild(doc.createTextNode("BSD Compatible."))
+  lic.appendChild(doc.createTextNode(far.License))
   header.appendChild(lic)
   spec = doc.createElement("Specification")
   spec.appendChild(doc.createTextNode("FRAMEWORK_BUILD_PACKAGING_SPECIFICATION 0x00000052"))
@@ -93,7 +110,7 @@ def getSpdGuidVersion(spdFile):
   return (XmlElement(spd, "/PackageSurfaceArea/SpdHeader/GuidValue"),
           XmlElement(spd, "/PackageSurfaceArea/SpdHeader/Version"))
 
-def makeFar(filelist, farname):
+def makeFar(files, farname):
 
   domImpl = xml.dom.minidom.getDOMImplementation()
   man = domImpl.createDocument(None, "FrameworkArchiveManifest", None)
@@ -114,7 +131,7 @@ def makeFar(filelist, farname):
   top_element.appendChild(exts)
 
   zip = zipfile.ZipFile(farname, "w")
-  for infile in filelist:
+  for infile in set(files):
     if not os.path.exists(inWorkspace(infile)):
       print "Skipping non-existent file '%s'." % infile
     (_, extension) = os.path.splitext(infile)
@@ -129,7 +146,7 @@ def makeFar(filelist, farname):
 
       spdfilename = farFileNode(man, inWorkspace(infile))
       zip.write(inWorkspace(infile), infile)
-      spdfilename.appendChild(man.createTextNode(infile))
+      spdfilename.appendChild(man.createTextNode(lean(infile)))
       package.appendChild(spdfilename)
 
       guidValue = man.createElement("GuidValue")
@@ -156,7 +173,7 @@ def makeFar(filelist, farname):
       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(spdfile))
+        content.appendChild(man.createTextNode(lean(spdfile)))
         packContents.appendChild(content)
 
     elif extension == ".fpd":
@@ -167,12 +184,12 @@ def makeFar(filelist, farname):
       fpdfilename = farFileNode(man, inWorkspace(infile))
       zip.write(inWorkspace(infile), infile)
       platform.appendChild(fpdfilename)
-      fpdfilename.appendChild( man.createTextNode(infile) )
+      fpdfilename.appendChild(man.createTextNode(lean(infile)))
 
     else:
       content = farFileNode(man, inWorkspace(infile))
       zip.write(inWorkspace(infile), infile)
-      content.appendChild(man.createTextNode(infile))
+      content.appendChild(man.createTextNode(lean(infile)))
       contents.appendChild(content)
 
   zip.writestr("FrameworkArchiveManifest.xml", man.toprettyxml(2*" "))
@@ -193,12 +210,10 @@ 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:], 'hf:', [ 'far=', 'help'])
+  optlist, args = getopt.getopt(sys.argv[1:], 'hf:t:', [ 'template=', 'far=', 'help'])
 
+  # First pass through the options list.
   for o, a in optlist:
     if o in ["-h", "--help"]:
       print """
@@ -207,12 +222,29 @@ 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.
+The file paths of .spd and .fpd are treated as relative to the WORKSPACE
+environment variable which must be set to a valid workspace root directory.
 """ % os.path.basename(sys.argv[0])
 
       sys.exit()
+      optlist.remove((o,a))
+    if o in ["-t", "--template"]:
+      # The template file is processed first, so that command line options can
+      # override it.
+      templateName = a
+      execfile(templateName)
+      optlist.remove((o,a))
+
+  # 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"]:
-      farName = a
+      far.FileName = a
+
+  # 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(args, farName)
+  makeFar(far.SpdFiles + far.FpdFiles + far.ExtraFiles + args, far.FileName)