]> git.proxmox.com Git - mirror_edk2.git/blobdiff - Tools/Python/MkFar.py
Corrected the regular expression because it will skip many includes.
[mirror_edk2.git] / Tools / Python / MkFar.py
index 7531527f582bfc522110fee95b5dd77f88f73abe..bdecd2137cf34b2ddcd2963b14d4a8ba84dc8cd0 100755 (executable)
@@ -1,5 +1,8 @@
 #!/usr/bin/env python
 
+"""This is a python script that takes user input from the command line and
+creates a far (Framework Archive Manifest) file for distribution."""
+
 import os, sys, getopt, string, xml.dom.minidom, zipfile, md5
 from XmlRoutines import *
 from WorkspaceRoutines import *
@@ -12,16 +15,21 @@ class Far:
     far.FarName=""
     far.Version=""
     far.License=""
+    far.Abstract=""
     far.Description=""
     far.Copyright=""
-    far.SpdFiles=""
-    far.FpdFile=""
-    far.ExtraFile=""
+    far.SpdFiles=[]
+    far.FpdFiles=[]
+    far.ExtraFiles=[]
 
 far = Far()
+"""The far object is constructed from the template file the user passed in."""
 
 def parseMsa(msaFile, spdDir):
 
+  """ XXX Parse an msa file and return a list of all the files that this msa
+  includes."""
+
   filelist = [msaFile]
 
   msaDir = os.path.dirname(msaFile)
@@ -40,6 +48,9 @@ def parseMsa(msaFile, spdDir):
 
 def parseSpd(spdFile):
 
+  """Parse an spd file and return a list of all the files that this spd
+  includes."""
+
   files = []
 
   spdDir = os.path.dirname(spdFile)
@@ -62,7 +73,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:
@@ -73,6 +85,9 @@ def parseSpd(spdFile):
 
 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))
@@ -103,12 +118,16 @@ def makeFarHeader(doc):
 
 def getSpdGuidVersion(spdFile):
 
+  """Returns a tuple (guid, version) which is read from the given spdFile."""
+
   spd = xml.dom.minidom.parse(inWorkspace(spdFile))
 
   return (XmlElement(spd, "/PackageSurfaceArea/SpdHeader/GuidValue"),
           XmlElement(spd, "/PackageSurfaceArea/SpdHeader/Version"))
 
-def makeFar(filelist, farname):
+def makeFar(files, farname):
+
+  """Make a far out of the given filelist and writes it to the file farname."""
 
   domImpl = xml.dom.minidom.getDOMImplementation()
   man = domImpl.createDocument(None, "FrameworkArchiveManifest", None)
@@ -129,9 +148,10 @@ 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
+      print "Error: Non-existent file '%s'." % infile
+      sys.exit()
     (_, extension) = os.path.splitext(infile)
     if extension == ".spd":
       filelist = parseSpd(infile)
@@ -144,7 +164,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")
@@ -171,7 +191,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":
@@ -182,12 +202,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*" "))
@@ -195,10 +215,19 @@ def makeFar(filelist, farname):
   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")
-  f=open(filename, "rb")
-  content.setAttribute("Md5sum", md5.md5(f.read()).hexdigest())
-  f.close()
+  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
@@ -211,28 +240,41 @@ if __name__ == '__main__':
   # Process the command line args.
   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 """
 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
+  %s --template far-template --far library.far MdePkg/MdePkg.spd
 
 The file paths of .spd and .fpd are treated as relative to the WORKSPACE
-envirnonment variable which must be set to a valid workspace root directory.
+environment variable which must be set to a valid workspace root directory.
+
+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.
 """ % 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"]:
       far.FileName = a
-      if os.path.exists(far.FileName):
-        print "Error: File %s exists. Not overwriting." % far.FileName
-        sys.exit()
 
-  makeFar(args, far.FileName)
+  # 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)