summaryrefslogtreecommitdiff
path: root/Tools/Python/MkFar.py
blob: 50102ce27ea824616427be000b6bd52cca83e0a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env python

import os, sys, re, getopt, string, glob, xml.dom.minidom, pprint, zipfile, tempfile
from XmlRoutines import *
from WorkspaceRoutines import *

def parseMsa(msaFile, spdDir):

  filelist = [msaFile]

  msaDir = os.path.dirname(msaFile)

  msa = xml.dom.minidom.parse(inWorkspace(msaFile))

  xmlPaths = [
    "/ModuleSurfaceArea/SourceFiles/Filename" ]

  for xmlPath in xmlPaths:
    for f in XmlList(msa, xmlPath):
      filelist.append(str(os.path.join(msaDir, XmlElementData(f))))

  return filelist

def parseSpd(spdFile):

  filelist = []

  spdDir = os.path.dirname(spdFile)

  spd = xml.dom.minidom.parse(inWorkspace(spdFile))

  xmlPaths = [
    "/PackageSurfaceArea/LibraryClassDeclarations/LibraryClass/IncludeHeader",
    "/PackageSurfaceArea/IndustryStdIncludes/IndustryStdHeader/IncludeHeader",
    "/PackageSurfaceArea/PackageHeaders/IncludePkgHeader" ]

  for xmlPath in xmlPaths:
    for f in XmlList(spd, xmlPath):
      filelist.append(str(os.path.join(spdDir, XmlElementData(f))))

  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):

  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 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(infile)

      package = man.createElement("FarPackage")
      packList.appendChild(package)

      spdfilename = man.createElement("FarFilename")
      package.appendChild(spdfilename)

      spdfilename.appendChild( man.createTextNode(infile) )
      zip.write(inWorkspace(infile), infile)

      for spdfile in filelist:
        content = man.createElement("FarFilename")
        content.appendChild( man.createTextNode(spdfile))
        contents.appendChild(content)
        zip.write(inWorkspace(spdfile), spdfile)

    elif extension == ".fpd":

      platform = man.createElement("FarPlatform")
      platList.appendChild(platform)

      fpdfilename = man.createElement("FarFilename")
      platform.appendChild(fpdfilename)

      fpdfilename.appendChild( man.createTextNode(infile) )
      zip.write(inWorkspace(infile), infile)

    else:
      print "Skipping file '%s' since is is not a .spd or .fpd." % infile

  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.
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'])

  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, farName)