X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=Tools%2FPython%2FInstallFar.py;h=7bc03aa0ca9ecf1a15ea1bf33668f72584bce08a;hp=c1dc4c10c02dcf73c263e6d0fad4aafa1ef61565;hb=c2b08e15a4711106fd95c13c583d2010ce1722b5;hpb=4040421aeed0f34fe2e039dafc1e1312dfa3e6a9 diff --git a/Tools/Python/InstallFar.py b/Tools/Python/InstallFar.py index c1dc4c10c0..7bc03aa0ca 100755 --- a/Tools/Python/InstallFar.py +++ b/Tools/Python/InstallFar.py @@ -7,11 +7,19 @@ import os, sys, getopt, string, xml.dom.minidom, zipfile, md5 from XmlRoutines import * from WorkspaceRoutines import * -verbose = False -force = False +class Flags: + """Keep track of some command line flags and operating modes.""" + def __init__(self): + self.verbose = False + self.force = False + self.reinstall = False + self.dir = '' class Database: + """This class encapsulates the FrameworkDatabase file for the workspace we + are operating on.""" + def __init__(self, filename="Tools/Conf/FrameworkDatabase.db"): # First try to get a lock file. @@ -35,17 +43,17 @@ class Database: for spdfile in XmlList(self.dom, "/FrameworkDatabase/PackageList/Filename"): filename = str(XmlElementData(spdfile)) spd = XmlParseFileSection(inWorkspace(filename), "SpdHeader") - self.installedPackages[XmlElement(spd, "/SpdHeader/GuidValue"), XmlElement(spd, "/SpdHeader/Version")] = \ + self.installedPackages[GetSpdGuidVersion(spd, 1)] = \ XmlElement(spd, "/SpdHeader/PackageName") for fpdfile in XmlList(self.dom, "/FrameworkDatabase/PlatformList/Filename"): filename = str(XmlElementData(fpdfile)) fpd = XmlParseFileSection(inWorkspace(filename), "PlatformHeader") - self.installedPlatforms[XmlElement(fpd, "/PlatformHeader/GuidValue"), XmlElement(fpd, "/PlatformHeader/Version") ] = \ + self.installedPlatforms[GetFpdGuidVersion(fpd, 1)] = \ XmlElement(fpd, "/PlatformHeader/PlatformName") for farfile in XmlList(self.dom, "/FrameworkDatabase/FarList/Filename"): - farGuid = farfile.getAttribute("FarGuid") + farGuid = Guid(farfile.getAttribute("FarGuid")) self.installedFars[farGuid] = XmlElementData(farfile) self.packageList = XmlNode(self.dom, "/FrameworkDatabase/PackageList") @@ -57,18 +65,22 @@ class Database: self.lock.close() os.unlink(self.lockfile) - def HasPackage(self, spdString): + def HasPackage(self, (guid, version)): """Return true iff this package is already installed.""" - spdHeader = XmlParseStringSection(spdString, "SpdHeader") - guid = XmlElement(spdHeader, "/SpdHeader/GuidValue") - version = XmlElement(spdHeader, "/SpdHeader/Version") + if version == "": + # Look for the guid. + for (g, v) in self.installedPackages.keys(): + if g == guid: + return True return self.installedPackages.has_key((guid, version)) - def HasPlatform(self, fpdString): + def HasPlatform(self, (guid, version)): """Return true iff this platform is already installed.""" - fpdHeader = XmlParseStringSection(fpdString, "PlatformHeader") - guid = XmlElement(fpdHeader, "/PlatformHeader/GuidValue") - version = XmlElement(fpdHeader, "/PlatformHeader/Version") + if version == "": + # Look for the guid. + for (g, v) in self.installedPlatforms.keys(): + if g == guid: + return True return self.installedPlatforms.has_key((guid, version)) def HasFar(self, farguid): @@ -76,22 +88,19 @@ class Database: return self.installedFars.has_key(farguid) def AddPackage(self, f): - filename = self.dom.createElement("Filename") - filename.appendChild(self.dom.createTextNode(f)) - self.packageList.appendChild(filename) + """Put this package in the database""" + XmlAppendChildElement(self.packageList, "Filename", f) def AddPlatform(self, f): - filename = self.dom.createElement("Filename") - filename.appendChild(self.dom.createTextNode(f)) - self.platformList.appendChild(filename) + """Put this platform in the database""" + XmlAppendChildElement(self.platformList, "Filename", f) def AddFar(self, f, guid=""): - filename = self.dom.createElement("Filename") - filename.setAttribute("FarGuid", guid) - filename.appendChild(self.dom.createTextNode(f)) - self.farList.appendChild(filename) + """Put this far in the database""" + XmlAppendChildElement(self.farList, "Filename", f, {"FarGuid":guid} ) def Write(self): + """Save the Xml tree out to the file.""" if True: XmlSaveFile(self.dom, self.DBFile) else: @@ -99,9 +108,9 @@ class Database: f.write(self.dom.toprettyxml(2*" ")) f.close() -def ExtractFile(zip, file, workspaceLocation=""): - - if verbose: +def ExtractFile(zip, file, defaultDir="", workspaceLocation="", md5sum=""): + """Unzip a file.""" + if flags.verbose: print "Extracting ", file destFile = os.path.join(inWorkspace(workspaceLocation), str(file)) @@ -110,11 +119,37 @@ def ExtractFile(zip, file, workspaceLocation=""): mkdir(destDir) f = open(destFile, "w") - f.write(zip.read(file)) + contents = zip.read(os.path.join(defaultDir,file)) + if md5sum and (md5.md5(contents).hexdigest() != md5sum): + print "Error: The md5 sum does not match on file %s." % file + f.write(contents) f.close() +def GetFpdGuidVersion(Dom, strip=0): + + """Get the Guid and version of the fpd from a dom object.""" + + gpath = ["PlatformSurfaceArea", "PlatformHeader", "GuidValue"] + vpath = ["PlatformSurfaceArea", "PlatformHeader", "Version"] + + return Guid(XmlElement(Dom, "/".join(gpath[strip:]))), \ + XmlElement(Dom, "/".join(vpath[strip:])) + +def GetSpdGuidVersion(Dom, strip=0): + + """Get the Guid and version of the spd from a dom object.""" + + gpath = ["PackageSurfaceArea", "SpdHeader", "GuidValue"] + vpath = ["PackageSurfaceArea", "SpdHeader", "Version"] + + return Guid(XmlElement(Dom, "/".join(gpath[strip:]))), \ + XmlElement(Dom, "/".join(vpath[strip:])) + def InstallFar(farfile, workspaceLocation=""): + """Unpack the far an install it in the workspace. We need to adhere to the + rules of far handling.""" + far = zipfile.ZipFile(farfile, "r") # Use this list to make sure we get everything from the far. @@ -126,30 +161,84 @@ def InstallFar(farfile, workspaceLocation=""): # First we need to make sure that the far will install cleanly. + installError = False # Let's hope for the best. + spdDoms = [] + farSpds = [] + # Check the packages for farPackage in XmlList(manifest, "/FrameworkArchiveManifest/FarPackageList/FarPackage/FarFilename"): spdfile = str(XmlElementData(farPackage)) - if fdb.HasPackage(far.read(spdfile)): - print "Error: This package is already installed: ", spdfile - installError = True + spd = XmlParseStringSection(far.read(spdfile), "SpdHeader") + packageGV = GetSpdGuidVersion(spd, 1) + if fdb.HasPackage(packageGV): + if not flags.reinstall: + print "Error: This package is already installed: ", spdfile + installError = True + + # Build up a list of the package guid versions that this far is bringing in. + # This is needed to satisfy dependencies of msas that are in the other packages of + # this far. + farSpds.append(packageGV) + + spdDoms.append((spd, spdfile)) + + for spd, spdfile in spdDoms: + # Now we need to get a list of every msa in this spd and check the package dependencies. + for msafile in XmlList(spd, "/PackageSurfaceArea/MsaFiles/Filename"): + msafilePath = str(os.path.join(os.path.dirname(spdfile), XmlElementData(msafile))) + + msa = XmlParseString(far.read(msafilePath)) + + for package in XmlList(msa, "/ModuleSurfaceArea/PackageDependencies/Package"): + guid = Guid(package.getAttribute("PackageGuid")) + version = package.getAttribute("PackageVersion") + + # Does anyone provide this package? + if not fdb.HasPackage((guid, version)) and not (guid, version) in farSpds: + print ("Error: The module %s depends on the package guid %s version %s, which " + \ + "is not installed in the workspace, nor is it provided by this far.") \ + % (msafilePath, guid, version) + installError = True # Check the platforms for farPlatform in XmlList(manifest, "/FrameworkArchiveManifest/FarPlatformList/FarPlatform/FarFilename"): fpdfile = str(XmlElementData(farPlatform)) - if fdb.HasPlatform(far.read(fpdfile)): - print "Error: This platform is already installed: ", fpdfile - installError = True + fpd = XmlParseString(far.read(fpdfile)) + if fdb.HasPlatform(GetFpdGuidVersion(fpd, 0)): + if not flags.reinstall: + print "Error: This platform is already installed: ", fpdfile + installError = True + + # Now we need to check that all the Platforms (and modules?) that are + # referenced by this fpd are installed in the workspace or are in this far. + packagesNeeded = set() + + # Go through the dependencies + for dependency in XmlList(fpd, "/PlatformSurfaceArea/FrameworkModules/ModuleSA") + \ + XmlList(fpd, "/PlatformSurfaceArea/FrameworkModules/ModuleSA/Libraries/Instance"): + packagesNeeded.add((Guid(dependency.getAttribute("PackageGuid")), + dependency.getAttribute("PackageVersion"))) + + # Let's see if all the packages are in the workspace + for guid, version in packagesNeeded: + # Does anyone provide this package? + if not fdb.HasPackage((guid, version)) and not (guid, version) in farSpds: + print ("Error: The fpd %s depends on the package guid %s version %s, which " + \ + "is not installed in the workspace, nor is it provided by this far.") \ + % (fpdfile, guid, version) + installError = True # Check the fars - thisFarGuid = XmlElement(manifest, "/FrameworkArchiveManifest/FarHeader/GuidValue") + thisFarGuid = Guid(XmlElement(manifest, "/FrameworkArchiveManifest/FarHeader/GuidValue")) if fdb.HasFar(thisFarGuid): - print "Error: There is a far with this guid already installed." - installError = True + if not flags.reinstall: + print "Error: There is a far with this guid already installed." + installError = True # We can not do the install if installError: - if force: - print "Ignoring previous errors as you requested." + if flags.force: + print "Warning: Ignoring previous errors as you requested." else: return False @@ -157,36 +246,41 @@ def InstallFar(farfile, workspaceLocation=""): for farPackage in XmlList(manifest, "/FrameworkArchiveManifest/FarPackageList/FarPackage"): filename = XmlElement(farPackage, "FarPackage/FarFilename") - fdb.AddPackage(filename) + if not flags.reinstall: + fdb.AddPackage(filename) ExtractFile(far, filename, workspaceLocation) zipContents.remove(filename) + DefaultPath = XmlElement(farPackage, "FarPackage/DefaultPath") + for content in XmlList(farPackage, "FarPackage/Contents/FarFilename"): filename = XmlElementData(content) - ExtractFile(far, filename, workspaceLocation) - zipContents.remove(filename) + ExtractFile(far, filename, DefaultPath, workspaceLocation, md5sum=content.getAttribute("Md5Sum")) + zipContents.remove(os.path.join(DefaultPath, filename)) # Install the platforms for farPlatform in XmlList(manifest, "/FrameworkArchiveManifest/FarPlatformList/FarPlatform"): filename = XmlElement(farPlatform, "FarPlatform/FarFilename") - fdb.AddPlatform(filename) - ExtractFile(far, filename, workspaceLocation) + if not flags.reinstall: + fdb.AddPlatform(filename) + ExtractFile(far, filename, "", workspaceLocation) zipContents.remove(filename) # Install the Contents for content in XmlList(manifest, "/FrameworkArchiveManifest/Contents/FarFilename"): filename = XmlElementData(content) - ExtractFile(far, filename, workspaceLocation) + ExtractFile(far, filename, "", workspaceLocation) zipContents.remove(filename) # What if there are more files in the far? if not zipContents == []: - print "There are still files in the far:", zipContents + print "Warning: There are files in the far that were not expected: ", zipContents - fdb.AddFar(farfile, thisFarGuid) + if not flags.reinstall: + fdb.AddFar(farfile, thisFarGuid) # If everything has gone well, we can put the manifest file in a safe place... farDir = inWorkspace("Tools/Conf/InstalledFars/") @@ -196,7 +290,8 @@ def InstallFar(farfile, workspaceLocation=""): f.close() # Write out the new database - fdb.Write() + if not flags.reinstall: + fdb.Write() far.close() @@ -204,22 +299,28 @@ def InstallFar(farfile, workspaceLocation=""): # into another script. if __name__ == '__main__': + flags = Flags() + # Process the command line args. - optlist, args = getopt.getopt(sys.argv[1:], '?hvf', ['help', 'verbose', 'force']) + optlist, args = getopt.getopt(sys.argv[1:], '?hvfd:', ['directory=', 'help', 'verbose', 'force', 'reinstall']) # First pass through the options list. for o, a in optlist: - if o in ["-h", "--help"]: + if o in ["-h", "-?", "--help"]: print """ -Install a far (Framework Archive) into the current workspace. +%s: Install a far (Framework Archive) into the current workspace. """ % os.path.basename(sys.argv[0]) sys.exit() optlist.remove((o,a)) if o in ["-v", "--verbose"]: - verbose = True + flags.verbose = True + if o in ["-d", "--directory"]: + flags.dir = a if o in ["-f", "--force"]: - force = True + flags.force = True + if o in ["--reinstall"]: + flags.reinstall = True for f in args: InstallFar(f)