2 # Install distribution package.
4 # Copyright (c) 2011 - 2014, 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.
23 from stat
import S_IWUSR
24 from traceback
import format_exc
25 from platform
import python_version
28 from sys
import platform
30 from Core
.DependencyRules
import DependencyRules
31 from Library
import GlobalData
32 from Logger
import StringTable
as ST
33 import Logger
.Log
as Logger
34 from Logger
.ToolError
import OPTION_MISSING
35 from Logger
.ToolError
import UNKNOWN_ERROR
36 from Logger
.ToolError
import ABORT_ERROR
37 from Logger
.ToolError
import CODE_ERROR
38 from Logger
.ToolError
import FatalError
43 # Check if the Depex is satisfied
45 # @param Guid: Guid of Dp
46 # @param Version: Version of Dp
47 # @param WorkspaceDir: Workspace Dir
49 def CheckDpDepex(Dep
, Guid
, Version
, WorkspaceDir
):
50 (Removable
, DependModuleList
) = Dep
.CheckDpDepexForRemove(Guid
, Version
)
52 Logger
.Info(ST
.MSG_CONFIRM_REMOVE
)
53 Logger
.Info(ST
.MSG_USER_DELETE_OP
)
54 Input
= stdin
.readline()
55 Input
= Input
.replace('\r', '').replace('\n', '')
56 if Input
.upper() != 'Y':
57 Logger
.Error("RmPkg", UNKNOWN_ERROR
, ST
.ERR_USER_INTERRUPT
)
61 # report list of modules that are not valid due to force
63 # also generate a log file for reference
65 Logger
.Info(ST
.MSG_INVALID_MODULE_INTRODUCED
)
66 LogFilePath
= os
.path
.normpath(os
.path
.join(WorkspaceDir
, GlobalData
.gINVALID_MODULE_FILE
))
67 Logger
.Info(ST
.MSG_CHECK_LOG_FILE
% LogFilePath
)
69 LogFile
= open(LogFilePath
, 'w')
71 for ModulePath
in DependModuleList
:
72 LogFile
.write("%s\n"%ModulePath
)
73 Logger
.Info(ModulePath
)
75 Logger
.Warn("\nRmPkg", ST
.ERR_FILE_WRITE_FAILURE
,
78 Logger
.Warn("\nRmPkg", ST
.ERR_FILE_OPEN_FAILURE
,
85 # removing readonly file on windows will get "Access is denied"
86 # error, so before removing, change the mode to be writeable
88 # @param Path: The Path to be removed
91 Logger
.Info(ST
.MSG_REMOVE_FILE
% Path
)
92 if not os
.access(Path
, os
.W_OK
):
93 os
.chmod(Path
, S_IWUSR
)
96 os
.removedirs(os
.path
.split(Path
)[0])
101 # @param DataBase: DataBase of UPT
102 # @param Guid: Guid of Dp
103 # @param Version: Version of Dp
104 # @param WorkspaceDir: Workspace Dir
106 def GetCurrentFileList(DataBase
, Guid
, Version
, WorkspaceDir
):
108 for Dir
in DataBase
.GetDpInstallDirList(Guid
, Version
):
109 RootDir
= os
.path
.normpath(os
.path
.join(WorkspaceDir
, Dir
))
110 for Root
, Dirs
, Files
in os
.walk(RootDir
):
111 Logger
.Debug(0, Dirs
)
113 FilePath
= os
.path
.join(Root
, File
)
114 if FilePath
not in NewFileList
:
115 NewFileList
.append(FilePath
)
119 ## Tool entrance method
121 # This method mainly dispatch specific methods per the command line options.
122 # If no error found, return zero value so the caller of this tool can know
123 # if it's executed successfully or not.
125 # @param Options: command option
127 def Main(Options
= None):
130 DataBase
= GlobalData
.gDB
131 if not Options
.DistributionFile
:
132 Logger
.Error("RmPkg",
134 ExtraData
=ST
.ERR_SPECIFY_PACKAGE
)
135 WorkspaceDir
= GlobalData
.gWORKSPACE
137 # Prepare check dependency
139 Dep
= DependencyRules(DataBase
)
142 # Get the Dp information
144 StoredDistFile
, Guid
, Version
= GetInstalledDpInfo(Options
.DistributionFile
, Dep
, DataBase
, WorkspaceDir
)
149 CheckDpDepex(Dep
, Guid
, Version
, WorkspaceDir
)
152 # remove distribution
154 RemoveDist(Guid
, Version
, StoredDistFile
, DataBase
, WorkspaceDir
, Options
.Yes
)
156 Logger
.Quiet(ST
.MSG_FINISH
)
160 except FatalError
, XExcept
:
161 ReturnCode
= XExcept
.args
[0]
162 if Logger
.GetLevel() <= Logger
.DEBUG_9
:
163 Logger
.Quiet(ST
.MSG_PYTHON_ON
% (python_version(), platform
) + \
165 except KeyboardInterrupt:
166 ReturnCode
= ABORT_ERROR
167 if Logger
.GetLevel() <= Logger
.DEBUG_9
:
168 Logger
.Quiet(ST
.MSG_PYTHON_ON
% (python_version(), platform
) + \
174 ST
.ERR_UNKNOWN_FATAL_REMOVING_ERR
,
175 ExtraData
=ST
.MSG_SEARCH_FOR_HELP
,
178 Logger
.Quiet(ST
.MSG_PYTHON_ON
% (python_version(), platform
) + \
180 ReturnCode
= CODE_ERROR
183 ## GetInstalledDpInfo method
185 # Get the installed distribution information
187 # @param DistributionFile: the name of the distribution
188 # @param Dep: the instance of DependencyRules
189 # @param DataBase: the internal database
190 # @param WorkspaceDir: work space directory
191 # @retval StoredDistFile: the distribution file that backed up
192 # @retval Guid: the Guid of the distribution
193 # @retval Version: the Version of distribution
195 def GetInstalledDpInfo(DistributionFile
, Dep
, DataBase
, WorkspaceDir
):
196 (Guid
, Version
, NewDpFileName
) = DataBase
.GetDpByName(os
.path
.split(DistributionFile
)[1])
198 Logger
.Error("RmPkg", UNKNOWN_ERROR
, ST
.ERR_PACKAGE_NOT_INSTALLED
% DistributionFile
)
203 if not Dep
.CheckDpExists(Guid
, Version
):
204 Logger
.Error("RmPkg", UNKNOWN_ERROR
, ST
.ERR_DISTRIBUTION_NOT_INSTALLED
)
206 # Check for Distribution files existence in /conf/upt, if not exist,
207 # Warn user and go on.
209 StoredDistFile
= os
.path
.normpath(os
.path
.join(WorkspaceDir
, GlobalData
.gUPT_DIR
, NewDpFileName
))
210 if not os
.path
.isfile(StoredDistFile
):
211 Logger
.Warn("RmPkg", ST
.WRN_DIST_NOT_FOUND
%StoredDistFile
)
212 StoredDistFile
= None
214 return StoredDistFile
, Guid
, Version
218 # remove a distribution
220 # @param Guid: the Guid of the distribution
221 # @param Version: the Version of distribution
222 # @param StoredDistFile: the distribution file that backed up
223 # @param DataBase: the internal database
224 # @param WorkspaceDir: work space directory
225 # @param ForceRemove: whether user want to remove file even it is modified
227 def RemoveDist(Guid
, Version
, StoredDistFile
, DataBase
, WorkspaceDir
, ForceRemove
):
229 # Get Current File List
231 NewFileList
= GetCurrentFileList(DataBase
, Guid
, Version
, WorkspaceDir
)
237 for (Path
, Md5Sum
) in DataBase
.GetDpFileList(Guid
, Version
):
238 if os
.path
.isfile(Path
):
239 if Path
in NewFileList
:
240 NewFileList
.remove(Path
)
243 # check whether modified by users
245 Md5Sigature
= md5
.new(open(str(Path
), 'rb').read())
246 if Md5Sum
!= Md5Sigature
.hexdigest():
247 Logger
.Info(ST
.MSG_CONFIRM_REMOVE2
% Path
)
248 Input
= stdin
.readline()
249 Input
= Input
.replace('\r', '').replace('\n', '')
250 if Input
.upper() != 'Y':
254 MissingFileList
.append(Path
)
256 for Path
in NewFileList
:
257 if os
.path
.isfile(Path
):
258 if (not ForceRemove
) and (not os
.path
.split(Path
)[1].startswith('.')):
259 Logger
.Info(ST
.MSG_CONFIRM_REMOVE3
% Path
)
260 Input
= stdin
.readline()
261 Input
= Input
.replace('\r', '').replace('\n', '')
262 if Input
.upper() != 'Y':
267 # Remove distribution files in /Conf/.upt
269 if StoredDistFile
is not None:
270 os
.remove(StoredDistFile
)
275 Logger
.Quiet(ST
.MSG_UPDATE_PACKAGE_DATABASE
)
276 DataBase
.RemoveDpObj(Guid
, Version
)