#!/bin/bash
target_install_path=/usr/local/Ascend/cann/opp
source_path=$PWD
INSTALL="y"
UNINSTALL="n"
QUIET="n"
while true
do
case $1 in
--quiet)
QUIET="y"
shift
;;
--install)
INSTALL="y"
shift
;;
--uninstall)
UNINSTALL="y"
shift
;;
--install-path=*)
INSTALL_PATH=$(echo $1 | cut -d"=" -f2-)
INSTALL_PATH=${INSTALL_PATH%*/}
shift
;;
--*)
shift
;;
*)
break
;;
esac
done
log() {
cur_date=`date +"%Y-%m-%d %H:%M:%S"`
echo "[hccl_custom_ops] [$cur_date] "$1
}
if [ -n "${INSTALL_PATH}" ]; then
if [[ ! "${INSTALL_PATH}" = /* ]]; then
log "[ERROR] use absolute path for --install-path argument"
exit 1
fi
if [ ! -d ${INSTALL_PATH} ]; then
mkdir -p ${INSTALL_PATH} >> /dev/null 2>&1
if [ $? -ne 0 ]; then
log "[ERROR] create ${INSTALL_PATH} failed"
exit 1
fi
fi
INSTALL_PATH=${INSTALL_PATH%*/}
if [[ "${INSTALL_PATH}" == */cann ]] || [[ "${INSTALL_PATH}" == */cann-* ]]; then
target_install_path="${INSTALL_PATH}"
else
target_install_path="${INSTALL_PATH}/cann"
fi
elif [ -n "${ASCEND_CUSTOM_OPP_PATH}" ]; then
if [ ! -d ${ASCEND_CUSTOM_OPP_PATH} ]; then
mkdir -p ${ASCEND_CUSTOM_OPP_PATH} >> /dev/null 2>&1
if [ $? -ne 0 ]; then
log "[ERROR] create ${ASCEND_CUSTOM_OPP_PATH} failed"
exit 1
fi
fi
target_install_path=${ASCEND_CUSTOM_OPP_PATH}
else
if [ "x${ASCEND_OPP_PATH}" == "x" ]; then
log "[ERROR] env ASCEND_OPP_PATH no exist"
exit 1
fi
target_install_path=$(dirname "${ASCEND_OPP_PATH}")
fi
if [ ! -d $target_install_path ];then
log "[ERROR] $target_install_path no exist"
exit 1
fi
if [ ! -x $target_install_path ] || [ ! -w $target_install_path ] || [ ! -r $target_install_path ];then
log "[WARNING] The directory $target_install_path does not have sufficient permissions. \
Please check and modify the folder permissions (e.g., using chmod), \
or use the --install-path option to specify an installation path and \
change the environment variable ASCEND_CUSTOM_OPP_PATH to the specified path."
fi
uninstall() {
log "[INFO] Uninstalling existing files..."
if [ ! -d "$source_path" ]; then
log "[ERROR] Source directory does not exist"
exit 1
fi
while IFS= read -r -d '' item; do
rel_path="${item#$source_path/}"
target_item="$target_install_path/$rel_path"
if [ -f "$target_item" ]; then
rm -f "$target_item" && log "[Info] Removed file: ${target_item}"
elif [ -d "$target_item" ] && [ -z "$(ls -A "$target_item" 2>/dev/null)" ]; then
rmdir "$target_item" 2>/dev/null && log "[Info] Removed empty directory: ${target_item}"
fi
done < <(find "$source_path" -depth -print0)
log "[INFO] Uninstalled successfully"
}
install() {
log "[INFO] Installing files..."
log "[INFO] Installation directory: ${target_install_path}"
if [ ! -d "$source_path" ]; then
log "[ERROR] Source directory does not exist"
exit 1
fi
conflict_files=()
while IFS= read -r -d '' file; do
rel_path="${file#$source_path/}"
if [ -e "$target_install_path/$rel_path" ]; then
conflict_files+=("$rel_path")
fi
done < <(find "$source_path" -type f -print0)
if [ ${#conflict_files[@]} -gt 0 ] && [ "$QUIET" != "y" ]; then
echo "Files already exist in the target directory. Do you want to uninstall them first? (y/N):"
read -r user_input
if [ "$user_input" = "y" ] || [ "$user_input" = "Y" ]; then
uninstall
else
log "[INFO] Installation cancelled by user"
exit 0
fi
elif [ ${#conflict_files[@]} -gt 0 ] && [ "$QUIET" = "y" ]; then
uninstall
fi
mkdir -p "$target_install_path"
cp -rf "$source_path"/* "$target_install_path"/ 2>/dev/null
if [ $? -ne 0 ]; then
log "[ERROR] Failed to copy files"
exit 1
fi
log "[INFO] Installation successfully"
}
main() {
if [ "$UNINSTALL" = "y" ]; then
uninstall
exit 0
fi
if [ "$INSTALL" = "y" ]; then
install
exit 0
fi
log "[ERROR] No operation specified. Use --install or --uninstall"
exit 1
}
main
exit 0