2 # Replace distribution package.
4 # Copyright (c) 2014 - 2018, Intel Corporation. All rights reserved.<BR>
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
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.
15 Replace a distribution package
20 from shutil
import rmtree
21 from traceback
import format_exc
22 from platform
import python_version
23 from sys
import platform
24 from Logger
import StringTable
as ST
25 from Logger
.ToolError
import UNKNOWN_ERROR
26 from Logger
.ToolError
import FatalError
27 from Logger
.ToolError
import ABORT_ERROR
28 from Logger
.ToolError
import CODE_ERROR
29 from Logger
.ToolError
import UPT_ALREADY_INSTALLED_ERROR
30 import Logger
.Log
as Logger
32 from Core
.DependencyRules
import DependencyRules
33 from Library
import GlobalData
34 from InstallPkg
import UnZipDp
35 from InstallPkg
import InstallDp
36 from RmPkg
import GetInstalledDpInfo
37 from RmPkg
import RemoveDist
39 ## Tool entrance method
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.
45 # @param Options: command Options
47 def Main(Options
= None):
48 ContentZipFile
, DistFile
= None, None
50 DataBase
= GlobalData
.gDB
51 WorkspaceDir
= GlobalData
.gWORKSPACE
52 Dep
= DependencyRules(DataBase
)
53 DistPkg
, ContentZipFile
, DpPkgFileName
, DistFile
= UnZipDp(WorkspaceDir
, Options
.PackFileToReplace
)
55 StoredDistFile
, OrigDpGuid
, OrigDpVersion
= GetInstalledDpInfo(Options
.PackFileToBeReplaced
, \
56 Dep
, DataBase
, WorkspaceDir
)
61 CheckReplaceDpx(Dep
, DistPkg
, OrigDpGuid
, OrigDpVersion
)
64 # Remove the old distribution
66 RemoveDist(OrigDpGuid
, OrigDpVersion
, StoredDistFile
, DataBase
, WorkspaceDir
, Options
.Yes
)
69 # Install the new distribution
71 InstallDp(DistPkg
, DpPkgFileName
, ContentZipFile
, Options
, Dep
, WorkspaceDir
, DataBase
)
74 except FatalError
as 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())
85 ReturnCode
= CODE_ERROR
89 ST
.ERR_UNKNOWN_FATAL_REPLACE_ERR
% (Options
.PackFileToReplace
, Options
.PackFileToBeReplaced
),
90 ExtraData
=ST
.MSG_SEARCH_FOR_HELP
,
93 Logger
.Quiet(ST
.MSG_PYTHON_ON
% (python_version(),
94 platform
) + format_exc())
97 Logger
.Quiet(ST
.MSG_REMOVE_TEMP_FILE_STARTED
)
101 ContentZipFile
.Close()
102 for TempDir
in GlobalData
.gUNPACK_DIR
:
104 GlobalData
.gUNPACK_DIR
= []
105 Logger
.Quiet(ST
.MSG_REMOVE_TEMP_FILE_DONE
)
108 Logger
.Quiet(ST
.MSG_FINISH
)
112 def CheckReplaceDpx(Dep
, DistPkg
, OrigDpGuid
, OrigDpVersion
):
114 for PkgInfo
in DistPkg
.PackageSurfaceArea
:
115 Guid
, Version
= PkgInfo
[0], PkgInfo
[1]
116 NewDpPkgList
.append((Guid
, Version
))
118 NewDpInfo
= "%s %s" % (DistPkg
.Header
.GetGuid(), DistPkg
.Header
.GetVersion())
119 OrigDpInfo
= "%s %s" % (OrigDpGuid
, OrigDpVersion
)
122 # check whether new distribution is already installed and not replacing itself
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
)
131 # check whether the original distribution could be replaced by new distribution
133 Logger
.Verbose(ST
.MSG_CHECK_DP_FOR_REPLACE
%(NewDpInfo
, OrigDpInfo
))
134 DepInfoResult
= Dep
.CheckDpDepexForReplace(OrigDpGuid
, OrigDpVersion
, NewDpPkgList
)
135 Replaceable
= DepInfoResult
[0]
137 Logger
.Error("\nReplacePkg", UNKNOWN_ERROR
,
138 ST
.ERR_PACKAGE_NOT_MATCH_DEPENDENCY
)
141 # check whether new distribution could be installed by dependency rule
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
)