]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Source/Python/UPT/RmPkg.py
BaseTools: Remove './SecMain' from 'run' target
[mirror_edk2.git] / BaseTools / Source / Python / UPT / RmPkg.py
CommitLineData
4234283c
LG
1## @file\r
2# Install distribution package.\r
3#\r
f7496d71 4# Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>\r
4234283c 5#\r
2e351cbe 6# SPDX-License-Identifier: BSD-2-Clause-Patent\r
4234283c
LG
7#\r
8\r
9'''\r
10RmPkg\r
11'''\r
12\r
13##\r
14# Import Modules\r
15#\r
16import os.path\r
17from stat import S_IWUSR\r
18from traceback import format_exc\r
19from platform import python_version\r
fcb1af1b 20from hashlib import md5\r
4234283c
LG
21from sys import stdin\r
22from sys import platform\r
23\r
24from Core.DependencyRules import DependencyRules\r
4234283c
LG
25from Library import GlobalData\r
26from Logger import StringTable as ST\r
27import Logger.Log as Logger\r
28from Logger.ToolError import OPTION_MISSING\r
29from Logger.ToolError import UNKNOWN_ERROR\r
30from Logger.ToolError import ABORT_ERROR\r
31from Logger.ToolError import CODE_ERROR\r
32from Logger.ToolError import FatalError\r
33\r
34\r
35## CheckDpDepex\r
36#\r
37# Check if the Depex is satisfied\r
38# @param Dep: Dep\r
39# @param Guid: Guid of Dp\r
40# @param Version: Version of Dp\r
41# @param WorkspaceDir: Workspace Dir\r
42#\r
43def CheckDpDepex(Dep, Guid, Version, WorkspaceDir):\r
44 (Removable, DependModuleList) = Dep.CheckDpDepexForRemove(Guid, Version)\r
45 if not Removable:\r
46 Logger.Info(ST.MSG_CONFIRM_REMOVE)\r
47 Logger.Info(ST.MSG_USER_DELETE_OP)\r
48 Input = stdin.readline()\r
49 Input = Input.replace('\r', '').replace('\n', '')\r
50 if Input.upper() != 'Y':\r
51 Logger.Error("RmPkg", UNKNOWN_ERROR, ST.ERR_USER_INTERRUPT)\r
52 return 1\r
53 else:\r
54 #\r
f7496d71 55 # report list of modules that are not valid due to force\r
4234283c
LG
56 # remove,\r
57 # also generate a log file for reference\r
58 #\r
59 Logger.Info(ST.MSG_INVALID_MODULE_INTRODUCED)\r
60 LogFilePath = os.path.normpath(os.path.join(WorkspaceDir, GlobalData.gINVALID_MODULE_FILE))\r
61 Logger.Info(ST.MSG_CHECK_LOG_FILE % LogFilePath)\r
62 try:\r
63 LogFile = open(LogFilePath, 'w')\r
64 try:\r
65 for ModulePath in DependModuleList:\r
66 LogFile.write("%s\n"%ModulePath)\r
67 Logger.Info(ModulePath)\r
68 except IOError:\r
f7496d71 69 Logger.Warn("\nRmPkg", ST.ERR_FILE_WRITE_FAILURE,\r
4234283c
LG
70 File=LogFilePath)\r
71 except IOError:\r
f7496d71 72 Logger.Warn("\nRmPkg", ST.ERR_FILE_OPEN_FAILURE,\r
4234283c 73 File=LogFilePath)\r
f7496d71 74 finally:\r
4234283c
LG
75 LogFile.close()\r
76\r
77## Remove Path\r
78#\r
79# removing readonly file on windows will get "Access is denied"\r
80# error, so before removing, change the mode to be writeable\r
81#\r
f7496d71 82# @param Path: The Path to be removed\r
4234283c
LG
83#\r
84def RemovePath(Path):\r
85 Logger.Info(ST.MSG_REMOVE_FILE % Path)\r
86 if not os.access(Path, os.W_OK):\r
87 os.chmod(Path, S_IWUSR)\r
88 os.remove(Path)\r
89 try:\r
90 os.removedirs(os.path.split(Path)[0])\r
91 except OSError:\r
92 pass\r
93## GetCurrentFileList\r
94#\r
95# @param DataBase: DataBase of UPT\r
96# @param Guid: Guid of Dp\r
97# @param Version: Version of Dp\r
98# @param WorkspaceDir: Workspace Dir\r
99#\r
100def GetCurrentFileList(DataBase, Guid, Version, WorkspaceDir):\r
101 NewFileList = []\r
102 for Dir in DataBase.GetDpInstallDirList(Guid, Version):\r
103 RootDir = os.path.normpath(os.path.join(WorkspaceDir, Dir))\r
104 for Root, Dirs, Files in os.walk(RootDir):\r
105 Logger.Debug(0, Dirs)\r
106 for File in Files:\r
107 FilePath = os.path.join(Root, File)\r
108 if FilePath not in NewFileList:\r
109 NewFileList.append(FilePath)\r
110 return NewFileList\r
111\r
112\r
113## Tool entrance method\r
114#\r
115# This method mainly dispatch specific methods per the command line options.\r
116# If no error found, return zero value so the caller of this tool can know\r
117# if it's executed successfully or not.\r
118#\r
f7496d71 119# @param Options: command option\r
4234283c
LG
120#\r
121def Main(Options = None):\r
122\r
123 try:\r
f7496d71 124 DataBase = GlobalData.gDB\r
4234283c 125 if not Options.DistributionFile:\r
f7496d71
LG
126 Logger.Error("RmPkg",\r
127 OPTION_MISSING,\r
4234283c 128 ExtraData=ST.ERR_SPECIFY_PACKAGE)\r
4234283c
LG
129 WorkspaceDir = GlobalData.gWORKSPACE\r
130 #\r
131 # Prepare check dependency\r
132 #\r
133 Dep = DependencyRules(DataBase)\r
f7496d71 134\r
4234283c 135 #\r
421ccda3 136 # Get the Dp information\r
4234283c 137 #\r
421ccda3
HC
138 StoredDistFile, Guid, Version = GetInstalledDpInfo(Options.DistributionFile, Dep, DataBase, WorkspaceDir)\r
139\r
f7496d71 140 #\r
4234283c
LG
141 # Check Dp depex\r
142 #\r
143 CheckDpDepex(Dep, Guid, Version, WorkspaceDir)\r
144\r
f7496d71 145 #\r
421ccda3 146 # remove distribution\r
4234283c 147 #\r
421ccda3 148 RemoveDist(Guid, Version, StoredDistFile, DataBase, WorkspaceDir, Options.Yes)\r
4234283c 149\r
4234283c 150 Logger.Quiet(ST.MSG_FINISH)\r
f7496d71 151\r
4234283c 152 ReturnCode = 0\r
f7496d71 153\r
5b0671c1 154 except FatalError as XExcept:\r
f7496d71 155 ReturnCode = XExcept.args[0]\r
4234283c
LG
156 if Logger.GetLevel() <= Logger.DEBUG_9:\r
157 Logger.Quiet(ST.MSG_PYTHON_ON % (python_version(), platform) + \\r
158 format_exc())\r
159 except KeyboardInterrupt:\r
160 ReturnCode = ABORT_ERROR\r
161 if Logger.GetLevel() <= Logger.DEBUG_9:\r
162 Logger.Quiet(ST.MSG_PYTHON_ON % (python_version(), platform) + \\r
163 format_exc())\r
164 except:\r
165 Logger.Error(\r
166 "\nRmPkg",\r
167 CODE_ERROR,\r
168 ST.ERR_UNKNOWN_FATAL_REMOVING_ERR,\r
c1387446 169 ExtraData=ST.MSG_SEARCH_FOR_HELP % ST.MSG_EDKII_MAIL_ADDR,\r
4234283c
LG
170 RaiseError=False\r
171 )\r
172 Logger.Quiet(ST.MSG_PYTHON_ON % (python_version(), platform) + \\r
173 format_exc())\r
174 ReturnCode = CODE_ERROR\r
175 return ReturnCode\r
4234283c 176\r
421ccda3
HC
177## GetInstalledDpInfo method\r
178#\r
179# Get the installed distribution information\r
180#\r
181# @param DistributionFile: the name of the distribution\r
182# @param Dep: the instance of DependencyRules\r
183# @param DataBase: the internal database\r
184# @param WorkspaceDir: work space directory\r
185# @retval StoredDistFile: the distribution file that backed up\r
186# @retval Guid: the Guid of the distribution\r
187# @retval Version: the Version of distribution\r
188#\r
189def GetInstalledDpInfo(DistributionFile, Dep, DataBase, WorkspaceDir):\r
190 (Guid, Version, NewDpFileName) = DataBase.GetDpByName(os.path.split(DistributionFile)[1])\r
191 if not Guid:\r
192 Logger.Error("RmPkg", UNKNOWN_ERROR, ST.ERR_PACKAGE_NOT_INSTALLED % DistributionFile)\r
193\r
194 #\r
195 # Check Dp existing\r
196 #\r
197 if not Dep.CheckDpExists(Guid, Version):\r
198 Logger.Error("RmPkg", UNKNOWN_ERROR, ST.ERR_DISTRIBUTION_NOT_INSTALLED)\r
199 #\r
f7496d71 200 # Check for Distribution files existence in /conf/upt, if not exist,\r
421ccda3
HC
201 # Warn user and go on.\r
202 #\r
203 StoredDistFile = os.path.normpath(os.path.join(WorkspaceDir, GlobalData.gUPT_DIR, NewDpFileName))\r
204 if not os.path.isfile(StoredDistFile):\r
205 Logger.Warn("RmPkg", ST.WRN_DIST_NOT_FOUND%StoredDistFile)\r
206 StoredDistFile = None\r
207\r
208 return StoredDistFile, Guid, Version\r
209\r
210## RemoveDist method\r
211#\r
212# remove a distribution\r
213#\r
214# @param Guid: the Guid of the distribution\r
215# @param Version: the Version of distribution\r
216# @param StoredDistFile: the distribution file that backed up\r
217# @param DataBase: the internal database\r
218# @param WorkspaceDir: work space directory\r
219# @param ForceRemove: whether user want to remove file even it is modified\r
220#\r
221def RemoveDist(Guid, Version, StoredDistFile, DataBase, WorkspaceDir, ForceRemove):\r
222 #\r
223 # Get Current File List\r
224 #\r
225 NewFileList = GetCurrentFileList(DataBase, Guid, Version, WorkspaceDir)\r
226\r
227 #\r
228 # Remove all files\r
229 #\r
230 MissingFileList = []\r
231 for (Path, Md5Sum) in DataBase.GetDpFileList(Guid, Version):\r
232 if os.path.isfile(Path):\r
233 if Path in NewFileList:\r
234 NewFileList.remove(Path)\r
235 if not ForceRemove:\r
236 #\r
237 # check whether modified by users\r
238 #\r
fb0b35e0
AC
239 Md5Signature = md5(open(str(Path), 'rb').read())\r
240 if Md5Sum != Md5Signature.hexdigest():\r
421ccda3
HC
241 Logger.Info(ST.MSG_CONFIRM_REMOVE2 % Path)\r
242 Input = stdin.readline()\r
243 Input = Input.replace('\r', '').replace('\n', '')\r
244 if Input.upper() != 'Y':\r
245 continue\r
246 RemovePath(Path)\r
247 else:\r
248 MissingFileList.append(Path)\r
f7496d71 249\r
421ccda3
HC
250 for Path in NewFileList:\r
251 if os.path.isfile(Path):\r
252 if (not ForceRemove) and (not os.path.split(Path)[1].startswith('.')):\r
253 Logger.Info(ST.MSG_CONFIRM_REMOVE3 % Path)\r
254 Input = stdin.readline()\r
255 Input = Input.replace('\r', '').replace('\n', '')\r
256 if Input.upper() != 'Y':\r
257 continue\r
258 RemovePath(Path)\r
259\r
260 #\r
261 # Remove distribution files in /Conf/.upt\r
262 #\r
263 if StoredDistFile is not None:\r
264 os.remove(StoredDistFile)\r
265\r
266 #\r
267 # update database\r
268 #\r
269 Logger.Quiet(ST.MSG_UPDATE_PACKAGE_DATABASE)\r
270 DataBase.RemoveDpObj(Guid, Version)\r