## @file\r
# Install distribution package.\r
#\r
-# Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>\r
+# Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>\r
#\r
-# This program and the accompanying materials are licensed and made available \r
-# under the terms and conditions of the BSD License which accompanies this \r
-# distribution. The full text of the license may be found at \r
+# This program and the accompanying materials are licensed and made available\r
+# under the terms and conditions of the BSD License which accompanies this\r
+# distribution. The full text of the license may be found at\r
# http://opensource.org/licenses/bsd-license.php\r
#\r
# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
from stat import S_IWUSR\r
from traceback import format_exc\r
from platform import python_version\r
-import md5\r
+from hashlib import md5\r
from sys import stdin\r
from sys import platform\r
\r
from Core.DependencyRules import DependencyRules\r
-from Library.Misc import CheckEnvVariable\r
from Library import GlobalData\r
from Logger import StringTable as ST\r
import Logger.Log as Logger\r
return 1\r
else:\r
#\r
- # report list of modules that are not valid due to force \r
+ # report list of modules that are not valid due to force\r
# remove,\r
# also generate a log file for reference\r
#\r
LogFile.write("%s\n"%ModulePath)\r
Logger.Info(ModulePath)\r
except IOError:\r
- Logger.Warn("\nRmPkg", ST.ERR_FILE_WRITE_FAILURE, \r
+ Logger.Warn("\nRmPkg", ST.ERR_FILE_WRITE_FAILURE,\r
File=LogFilePath)\r
except IOError:\r
- Logger.Warn("\nRmPkg", ST.ERR_FILE_OPEN_FAILURE, \r
+ Logger.Warn("\nRmPkg", ST.ERR_FILE_OPEN_FAILURE,\r
File=LogFilePath)\r
- finally: \r
+ finally:\r
LogFile.close()\r
\r
## Remove Path\r
# removing readonly file on windows will get "Access is denied"\r
# error, so before removing, change the mode to be writeable\r
#\r
-# @param Path: The Path to be removed \r
+# @param Path: The Path to be removed\r
#\r
def RemovePath(Path):\r
Logger.Info(ST.MSG_REMOVE_FILE % Path)\r
# If no error found, return zero value so the caller of this tool can know\r
# if it's executed successfully or not.\r
#\r
-# @param Options: command option \r
+# @param Options: command option\r
#\r
def Main(Options = None):\r
\r
try:\r
- DataBase = GlobalData.gDB \r
+ DataBase = GlobalData.gDB\r
if not Options.DistributionFile:\r
- Logger.Error("RmPkg", \r
- OPTION_MISSING, \r
+ Logger.Error("RmPkg",\r
+ OPTION_MISSING,\r
ExtraData=ST.ERR_SPECIFY_PACKAGE)\r
- CheckEnvVariable()\r
WorkspaceDir = GlobalData.gWORKSPACE\r
#\r
# Prepare check dependency\r
#\r
Dep = DependencyRules(DataBase)\r
- \r
- if Options.DistributionFile:\r
- (Guid, Version, NewDpFileName) = \\r
- DataBase.GetDpByName(os.path.split(Options.DistributionFile)[1])\r
- if not Guid:\r
- Logger.Error("RmPkg", UNKNOWN_ERROR, ST.ERR_PACKAGE_NOT_INSTALLED % Options.DistributionFile)\r
- else:\r
- Guid = Options.PackageGuid\r
- Version = Options.PackageVersion\r
- #\r
- # Check Dp existing\r
- #\r
- if not Dep.CheckDpExists(Guid, Version):\r
- Logger.Error("RmPkg", UNKNOWN_ERROR, ST.ERR_DISTRIBUTION_NOT_INSTALLED)\r
- #\r
- # Check for Distribution files existence in /conf/upt, if not exist, \r
- # Warn user and go on.\r
- #\r
- StoredDistFile = os.path.normpath(os.path.join(WorkspaceDir, GlobalData.gUPT_DIR, NewDpFileName))\r
- if not os.path.isfile(StoredDistFile):\r
- Logger.Warn("RmPkg", ST.WRN_DIST_NOT_FOUND%StoredDistFile)\r
- StoredDistFile = None\r
- \r
- # \r
- # Check Dp depex\r
- #\r
- CheckDpDepex(Dep, Guid, Version, WorkspaceDir)\r
\r
#\r
- # Get Current File List\r
+ # Get the Dp information\r
#\r
- NewFileList = GetCurrentFileList(DataBase, Guid, Version, WorkspaceDir)\r
+ StoredDistFile, Guid, Version = GetInstalledDpInfo(Options.DistributionFile, Dep, DataBase, WorkspaceDir)\r
\r
#\r
- # Remove all files\r
+ # Check Dp depex\r
#\r
- MissingFileList = []\r
- for (Path, Md5Sum) in DataBase.GetDpFileList(Guid, Version):\r
- if os.path.isfile(Path):\r
- if Path in NewFileList:\r
- NewFileList.remove(Path)\r
- if not Options.Yes:\r
- #\r
- # check whether modified by users\r
- #\r
- Md5Sigature = md5.new(open(str(Path), 'rb').read())\r
- if Md5Sum != Md5Sigature.hexdigest():\r
- Logger.Info(ST.MSG_CONFIRM_REMOVE2 % Path)\r
- Input = stdin.readline()\r
- Input = Input.replace('\r', '').replace('\n', '')\r
- if Input.upper() != 'Y':\r
- continue\r
- RemovePath(Path)\r
- else:\r
- MissingFileList.append(Path)\r
- \r
- for Path in NewFileList:\r
- if os.path.isfile(Path):\r
- if (not Options.Yes) and (not os.path.split(Path)[1].startswith('.')):\r
- Logger.Info(ST.MSG_CONFIRM_REMOVE3 % Path)\r
- Input = stdin.readline()\r
- Input = Input.replace('\r', '').replace('\n', '')\r
- if Input.upper() != 'Y':\r
- continue\r
- RemovePath(Path)\r
+ CheckDpDepex(Dep, Guid, Version, WorkspaceDir)\r
\r
#\r
- # Remove distribution files in /Conf/.upt\r
+ # remove distribution\r
#\r
- if StoredDistFile is not None:\r
- os.remove(StoredDistFile)\r
+ RemoveDist(Guid, Version, StoredDistFile, DataBase, WorkspaceDir, Options.Yes)\r
\r
- #\r
- # update database\r
- #\r
- Logger.Quiet(ST.MSG_UPDATE_PACKAGE_DATABASE)\r
- DataBase.RemoveDpObj(Guid, Version)\r
Logger.Quiet(ST.MSG_FINISH)\r
- \r
+\r
ReturnCode = 0\r
- \r
- except FatalError, XExcept:\r
- ReturnCode = XExcept.args[0] \r
+\r
+ except FatalError as XExcept:\r
+ ReturnCode = XExcept.args[0]\r
if Logger.GetLevel() <= Logger.DEBUG_9:\r
Logger.Quiet(ST.MSG_PYTHON_ON % (python_version(), platform) + \\r
format_exc())\r
format_exc())\r
ReturnCode = CODE_ERROR\r
return ReturnCode\r
- \r
\r
+## GetInstalledDpInfo method\r
+#\r
+# Get the installed distribution information\r
+#\r
+# @param DistributionFile: the name of the distribution\r
+# @param Dep: the instance of DependencyRules\r
+# @param DataBase: the internal database\r
+# @param WorkspaceDir: work space directory\r
+# @retval StoredDistFile: the distribution file that backed up\r
+# @retval Guid: the Guid of the distribution\r
+# @retval Version: the Version of distribution\r
+#\r
+def GetInstalledDpInfo(DistributionFile, Dep, DataBase, WorkspaceDir):\r
+ (Guid, Version, NewDpFileName) = DataBase.GetDpByName(os.path.split(DistributionFile)[1])\r
+ if not Guid:\r
+ Logger.Error("RmPkg", UNKNOWN_ERROR, ST.ERR_PACKAGE_NOT_INSTALLED % DistributionFile)\r
+\r
+ #\r
+ # Check Dp existing\r
+ #\r
+ if not Dep.CheckDpExists(Guid, Version):\r
+ Logger.Error("RmPkg", UNKNOWN_ERROR, ST.ERR_DISTRIBUTION_NOT_INSTALLED)\r
+ #\r
+ # Check for Distribution files existence in /conf/upt, if not exist,\r
+ # Warn user and go on.\r
+ #\r
+ StoredDistFile = os.path.normpath(os.path.join(WorkspaceDir, GlobalData.gUPT_DIR, NewDpFileName))\r
+ if not os.path.isfile(StoredDistFile):\r
+ Logger.Warn("RmPkg", ST.WRN_DIST_NOT_FOUND%StoredDistFile)\r
+ StoredDistFile = None\r
+\r
+ return StoredDistFile, Guid, Version\r
+\r
+## RemoveDist method\r
+#\r
+# remove a distribution\r
+#\r
+# @param Guid: the Guid of the distribution\r
+# @param Version: the Version of distribution\r
+# @param StoredDistFile: the distribution file that backed up\r
+# @param DataBase: the internal database\r
+# @param WorkspaceDir: work space directory\r
+# @param ForceRemove: whether user want to remove file even it is modified\r
+#\r
+def RemoveDist(Guid, Version, StoredDistFile, DataBase, WorkspaceDir, ForceRemove):\r
+ #\r
+ # Get Current File List\r
+ #\r
+ NewFileList = GetCurrentFileList(DataBase, Guid, Version, WorkspaceDir)\r
+\r
+ #\r
+ # Remove all files\r
+ #\r
+ MissingFileList = []\r
+ for (Path, Md5Sum) in DataBase.GetDpFileList(Guid, Version):\r
+ if os.path.isfile(Path):\r
+ if Path in NewFileList:\r
+ NewFileList.remove(Path)\r
+ if not ForceRemove:\r
+ #\r
+ # check whether modified by users\r
+ #\r
+ Md5Signature = md5(open(str(Path), 'rb').read())\r
+ if Md5Sum != Md5Signature.hexdigest():\r
+ Logger.Info(ST.MSG_CONFIRM_REMOVE2 % Path)\r
+ Input = stdin.readline()\r
+ Input = Input.replace('\r', '').replace('\n', '')\r
+ if Input.upper() != 'Y':\r
+ continue\r
+ RemovePath(Path)\r
+ else:\r
+ MissingFileList.append(Path)\r
+\r
+ for Path in NewFileList:\r
+ if os.path.isfile(Path):\r
+ if (not ForceRemove) and (not os.path.split(Path)[1].startswith('.')):\r
+ Logger.Info(ST.MSG_CONFIRM_REMOVE3 % Path)\r
+ Input = stdin.readline()\r
+ Input = Input.replace('\r', '').replace('\n', '')\r
+ if Input.upper() != 'Y':\r
+ continue\r
+ RemovePath(Path)\r
+\r
+ #\r
+ # Remove distribution files in /Conf/.upt\r
+ #\r
+ if StoredDistFile is not None:\r
+ os.remove(StoredDistFile)\r
+\r
+ #\r
+ # update database\r
+ #\r
+ Logger.Quiet(ST.MSG_UPDATE_PACKAGE_DATABASE)\r
+ DataBase.RemoveDpObj(Guid, Version)\r