]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/PackagingTool/InstallPkg.py
Check In tool source code based on Build tool project revision r1655.
[mirror_edk2.git] / BaseTools / Source / Python / PackagingTool / InstallPkg.py
diff --git a/BaseTools/Source/Python/PackagingTool/InstallPkg.py b/BaseTools/Source/Python/PackagingTool/InstallPkg.py
new file mode 100644 (file)
index 0000000..963a654
--- /dev/null
@@ -0,0 +1,309 @@
+## @file\r
+# Install distribution package.\r
+#\r
+# Copyright (c) 2007, Intel Corporation\r
+# All rights reserved. This program and the accompanying materials\r
+# are licensed and made available under the terms and conditions of the BSD License\r
+# which accompanies this distribution.  The full text of the license may be found at\r
+# http://opensource.org/licenses/bsd-license.php\r
+#\r
+# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+#\r
+\r
+##\r
+# Import Modules\r
+#\r
+import os\r
+import sys\r
+import glob\r
+import shutil\r
+import traceback\r
+import platform\r
+from optparse import OptionParser\r
+\r
+import Common.EdkLogger as EdkLogger\r
+from Common.BuildToolError import *\r
+from Common.Misc import *\r
+from Common.XmlParser import *\r
+from Common.InfClassObjectLight import Inf\r
+from Common.DecClassObjectLight import Dec\r
+\r
+from PackageFile import *\r
+from IpiDb import *\r
+from DependencyRules import *\r
+import md5\r
+\r
+# Version and Copyright\r
+VersionNumber = "0.1"\r
+__version__ = "%prog Version " + VersionNumber\r
+__copyright__ = "Copyright (c) 2008, Intel Corporation  All rights reserved."\r
+\r
+## Check environment variables\r
+#\r
+#  Check environment variables that must be set for build. Currently they are\r
+#\r
+#   WORKSPACE           The directory all packages/platforms start from\r
+#   EDK_TOOLS_PATH      The directory contains all tools needed by the build\r
+#   PATH                $(EDK_TOOLS_PATH)/Bin/<sys> must be set in PATH\r
+#\r
+#   If any of above environment variable is not set or has error, the build\r
+#   will be broken.\r
+#\r
+def CheckEnvVariable():\r
+    # check WORKSPACE\r
+    if "WORKSPACE" not in os.environ:\r
+        EdkLogger.error("InstallPkg", ATTRIBUTE_NOT_AVAILABLE, "Environment variable not found",\r
+                        ExtraData="WORKSPACE")\r
+\r
+    WorkspaceDir = os.path.normpath(os.environ["WORKSPACE"])\r
+    if not os.path.exists(WorkspaceDir):\r
+        EdkLogger.error("InstallPkg", FILE_NOT_FOUND, "WORKSPACE doesn't exist", ExtraData="%s" % WorkspaceDir)\r
+    elif ' ' in WorkspaceDir:\r
+        EdkLogger.error("InstallPkg", FORMAT_NOT_SUPPORTED, "No space is allowed in WORKSPACE path", \r
+                        ExtraData=WorkspaceDir)\r
+    os.environ["WORKSPACE"] = WorkspaceDir\r
+\r
+## Parse command line options\r
+#\r
+# Using standard Python module optparse to parse command line option of this tool.\r
+#\r
+#   @retval Opt   A optparse.Values object containing the parsed options\r
+#   @retval Args  Target of build command\r
+#\r
+def MyOptionParser():\r
+    UsageString = "%prog -i <distribution_package> [-t] [-f] [-q | -v] [-h]"\r
+\r
+    Parser = OptionParser(description=__copyright__,version=__version__,prog="InstallPkg",usage=UsageString)\r
+\r
+    Parser.add_option("-?", action="help", help="show this help message and exit")\r
+\r
+    Parser.add_option("-i", "--distribution-package", action="store", type="string", dest="PackageFile",\r
+            help="The distribution package to be installed")\r
+\r
+    Parser.add_option("-t", "--install-tools", action="store_true", type=None, dest="Tools",\r
+            help="Specify it to install tools or ignore the tools of the distribution package.")\r
+    \r
+    Parser.add_option("-f", "--misc-files", action="store_true", type=None, dest="MiscFiles",\r
+            help="Specify it to install misc file or ignore the misc files of the distribution package.")\r
+\r
+    Parser.add_option("-q", "--quiet", action="store_const", dest="LogLevel", const=EdkLogger.QUIET,\r
+            help="Disable all messages except FATAL ERRORS.")\r
+\r
+    Parser.add_option("-v", "--verbose", action="store_const", dest="LogLevel", const=EdkLogger.VERBOSE,\r
+            help="Turn on verbose output")\r
+\r
+    Parser.add_option("-d", "--debug", action="store", type="int", dest="LogLevel",\r
+            help="Enable debug messages at specified level.")\r
+\r
+    Parser.set_defaults(LogLevel=EdkLogger.INFO)\r
+\r
+    (Opt, Args)=Parser.parse_args()\r
+\r
+    return Opt\r
+\r
+def InstallNewPackage(WorkspaceDir, Path):\r
+    FullPath = os.path.normpath(os.path.join(WorkspaceDir, Path))\r
+    if os.path.exists(FullPath):\r
+        print "Directory [%s] already exists, please select another location, press [Enter] with no input to quit:" %Path\r
+        Input = sys.stdin.readline()\r
+        Input = Input.replace('\r', '').replace('\n', '')\r
+        if Input == '':\r
+            EdkLogger.error("InstallPkg", UNKNOWN_ERROR, "User interrupt")\r
+        Input = Input.replace('\r', '').replace('\n', '')\r
+        return InstallNewPackage(WorkspaceDir, Input)\r
+    else:\r
+        return Path\r
+\r
+def InstallNewFile(WorkspaceDir, File):\r
+    FullPath = os.path.normpath(os.path.join(WorkspaceDir, File))\r
+    if os.path.exists(FullPath):\r
+        print "File [%s] already exists, please select another path, press [Enter] with no input to quit:" %File\r
+        Input = sys.stdin.readline()\r
+        Input = Input.replace('\r', '').replace('\n', '')\r
+        if Input == '':\r
+            EdkLogger.error("InstallPkg", UNKNOWN_ERROR, "User interrupt")\r
+        Input = Input.replace('\r', '').replace('\n', '')\r
+        return InstallNewFile(WorkspaceDir, Input)\r
+    else:\r
+        return File\r
+\r
+## Tool entrance method\r
+#\r
+# This method mainly dispatch specific methods per the command line options.\r
+# If no error found, return zero value so the caller of this tool can know\r
+# if it's executed successfully or not.\r
+#\r
+#   @retval 0     Tool was successful\r
+#   @retval 1     Tool failed\r
+#\r
+def Main():\r
+    EdkLogger.Initialize()\r
+    Options = None\r
+    DistFileName = 'dist.pkg'\r
+    ContentFileName = 'content.zip'\r
+    DistFile, ContentZipFile, UnpackDir = None, None, None\r
+    \r
+    Options = MyOptionParser()\r
+    try:\r
+        if Options.LogLevel < EdkLogger.DEBUG_9:\r
+            EdkLogger.SetLevel(Options.LogLevel + 1)\r
+        else:\r
+            EdkLogger.SetLevel(Options.LogLevel)\r
+\r
+        CheckEnvVariable()\r
+        WorkspaceDir = os.environ["WORKSPACE"]\r
+        if not Options.PackageFile:\r
+            EdkLogger.error("InstallPkg", OPTION_NOT_SUPPORTED, ExtraData="Must specify one distribution package")\r
+\r
+        # unzip dist.pkg file\r
+        EdkLogger.quiet("Unzipping and parsing distribution package XML file ... ")\r
+        DistFile = PackageFile(Options.PackageFile)\r
+        UnpackDir = os.path.normpath(os.path.join(WorkspaceDir, ".tmp"))\r
+        DistPkgFile = DistFile.UnpackFile(DistFileName, os.path.normpath(os.path.join(UnpackDir, DistFileName)))\r
+        if not DistPkgFile:\r
+            EdkLogger.error("InstallPkg", FILE_NOT_FOUND, "File [%s] is broken in distribution package" %DistFileName)\r
+        \r
+        # Generate distpkg\r
+        DistPkgObj = DistributionPackageXml()\r
+        DistPkg = DistPkgObj.FromXml(DistPkgFile)\r
+\r
+        # prepare check dependency\r
+        Db = IpiDatabase(os.path.normpath(os.path.join(WorkspaceDir, "Conf/DistributionPackageDatabase.db")))\r
+        Db.InitDatabase()\r
+        Dep = DependencyRules(Db)\r
+        \r
+        # Check distribution package exist\r
+        if Dep.CheckDpExists(DistPkg.Header.Guid, DistPkg.Header.Version):\r
+            EdkLogger.error("InstallPkg", UNKNOWN_ERROR, "This distribution package has been installed", ExtraData=DistPkg.Header.Name)\r
+        \r
+        # unzip contents.zip file\r
+        ContentFile = DistFile.UnpackFile(ContentFileName, os.path.normpath(os.path.join(UnpackDir, ContentFileName)))\r
+        ContentZipFile = PackageFile(ContentFile)\r
+        if not ContentFile:\r
+            EdkLogger.error("InstallPkg", FILE_NOT_FOUND, "File [%s] is broken in distribution package" %ContentFileName)\r
+        \r
+        # verify MD5 signature\r
+        Md5Sigature = md5.new(open(ContentFile).read())\r
+        if DistPkg.Header.Signature != Md5Sigature.hexdigest():\r
+            EdkLogger.error("InstallPkg", FILE_CHECKSUM_FAILURE, ExtraData=ContentFile)\r
+        \r
+        # Check package exist and install\r
+        for Guid,Version,Path in DistPkg.PackageSurfaceArea:\r
+            PackagePath = os.path.dirname(Path)\r
+            NewPackagePath = PackagePath\r
+            Package = DistPkg.PackageSurfaceArea[Guid,Version,Path]\r
+            EdkLogger.info("Installing package ... %s" % Package.PackageHeader.Name)\r
+            if Dep.CheckPackageExists(Guid, Version):\r
+                EdkLogger.quiet("Package [%s] has been installed" %Path)\r
+            NewPackagePath = InstallNewPackage(WorkspaceDir, PackagePath)\r
+            Package.FileList = []\r
+            for Item in Package.MiscFiles.Files:\r
+                FromFile = os.path.join(PackagePath, Item.Filename)\r
+                ToFile = os.path.normpath(os.path.join(WorkspaceDir, NewPackagePath, Item.Filename))\r
+                ContentZipFile.UnpackFile(FromFile, ToFile)\r
+                Package.FileList.append(ToFile)\r
+            \r
+            # Update package\r
+            Package.PackageHeader.CombinePath = Package.PackageHeader.CombinePath.replace(PackagePath, NewPackagePath, 1)\r
+            # Update modules of package\r
+            Module = None\r
+            for ModuleGuid, ModuleVersion, ModulePath in Package.Modules:\r
+                Module = Package.Modules[ModuleGuid, ModuleVersion, ModulePath]\r
+                NewModulePath = ModulePath.replace(PackagePath, NewPackagePath, 1)\r
+                del Package.Modules[ModuleGuid, ModuleVersion, ModulePath]\r
+                Package.Modules[ModuleGuid, ModuleVersion, NewModulePath] = Module\r
+            del DistPkg.PackageSurfaceArea[Guid,Version,Path]\r
+            DistPkg.PackageSurfaceArea[Guid,Version,Package.PackageHeader.CombinePath] = Package\r
+\r
+#            SaveFileOnChange(os.path.join(Options.InstallDir, ModulePath, Module.Header.Name, ".inf"), Inf.ModuleToInf(Module), False)\r
+#            EdkLogger.info("Installing package ... %s" % Package.Header.Name)\r
+#            shutil.copytree(os.path.join(ContentFileDir, Path), Options.InstallDir)\r
+#            SaveFileOnChange(os.path.join(Options.InstallDir, Path, Package.Header.Name, ".dec"), Dec.PackageToDec(Package), False)\r
+\r
+        # Check module exist and install\r
+        Module = None\r
+        for Guid,Version,Path in DistPkg.ModuleSurfaceArea:\r
+            ModulePath = os.path.dirname(Path)\r
+            NewModulePath = ModulePath\r
+            Module = DistPkg.ModuleSurfaceArea[Guid,Version,Path]\r
+            EdkLogger.info("Installing module ... %s" % Module.ModuleHeader.Name)\r
+            if Dep.CheckModuleExists(Guid, Version):\r
+                EdkLogger.quiet("Module [%s] has been installed" %Path)\r
+            NewModulePath = InstallNewPackage(WorkspaceDir, ModulePath)\r
+            Module.FileList = []\r
+            for Item in Module.MiscFiles.Files:\r
+                ModulePath = ModulePath[os.path.normpath(ModulePath).rfind(os.path.normpath('/'))+1:]\r
+                FromFile = os.path.join(ModulePath, Item.Filename)\r
+                ToFile = os.path.normpath(os.path.join(WorkspaceDir, NewModulePath, Item.Filename))\r
+                ContentZipFile.UnpackFile(FromFile, ToFile)\r
+                Module.FileList.append(ToFile)\r
+            \r
+#            EdkLogger.info("Installing module ... %s" % Module.Header.Name)\r
+#            shutil.copytree(os.path.join(ContentFileDir, Path), Options.InstallDir)\r
+#            SaveFileOnChange(os.path.join(Options.InstallDir, Path, Module.Header.Name, ".inf"), Inf.ModuleToInf(Module), False)\r
+            \r
+            # Update module\r
+            Module.ModuleHeader.CombinePath = Module.ModuleHeader.CombinePath.replace(os.path.dirname(Path), NewModulePath, 1)\r
+            del DistPkg.ModuleSurfaceArea[Guid,Version,Path]\r
+            DistPkg.ModuleSurfaceArea[Guid,Version,Module.ModuleHeader.CombinePath] = Module\r
+#            \r
+#        \r
+#        for Guid,Version,Path in DistPkg.PackageSurfaceArea:\r
+#            print Guid,Version,Path\r
+#            for item in DistPkg.PackageSurfaceArea[Guid,Version,Path].FileList:\r
+#                print item\r
+#        for Guid,Version,Path in DistPkg.ModuleSurfaceArea:\r
+#            print Guid,Version,Path\r
+#            for item in DistPkg.ModuleSurfaceArea[Guid,Version,Path].FileList:\r
+#                print item\r
+\r
+        if Options.Tools:\r
+            EdkLogger.info("Installing tools ... ")\r
+            for File in DistPkg.Tools.Files:\r
+                FromFile = File.Filename\r
+                ToFile = InstallNewFile(WorkspaceDir, FromFile)\r
+                ContentZipFile.UnpackFile(FromFile, ToFile)\r
+        if Options.MiscFiles:\r
+            EdkLogger.info("Installing misc files ... ")\r
+            for File in DistPkg.MiscellaneousFiles.Files:\r
+                FromFile = File.Filename\r
+                ToFile = InstallNewFile(WorkspaceDir, FromFile)\r
+                ContentZipFile.UnpackFile(FromFile, ToFile)\r
+\r
+        # update database\r
+        EdkLogger.quiet("Update Distribution Package Database ...")\r
+        Db.AddDPObject(DistPkg)\r
+\r
+    except FatalError, X:\r
+        if Options and Options.LogLevel < EdkLogger.DEBUG_9:\r
+            EdkLogger.quiet("(Python %s on %s) " % (platform.python_version(), sys.platform) + traceback.format_exc())\r
+        ReturnCode = X.args[0]\r
+    except KeyboardInterrupt:\r
+        ReturnCode = ABORT_ERROR\r
+        if Options and Options.LogLevel < EdkLogger.DEBUG_9:\r
+            EdkLogger.quiet("(Python %s on %s) " % (platform.python_version(), sys.platform) + traceback.format_exc())\r
+    except:\r
+        EdkLogger.error(\r
+                    "\nInstallPkg",\r
+                    CODE_ERROR,\r
+                    "Unknown fatal error when installing [%s]" % Options.PackageFile,\r
+                    ExtraData="\n(Please send email to dev@buildtools.tianocore.org for help, attaching following call stack trace!)\n",\r
+                    RaiseError=False\r
+                    )\r
+        EdkLogger.quiet("(Python %s on %s) " % (platform.python_version(), sys.platform) + traceback.format_exc())\r
+        ReturnCode = CODE_ERROR\r
+    finally:\r
+        EdkLogger.quiet("Removing temp files ... ")\r
+        if DistFile:\r
+            DistFile.Close()\r
+        if ContentZipFile:\r
+            ContentZipFile.Close()\r
+        if UnpackDir:\r
+            shutil.rmtree(UnpackDir)\r
+        \r
+        EdkLogger.quiet("DONE")\r
+        Progressor.Abort()\r
+\r
+if __name__ == '__main__':\r
+    sys.exit(Main())\r