#!/bin/bash
USERNAME=$(id -un)
USERGROUP=$(id -gn)
COMMON_SHELL_PATH="common.sh"
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
info() {
echo -e "${GREEN}[INFO]${NC} $1"
}
warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
error() {
echo -e "${RED}[ERROR]${NC} $1"
}
create_install_dir() {
info "Creating install directory: $INSTALL_DIR"
if ! mkdir -p -m 777 "$INSTALL_DIR"; then
error "Failed to create directory: $INSTALL_DIR"
error "Please check if you have sufficient permissions, or create the directory manually"
exit 1
fi
chmod -R 777 "$INSTALL_DIR"
info "Set directory permissions to 777: $INSTALL_DIR"
if [ $? -ne 0 ]; then
warn "Failed to set directory permissions to 777: $INSTALL_DIR"
fi
}
function convert_install_path() {
local _install_path="$1"
_install_path=`echo "${_install_path}" | sed -r "s/((\/)|(\/\.))*$//g" | sed -r "s|/\./|/|g"`
if [ -z "${_install_path}" ]; then
_install_path="/"
fi
if [[ "${_install_path}" =~ ^\./.* ]] || [[ "${_install_path}" =~ ^\.$ ]]; then
_install_path=`echo "${_install_path}" | sed -r "s|^\./||" | sed -r "s|^\.$||"`
if [ -z "${_install_path}" ]; then
_install_path="${run_path}"
else
_install_path="${run_path}/${_install_path}"
fi
else
local _prefix=`echo "${_install_path}" | cut -d"/" -f1`
if [ ! -z "${_prefix}" ] && [ "~" != "${_prefix}" ]; then
_install_path="${run_path}/${_install_path}"
fi
fi
local _suffix_path=`echo "${_install_path}" | cut -d"~" -f2`
if [ "${_suffix_path}" != "${_install_path}" ]; then
local _home_path=`eval echo "~" | sed "s/\/*$//g"`
_install_path="${_home_path}${_suffix_path}"
fi
_install_path=`echo "${_install_path}" | sed -r "s|//+|/|g"`
if command -v realpath >/dev/null 2>&1; then
_install_path=$(realpath -m "${_install_path}" 2>/dev/null || echo "${_install_path}")
fi
echo "${_install_path}"
}
main_install() {
INSTALL_DIR=$(get_install_path)
if [[ "$SILENT_MODE" != "true" ]]; then
echo "========================================="
echo " mindstudio-accucmp Installer"
echo "========================================="
echo "Install directory: $INSTALL_DIR"
echo ""
fi
create_install_dir
info "Installing files..."
if [[ "$SUDO_USED" == "true" ]]; then
sudo cp -r "$PWD"/msaccucmp/* "$INSTALL_DIR"/
else
cp -r "$PWD"/msaccucmp/* "$INSTALL_DIR"/
fi
chmod -R 750 "$INSTALL_DIR"
if [ $? -ne 0 ]; then
warn "Failed to set directory permissions to 750: $INSTALL_DIR"
fi
info "Set directory permissions to 750: $INSTALL_DIR"
info "Installation completed!"
info "Install directory: $INSTALL_DIR"
register_uninstall
}
function get_install_path() {
if [ -z "${input_install_path}" ]; then
local _install_path
_install_path="/usr/local/Ascend/tools/operator_cmp/compare"
else
_install_path="${input_install_path}/tools/operator_cmp/compare"
fi
install_path=$(convert_install_path "${_install_path}")
echo "${install_path}"
}
register_uninstall() {
info "Registering uninstall script..."
local _install_path="$INSTALL_DIR"
local _info_dir="${_install_path}/../../../share/info/operator_cmp"
local _script_dir="${SHELL_DIR}"
local _uninstall_source="${_script_dir}/uninstall.sh"
local _cann_uninstall="${_install_path}/../../../cann_uninstall.sh"
local _version_source="${_script_dir}/version.info"
if [ -f "${_cann_uninstall}" ] && ! grep -q "uninstall_package \"share/info/operator_cmp\"" "${_cann_uninstall}"; then
sed -i "/^exit /i uninstall_package \"share/info/operator_cmp\"" "${_cann_uninstall}"
fi
local _parent_dir=$(dirname "${_info_dir}")
info "Checking parent directory: ${_parent_dir}"
if [ ! -d "${_parent_dir}" ]; then
error "Parent directory does not exist: ${_parent_dir}"
error "Please ensure the installation directory structure is correct"
return 1
fi
if [ ! -w "${_parent_dir}" ]; then
error "No write permission to parent directory: ${_parent_dir}"
error "Current user: $(whoami)"
error "Please run with appropriate permissions"
return 1
fi
info "Creating directory: ${_info_dir}"
mkdir -p "${_info_dir}" 2>&1
local _mkdir_result=$?
if [ ${_mkdir_result} -ne 0 ]; then
error "Failed to create directory: ${_info_dir}"
error "Parent directory: ${_parent_dir}"
error "Parent directory writable: $([ -w "${_parent_dir}" ] && echo "yes" || echo "no")"
error "mkdir exit code: ${_mkdir_result}"
return 1
fi
if [ ! -d "${_info_dir}" ]; then
error "Directory was not created: ${_info_dir}"
return 1
fi
if [ ! -w "${_info_dir}" ]; then
error "Directory exists but is not writable: ${_info_dir}"
return 1
fi
if [ -f "${_version_source}" ]; then
info "Copying version info from ${_version_source} to ${_info_dir}/version.info"
cp "${_version_source}" "${_info_dir}/version.info" 2>&1
local _cp_result=$?
if [ ${_cp_result} -ne 0 ]; then
error "Failed to copy version info"
error "Source: ${_version_source}"
error "Destination: ${_info_dir}/version.info"
error "Destination directory writable: $([ -w "${_info_dir}" ] && echo "yes" || echo "no")"
error "cp exit code: ${_cp_result}"
return 1
fi
info "Version info registered successfully"
else
warn "Version info not found: ${_version_source}"
warn "Script directory: ${_script_dir}"
warn "Current working directory: $(pwd)"
warn "Script path (\$0): $0"
return 1
fi
if [ -f "${_uninstall_source}" ]; then
info "Copying uninstall script from ${_uninstall_source} to ${_info_dir}/uninstall.sh"
cp "${_uninstall_source}" "${_info_dir}/uninstall.sh" 2>&1
local _cp_result=$?
if [ ${_cp_result} -ne 0 ]; then
error "Failed to copy uninstall script"
error "Source: ${_uninstall_source}"
error "Destination: ${_info_dir}/uninstall.sh"
error "Destination directory writable: $([ -w "${_info_dir}" ] && echo "yes" || echo "no")"
error "cp exit code: ${_cp_result}"
return 1
fi
chmod +x "${_info_dir}/uninstall.sh" 2>/dev/null || true
info "Uninstall script registered successfully"
else
warn "Uninstall script not found: ${_uninstall_source}"
return 1
fi
return 0
}
main_uninstall() {
OPERATOR_CMP_DIR=$(get_install_path)
local _install_path="$INSTALL_DIR"
local _cann_uninstall="${_install_path}/../../../cann_uninstall.sh"
if [[ "$SILENT_MODE" != "true" ]]; then
echo "========================================="
echo " mindstudio-accucmp Uninstaller"
echo "========================================="
echo "Target directory: $OPERATOR_CMP_DIR"
echo ""
fi
if [[ ! -d "$OPERATOR_CMP_DIR" ]]; then
warn "Directory does not exist: $OPERATOR_CMP_DIR"
warn "Nothing to uninstall"
return 0
fi
if [[ ! -w "$(dirname "$OPERATOR_CMP_DIR")" ]]; then
error "No permission to delete $OPERATOR_CMP_DIR"
error "Current user: $(whoami)"
error "Please run uninstall with a user that has permissions"
error "or manually delete: rm -rf $OPERATOR_CMP_DIR"
exit 1
fi
info "Uninstalling mindstudio-accucmp..."
info "Removing directory: $OPERATOR_CMP_DIR"
if rm -rf "$OPERATOR_CMP_DIR"; then
info "mindstudio-accucmp has been successfully removed"
return 0
else
error "Deletion failed, please check permissions"
exit 1
fi
if [ -f "${_cann_uninstall}" ]; then
sed -i "/uninstall_package \"share\/info\/operator_cmp\"/d" "${_cann_uninstall}"
fi
}
main() {
if [[ "$uninstall_flag" == "y" ]]; then
main_uninstall
else
main_install
fi
exit 0
}
source ${COMMON_SHELL_PATH}
install_file=""
input_install_path=""
install_for_all=n
install_flag=n
uninstall_flag=n
upgrade_flag=n
quiet_flag=n
version_flag=n
check_flag=n
pylocal=y
force_flag=n
run_path=`echo "$2" | cut -d"-" -f3-`
if [ -z "${run_path}" ]; then
run_path=`pwd`
else
run_path=`echo "${run_path}" | sed "s/((\/)|(\/\.))*$//g"`
[ -z "${run_path}" ] && run_path="/"
if [ ! -d "${run_path}" ]; then
log_and_print $LEVEL_ERROR "Run package path is invalid: $run_path"
exit 1
fi
if [[ ! "${run_path}" =~ ^/ ]]; then
run_path="$(cd "${run_path}" && pwd)"
fi
fi
shift 2
start_log
for arg in "$@"; do
case "$arg" in
--force)
force_flag=y
;;
--check)
check_flag=y
;;
--quiet)
quiet_flag=y
;;
--install-path=*)
input_install_path=`echo "$arg" | cut -d"=" -f2`
;;
--install-for-all)
install_for_all=y
;;
--full)
install_flag=y
;;
--install)
install_flag=y
;;
--run)
install_flag=y
;;
--devel)
install_flag=y
;;
--uninstall)
uninstall_flag=y
;;
--upgrade)
upgrade_flag=y
;;
--version)
version_flag=y
;;
-*)
echo "Unsupported parameters: $arg"
;;
*)
if [ ! -z "$arg" ]; then
echo "Unsupported parameters: $arg"
fi
break
;;
esac
done
main