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