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