• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1## @file
2# Replace distribution package.
3#
4# Copyright (c) 2014, Intel Corporation. All rights reserved.<BR>
5#
6# This program and the accompanying materials are licensed and made available
7# under the terms and conditions of the BSD License which accompanies this
8# distribution. The full text of the license may be found at
9# http://opensource.org/licenses/bsd-license.php
10#
11# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13#
14"""
15Replace a distribution package
16"""
17##
18# Import Modules
19#
20from shutil import rmtree
21from traceback import format_exc
22from platform import python_version
23from sys import platform
24from Logger import StringTable as ST
25from Logger.ToolError import UNKNOWN_ERROR
26from Logger.ToolError import FatalError
27from Logger.ToolError import ABORT_ERROR
28from Logger.ToolError import CODE_ERROR
29from Logger.ToolError import UPT_ALREADY_INSTALLED_ERROR
30import Logger.Log as Logger
31
32from Core.DependencyRules import DependencyRules
33from Library import GlobalData
34from InstallPkg import UnZipDp
35from InstallPkg import InstallDp
36from RmPkg import GetInstalledDpInfo
37from RmPkg import RemoveDist
38
39## Tool entrance method
40#
41# This method mainly dispatch specific methods per the command line options.
42# If no error found, return zero value so the caller of this tool can know
43# if it's executed successfully or not.
44#
45# @param  Options: command Options
46#
47def Main(Options = None):
48    ContentZipFile, DistFile = None, None
49    try:
50        DataBase = GlobalData.gDB
51        WorkspaceDir = GlobalData.gWORKSPACE
52        Dep = DependencyRules(DataBase)
53        DistPkg, ContentZipFile, DpPkgFileName, DistFile = UnZipDp(WorkspaceDir, Options.PackFileToReplace)
54
55        StoredDistFile, OrigDpGuid, OrigDpVersion = GetInstalledDpInfo(Options.PackFileToBeReplaced, \
56                                                                       Dep, DataBase, WorkspaceDir)
57
58        #
59        # check dependency
60        #
61        CheckReplaceDpx(Dep, DistPkg, OrigDpGuid, OrigDpVersion)
62
63        #
64        # Remove the old distribution
65        #
66        RemoveDist(OrigDpGuid, OrigDpVersion, StoredDistFile, DataBase, WorkspaceDir, Options.Yes)
67
68        #
69        # Install the new distribution
70        #
71        InstallDp(DistPkg, DpPkgFileName, ContentZipFile, Options, Dep, WorkspaceDir, DataBase)
72        ReturnCode = 0
73
74    except FatalError, XExcept:
75        ReturnCode = XExcept.args[0]
76        if Logger.GetLevel() <= Logger.DEBUG_9:
77            Logger.Quiet(ST.MSG_PYTHON_ON % (python_version(),
78                platform) + format_exc())
79    except KeyboardInterrupt:
80        ReturnCode = ABORT_ERROR
81        if Logger.GetLevel() <= Logger.DEBUG_9:
82            Logger.Quiet(ST.MSG_PYTHON_ON % (python_version(),
83                platform) + format_exc())
84    except:
85        ReturnCode = CODE_ERROR
86        Logger.Error(
87                    "\nReplacePkg",
88                    CODE_ERROR,
89                    ST.ERR_UNKNOWN_FATAL_REPLACE_ERR % (Options.PackFileToReplace, Options.PackFileToBeReplaced),
90                    ExtraData=ST.MSG_SEARCH_FOR_HELP,
91                    RaiseError=False
92                    )
93        Logger.Quiet(ST.MSG_PYTHON_ON % (python_version(),
94            platform) + format_exc())
95
96    finally:
97        Logger.Quiet(ST.MSG_REMOVE_TEMP_FILE_STARTED)
98        if DistFile:
99            DistFile.Close()
100        if ContentZipFile:
101            ContentZipFile.Close()
102        if GlobalData.gUNPACK_DIR:
103            rmtree(GlobalData.gUNPACK_DIR)
104            GlobalData.gUNPACK_DIR = None
105        Logger.Quiet(ST.MSG_REMOVE_TEMP_FILE_DONE)
106
107    if ReturnCode == 0:
108        Logger.Quiet(ST.MSG_FINISH)
109
110    return ReturnCode
111
112def CheckReplaceDpx(Dep, DistPkg, OrigDpGuid, OrigDpVersion):
113    NewDpPkgList = []
114    for PkgInfo in DistPkg.PackageSurfaceArea:
115        Guid, Version = PkgInfo[0], PkgInfo[1]
116        NewDpPkgList.append((Guid, Version))
117
118    NewDpInfo = "%s %s" % (DistPkg.Header.GetGuid(), DistPkg.Header.GetVersion())
119    OrigDpInfo = "%s %s" % (OrigDpGuid, OrigDpVersion)
120
121    #
122    # check whether new distribution is already installed and not replacing itself
123    #
124    if (NewDpInfo != OrigDpInfo):
125        if Dep.CheckDpExists(DistPkg.Header.GetGuid(), DistPkg.Header.GetVersion()):
126            Logger.Error("\nReplacePkg", UPT_ALREADY_INSTALLED_ERROR,
127                ST.WRN_DIST_PKG_INSTALLED,
128                ExtraData=ST.MSG_REPLACE_ALREADY_INSTALLED_DP)
129
130    #
131    # check whether the original distribution could be replaced by new distribution
132    #
133    Logger.Verbose(ST.MSG_CHECK_DP_FOR_REPLACE%(NewDpInfo, OrigDpInfo))
134    DepInfoResult = Dep.CheckDpDepexForReplace(OrigDpGuid, OrigDpVersion, NewDpPkgList)
135    Replaceable = DepInfoResult[0]
136    if not Replaceable:
137        Logger.Error("\nReplacePkg", UNKNOWN_ERROR,
138            ST.ERR_PACKAGE_NOT_MATCH_DEPENDENCY)
139
140    #
141    # check whether new distribution could be installed by dependency rule
142    #
143    Logger.Verbose(ST.MSG_CHECK_DP_FOR_INSTALL%str(NewDpInfo))
144    if not Dep.ReplaceCheckNewDpDepex(DistPkg, OrigDpGuid, OrigDpVersion):
145        Logger.Error("\nReplacePkg", UNKNOWN_ERROR,
146            ST.ERR_PACKAGE_NOT_MATCH_DEPENDENCY,
147            ExtraData=DistPkg.Header.Name)
148
149