#!/bin/bash
readonly PACKAGE_SHORT_NAME="pyACL"
upgrade_flag=n
install_path="$(dirname $(
cd "$(dirname "$0")"
pwd
))"
python_path="$(
cd "$(dirname "$0")/../../../../python"
pwd
)"
PACKAGE_ARCH=$(arch)
lib_path="$(
cd "$(dirname "$0")/../../../../${PACKAGE_ARCH}-linux/lib64"
pwd
)"
config_file_path="${install_path}/ascend-${PACKAGE_SHORT_NAME}_install.info"
version_file_path="${install_path}/version.info"
scene_file_path="${install_path}/scene.info"
function print() {
echo "[pyACL] [$(date +"%Y-%m-%d %H:%M:%S")] [$1]: $2"
}
function rm_file_safe() {
local file_path=$1
if [ -n "${file_path}" ]; then
if [ -f "${file_path}" ] || [ -h "${file_path}" ]; then
rm -f "${file_path}"
print "INFO" "delete file ${file_path} successfully"
else
print "WARNING" "the file ${file_path} is not exist"
fi
else
print "WARNING" "the file ${file_path} path is NULL"
fi
}
function rm_dir_safe() {
local dir_path=$1
if [ -n "${dir_path}" ] && [[ ! "${dir_path}" =~ ^/+$ ]]; then
if [ -d "${dir_path}" ]; then
rm -rf "${dir_path}"
print "INFO" "delete directory ${dir_path} successfully"
else
print "WARNING" "the directory ${dir_path} is not exist"
fi
else
print "WARNING" "the directory ${dir_path} path is NULL"
fi
}
function delete_empty_folder() {
if [ -d "${1}" ]; then
if [ ! "$(ls -A ${1})" ]; then
rm_dir_safe ${1}
fi
fi
}
chmod_to_modify() {
chmod 750 -R $install_path 2> /dev/null
chmod 750 $python_path 2> /dev/null
}
function __remove_uninstall_package() {
local uninstall_file=$1
if [ -f "${uninstall_file}" ]; then
sed -i "/uninstall_package \"share\/info\/pyACL\/script\"/d" "${uninstall_file}"
if [ $? -ne 0 ]; then
print "ERROR" "remove ${uninstall_file} uninstall_package command failed!"
exit 1
fi
fi
num=$(grep "^uninstall_package " ${uninstall_file} | wc -l)
if [ ${num} -eq 0 ]; then
rm -f "${uninstall_file}" > /dev/null 2>&1
if [ $? -ne 0 ]; then
print "ERROR" "delete file: ${uninstall_file}failed, please delete it by yourself."
fi
fi
}
function unregist_uninstall() {
if [ -f "${totals_version_path}/cann_uninstall.sh" ]; then
chmod u+w ${totals_version_path}/cann_uninstall.sh
__remove_uninstall_package "${totals_version_path}/cann_uninstall.sh"
if [ -f "${totals_version_path}/cann_uninstall.sh" ]; then
chmod u-w ${totals_version_path}/cann_uninstall.sh
fi
fi
}
function deal_python_dir() {
delete_empty_folder "$python_path/site-packages/acl/"
delete_empty_folder "$python_path/site-packages"
delete_empty_folder "$python_path"
}
function deal_install_dir() {
rm_file_safe ${install_path}/script/uninstall.sh
delete_empty_folder "${install_path}/script/"
delete_empty_folder "${install_path}"
delete_empty_folder "${install_path%/*}"
delete_empty_folder "${install_path%/*/*}"
}
function deal_uninstall() {
rm_file_safe ${config_file_path}
rm_file_safe ${version_file_path}
rm_file_safe ${scene_file_path}
rm_file_safe "$python_path/site-packages/acl/acl.so"
rm_file_safe "$python_path/site-packages/acl.so"
deal_python_dir
deal_install_dir
totals_version_path=${install_path%/*/*/*}
unregist_uninstall
delete_empty_folder "${totals_version_path}"
}
function deal_jemalloc_uninstall() {
local change_flag=n
if [ -f "${lib_path}/libjemalloc.so" ]; then
if [ ! -w "$lib_path" ];then
chmod u+w $lib_path
change_flag=y
fi
rm_file_safe "${lib_path}/libjemalloc.so"
if [ "$change_flag" = "y" ];then
chmod u-w $lib_path
fi
fi
}
function main() {
upgrade_flag=$1
chmod_to_modify
deal_jemalloc_uninstall
deal_uninstall
}
main $*