]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Python/InstallFar.py
Adding several dependency checks for far installation. Redoing the XML output.
[mirror_edk2.git] / Tools / Python / InstallFar.py
1 #!/usr/bin/env python
2
3 """This is a python script that takes user input from the command line and
4 installs a far (Framework Archive Manifest) file into the workspace."""
5
6 import os, sys, getopt, string, xml.dom.minidom, zipfile, md5
7 from XmlRoutines import *
8 from WorkspaceRoutines import *
9
10 class Flags:
11 """Keep track of some command line flags and operating modes."""
12 def __init__(self):
13 self.verbose = False
14 self.force = False
15 self.reinstall = False
16
17 class Database:
18
19 """This class encapsulates the FrameworkDatabase file for the workspace we
20 are operating on."""
21
22 def __init__(self, filename="Tools/Conf/FrameworkDatabase.db"):
23
24 # First try to get a lock file.
25 self.DBFile = inWorkspace(filename)
26 self.lockfile = inWorkspace("Tools/Conf/FrameworkDatabase.lock")
27 if os.path.exists(self.lockfile):
28 self.itsMyLockFile = False
29 print "Error: The database file is locked by ", self.lockfile
30 raise OSError("The Database is locked.")
31 else:
32 self.lock = open(self.lockfile, 'w')
33 self.lock.write("pid "+str(os.getpid()))
34 self.itsMyLockFile = True
35
36 self.dom = XmlParseFile(inWorkspace(filename))
37
38 self.installedPackages = {}
39 self.installedPlatforms = {}
40 self.installedFars = {}
41
42 for spdfile in XmlList(self.dom, "/FrameworkDatabase/PackageList/Filename"):
43 filename = str(XmlElementData(spdfile))
44 spd = XmlParseFileSection(inWorkspace(filename), "SpdHeader")
45 self.installedPackages[GetSpdGuidVersion(spd, 1)] = \
46 XmlElement(spd, "/SpdHeader/PackageName")
47
48 for fpdfile in XmlList(self.dom, "/FrameworkDatabase/PlatformList/Filename"):
49 filename = str(XmlElementData(fpdfile))
50 fpd = XmlParseFileSection(inWorkspace(filename), "PlatformHeader")
51 self.installedPlatforms[GetFpdGuidVersion(fpd, 1)] = \
52 XmlElement(fpd, "/PlatformHeader/PlatformName")
53
54 for farfile in XmlList(self.dom, "/FrameworkDatabase/FarList/Filename"):
55 farGuid = farfile.getAttribute("FarGuid")
56 self.installedFars[farGuid] = XmlElementData(farfile)
57
58 self.packageList = XmlNode(self.dom, "/FrameworkDatabase/PackageList")
59 self.platformList = XmlNode(self.dom, "/FrameworkDatabase/PlatformList")
60 self.farList = XmlNode(self.dom, "/FrameworkDatabase/FarList")
61
62 def __del__(self):
63 if self.itsMyLockFile:
64 self.lock.close()
65 os.unlink(self.lockfile)
66
67 def HasPackage(self, (guid, version)):
68 """Return true iff this package is already installed."""
69 if version == "":
70 # Look for the guid.
71 for (g, v) in self.installedPackages.keys():
72 if g == guid:
73 return True
74 return self.installedPackages.has_key((guid, version))
75
76 def HasPlatform(self, (guid, version)):
77 """Return true iff this platform is already installed."""
78 if version == "":
79 # Look for the guid.
80 for (g, v) in self.installedPlatforms.keys():
81 if g == guid:
82 return True
83 return self.installedPlatforms.has_key((guid, version))
84
85 def HasFar(self, farguid):
86 """Return true iff this far is already installed."""
87 return self.installedFars.has_key(farguid)
88
89 def AddPackage(self, f):
90 """Put this package in the database"""
91 XmlAppendChildElement(self.packageList, "Filename", f)
92
93 def AddPlatform(self, f):
94 """Put this platform in the database"""
95 XmlAppendChildElement(self.platformList, "Filename", f)
96
97 def AddFar(self, f, guid=""):
98 """Put this far in the database"""
99 XmlAppendChildElement(self.farList, "Filename", f, {"FarGuid":guid} )
100
101 def Write(self):
102 """Save the Xml tree out to the file."""
103 if True:
104 XmlSaveFile(self.dom, self.DBFile)
105 else:
106 f=open(self.DBFile, 'w')
107 f.write(self.dom.toprettyxml(2*" "))
108 f.close()
109
110 def ExtractFile(zip, file, defaultDir="", workspaceLocation="", md5sum=""):
111 """Unzip a file."""
112 if flags.verbose:
113 print "Extracting ", file
114
115 destFile = os.path.join(inWorkspace(workspaceLocation), str(file))
116 destDir = os.path.dirname(destFile)
117
118 mkdir(destDir)
119
120 f = open(destFile, "w")
121 contents = zip.read(os.path.join(defaultDir,file))
122 if md5sum and (md5.md5(contents).hexdigest() != md5sum):
123 print "Error: The md5 sum does not match on file %s." % file
124 f.write(contents)
125 f.close()
126
127 def GetFpdGuidVersion(Dom, strip=0):
128
129 """Get the Guid and version of the fpd from a dom object."""
130
131 gpath = ["PlatformSurfaceArea", "PlatformHeader", "GuidValue"]
132 vpath = ["PlatformSurfaceArea", "PlatformHeader", "Version"]
133
134 return string.lower(XmlElement(Dom, "/".join(gpath[strip:]))), \
135 XmlElement(Dom, "/".join(vpath[strip:]))
136
137 def GetSpdGuidVersion(Dom, strip=0):
138
139 """Get the Guid and version of the spd from a dom object."""
140
141 gpath = ["PackageSurfaceArea", "SpdHeader", "GuidValue"]
142 vpath = ["PackageSurfaceArea", "SpdHeader", "Version"]
143
144 return string.lower(XmlElement(Dom, "/".join(gpath[strip:]))), \
145 XmlElement(Dom, "/".join(vpath[strip:]))
146
147 def InstallFar(farfile, workspaceLocation=""):
148
149 """Unpack the far an install it in the workspace. We need to adhere to the
150 rules of far handling."""
151
152 far = zipfile.ZipFile(farfile, "r")
153
154 # Use this list to make sure we get everything from the far.
155 zipContents = far.namelist()
156
157 manifest = xml.dom.minidom.parseString(far.read("FrameworkArchiveManifest.xml"))
158 zipContents.remove("FrameworkArchiveManifest.xml")
159 fdb = Database()
160
161 # First we need to make sure that the far will install cleanly.
162
163 installError = False # Let's hope for the best.
164 spdDoms = []
165 farSpds = []
166
167 # Check the packages
168 for farPackage in XmlList(manifest, "/FrameworkArchiveManifest/FarPackageList/FarPackage/FarFilename"):
169 spdfile = str(XmlElementData(farPackage))
170 spd = XmlParseStringSection(far.read(spdfile), "SpdHeader")
171 packageGV = GetSpdGuidVersion(spd, 1)
172 if fdb.HasPackage(packageGV):
173 if not flags.reinstall:
174 print "Error: This package is already installed: ", spdfile
175 installError = True
176
177 # Build up a list of the package guid versions that this far is bringing in.
178 # This is needed to satisfy dependencies of msas that are in the other packages of
179 # this far.
180 farSpds.append(packageGV)
181
182 spdDoms.append((spd, spdfile))
183
184 for spd, spdfile in spdDoms:
185 # Now we need to get a list of every msa in this spd and check the package dependencies.
186 for msafile in XmlList(spd, "/PackageSurfaceArea/MsaFiles/Filename"):
187 msafilePath = str(os.path.join(os.path.dirname(spdfile), XmlElementData(msafile)))
188
189 msa = XmlParseString(far.read(msafilePath))
190
191 for package in XmlList(msa, "/ModuleSurfaceArea/PackageDependencies/Package"):
192 guid = package.getAttribute("PackageGuid")
193 version = package.getAttribute("PackageVersion")
194
195 # Does anyone provide this package?
196 if not fdb.HasPackage((guid, version)) and not (guid, version) in farSpds:
197 print ("Error: The module %s depends on the package guid %s version %s, which " + \
198 "is not installed in the workspace, nor is it provided by this far.") \
199 % (msafilePath, guid, version)
200 installError = True
201
202 # Check the platforms
203 for farPlatform in XmlList(manifest, "/FrameworkArchiveManifest/FarPlatformList/FarPlatform/FarFilename"):
204 fpdfile = str(XmlElementData(farPlatform))
205 fpd = XmlParseString(far.read(fpdfile))
206 if fdb.HasPlatform(GetFpdGuidVersion(fpd, 0)):
207 if not flags.reinstall:
208 print "Error: This platform is already installed: ", fpdfile
209 installError = True
210
211 # Now we need to check that all the Platforms (and modules?) that are
212 # referenced by this fpd are installed in the workspace or are in this far.
213 packagesNeeded = set()
214
215 # Go through the dependencies
216 for dependency in XmlList(fpd, "/PlatformSurfaceArea/FrameworkModules/ModuleSA") + \
217 XmlList(fpd, "/PlatformSurfaceArea/FrameworkModules/ModuleSA/Libraries/Instance"):
218 packagesNeeded.add((string.lower(dependency.getAttribute("PackageGuid")),
219 dependency.getAttribute("PackageVersion")))
220
221 # Let's see if all the packages are in the workspace
222 for guid, version in packagesNeeded:
223 # Does anyone provide this package?
224 if not fdb.HasPackage((guid, version)) and not (guid, version) in farSpds:
225 print ("Error: The fpd %s depends on the package guid %s version %s, which " + \
226 "is not installed in the workspace, nor is it provided by this far.") \
227 % (fpdfile, guid, version)
228 installError = True
229
230 # Check the fars
231 thisFarGuid = string.lower(XmlElement(manifest, "/FrameworkArchiveManifest/FarHeader/GuidValue"))
232 if fdb.HasFar(thisFarGuid):
233 if not flags.reinstall:
234 print "Error: There is a far with this guid already installed."
235 installError = True
236
237 # We can not do the install
238 if installError:
239 if flags.force:
240 print "Warning: Ignoring previous errors as you requested."
241 else:
242 return False
243
244 # Install the packages
245 for farPackage in XmlList(manifest, "/FrameworkArchiveManifest/FarPackageList/FarPackage"):
246
247 filename = XmlElement(farPackage, "FarPackage/FarFilename")
248 if not flags.reinstall:
249 fdb.AddPackage(filename)
250 ExtractFile(far, filename, workspaceLocation)
251 zipContents.remove(filename)
252
253 DefaultPath = XmlElement(farPackage, "FarPackage/DefaultPath")
254
255 for content in XmlList(farPackage, "FarPackage/Contents/FarFilename"):
256
257 filename = XmlElementData(content)
258 ExtractFile(far, filename, DefaultPath, workspaceLocation, md5sum=content.getAttribute("Md5Sum"))
259 zipContents.remove(os.path.join(DefaultPath, filename))
260
261 # Install the platforms
262 for farPlatform in XmlList(manifest, "/FrameworkArchiveManifest/FarPlatformList/FarPlatform"):
263
264 filename = XmlElement(farPlatform, "FarPlatform/FarFilename")
265 if not flags.reinstall:
266 fdb.AddPlatform(filename)
267 ExtractFile(far, filename, "", workspaceLocation)
268 zipContents.remove(filename)
269
270 # Install the Contents
271 for content in XmlList(manifest, "/FrameworkArchiveManifest/Contents/FarFilename"):
272
273 filename = XmlElementData(content)
274 ExtractFile(far, filename, "", workspaceLocation)
275 zipContents.remove(filename)
276
277 # What if there are more files in the far?
278 if not zipContents == []:
279 print "Warning: There are files in the far that were not expected: ", zipContents
280
281 if not flags.reinstall:
282 fdb.AddFar(farfile, thisFarGuid)
283
284 # If everything has gone well, we can put the manifest file in a safe place...
285 farDir = inWorkspace("Tools/Conf/InstalledFars/")
286 mkdir(farDir)
287 f=open(os.path.join(farDir, thisFarGuid), 'w')
288 f.write(far.read("FrameworkArchiveManifest.xml"))
289 f.close()
290
291 # Write out the new database
292 if not flags.reinstall:
293 fdb.Write()
294
295 far.close()
296
297 # This acts like the main() function for the script, unless it is 'import'ed
298 # into another script.
299 if __name__ == '__main__':
300
301 flags = Flags()
302
303 # Process the command line args.
304 optlist, args = getopt.getopt(sys.argv[1:], '?hvf', ['help', 'verbose', 'force', 'reinstall'])
305
306 # First pass through the options list.
307 for o, a in optlist:
308 if o in ["-h", "-?", "--help"]:
309 print """
310 %s: Install a far (Framework Archive) into the current workspace.
311 """ % os.path.basename(sys.argv[0])
312
313 sys.exit()
314 optlist.remove((o,a))
315 if o in ["-v", "--verbose"]:
316 flags.verbose = True
317 if o in ["-f", "--force"]:
318 flags.force = True
319 if o in ["--reinstall"]:
320 flags.reinstall = True
321
322 for f in args:
323 InstallFar(f)
324 if args == []:
325 print "Please pass a far filename on the command line."