已合并
Use FetchContent to reuse CANN engineering common cmake repo #988
shengxinchen创建于 5月26日
Use FetchContent to reuse CANN engineering common cmake repo #988
已合并
shengxinchen创建于 5月26日
46 个文件变更+86-13832
MCMakeLists.txt+8-1
@@ -9,8 +9,15 @@
9# --------------------------------------------------------------------------------9# --------------------------------------------------------------------------------
10 10 
11cmake_minimum_required(VERSION 3.10)11cmake_minimum_required(VERSION 3.10)
12+include(cmake/fetch_cann_cmake.cmake)
13+project(pto-isa)
14+ 
15+init_cann_project()
16+add_cann_target_options()
17+ 
12include(cmake/package.cmake)18include(cmake/package.cmake)
13include(cmake/func.cmake)19include(cmake/func.cmake)
14include(version.cmake)20include(version.cmake)
15-add_version_info_targets()21+check_cann_pkg_build_deps("pto-isa")
22+add_cann_version_info_targets()
16pack_built_in()23pack_built_in()
MSECURITY.md+1-1
@@ -24,7 +24,7 @@ This repository contains references to the following public URLs:
24 24 
25| Type | Open-source URL | File | Public URL | Purpose |25| Type | Open-source URL | File | Public URL | Purpose |
26| :--------: | :-------------: | --------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------- |26| :--------: | :-------------: | --------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------- |
27-| Dependency | N/A | cmake/third_party/makeself-fetch.cmake | https://gitcode.com/cann-src-third-party/makeself/releases/download/release-2.5.0-patch1.0/makeself-release-2.5.0-patch1.tar.gz | Downloads `makeself` source as a build dep |27+| Dependency | N/A | cmake/fetch_cann_cmake.cmake | https://gitcode.com/cann/cmake.git | Pulls the shared CANN engineering cmake repo |
28| Dependency | N/A | cmake/third_party/json.cmake | https://gitcode.com/cann-src-third-party/json/releases/download/v3.11.3/include.zip | Downloads `json` headers as a build dep |28| Dependency | N/A | cmake/third_party/json.cmake | https://gitcode.com/cann-src-third-party/json/releases/download/v3.11.3/include.zip | Downloads `json` headers as a build dep |
29| Dependency | N/A | cmake/third_party/gtest.cmake | https://gitcode.com/cann-src-third-party/googletest/releases/download/v1.14.0/googletest-1.14.0.tar.gz | Downloads GoogleTest as a build/test dep |29| Dependency | N/A | cmake/third_party/gtest.cmake | https://gitcode.com/cann-src-third-party/googletest/releases/download/v1.14.0/googletest-1.14.0.tar.gz | Downloads GoogleTest as a build/test dep |
30 30 
MSECURITY_zh.md+1-1
@@ -24,7 +24,7 @@
24 24 
25| 类型 | 开源 URL | 文件 | 公网 URL | 目的 |25| 类型 | 开源 URL | 文件 | 公网 URL | 目的 |
26| :--: | :--: | --- | --- | --- |26| :--: | :--: | --- | --- | --- |
27-| 依赖 | N/A | cmake/third_party/makeself-fetch.cmake | https://gitcode.com/cann-src-third-party/makeself/releases/download/release-2.5.0-patch1.0/makeself-release-2.5.0-patch1.tar.gz | 下载 `makeself` 源码作为构建依赖 |27+| 依赖 | N/A | cmake/fetch_cann_cmake.cmake | https://gitcode.com/cann/cmake.git | 拉取 CANN 工程公共 cmake 仓 |
28| 依赖 | N/A | cmake/third_party/json.cmake | https://gitcode.com/cann-src-third-party/json/releases/download/v3.11.3/include.zip | 下载 `json` 头文件作为构建依赖 |28| 依赖 | N/A | cmake/third_party/json.cmake | https://gitcode.com/cann-src-third-party/json/releases/download/v3.11.3/include.zip | 下载 `json` 头文件作为构建依赖 |
29| 依赖 | N/A | cmake/third_party/gtest.cmake | https://gitcode.com/cann-src-third-party/googletest/releases/download/v1.14.0/googletest-1.14.0.tar.gz | 下载 GoogleTest 作为构建/测试依赖 |29| 依赖 | N/A | cmake/third_party/gtest.cmake | https://gitcode.com/cann-src-third-party/googletest/releases/download/v1.14.0/googletest-1.14.0.tar.gz | 下载 GoogleTest 作为构建/测试依赖 |
30 30 
Mcmake/README.md+4-3
@@ -5,13 +5,14 @@ This directory contains CMake helper modules used by the repository, mainly for
5## What’s Inside5## What’s Inside
6 6 
7- Packaging-related CMake logic (the `package` target)7- Packaging-related CMake logic (the `package` target)
8-- Built-in `makeself` installer generation8+- FetchContent integration that pulls the shared CANN engineering cmake repo
9- Third-party dependency download/integration scripts under `cmake/third_party/`9- Third-party dependency download/integration scripts under `cmake/third_party/`
10 10 
11## Key Files11## Key Files
12 12 
13-- `cmake/package.cmake`: Packaging entry functions included by the top-level `CMakeLists.txt`13+- `cmake/package.cmake`: Packaging entry functions included by the top-level `CMakeLists.txt`; delegates the cpack configuration to `set_cann_cpack_config` provided by the engineering repo
14-- `cmake/makeself_built_in.cmake`: Built-in `makeself` packaging logic14+- `cmake/fetch_cann_cmake.cmake`: Fetches `https://gitcode.com/cann/cmake` and exposes its common functions (`init_cann_project`, `set_cann_cpack_config`, the bundled `makeself`/install scripts, etc.)
15+- `cmake/func.cmake`: Project-local helpers (protobuf, signing, packing)
15- `cmake/third_party/`: Third-party dependency helper scripts16- `cmake/third_party/`: Third-party dependency helper scripts
16 17 
17## Entry Points18## Entry Points
Mcmake/README_zh.md+4-3
@@ -5,13 +5,14 @@
5## 目录内容5## 目录内容
6 6 
7- 与打包相关的 CMake 逻辑(`package` 目标)7- 与打包相关的 CMake 逻辑(`package` 目标)
8-- 内置的 `makeself` 安装包生成8+- 通过 FetchContent 复用 CANN 工程公共 cmake 仓
9- `cmake/third_party/` 下的第三方依赖下载/集成脚本9- `cmake/third_party/` 下的第三方依赖下载/集成脚本
10 10 
11## 关键文件11## 关键文件
12 12 
13-- `cmake/package.cmake`:由顶层 `CMakeLists.txt` 引入的打包入口函数13+- `cmake/package.cmake`:由顶层 `CMakeLists.txt` 引入的打包入口函数,调用工程仓提供的 `set_cann_cpack_config` 完成 cpack 配置
14-- `cmake/makeself_built_in.cmake`:内置 `makeself` 打包逻辑14+- `cmake/fetch_cann_cmake.cmake`:拉取 `https://gitcode.com/cann/cmake`,并暴露公共函数(`init_cann_project`、`set_cann_cpack_config`、内置 `makeself` 及 install 脚本等)
15+- `cmake/func.cmake`:项目内本地辅助函数(protobuf、签名、打包)
15- `cmake/third_party/`:第三方依赖辅助脚本16- `cmake/third_party/`:第三方依赖辅助脚本
16 17 
17## 入口18## 入口
Rscripts/package/common/sh/common_interface.fishcmake/fetch_cann_cmake.cmake+29-26
@@ -1,33 +1,36 @@
1-#!/usr/bin/env fish1+# -----------------------------------------------------------------------------------------------------------
2-# --------------------------------------------------------------------------------2+# Copyright (c) 2026 Huawei Technologies Co., Ltd.
3-# Copyright (c) 2025 Huawei Technologies Co., Ltd.
4# This program is free software, you can redistribute it and/or modify it under the terms and conditions of3# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
5# CANN Open Software License Agreement Version 2.0 (the "License").4# CANN Open Software License Agreement Version 2.0 (the "License").
6# Please refer to the License for details. You may not use this file except in compliance with the License.5# Please refer to the License for details. You may not use this file except in compliance with the License.
7# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,6# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
8# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.7# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
9# See LICENSE in the root of the software repository for the full text of the License.8# See LICENSE in the root of the software repository for the full text of the License.
10-# --------------------------------------------------------------------------------9+# -----------------------------------------------------------------------------------------------------------
11 10 
12-function mk_custom_path11+if(NOT PROJECT_SOURCE_DIR)
13- set -l custom_file_path $argv[1]12+ set(CANN_CMAKE_TAG "master-017")
14- if test (id -u) -eq 013+ if(CANN_3RD_LIB_PATH AND IS_DIRECTORY "${CANN_3RD_LIB_PATH}/cann-cmake")
15- return 014+ include("${CANN_3RD_LIB_PATH}/cann-cmake/function/prepare.cmake")
16- end15+ else()
17- while read line16+ include(FetchContent)
18- set -l _custom_path (echo "$line" | cut --only-delimited -d= -f2)17+ if(CANN_3RD_LIB_PATH AND EXISTS "${CANN_3RD_LIB_PATH}/cmake-${CANN_CMAKE_TAG}.tar.gz")
19- if test -z $_custom_path18+ FetchContent_Declare(
20- continue19+ cann-cmake
21- end20+ URL "${CANN_3RD_LIB_PATH}/cmake-${CANN_CMAKE_TAG}.tar.gz"
22- set -l _custom_path (eval echo "$_custom_path")21+ )
23- if not test -d $_custom_path22+ else()
24- mkdir -p "$_custom_path"23+ FetchContent_Declare(
25- if not test $status -eq 024+ cann-cmake
26- set -l cur_date (date +"%Y-%m-%d %H:%M:%S")25+ GIT_REPOSITORY https://gitcode.com/cann/cmake.git
27- echo "[Common] [$cur_date] [ERROR]: create $_custom_path failed."26+ GIT_TAG ${CANN_CMAKE_TAG}
28- return 127+ GIT_SHALLOW TRUE
29- end28+ )
30- end29+ endif()
31- end < $custom_file_path30+ FetchContent_GetProperties(cann-cmake)
32- return 031+ if(NOT cann-cmake_POPULATED)
33-end32+ FetchContent_Populate(cann-cmake)
33+ endif()
34+ include("${cann-cmake_SOURCE_DIR}/function/prepare.cmake")
35+ endif()
36+endif()
Mcmake/func.cmake+0-81
@@ -376,84 +376,3 @@ function(sign_file)
376 set(${ARG_RESULT_VAR} "${output_sig}" PARENT_SCOPE)376 set(${ARG_RESULT_VAR} "${output_sig}" PARENT_SCOPE)
377 endif()377 endif()
378endfunction()378endfunction()
379- 
380-macro(replace_cur_major_minor_ver)
381- string(REPLACE CUR_MAJOR_MINOR_VER "${CANN_VERSION_${CANN_VERSION_CURRENT_PACKAGE}_VERSION_MAJOR_MINOR}" depend "${depend}")
382-endmacro()
383- 
384-# 设置包和版本号
385-function(set_package name)
386- cmake_parse_arguments(VERSION "" "VERSION" "" ${ARGN})
387- set(VERSION "${VERSION_VERSION}")
388- if(NOT name)
389- message(FATAL_ERROR "The name parameter is not set in set_package.")
390- endif()
391- if(NOT VERSION)
392- message(FATAL_ERROR "The VERSION parameter is not set in set_package(${name}).")
393- endif()
394- string(REGEX MATCH "^([0-9]+\\.[0-9]+)" VERSION_MAJOR_MINOR "${VERSION}")
395- list(APPEND CANN_VERSION_PACKAGES "${name}")
396- set(CANN_VERSION_PACKAGES "${CANN_VERSION_PACKAGES}" PARENT_SCOPE)
397- set(CANN_VERSION_CURRENT_PACKAGE "${name}" PARENT_SCOPE)
398- set(CANN_VERSION_${name}_VERSION "${VERSION}" PARENT_SCOPE)
399- set(CANN_VERSION_${name}_VERSION_MAJOR_MINOR "${VERSION_MAJOR_MINOR}" PARENT_SCOPE)
400- set(CANN_VERSION_${name}_BUILD_DEPS PARENT_SCOPE)
401- set(CANN_VERSION_${name}_RUN_DEPS PARENT_SCOPE)
402-endfunction()
403- 
404-# 设置构建依赖
405-function(set_build_dependencies pkg_name depend)
406- if(NOT CANN_VERSION_CURRENT_PACKAGE)
407- message(FATAL_ERROR "The set_package must be invoked first.")
408- endif()
409- if(NOT pkg_name)
410- message(FATAL_ERROR "The pkg_name parameter is not set in set_build_dependencies.")
411- endif()
412- if(NOT depend)
413- message(FATAL_ERROR "The depend parameter is not set in set_build_dependencies.")
414- endif()
415- replace_cur_major_minor_ver()
416- list(APPEND CANN_VERSION_${CANN_VERSION_CURRENT_PACKAGE}_BUILD_DEPS "${pkg_name}" "${depend}")
417- set(CANN_VERSION_${CANN_VERSION_CURRENT_PACKAGE}_BUILD_DEPS "${CANN_VERSION_${CANN_VERSION_CURRENT_PACKAGE}_BUILD_DEPS}" PARENT_SCOPE)
418-endfunction()
419- 
420-# 设置运行依赖
421-function(set_run_dependencies pkg_name depend)
422- if(NOT CANN_VERSION_CURRENT_PACKAGE)
423- message(FATAL_ERROR "The set_package must be invoked first.")
424- endif()
425- if(NOT pkg_name)
426- message(FATAL_ERROR "The pkg_name parameter is not set in set_run_dependencies.")
427- endif()
428- if(NOT depend)
429- message(FATAL_ERROR "The depend parameter is not set in set_run_dependencies.")
430- endif()
431- replace_cur_major_minor_ver()
432- list(APPEND CANN_VERSION_${CANN_VERSION_CURRENT_PACKAGE}_RUN_DEPS "${pkg_name}" "${depend}")
433- set(CANN_VERSION_${CANN_VERSION_CURRENT_PACKAGE}_RUN_DEPS "${CANN_VERSION_${CANN_VERSION_CURRENT_PACKAGE}_RUN_DEPS}" PARENT_SCOPE)
434-endfunction()
435- 
436-# 检查构建依赖
437-function(check_pkg_build_deps pkg_name)
438- execute_process(
439- COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/scripts/check_build_dependencies.py "${ASCEND_INSTALL_PATH}" ${CANN_VERSION_${pkg_name}_BUILD_DEPS}
440- RESULT_VARIABLE result
441- )
442- if(result)
443- message(FATAL_ERROR "Check ${pkg_name} build dependencies failed!")
444- endif()
445-endfunction()
446- 
447-# 添加生成version.info的目标
448-# 目标名格式为:version_${包名}_info
449-function(add_version_info_targets)
450- foreach(pkg_name ${CANN_VERSION_PACKAGES})
451- add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/version.${pkg_name}.info
452- COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/scripts/generate_version_info.py --output ${CMAKE_BINARY_DIR}/version.${pkg_name}.info
453- "${CANN_VERSION_${pkg_name}_VERSION}" ${CANN_VERSION_${pkg_name}_RUN_DEPS}
454- DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/version.cmake ${CMAKE_CURRENT_SOURCE_DIR}/scripts/generate_version_info.py
455- VERBATIM
456- )
457- add_custom_target(version_${pkg_name}_info ALL DEPENDS ${CMAKE_BINARY_DIR}/version.${pkg_name}.info)
458- endforeach()
459-endfunction()
Dcmake/makeself_built_in.cmake+0-119
@@ -1,119 +0,0 @@
1-# ----------------------------------------------------------------------------
2-# Copyright (c) 2025 Huawei Technologies Co., Ltd.
3-# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4-# CANN Open Software License Agreement Version 2.0 (the "License").
5-# Please refer to the License for details. You may not use this file except in compliance with the License.
6-# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7-# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8-# See LICENSE in the root of the software repository for the full text of the License.
9-# ----------------------------------------------------------------------------
10-# makeself.cmake - 自定义 makeself 打包脚本
11- 
12-# 设置 makeself 路径
13-set(MAKESELF_EXE ${CPACK_MAKESELF_PATH}/makeself.sh)
14-set(MAKESELF_HEADER_EXE ${CPACK_MAKESELF_PATH}/makeself-header.sh)
15-if(NOT MAKESELF_EXE)
16- message(FATAL_ERROR "makeself not found! Install it with: sudo apt install makeself")
17-endif()
18- 
19-# 创建临时安装目录
20-set(STAGING_DIR "${CPACK_CMAKE_BINARY_DIR}/_CPack_Packages/makeself_staging")
21-file(MAKE_DIRECTORY "${STAGING_DIR}")
22- 
23-# 执行安装到临时目录
24-execute_process(
25- COMMAND "${CMAKE_COMMAND}" --install "${CPACK_CMAKE_BINARY_DIR}" --prefix "${STAGING_DIR}"
26- RESULT_VARIABLE INSTALL_RESULT
27-)
28- 
29-if(NOT INSTALL_RESULT EQUAL 0)
30- message(FATAL_ERROR "Installation to staging directory failed: ${INSTALL_RESULT}")
31-endif()
32- 
33-# 生成安装配置文件
34-set(CSV_OUTPUT ${CPACK_CMAKE_BINARY_DIR}/filelist.csv)
35-execute_process(
36- COMMAND python3 ${CPACK_CMAKE_SOURCE_DIR}/scripts/package/package.py --pkg_name pto_isa --chip_name ${CPACK_SOC} --os_arch linux-${CPACK_ARCH}
37- WORKING_DIRECTORY ${CPACK_CMAKE_BINARY_DIR}
38- OUTPUT_VARIABLE result
39- ERROR_VARIABLE error
40- RESULT_VARIABLE code
41- OUTPUT_STRIP_TRAILING_WHITESPACE
42-)
43-message(STATUS "package.py result: ${code}")
44-if (NOT code EQUAL 0)
45- message(FATAL_ERROR "Filelist generation failed: ${result}")
46-else ()
47- message(STATUS "Filelist generated successfully: ${result}")
48- 
49- if (NOT EXISTS ${CSV_OUTPUT})
50- message(FATAL_ERROR "Output file not created: ${CSV_OUTPUT}")
51- endif ()
52-endif ()
53-set(SCENE_OUT_PUT
54- ${CPACK_CMAKE_BINARY_DIR}/scene.info
55-)
56-set(NN_VERSION_OUT_PUT
57- ${CPACK_CMAKE_BINARY_DIR}/pto_isa_version.h
58-)
59- 
60-configure_file(
61- ${SCENE_OUT_PUT}
62- ${STAGING_DIR}/share/info/pto_isa/
63- COPYONLY
64-)
65-configure_file(
66- ${CSV_OUTPUT}
67- ${STAGING_DIR}/share/info/pto_isa/script/
68- COPYONLY
69-)
70-configure_file(
71- ${NN_VERSION_OUT_PUT}
72- ${STAGING_DIR}/${CMAKE_SYSTEM_PROCESSOR}-linux/include/version/pto_isa_version.h
73- COPYONLY
74-)
75- 
76-# 统一设置安装的文件权限为555
77-execute_process(
78- COMMAND find ${STAGING_DIR} -type f -exec chmod 555 {} \;
79- RESULT_VARIABLE CHMOD_RESULT
80-)
81-if(EXISTS "${STAGING_DIR}/${CMAKE_SYSTEM_PROCESSOR}-linux/include/version/pto_isa_version.h")
82- execute_process(COMMAND chmod 440 "${STAGING_DIR}/${CMAKE_SYSTEM_PROCESSOR}-linux/include/version/pto_isa_version.h")
83-endif()
84-if(EXISTS "${STAGING_DIR}/${CMAKE_SYSTEM_PROCESSOR}-linux/conf/path.cfg")
85- execute_process(COMMAND chmod 440 "${STAGING_DIR}/${CMAKE_SYSTEM_PROCESSOR}-linux/conf/path.cfg")
86-endif()
87- 
88-# makeself打包
89-file(STRINGS ${CPACK_CMAKE_BINARY_DIR}/makeself.txt script_output)
90-string(REPLACE " " ";" makeself_param_string "${script_output}")
91-string(REGEX MATCH "cann.*\\.run" package_name "${makeself_param_string}")
92- 
93-list(LENGTH makeself_param_string LIST_LENGTH)
94-math(EXPR INSERT_INDEX "${LIST_LENGTH} - 2")
95-list(INSERT makeself_param_string ${INSERT_INDEX} "${STAGING_DIR}")
96- 
97-message(STATUS "script output: ${script_output}")
98-message(STATUS "makeself: ${makeself_param_string}")
99-message(STATUS "package: ${package_name}")
100- 
101-execute_process(COMMAND bash ${MAKESELF_EXE}
102- --header ${MAKESELF_HEADER_EXE}
103- --help-header share/info/pto_isa/script/help.info
104- ${makeself_param_string} share/info/pto_isa/script/install.sh
105- WORKING_DIRECTORY ${STAGING_DIR}
106- RESULT_VARIABLE EXEC_RESULT
107- ERROR_VARIABLE EXEC_ERROR
108-)
109- 
110-if(NOT EXEC_RESULT EQUAL 0)
111- message(FATAL_ERROR "makeself packaging failed: ${EXEC_ERROR}")
112-endif()
113- 
114-execute_process(
115- COMMAND mkdir -p ${CPACK_PACKAGE_DIRECTORY}
116- COMMAND mv ${STAGING_DIR}/${package_name} ${CPACK_PACKAGE_DIRECTORY}/
117- COMMAND echo "Move ${STAGING_DIR}/${package_name} to ${CPACK_PACKAGE_DIRECTORY}/"
118- WORKING_DIRECTORY ${STAGING_DIR}
119-)
Mcmake/package.cmake+20-46
@@ -9,9 +9,6 @@
9# ----------------------------------------------------------------------------9# ----------------------------------------------------------------------------
10#### CPACK to package run #####10#### CPACK to package run #####
11 11 
12-# download makeself package
13-include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/third_party/makeself-fetch.cmake)
14- 
15function(pack_built_in)12function(pack_built_in)
16 #### built-in package ####13 #### built-in package ####
17 message(STATUS "System processor: ${CMAKE_SYSTEM_PROCESSOR}")14 message(STATUS "System processor: ${CMAKE_SYSTEM_PROCESSOR}")
@@ -39,38 +36,32 @@ function(pack_built_in)
39 )36 )
40 37 
41 set(SCRIPTS_FILES38 set(SCRIPTS_FILES
42- ${CMAKE_SOURCE_DIR}/scripts/package/common/sh/check_version_required.awk39+ ${CANN_CMAKE_DIR}/scripts/install/check_version_required.awk
43- ${CMAKE_SOURCE_DIR}/scripts/package/common/sh/common_func.inc40+ ${CANN_CMAKE_DIR}/scripts/install/common_func.inc
44- ${CMAKE_SOURCE_DIR}/scripts/package/common/sh/common_interface.bash41+ ${CANN_CMAKE_DIR}/scripts/install/common_interface.sh
45- ${CMAKE_SOURCE_DIR}/scripts/package/common/sh/common_interface.csh42+ ${CANN_CMAKE_DIR}/scripts/install/common_interface.csh
46- ${CMAKE_SOURCE_DIR}/scripts/package/common/sh/common_interface.fish43+ ${CANN_CMAKE_DIR}/scripts/install/common_interface.fish
47- ${CMAKE_SOURCE_DIR}/scripts/package/common/sh/version_compatiable.inc44+ ${CANN_CMAKE_DIR}/scripts/install/version_compatiable.inc
48- ${CMAKE_SOURCE_DIR}/scripts/package/common/py/merge_binary_info_config.py45+ ${CANN_CMAKE_DIR}/scripts/package/merge_binary_info_config.py
49 )46 )
50 47 
51 install(FILES ${SCRIPTS_FILES}48 install(FILES ${SCRIPTS_FILES}
52 DESTINATION share/info/pto_isa/script49 DESTINATION share/info/pto_isa/script
53 )50 )
54 set(COMMON_FILES51 set(COMMON_FILES
55- ${CMAKE_SOURCE_DIR}/scripts/package/common/sh/install_common_parser.sh52+ ${CANN_CMAKE_DIR}/scripts/install/install_common_parser.sh
56- ${CMAKE_SOURCE_DIR}/scripts/package/common/sh/common_func_v2.inc53+ ${CANN_CMAKE_DIR}/scripts/install/common_func_v2.inc
57- ${CMAKE_SOURCE_DIR}/scripts/package/common/sh/common_installer.inc54+ ${CANN_CMAKE_DIR}/scripts/install/common_installer.inc
58- ${CMAKE_SOURCE_DIR}/scripts/package/common/sh/script_operator.inc55+ ${CANN_CMAKE_DIR}/scripts/install/script_operator.inc
59- ${CMAKE_SOURCE_DIR}/scripts/package/common/sh/version_cfg.inc56+ ${CANN_CMAKE_DIR}/scripts/install/version_cfg.inc
60 )57 )
61 58 
62 set(PACKAGE_FILES59 set(PACKAGE_FILES
63 ${COMMON_FILES}60 ${COMMON_FILES}
64- ${CMAKE_SOURCE_DIR}/scripts/package/common/sh/multi_version.inc61+ ${CANN_CMAKE_DIR}/scripts/install/multi_version.inc
65- )
66- set(LATEST_MANGER_FILES
67- ${COMMON_FILES}
68- ${CMAKE_SOURCE_DIR}/scripts/package/common/sh/common_func.inc
69- ${CMAKE_SOURCE_DIR}/scripts/package/common/sh/version_compatiable.inc
70- ${CMAKE_SOURCE_DIR}/scripts/package/common/sh/check_version_required.awk
71 )62 )
72 set(CONF_FILES63 set(CONF_FILES
73- ${CMAKE_SOURCE_DIR}/scripts/package/common/cfg/path.cfg64+ ${CANN_CMAKE_DIR}/scripts/package/cfg/path.cfg
74 )65 )
75 install(FILES ${CMAKE_BINARY_DIR}/version.pto-isa.info66 install(FILES ${CMAKE_BINARY_DIR}/version.pto-isa.info
76 DESTINATION share/info/pto_isa67 DESTINATION share/info/pto_isa
@@ -107,27 +98,10 @@ function(pack_built_in)
107 98 
108 message(STATUS "current compute_unit is: ${compute_unit}")99 message(STATUS "current compute_unit is: ${compute_unit}")
109 100 
110- # ============= CPack =============101+ set_cann_cpack_config(pto-isa
111- set(CPACK_PACKAGE_NAME "${PROJECT_NAME}")102+ NO_COMPONENT_INSTALL
112- set(CPACK_PACKAGE_VERSION "${PROJECT_VERSION}")103+ COMPUTE_UNIT "${compute_unit}"
113- set(CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}-${CMAKE_SYSTEM_NAME}")104+ SHARE_INFO_NAME pto_isa
114- 105+ OUTPUT "${CMAKE_SOURCE_DIR}/build_out"
115- set(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR}/build_out)106+ )
116- 
117- set(CPACK_CMAKE_SOURCE_DIR "${CMAKE_SOURCE_DIR}")
118- set(CPACK_CMAKE_BINARY_DIR "${CMAKE_BINARY_DIR}")
119- set(CPACK_CMAKE_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}")
120- set(CPACK_CMAKE_CURRENT_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
121- set(CPACK_MAKESELF_PATH "${MAKESELF_PATH}")
122- # set(CPACK_COMPONENTS_ALL runtime documentation)
123- set(CPACK_SOC "${compute_unit}")
124- set(CPACK_ARCH "${ARCH}")
125- set(CPACK_SET_DESTDIR ON)
126- set(CPACK_GENERATOR External)
127- set(CPACK_EXTERNAL_PACKAGE_SCRIPT "${CMAKE_SOURCE_DIR}/cmake/makeself_built_in.cmake")
128- set(CPACK_EXTERNAL_ENABLE_STAGING true)
129- set(CPACK_PACKAGE_DIRECTORY "${CMAKE_INSTALL_PREFIX}")
130- 
131- message(STATUS "CMAKE_INSTALL_PREFIX = ${CMAKE_INSTALL_PREFIX}")
132- include(CPack)
133endfunction()107endfunction()
Dcmake/third_party/makeself-fetch.cmake+0-40
@@ -1,40 +0,0 @@
1-# ----------------------------------------------------------------------------
2-# Copyright (c) 2025 Huawei Technologies Co., Ltd.
3-# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4-# CANN Open Software License Agreement Version 2.0 (the "License").
5-# Please refer to the License for details. You may not use this file except in compliance with the License.
6-# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7-# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8-# See LICENSE in the root of the software repository for the full text of the License.
9-# ----------------------------------------------------------------------------
10- 
11-set(MAKESELF_NAME "makeself")
12-set(MAKESELF_PATH ${CANN_3RD_LIB_PATH}/makeself)
13- 
14-if(POLICY CMP0135)
15- cmake_policy(SET CMP0135 NEW)
16-endif()
17- 
18-# 默认配置的makeself还是不存在则下载
19-if (NOT EXISTS "${MAKESELF_PATH}/makeself-header.sh" OR NOT EXISTS "${MAKESELF_PATH}/makeself.sh")
20- set(MAKESELF_URL "https://gitcode.com/cann-src-third-party/makeself/releases/download/release-2.5.0-patch1.0/makeself-release-2.5.0-patch1.tar.gz")
21- message(STATUS "Downloading ${MAKESELF_NAME} from ${MAKESELF_URL}")
22- 
23- include(FetchContent)
24- FetchContent_Declare(
25- ${MAKESELF_NAME}
26- URL ${MAKESELF_URL}
27- URL_HASH SHA256=bfa730a5763cdb267904a130e02b2e48e464986909c0733ff1c96495f620369a
28- DOWNLOAD_DIR ${CANN_3RD_PKG_PATH}
29- SOURCE_DIR "${MAKESELF_PATH}" # 直接解压到此目录
30- )
31- FetchContent_MakeAvailable(${MAKESELF_NAME})
32- execute_process(
33- COMMAND chmod 700 "${CMAKE_BINARY_DIR}/makeself/makeself.sh"
34- COMMAND chmod 700 "${CMAKE_BINARY_DIR}/makeself/makeself-header.sh"
35- -E env
36- CMAKE_TLS_VERIFY=0
37- RESULT_VARIABLE CHMOD_RESULT
38- ERROR_VARIABLE CHMOD_ERROR
39- )
40-endif()
Dscripts/check_build_dependencies.py+0-194
@@ -1,194 +0,0 @@
1-#!/usr/bin/env python3
2-# -*- coding: UTF-8 -*-
3-# -----------------------------------------------------------------------------------------------------------
4-# Copyright (c) 2026 Huawei Technologies Co., Ltd.
5-# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
6-# CANN Open Software License Agreement Version 2.0 (the "License").
7-# Please refer to the License for details. You may not use this file except in compliance with the License.
8-# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
9-# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
10-# See LICENSE in the root of the software repository for the full text of the License.
11-# -----------------------------------------------------------------------------------------------------------
12- 
13-"""检查构建依赖。"""
14- 
15-import argparse
16-import logging
17-import os
18-import sys
19-from typing import Iterator, List, NamedTuple, Optional
20- 
21- 
22-class Receiver(NamedTuple):
23- """消息接收器。"""
24- warn_msgs: List[str]
25- err_msgs: List[str]
26- 
27- 
28-def parse_version_line(line: str) -> str:
29- """解析版本行。"""
30- version = line.strip().split('=', maxsplit=1)[1]
31- return version.split('-')[0]
32- 
33- 
34-def read_pkg_version(recv: Receiver, ascend_install_path: str, name: str) -> Optional[str]:
35- """读取包版本。"""
36- filepath = os.path.join(ascend_install_path, 'share', 'info', name, 'version.info')
37- if not os.path.isfile(filepath):
38- recv.err_msgs.append(f'{filepath} does not exist in read_pkg_version!')
39- return None
40- 
41- with open(filepath, encoding='utf-8') as file:
42- for line in file:
43- if line.startswith('Version='):
44- return parse_version_line(line)
45- 
46- recv.err_msgs.append(f'The version field was not found in {filepath} in read_pkg_version!')
47- return None
48- 
49- 
50-def check_build_dep_item(version: str, dep: str) -> bool:
51- """检查构建依赖项。"""
52- def split_version(version: str) -> List[int]:
53- return [int(num) for num in version.split('.')]
54- 
55- def check_ge() -> bool:
56- for req, rel in zip(dep_parts, version_parts):
57- if req > rel:
58- return False
59- if rel > req:
60- return True
61- return True
62- 
63- def check_gt() -> bool:
64- for req, rel in zip(dep_parts, version_parts):
65- if req > rel:
66- return False
67- if rel > req:
68- return True
69- return False
70- 
71- def check_le() -> bool:
72- for req, rel in zip(dep_parts, version_parts):
73- if rel > req:
74- return False
75- if req > rel:
76- return True
77- return True
78- 
79- def check_lt() -> bool:
80- for req, rel in zip(dep_parts, version_parts):
81- if rel > req:
82- return False
83- if req > rel:
84- return True
85- return False
86- 
87- def check_eq() -> bool:
88- for req, rel in zip(dep_parts, version_parts):
89- if req != rel:
90- return False
91- return True
92- 
93- version_parts = split_version(version)
94- 
95- if dep.startswith('>='):
96- dep = dep[2:]
97- check_func = check_ge
98- elif dep.startswith('>'):
99- dep = dep[1:]
100- check_func = check_gt
101- elif dep.startswith('<='):
102- dep = dep[2:]
103- check_func = check_le
104- elif dep.startswith('<'):
105- dep = dep[1:]
106- check_func = check_lt
107- else:
108- check_func = check_eq
109- 
110- dep_parts = split_version(dep)
111- 
112- if check_func is check_eq:
113- return check_func()
114- 
115- if len(dep_parts) < len(version_parts):
116- dep_parts.extend([0] * (len(version_parts) - len(dep_parts)))
117- elif len(dep_parts) > len(version_parts):
118- version_parts.extend([0] * (len(dep_parts) - len(version_parts)))
119- 
120- return check_func()
121- 
122- 
123-def check_build_dep(version: str, dep_info: str) -> bool:
124- """检查构建依赖。"""
125- def check_range(dep: str, deps_iter: Iterator[str]) -> bool:
126- result = check_build_dep_item(version, dep)
127- for dep in deps_iter:
128- if dep.startswith('<') or dep.startswith('<='):
129- return result and check_build_dep_item(version, dep)
130- result &= check_build_dep_item(version, dep)
131- return result
132- 
133- deps = [dep.strip() for dep in dep_info.split(',')]
134- deps_iter = iter(deps)
135- 
136- for dep in deps_iter:
137- if dep.startswith('>=') or dep.startswith('>'):
138- result = check_range(dep, deps_iter)
139- else:
140- result = check_build_dep_item(version, dep)
141- if result:
142- return True
143- return False
144- 
145- 
146- 
147-def check_build_deps(recv: Receiver, ascend_install_path: str, deps: list):
148- """检查构建依赖。"""
149- deps_iter = iter(deps)
150- for dep_pkg in deps_iter:
151- dep_info = next(deps_iter)
152- version = read_pkg_version(recv, ascend_install_path, dep_pkg)
153- if not version:
154- continue
155- try:
156- if not check_build_dep(version, dep_info):
157- warn_msg = 'Check build dependency failed! ' \
158- f'Required {dep_pkg} version is {dep_info}, but {dep_pkg} version is {version}.'
159- recv.warn_msgs.append(warn_msg)
160- except ValueError:
161- err_msg = f'Check build dependency error! version is {version}, dep_info is {dep_info}.'
162- recv.err_msgs.append(err_msg)
163- 
164- 
165-def main():
166- """主流程。"""
167- parser = argparse.ArgumentParser()
168- parser.add_argument('ascend_install_path', help='Ascend install path.')
169- parser.add_argument('deps', nargs='*', help='Dependency informations.')
170- args = parser.parse_args()
171- 
172- logging.basicConfig(format=f'{os.path.basename(__file__)}: %(levelname)s: %(message)s')
173- if len(args.deps) % 2 != 0:
174- logging.error('The deps argument must contain an even number of elements!')
175- return False
176- 
177- recv = Receiver([], [])
178- check_build_deps(recv, args.ascend_install_path, args.deps)
179- 
180- if recv.warn_msgs:
181- for warn_msg in recv.warn_msgs:
182- logging.warning(warn_msg)
183- 
184- if recv.err_msgs:
185- for err_msg in recv.err_msgs:
186- logging.error(err_msg)
187- return False
188- 
189- return True
190- 
191- 
192-if __name__ == '__main__':
193- if not main():
194- sys.exit(1)
Dscripts/generate_version_info.py+0-63
@@ -1,63 +0,0 @@
1-#!/usr/bin/env python3
2-# -*- coding: UTF-8 -*-
3-# -----------------------------------------------------------------------------------------------------------
4-# Copyright (c) 2026 Huawei Technologies Co., Ltd.
5-# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
6-# CANN Open Software License Agreement Version 2.0 (the "License").
7-# Please refer to the License for details. You may not use this file except in compliance with the License.
8-# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
9-# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
10-# See LICENSE in the root of the software repository for the full text of the License.
11-# -----------------------------------------------------------------------------------------------------------
12- 
13-"""生成version.info文件。"""
14- 
15-import argparse
16-import logging
17-import os
18-import sys
19-from datetime import datetime, timedelta, timezone
20-from typing import Iterable, Iterator
21- 
22- 
23-def gen_version_info_content(version: str, deps: Iterable[str]) -> Iterator[str]:
24- """生成version.info文件内容。"""
25- yield f'Version={version}'
26- yield 'version_dir=cann'
27- deps_iter = iter(deps)
28- for dep_pkg in deps_iter:
29- dep_info = next(deps_iter)
30- yield f'required_package_{dep_pkg}_version="{dep_info}"'
31- if os.environ.get('tagInfo'):
32- tag_info = os.environ.get('tagInfo')
33- timestamp = '_'.join(tag_info.split('_')[-3:-1])
34- else:
35- timestamp = datetime.now(timezone(timedelta(hours=8))).strftime('%Y%m%d_%H%M%S%f')[:-3]
36- yield f'timestamp={timestamp}'
37- yield '' # for last \n
38- 
39- 
40-def main():
41- """主流程。"""
42- parser = argparse.ArgumentParser()
43- parser.add_argument('version', help='Version number.')
44- parser.add_argument('deps', nargs='*', help='Dependency informations.')
45- parser.add_argument('--output', required=True, help='Output file path.')
46- args = parser.parse_args()
47- 
48- logging.basicConfig(format=f'{os.path.basename(__file__)}: %(levelname)s: %(message)s')
49- 
50- if len(args.deps) % 2 != 0:
51- logging.error('The deps argument must contain an even number of elements!')
52- return False
53- 
54- content = '\n'.join(gen_version_info_content(args.version, args.deps))
55- with open(args.output, 'w', encoding='utf-8') as file:
56- file.write(content)
57- 
58- return True
59- 
60- 
61-if __name__ == '__main__':
62- if not main():
63- sys.exit(1)
Mscripts/package/README.md+5-3
@@ -4,11 +4,13 @@ Packaging and release scripts/templates for PTO Tile Lib. This directory is used
4 4 
5## Layout (High Level)5## Layout (High Level)
6 6 
7-- `package.py`: Main packaging entry script
8- `pto_isa/`: Package templates and helper scripts specific to PTO Tile Lib7- `pto_isa/`: Package templates and helper scripts specific to PTO Tile Lib
9-- `latest_manager/`: Helpers for managing “latest” package versions
10- `module/`: Module-level packaging descriptors8- `module/`: Module-level packaging descriptors
11-- `common/`: Shared helpers and configuration fragments9+ 
10+Common helpers (`package.py`, `install_common_parser.sh`, install/cleanup
11+fragments, etc.) are reused from the CANN engineering cmake repo
12+(`https://gitcode.com/cann/cmake`) and pulled in at configure time via
13+`cmake/fetch_cann_cmake.cmake`.
12 14 
13## Build and Install15## Build and Install
14 16 
Mscripts/package/README_zh.md+4-3
@@ -4,11 +4,12 @@ PTO Tile Lib 的打包/发布脚本与模板,用于生成可分发产物。
4 4 
5## 目录结构(概览)5## 目录结构(概览)
6 6 
7-- `package.py`:主要打包入口脚本
8- `pto_isa/`:PTO Tile Lib 专用的打包模板与辅助脚本7- `pto_isa/`:PTO Tile Lib 专用的打包模板与辅助脚本
9-- `latest_manager/`:管理 “latest” 版本包的辅助工具
10- `module/`:模块级打包描述8- `module/`:模块级打包描述
11-- `common/`:共享辅助工具与配置片段9+ 
10+公共脚本(`package.py``install_common_parser.sh`、安装/清理片段等)已复用自
11+CANN 工程公共 cmake 仓(`https://gitcode.com/cann/cmake`),在 configure 阶段
12+`cmake/fetch_cann_cmake.cmake` 拉取。
12 13 
13## 构建与安装14## 构建与安装
14 15 
Dscripts/package/common/__init__.py+0-11
@@ -1,11 +0,0 @@
1-#!/usr/bin/env python3
2-# -*- coding: UTF-8 -*-
3-# --------------------------------------------------------------------------------
4-# Copyright (c) 2025 Huawei Technologies Co., Ltd.
5-# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
6-# CANN Open Software License Agreement Version 2.0 (the "License").
7-# Please refer to the License for details. You may not use this file except in compliance with the License.
8-# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
9-# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
10-# See LICENSE in the root of the software repository for the full text of the License.
11-# --------------------------------------------------------------------------------
Dscripts/package/common/cfg/path.cfg+0-2
@@ -1,2 +0,0 @@
1-CUSTOM_DATA_PATH=$HOME/Ascend/latest/data/
2-CUSTOM_CONF_PATH=$HOME/Ascend/latest/conf/
Dscripts/package/common/py/__init__.py+0-11
@@ -1,11 +0,0 @@
1-#!/usr/bin/env python3
2-# -*- coding: UTF-8 -*-
3-# --------------------------------------------------------------------------------
4-# Copyright (c) 2025 Huawei Technologies Co., Ltd.
5-# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
6-# CANN Open Software License Agreement Version 2.0 (the "License").
7-# Please refer to the License for details. You may not use this file except in compliance with the License.
8-# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
9-# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
10-# See LICENSE in the root of the software repository for the full text of the License.
11-# --------------------------------------------------------------------------------
Dscripts/package/common/py/filelist.py+0-511
@@ -1,511 +0,0 @@
1-#!/usr/bin/env python3
2-# -*- coding: UTF-8 -*-
3-# --------------------------------------------------------------------------------
4-# Copyright (c) 2025 Huawei Technologies Co., Ltd.
5-# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
6-# CANN Open Software License Agreement Version 2.0 (the "License").
7-# Please refer to the License for details. You may not use this file except in compliance with the License.
8-# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
9-# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
10-# See LICENSE in the root of the software repository for the full text of the License.
11-# --------------------------------------------------------------------------------
12- 
13-"""filelist相关类。"""
14- 
15-import itertools
16-import os
17-from enum import IntEnum
18-from collections import Counter
19-from functools import partial
20-from itertools import chain, repeat
21-from operator import and_, attrgetter, contains, itemgetter, lt, methodcaller, ne, not_
22-from typing import Callable, Iterator, List, NamedTuple, Set, Tuple
23- 
24-from .utils.pkg_utils import (TOP_DIR, FilelistError, GenerateFilelistError,
25- conditional_apply, pairwise, swap_args, config_feature_to_string)
26-from .utils.funcbase import (any_, constant, dispatch, identity, invoke, pipe, side_effect, star_apply)
27-from .utils.comm_log import CommLog
28- 
29- 
30-class FileItem(NamedTuple):
31- """文件条目"""
32- module: str
33- operation: str
34- relative_path_in_pkg: str
35- relative_install_path: str
36- is_in_docker: str
37- permission: str
38- owner_group: str
39- install_type: str
40- softlink: List[str]
41- feature: Set[str]
42- is_common_path: str
43- configurable: str
44- hash_value: str
45- block: str
46- pkg_inner_softlink: List[str]
47- chip: Set[str]
48- is_dir: bool
49- 
50- 
51-def create_file_item(*args, **kwargs) -> FileItem:
52- """创建文件条目。"""
53- file_item = FileItem(*args, **kwargs)
54- 
55- if not isinstance(file_item.feature, set):
56- raise TypeError('The feature parameter should be a set.')
57- if not isinstance(file_item.chip, set):
58- raise TypeError('The chip parameter should be a set.')
59- if not isinstance(file_item.softlink, list):
60- raise TypeError('The softlink parameter should be a list.')
61- if not isinstance(file_item.pkg_inner_softlink, list):
62- raise TypeError('The pkg_inner_softlink parameter should be a list.')
63- 
64- return file_item
65- 
66- 
67-# 文件列表
68-FileList = List[FileItem]
69- 
70- 
71-def soft_links_to_string(soft_links: List[str]) -> str:
72- """软链接转换为字符串。"""
73- if not soft_links:
74- return 'NA'
75- return ';'.join(soft_links)
76- 
77- 
78-def file_item_to_string(item: FileItem) -> str:
79- """文件条目转换为字符串。"""
80- return ','.join([
81- item.module, item.operation, item.relative_path_in_pkg, item.relative_install_path,
82- item.is_in_docker, item.permission, item.owner_group, item.install_type,
83- soft_links_to_string(item.softlink), config_feature_to_string(item.feature),
84- item.is_common_path, item.configurable, item.hash_value, item.block,
85- soft_links_to_string(item.pkg_inner_softlink), config_feature_to_string(item.chip)
86- ])
87- 
88- 
89-def get_filelist_header_string() -> str:
90- """获取文件列表表头。"""
91- return ','.join([
92- 'module', 'operation', 'relative_path_in_pkg', 'relative_install_path',
93- 'is_in_docker', 'permission', 'owner:group', 'install_type',
94- 'softlink', 'feature', 'is_common_path', 'configurable', 'hash',
95- 'block', 'pkg_inner_softlink', 'chip'
96- ])
97- 
98- 
99-def get_soft_links_not_in_common_paths(filelist: FileList, target_env: str) -> Iterator[List[str]]:
100- for file_item_t in filelist:
101- if file_item_t.relative_install_path.startswith(target_env):
102- for softlink in file_item_t.softlink:
103- if not softlink.startswith(target_env):
104- yield softlink
105- 
106- 
107-def fill_is_common_path(filelist: FileList, target_env: str) -> Iterator[FileItem]:
108- """填充文件条目中是否为公共目录字段。"""
109- soft_links = set(get_soft_links_not_in_common_paths(filelist, target_env))
110- for file_item in filelist:
111- if file_item.relative_install_path.startswith(target_env):
112- yield file_item._replace(is_common_path='Y')
113- else:
114- is_soft_links_prefix = map(
115- methodcaller('startswith', f'{file_item.relative_install_path}/'), soft_links
116- )
117- if any(is_soft_links_prefix):
118- yield file_item._replace(is_common_path='YY')
119- else:
120- yield file_item
121- 
122- 
123-def is_relative_install_path(path: str) -> bool:
124- """是否为相对路径。"""
125- if path.startswith('/'):
126- return False
127- return True
128- 
129- 
130-def is_specific_operations(file_item: FileItem, operations: List[str]) -> bool:
131- """是否为特定的操作类型。"""
132- if file_item.operation in operations:
133- return True
134- return False
135- 
136- 
137-def is_specific_install_type(file_item: FileItem, install_types: Set[str]) -> bool:
138- """是否为特定的安装类型。"""
139- item_install_types = set(file_item.install_type.split(';'))
140- if 'all' in item_install_types:
141- return True
142- if item_install_types & install_types:
143- return True
144- return False
145- 
146- 
147-def get_install_path_dirs(install_path: str) -> Iterator[str]:
148- """获取安装路径父目录。"""
149- install_path = os.path.dirname(install_path)
150- while install_path not in ('', '/'):
151- yield install_path
152- install_path = os.path.dirname(install_path)
153- 
154- 
155-def get_missing_dir_set(filelist: FileList) -> Set[str]:
156- """获取缺失目录集合。
157- 
158- 文件列表可能出现某一级目录缺失情况。
159- 如配置了file_info:aaa/bbb/ccc.txt,但只配置了dir_info:aaa,
160- 那么缺失dir_info:aaa/bbb
161- """
162- parent_dirs: Set[str] = invoke(
163- pipe(
164- dispatch(
165- pipe(
166- partial(
167- filter,
168- partial(is_specific_operations, operations={'copy', 'copy_entity'}),
169- ),
170- partial(map, attrgetter('relative_install_path')),
171- partial(filter, is_relative_install_path),
172- set,
173- partial(map, get_install_path_dirs),
174- chain.from_iterable,
175- ),
176- pipe(
177- partial(map, attrgetter('softlink')),
178- chain.from_iterable,
179- partial(
180- filter,
181- pipe(
182- dispatch(
183- bool,
184- is_relative_install_path,
185- partial(ne, 'NA'),
186- ),
187- all
188- )
189- ),
190- set,
191- partial(map, get_install_path_dirs),
192- chain.from_iterable,
193- ),
194- pipe(
195- partial(map, attrgetter('pkg_inner_softlink')),
196- chain.from_iterable,
197- partial(
198- filter,
199- pipe(
200- dispatch(
201- bool,
202- partial(ne, 'NA'),
203- ),
204- all
205- )
206- ),
207- set,
208- partial(map, get_install_path_dirs),
209- chain.from_iterable,
210- ),
211- ),
212- chain.from_iterable,
213- set,
214- ),
215- filelist
216- )
217- mkdir_installs: Set[str] = {
218- file_item.relative_install_path
219- for file_item in filter(
220- partial(is_specific_operations, operations={'mkdir'}),
221- filelist
222- )
223- if is_relative_install_path(file_item.relative_install_path)
224- }
225- 
226- mkdir_parent_dirs: Set[str] = set(
227- itertools.chain.from_iterable(
228- map(get_install_path_dirs, mkdir_installs)
229- )
230- )
231- 
232- missing_dir_set = sorted((parent_dirs | mkdir_parent_dirs) - mkdir_installs)
233- return set(missing_dir_set)
234- 
235- 
236-def print_missing_dir_set(missing_dir_set: Set[str], in_msg: str = None) -> Set[str]:
237- """打印缺失目录集合。"""
238- if in_msg:
239- tail_msg = f' {in_msg}'
240- else:
241- tail_msg = ''
242- for path in sorted(missing_dir_set):
243- CommLog.cilog_error(f'missing dir info path "{path}"{tail_msg}')
244- return missing_dir_set
245- 
246- 
247-def print_unsafe_paths(unsafe_paths: Tuple[str, ...]) -> Tuple[str, ...]:
248- """打印非安全路径。"""
249- for path in unsafe_paths:
250- CommLog.cilog_error(f'unsafe path "{path}" in move scene.')
251- return unsafe_paths
252- 
253- 
254-# 获取filelist中所有的特性集合
255-get_features_in_filelist = pipe(
256- partial(map, attrgetter('feature')),
257- chain.from_iterable, # 展开集合序列为元素序列
258- set, # 去重
259- partial(filter, partial(ne, 'comm')), # 排除comm特性
260- set,
261-)
262- 
263-# 获取filelist中所有的芯片集合
264-get_chips_in_filelist = pipe(
265- partial(map, attrgetter('chip')),
266- chain.from_iterable, # 展开集合序列为元素序列
267- set, # 去重
268-)
269- 
270- 
271-def check_features_in_filelist(features: Set[str], filelist: FileList) -> Set[str]:
272- """检查文件列表中特性配置目录规范。"""
273- return invoke(
274- pipe(
275- # 过滤指定features的file_item
276- partial(
277- filter,
278- pipe(attrgetter('feature'), partial(and_, features), bool)
279- ),
280- list,
281- get_missing_dir_set,
282- partial(print_missing_dir_set, in_msg=f'in features {features}'),
283- ),
284- filelist
285- )
286- 
287- 
288-def check_chip_in_filelist(chip: str, filelist: FileList) -> Set[str]:
289- """检查文件列表中芯片配置目录规范。"""
290- return invoke(
291- pipe(
292- # 过滤指定chip的file_item
293- partial(
294- filter,
295- any_(
296- pipe(
297- attrgetter('chip'), not_
298- ), # 没有配置chip
299- pipe(
300- attrgetter('chip'), partial(swap_args(contains), chip), bool
301- ), # 配置了指定chip
302- ),
303- ),
304- list,
305- get_missing_dir_set,
306- partial(print_missing_dir_set, in_msg=f'in chip {chip}'),
307- ),
308- filelist
309- )
310- 
311- 
312-check_filelist_features = any_(
313- pipe(
314- dispatch(
315- pipe(
316- get_features_in_filelist,
317- # 对于每个feature,与comm组成一个set
318- partial(map, lambda x: {x, 'comm'}),
319- # 此时为feature集合序列
320- ),
321- repeat, # 重复filelist
322- ),
323- tuple,
324- star_apply(zip),
325- # 此时为元组序列,元组的第1个元素是过滤的feature集合,第2个元素是filelist
326- partial(itertools.starmap, check_features_in_filelist),
327- # 此时为集合序列;合并为一个集合
328- chain.from_iterable,
329- set,
330- ),
331- pipe(
332- dispatch(
333- get_chips_in_filelist,
334- repeat, # 重复filelist
335- ),
336- tuple,
337- star_apply(zip),
338- # 此时为元组序列,元组的第1个元素是chip集合,第2个元素是filelist
339- partial(itertools.starmap, check_chip_in_filelist),
340- # 此时为集合序列;合并为一个集合
341- chain.from_iterable,
342- set,
343- )
344-)
345- 
346- 
347-# 检查move是否安全,是否存在同一个源路径被mv多次
348-check_move_safe = pipe(
349- partial(
350- filter,
351- partial(is_specific_operations, operations={'copy', 'copy_entity', 'move'}),
352- ),
353- partial(map, attrgetter('relative_path_in_pkg')),
354- Counter,
355- methodcaller('items'),
356- partial(filter, pipe(itemgetter(1), partial(lt, 1))),
357- partial(map, itemgetter(0)),
358- tuple,
359- print_unsafe_paths,
360-)
361- 
362- 
363-def check_filelist(filelist: FileList, check_features: bool, check_move: bool):
364- """检查文件列表是否符合规范。"""
365- if check_features:
366- check_features_func = check_filelist_features
367- else:
368- check_features_func = constant(set())
369- 
370- if check_move:
371- check_move_func = check_move_safe
372- else:
373- check_move_func = constant(tuple())
374- 
375- # 此处使用any_,短路部分报错
376- check_func = any_(
377- pipe(
378- get_missing_dir_set,
379- print_missing_dir_set,
380- ),
381- pipe(
382- partial(filter, partial(is_specific_install_type, install_types={'run'})),
383- list,
384- get_missing_dir_set,
385- partial(print_missing_dir_set, in_msg='in run install type'),
386- ),
387- check_features_func,
388- check_move_func,
389- )
390- missing = check_func(filelist)
391- 
392- if missing:
393- raise FilelistError()
394- 
395- 
396-def get_common_path(args: List[str]) -> str:
397- """公共路径前缀。"""
398- try:
399- return os.path.commonpath(args)
400- except ValueError:
401- return ''
402- 
403- 
404-class FileItemRelation(IntEnum):
405- """文件条目之间的关系。"""
406- NOT_NESTED = 0 # 不是嵌套文件
407- NESTED = 1 # 嵌套文件
408- SAME = 2 # 相同文件
409- 
410- 
411-def is_nested_file_item(item: FileItem, base_item: FileItem) -> FileItemRelation:
412- """是否为嵌套的文件。"""
413- if base_item is None:
414- return FileItemRelation.NOT_NESTED
415- 
416- if item == base_item:
417- return FileItemRelation.SAME
418- 
419- install_path = item.relative_install_path
420- base_install_path = base_item.relative_install_path
421- 
422- common_install_path = get_common_path([install_path, base_install_path])
423- if common_install_path != base_install_path:
424- return FileItemRelation.NOT_NESTED
425- 
426- pkg_path = item.relative_path_in_pkg
427- base_pkg_path = base_item.relative_path_in_pkg
428- 
429- install_rel_path = os.path.relpath(install_path, base_install_path)
430- pkg_rel_path = os.path.relpath(pkg_path, base_pkg_path)
431- if install_rel_path != pkg_rel_path:
432- # 确保打包与安装相对路径一致
433- raise FilelistError(f'nested paths {item} and {base_item} are illegal.')
434- return FileItemRelation.NESTED
435- 
436- 
437-def found_nested_file_item(item: FileItem, base_item: FileItem):
438- """发现嵌套元素。"""
439- raise FilelistError(f'found nested paths {item} and {base_item}!')
440- 
441- 
442-def convert_nested_path_in_filelist(filelist: FileList):
443- """filelist中嵌套路径元素转为del。"""
444- pre_item = None
445- for item in filelist:
446- ret = is_nested_file_item(item, pre_item)
447- if ret == FileItemRelation.NESTED:
448- yield item._replace(operation='del')
449- elif any((
450- ret == FileItemRelation.NOT_NESTED,
451- (ret == FileItemRelation.SAME and not item.is_dir)
452- )):
453- yield item
454- pre_item = item
455- 
456- 
457-# 检查文件列表中的嵌套路径。入参: filelist
458-check_nested_path_in_filelist = pipe(
459- partial(filter, partial(is_specific_operations, operations={'copy', 'copy_entity'})),
460- partial(sorted, key=attrgetter('relative_install_path')),
461- pairwise,
462- partial(
463- map,
464- conditional_apply(star_apply(is_nested_file_item), star_apply(found_nested_file_item))
465- ),
466- list,
467-)
468- 
469- 
470-# 变换文件列表中嵌套路径。入参: filelist
471-transform_nested_path_in_filelist = pipe(
472- dispatch(
473- partial(
474- itertools.filterfalse, partial(is_specific_operations, operations={'copy'})
475- ),
476- pipe(
477- partial(filter, partial(is_specific_operations, operations={'copy'})),
478- partial(sorted, key=attrgetter('relative_install_path')),
479- convert_nested_path_in_filelist
480- ),
481- ),
482- chain.from_iterable,
483- list,
484- side_effect(check_nested_path_in_filelist),
485-)
486- 
487- 
488-def generate_filelist(filelist: FileList, filename: str):
489- """生成文件列表文件。"""
490- content_list = list(
491- itertools.chain(
492- [get_filelist_header_string()],
493- [file_item_to_string(item) for item in filelist]
494- )
495- )
496- content = '\n'.join(content_list)
497- filepath = os.path.join(TOP_DIR, "build", filename)
498- try:
499- with open(filepath, 'w', encoding='utf-8') as file:
500- file.write(content)
501- # filelist.csv文件末尾补充一个换行符
502- file.write('\n')
503- except OSError as ex:
504- raise GenerateFilelistError(filename) from ex
505- 
506- 
507-def get_transform_nested_path_func(parallel: bool) -> Callable[[FileList], FileList]:
508- """获取转换嵌套路径函数。"""
509- if parallel:
510- return transform_nested_path_in_filelist
511- return identity
Dscripts/package/common/py/merge_binary_info_config.py+0-75
@@ -1,75 +0,0 @@
1-#!/usr/bin/env python3
2-# -*- coding: UTF-8 -*-
3-# --------------------------------------------------------------------------------
4-# Copyright (c) 2025 Huawei Technologies Co., Ltd.
5-# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
6-# CANN Open Software License Agreement Version 2.0 (the "License").
7-# Please refer to the License for details. You may not use this file except in compliance with the License.
8-# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
9-# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
10-# See LICENSE in the root of the software repository for the full text of the License.
11-# --------------------------------------------------------------------------------
12- 
13-"""合并算子binary_info_config.json。"""
14- 
15-import argparse
16-import json
17-import os
18-import sys
19-from typing import List
20- 
21- 
22-def load_json_file(json_file: str):
23- """加载json文件。"""
24- with open(json_file, encoding='utf-8') as file:
25- json_content = json.load(file)
26- return json_content
27- 
28- 
29-def save_json_file(output_file: str, content):
30- """保存json文件。"""
31- output_dir = os.path.dirname(output_file)
32- if not os.path.exists(output_dir):
33- os.makedirs(output_dir, exist_ok=True)
34- 
35- with open(output_file, 'w', encoding='utf-8') as file:
36- json.dump(content, file, ensure_ascii=True, indent=2)
37- 
38- 
39-def update_config(base_content, update_content):
40- """更新配置。"""
41- new_content = base_content.copy()
42- new_content.update(update_content)
43- return dict(sorted(new_content.items()))
44- 
45- 
46-def parse_args(argv: List[str]):
47- """入参解析。"""
48- parser = argparse.ArgumentParser()
49- parser.add_argument('--base-file',
50- required=True,
51- help='the basic binary_info_config file')
52- parser.add_argument('--update-file',
53- required=True,
54- help='the update binary_info_config file')
55- parser.add_argument('--output-file',
56- required=True,
57- type=os.path.realpath,
58- help='the output binary_info_config file')
59- args = parser.parse_args(argv)
60- return args
61- 
62- 
63-def main(argv: List[str]) -> bool:
64- """主流程。"""
65- args = parse_args(argv)
66- base_content = load_json_file(args.base_file)
67- update_content = load_json_file(args.update_file)
68- result = update_config(base_content, update_content)
69- save_json_file(args.output_file, result)
70- return True
71- 
72- 
73-if __name__ == '__main__':
74- if not main(sys.argv[1:]): # pragma: no cover
75- sys.exit(1) # pragma: no cover
Dscripts/package/common/py/packer.py+0-158
@@ -1,158 +0,0 @@
1-#!/usr/bin/env python3
2-# -*- coding: UTF-8 -*-
3-# --------------------------------------------------------------------------------
4-# Copyright (c) 2025 Huawei Technologies Co., Ltd.
5-# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
6-# CANN Open Software License Agreement Version 2.0 (the "License").
7-# Please refer to the License for details. You may not use this file except in compliance with the License.
8-# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
9-# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
10-# See LICENSE in the root of the software repository for the full text of the License.
11-# --------------------------------------------------------------------------------
12- 
13-import shutil
14-from argparse import Namespace
15-from itertools import chain
16-from typing import Callable, Dict, List, NamedTuple, Optional, Tuple, Union
17- 
18-from .utils.comm_log import CommLog
19- 
20- 
21-class PackageName:
22- """包名。"""
23- 
24- def __init__(self,
25- package_attr,
26- args: Namespace,
27- version: str):
28- self.product_name = package_attr.get('product_name')
29- self.chip_name = args.chip_name or package_attr.get('chip_name')
30- self.suffix = args.suffix or package_attr.get('suffix')
31- self.func_name = get_func_name(args.func_name, package_attr)
32- self.chip_plat = package_attr.get('chip_plat')
33- self.deploy_type = package_attr.get('deploy_type')
34- self.version = version.lower()
35- self.not_in_name_list = args.not_in_name.split(",")
36- self.os_arch = args.os_arch
37- self.package_suffix = args.package_suffix
38- self.ext_name = args.ext_name
39- if args.pkg_name_style == 'underline':
40- self.name_sep = '_'
41- else:
42- self.name_sep = '-'
43- 
44- def get_attribute(self, name: str) -> Optional[str]:
45- """获取属性。"""
46- if name in self.not_in_name_list:
47- return None
48- return getattr(self, name)
49- 
50- def getvalue(self) -> str:
51- product_name = self.get_attribute('product_name')
52- chip_name = self.get_attribute('chip_name')
53- func_name = self.get_attribute('func_name')
54- version = self.get_attribute('version')
55- os_arch = self.get_attribute('os_arch')
56- chip_plat = self.get_attribute('chip_plat')
57- deploy_type = self.get_attribute('deploy_type')
58- ext_name = self.get_attribute('ext_name')
59- package_suffix = "debug" if self.package_suffix == "debug" else None
60- 
61- region1 = "-".join(filter(None, [product_name, remove_ascend(chip_name), func_name]))
62- region2 = ".".join(filter(None, [version]))
63- region3 = "-".join(filter(None, [os_arch, chip_plat, deploy_type, package_suffix, ext_name]))
64- package_name = "_".join(filter(None, [region1, region2, region3]))
65- 
66- return f"{package_name}.{self.suffix}"
67- 
68- 
69-class MakeselfPkgParams(NamedTuple):
70- """run包打包参数。"""
71- package_name: str
72- comments: str
73- cleanup: Optional[str] = None
74- 
75- 
76-def remove_ascend(text):
77- if text is None:
78- return None
79- text_lower = text.lower()
80- if "ascend" in text_lower:
81- return text_lower.replace("ascend", "")
82- return text_lower
83- 
84- 
85-def get_func_name(func_name: str, package_attr) -> str:
86- """获取包func_name。"""
87- return func_name or package_attr.get('func_name')
88- 
89- 
90-def get_compress_tool() -> str:
91- tools = ["pigz", "gzip", "bzip2", "xz"]
92- for tool in tools:
93- path = shutil.which(tool)
94- if path:
95- return "--" + tool
96- CommLog.cilog_error("The system does not come with a compression tool pre-installed."
97- "Please ensure at least one of the folllowing compression tools is available: %s", tools)
98- return ""
99- 
100- 
101-def get_compress_format() -> str:
102- tar_format = "gnu"
103- path = shutil.which("bsdtar")
104- if path:
105- tar_format = "ustar"
106- return tar_format
107- 
108- 
109-def compose_makeself_command(params: MakeselfPkgParams) -> str:
110- """组装makeself包打包命令。"""
111- 
112- def get_cleanup_commands() -> List[str]:
113- if params.cleanup:
114- return ['--cleanup', params.cleanup]
115- return []
116-
117- compress_tool = get_compress_tool()
118- tar_format = get_compress_format()
119- commands = chain(
120- [
121- compress_tool, '--complevel', '4',
122- '--nomd5', '--sha256', '--nooverwrite', '--chown', '--tar-format', tar_format,
123- '--tar-extra', '--numeric-owner', '--tar-quietly'
124- ],
125- get_cleanup_commands(),
126- [params.package_name, params.comments]
127- )
128-
129- command = ' '.join(commands)
130- return command
131- 
132- 
133-def create_makeself_pkg_params_factory(package_name: str,
134- comments: str
135- ) -> Callable[[Dict], MakeselfPkgParams]:
136- """创建Makeself打包参数工厂。"""
137- 
138- def create_makeself_pkg_params(package_attr: Dict) -> MakeselfPkgParams:
139- """创建Makeself打包参数。"""
140- cleanup = package_attr.get('cleanup')
141- params = MakeselfPkgParams(
142- package_name=package_name,
143- comments=comments,
144- cleanup=cleanup,
145- )
146- 
147- return params
148- 
149- return create_makeself_pkg_params
150- 
151- 
152-def create_run_package_command(params: MakeselfPkgParams
153- ) -> Tuple[Optional[str], Optional[str]]:
154- """
155- 功能描述: 组装打run包命令
156- 返回值: command
157- """
158- return compose_makeself_command(params), None
Dscripts/package/common/py/pkg_parser.py+0-1270
@@ -1,1270 +0,0 @@
1-#!/usr/bin/env python3
2-# -*- coding: UTF-8 -*-
3-# --------------------------------------------------------------------------------
4-# Copyright (c) 2025 Huawei Technologies Co., Ltd.
5-# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
6-# CANN Open Software License Agreement Version 2.0 (the "License").
7-# Please refer to the License for details. You may not use this file except in compliance with the License.
8-# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
9-# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
10-# See LICENSE in the root of the software repository for the full text of the License.
11-# --------------------------------------------------------------------------------
12- 
13-import copy
14-import glob
15-import hashlib
16-import itertools
17-import re
18-import xml.etree.ElementTree as ET
19-import os
20-import sys
21-from argparse import Namespace
22-from functools import partial
23-from io import StringIO
24-from itertools import chain
25-from operator import attrgetter, itemgetter, methodcaller
26-from typing import (
27- Any, Callable, Dict, Iterable, Iterator, List, NamedTuple, Optional, Set, Tuple, Union
28-)
29- 
30-from .utils import pkg_utils
31-from .filelist import FileItem, FileList, fill_is_common_path
32-from .utils.pkg_utils import (
33- ContainAsteriskError, FAIL, BLOCK_CONFIG_PATH,
34- BlockConfigError, EnvNotSupported, IllegalVersionDir,
35- InstallScriptFormatError, InstallScriptNotInPackageInfo, PackageError,
36- MultiPkgModError, MultiPkgSoftlinkError, PkgInnerSoftlinkNotAllowed,
37- ParseOsArchError, VersionInfoNotExist, config_feature_to_set,
38- flatten, star_pipe, merge_dict, yield_if
39-)
40-from .utils.funcbase import constant, dispatch, invoke, pipe, star_apply
41-from .utils.comm_log import CommLog
42-from .version_info import (
43- VersionInfo, VersionXml, VersionFormatNotMatch, is_multi_version
44-)
45- 
46-# 环境变量字典
47-EnvDict = Dict[str, str]
48- 
49-# 文件信息
50-FileInfo = Dict[str, str]
51- 
52-# 包属性
53-PackageAttr = Dict[str, Union[str, bool]]
54- 
55-# 生成信息
56-GenerateInfo = Dict[str, str]
57- 
58- 
59-class ParseOption(NamedTuple):
60- """解析参数。"""
61- os_arch: Optional[str]
62- pkg_version: Optional[str]
63- build_type: Optional[str]
64- package_check: bool
65- ext_name: str = ''
66- 
67- 
68-def parse_os_arch(os_arch: str) -> Tuple[str, str, str]:
69- """解析系统和架构。"""
70- match = re.match("^([a-z]+)(\\d+(\\.\\d+)*)?[.-]?(\\S*)", os_arch)
71- if match:
72- os_name = match.group(1)
73- os_ver = match.group(2)
74- if match.group(4):
75- arch = match.group(4)
76- else:
77- # 如果os_arch中没有配置ARCH,ARCH默认值为aarch64
78- arch = 'aarch64'
79- 
80- return os_name, os_ver, arch
81- 
82- raise ParseOsArchError()
83- 
84- 
85-def replace_env(env_dict: EnvDict, in_str: str):
86- """替换环境变量为实际值。"""
87- env_list = re.findall(".*?\\$\\((.*?)\\).*?", in_str)
88- for env in env_list:
89- if env == 'FILE':
90- continue
91- if env in env_dict:
92- if env_dict[env] is not None:
93- in_str = in_str.replace(f"$({env})", env_dict[env])
94- else:
95- in_str = in_str.replace(f"$({env})", '')
96- else:
97- raise EnvNotSupported(f"Error: {env} not supported.")
98- return in_str
99- 
100- 
101-class ParseEnv(NamedTuple):
102- """解析上下文环境。"""
103- env_dict: EnvDict
104- parse_option: ParseOption
105- delivery_dir: str
106- top_dir: str
107- package_attr: Dict
108- 
109- 
110-class BlockElement(NamedTuple):
111- """块配置。"""
112- name: str
113- block_conf_path: str
114- dst_path: str
115- chips: Set[str]
116- features: Set[str]
117- attrs: Dict[str, str]
118- 
119- 
120-# BlockElement直接透传给LoadedBlockElement的参数列表
121-BLOCK_ELEMENT_PASS_THROUGH_ARGS = ['dst_path', 'chips', 'features', 'attrs']
122- 
123- 
124-class LoadedBlockElement(NamedTuple):
125- """加载后的块配置。"""
126- root_ele: ET.Element
127- use_move: bool
128- dst_path: str
129- chips: Set[str]
130- features: Set[str]
131- attrs: Dict[str, str]
132- 
133- 
134-class FileInfoParsedResult(NamedTuple):
135- """file_info元素解析结果。"""
136- file_info: FileInfo
137- move_infos: List[FileInfo]
138- dir_infos: List[Dict[str, str]]
139- expand_infos: List[Dict[str, str]]
140- 
141- 
142-class PkgMod(NamedTuple):
143- """包内文件权限。"""
144- path: str
145- mod: int
146- 
147- 
148-class PkgSoftlink(NamedTuple):
149- """包内软链接。"""
150- dst_path: str
151- src_path: str
152- 
153- 
154-class BlockConfig(NamedTuple):
155- """块配置。"""
156- 
157- dir_install_list: List[Dict]
158- move_files: List[FileInfo]
159- expand_content_list: List[Dict]
160- package_content_list: List[Dict]
161- pkg_mods: List[PkgMod]
162- pkg_softlinks: List[PkgSoftlink]
163- generate_infos: List[GenerateInfo]
164- 
165- 
166-class PackerConfig(NamedTuple):
167- """打包相关配置。"""
168- fill_is_common_path: Callable[[FileList], Iterator[FileItem]]
169- 
170- 
171-class XmlConfig(NamedTuple):
172- """打包xml配置。"""
173- default_config: Dict[str, str]
174- package_attr: PackageAttr
175- version_info: VersionInfo
176- blocks: List[BlockConfig]
177- version: str
178- version_xml: Optional[VersionXml]
179- packer_config: PackerConfig
180- 
181- def _collect_list(self, list_name):
182- result = []
183- for block in self.blocks:
184- result.extend(getattr(block, list_name))
185- return result
186- 
187- @property
188- def dir_install_list(self):
189- return self._collect_list('dir_install_list')
190- 
191- @property
192- def move_content_list(self):
193- return self._collect_list('move_files')
194- 
195- @property
196- def expand_content_list(self):
197- return self._collect_list('expand_content_list')
198- 
199- @property
200- def package_content_list(self):
201- return self._collect_list('package_content_list')
202- 
203- @property
204- def pkg_mods(self) -> List[PkgMod]:
205- return self._collect_list('pkg_mods')
206- 
207- @property
208- def pkg_softlinks(self) -> List[PkgSoftlink]:
209- return self._collect_list('pkg_softlinks')
210- 
211- @property
212- def generate_infos(self) -> List[GenerateInfo]:
213- return self._collect_list('generate_infos')
214- 
215- 
216-# 默认包属性
217-DEFAULT_PACKAGE_ATTR = {
218- 'gen_version_info': True,
219-}
220- 
221- 
222-def parse_package_info(package_info_ele: Optional[ET.Element]) -> Dict:
223- """解析package_info元素。"""
224- 
225- def get_package_info_attrs(ele: ET.Element) -> Iterator[Tuple[str, Union[str, bool]]]:
226- # expand_asterisk: 展开配置中星号
227- # parallel: 并行复制文件
228- # parallel_limit: 限制并发数
229- # package_check: 检查filelist.csv中配置目录是否完整
230- # check_features: 检查filelist.csv中所有feature是否符合package_check
231- # gen_version_info: 是否生成version.info文件
232- bool_attrs = (
233- 'expand_asterisk', 'parallel', 'parallel_limit', 'package_check', 'check_features',
234- 'use_move', 'gen_version_info', 'copy_all'
235- )
236- bool_values = ('t', 'true', 'y', 'yes')
237- if ele.tag in bool_attrs:
238- if ele.text.lower() in bool_values:
239- yield ele.tag, True
240- else:
241- yield ele.tag, False
242- else:
243- yield ele.tag, ele.text
244- 
245- if not package_info_ele:
246- return {}
247- 
248- attr = dict(
249- chain.from_iterable(map(get_package_info_attrs, list(package_info_ele)))
250- )
251- 
252- return attr
253- 
254- 
255-def parse_package_attr_by_args(args: Namespace) -> Dict:
256- """通过命令行参数解析"""
257- 
258- def pairs():
259- if hasattr(args, 'chip_name') and args.chip_name:
260- yield 'chip_name', args.chip_name
261- if hasattr(args, 'suffix') and args.suffix:
262- yield 'suffix', args.suffix
263- if hasattr(args, 'func_name') and args.func_name:
264- yield 'func_name', args.func_name
265- 
266- return dict(pairs())
267- 
268- 
269-def parse_package_attr(root_ele: ET.Element, args: Namespace) -> Dict:
270- """通过根元素解析package_info元素。"""
271- package_info_ele = root_ele.find("package_info")
272- return merge_dict(
273- DEFAULT_PACKAGE_ATTR,
274- parse_package_info(package_info_ele),
275- parse_package_attr_by_args(args),
276- )
277- 
278- 
279-def render_cann_version(a_ver: int,
280- b_ver: int,
281- c_ver: Optional[int],
282- d_ver: Optional[int],
283- e_ver: Optional[int],
284- f_ver: Optional[int]) -> str:
285- """渲染CANN版本号。"""
286- buffer = StringIO()
287- buffer.write('(')
288- buffer.write(f'({a_ver + 1} * 100000000) + ({b_ver + 1} * 1000000)')
289- if c_ver is not None:
290- buffer.write(f' + ({c_ver + 1} * 10000)')
291- if d_ver is not None:
292- buffer.write(f' + (({d_ver + 1} * 100) + 5000)')
293- if e_ver is not None:
294- buffer.write(f' + ({e_ver + 1} * 100)')
295- if f_ver is not None:
296- buffer.write(f' + {f_ver}')
297- buffer.write(')')
298- return buffer.getvalue()
299-def render_semver(package_name: str, version: str) -> Iterator[Tuple[str, str]]:
300- """
301- 将语义化版本号转换为可比较的证书表达式,严格遵循SemVer规范
302- 排序规则:
303- 1. 正式版本 > 所有对应预发布版本 (如 8.0.5 > 8.0.5-rc.1
304- 2. 预发布类型优先级:rc > beta > alpha > 其他类型 (如 rc.1 > beta.100
305- 3. 同类型预发布版本:序号越大优先级越高
306- 4. 多段序号比较:从左到右逐段比较
307- """
308- expr_buffer = StringIO()
309- expr_buffer.write('(')
310- 
311- # 移除构建元数据
312- version = version.split('+')[0]
313- # 分离正式版本和预发布版本
314- pre_release = None
315- if '-' in version:
316- release_part, pre_release = version.split('-', 1)
317- release_part = release_part.split('.')
318- else:
319- release_part = version.split('.')
320- if len(release_part) > 3:
321- pre_release = '.'.join(release_part[3:])
322- release_part = release_part[:3]
323- # 解析正式版本号
324- try:
325- major, minor, patch = map(int, release_part)
326- except (ValueError, TypeError) as ex:
327- raise IllegalVersionDir(f"无效的版本号格式: {version}") from ex
328- 
329- yield f'{package_name}_VERSION_STR', f'"{version}"'
330- yield f'{package_name}_MAJOR', str(major)
331- yield f'{package_name}_MINOR', str(minor)
332- yield f'{package_name}_PATCH', str(patch)
333- 
334- expr_buffer.write(f'({major} * 10000000) + ({minor} * 100000) + ({patch} * 1000)')
335- 
336- if not pre_release:
337- expr_buffer.write(')')
338- yield f'{package_name}_PRERELEASE', '""'
339- yield f'{package_name}_VERSION_NUM', expr_buffer.getvalue()
340- return
341- 
342- yield f'{package_name}_PRERELEASE', F'"{pre_release}"'
343- 
344- # 预发布类型权重(值越小优先级越高)
345- type_weights = {
346- 'rc': 100,
347- 'beta': 200,
348- 'alpha': 300,
349- }
350- 
351- def calc_pre_release() -> Tuple[int, int]:
352- """计算预发布版本"""
353- if '.' in pre_release:
354- pre_parts = pre_release.split('.')
355- pre_type = pre_parts[0]
356- 
357- pre_nums = []
358- for part in pre_parts[1:]:
359- if part.isdigit():
360- pre_nums.append(int(part))
361- if not pre_nums:
362- pre_nums = [0]
363- 
364- pre_type_weight = type_weights.get(pre_type, 400)
365- 
366- num_str = ''.join(map(str, pre_nums))
367- num_value = int(num_str)
368- return pre_type_weight, num_value
369- for pre_type in type_weights:
370- if pre_release.startswith(pre_type):
371- pre_type_weight = type_weights[pre_type]
372- num_value = int(pre_release[len(pre_type):])
373- return pre_type_weight, num_value
374- 
375- return None, None
376- 
377- try:
378- pre_type_weight, num_value = calc_pre_release()
379- except (ValueError, TypeError) as ex:
380- raise IllegalVersionDir(f"无效的预发布版本:{pre_release}") from ex
381- 
382- if not pre_type_weight:
383- raise IllegalVersionDir(f"无效的预发布版本:{pre_release}")
384- 
385- expr_buffer.write(f' - {pre_type_weight} + {num_value}')
386- expr_buffer.write(')')
387- 
388- yield f'{package_name}_VERSION_NUM', expr_buffer.getvalue()
389- 
390- 
391-def get_cann_version_info(name: str, version: str) -> Iterator[Tuple[str, str]]:
392- """获取CANN版本号信息。"""
393- version_info = []
394- 
395- # 删除字符串中的_VERSION
396- package_name = name[:-8]
397- 
398- if not version:
399- yield f'{package_name}_VERSION_STR', '"0"'
400- return
401- 
402- yield from render_semver(package_name, version)
403-def get_default_env_items() -> Iterator[Tuple[str, str]]:
404- """获取默认环境字典条目。"""
405- yield 'VERSION_DIR', ''
406- yield 'HOME', os.environ.get('HOME')
407- 
408- 
409-def get_env_items_by_version(version: Optional[str]) -> Iterator[Tuple[str, str]]:
410- """根据version获取环境字典条目。"""
411- if version:
412- yield 'ASCEND_VER', version
413- 
414- version_parts = version.split('.')
415- for idx in range(1, len(version_parts) + 1):
416- yield f'CUR_VER[{idx}]', '.'.join(version_parts[:idx])
417- yield 'CUR_VER', version
418- yield 'LOWER_CUR_VER', version.lower()
419- 
420- 
421-def get_env_items_by_version_dir(version_dir: Optional[str]) -> Iterator[Tuple[str, str]]:
422- """根据version_dir获取环境字典条目。"""
423- if version_dir:
424- yield 'VERSION_DIR', version_dir
425- 
426- 
427-def get_os_arch_default_env_items() -> Iterator[Tuple[str, str]]:
428- """获取系统相关默认环境字典条目。"""
429- yield 'OS_NAME', 'linux'
430- yield 'OS_VER', ''
431- yield 'ARM', 'aarch64'
432- yield 'TARGET_ENV', '$(TARGET_ENV)'
433- 
434- 
435-def get_env_items_by_os_arch(os_arch: str) -> Iterator[Tuple[str, str]]:
436- """根据os_arch获取环境字典条目。"""
437- if os_arch:
438- os_name, os_ver, arch = parse_os_arch(os_arch)
439- yield 'OS_NAME', os_name
440- yield 'OS_VER', os_ver
441- yield 'ARCH', arch
442- yield 'OS_ARCH', os_arch
443- if arch in ('arm', 'sw_64'):
444- yield 'ARM', arch
445- else:
446- yield 'ARM', 'aarch64'
447- yield 'TARGET_ENV', f"{arch}-linux"
448- else:
449- yield from get_os_arch_default_env_items()
450- 
451- 
452-def get_env_items_by_timestamp(timestamp: Optional[str]) -> Iterator[Tuple[str, str]]:
453- """根据timestamp获取环境字典条目。"""
454- if timestamp:
455- yield 'TIMESTAMP', timestamp
456- yield 'TIMESTAMP_NO', timestamp.replace('_', '')
457- else:
458- yield 'TIMESTAMP', '0'
459- yield 'TIMESTAMP_NO', '0'
460- 
461- 
462-def parse_env_dict(os_arch: str,
463- package_attr: PackageAttr,
464- version: Optional[str],
465- version_dir: Optional[str],
466- timestamp: Optional[str]) -> EnvDict:
467- """解析环境变量字典。"""
468- env_dict = dict(
469- chain(
470- get_default_env_items(),
471- yield_if(('ARCH', package_attr.get('default_arch')), itemgetter(1)),
472- get_env_items_by_os_arch(os_arch),
473- get_env_items_by_version(version),
474- get_env_items_by_version_dir(version_dir),
475- yield_if(('VERSION_DIR', version_dir), constant(version_dir)),
476- get_env_items_by_timestamp(timestamp),
477- )
478- )
479- 
480- return env_dict
481- 
482- 
483-def get_timestamp(args: Namespace) -> Optional[str]:
484- """获取触发时间戳。"""
485- if 'tag' not in args:
486- return None
487- 
488- tag = args.tag
489- if tag:
490- timestamp_re = r"\d{8}_\d{9}"
491- timestamp_list = re.findall(timestamp_re, tag)
492- if not timestamp_list:
493- raise PackageError("The {} format is incorrect.".format(tag))
494- timestamp = timestamp_list[-1]
495- else:
496- timestamp = None
497- return timestamp
498- 
499- 
500-def extract_element_attrib(ele: ET.Element) -> Dict:
501- """提取元素属性。"""
502- return ele.attrib.copy()
503- 
504- 
505-def extract_generate_info_content(generate_info_ele: ET.Element, env_dict: EnvDict) -> Dict:
506- """提取生成信息内容。"""
507- file_content = {
508- sub_item.tag: replace_env(env_dict, sub_item.text)
509- for sub_item in list(generate_info_ele)
510- }
511- return {
512- 'content': file_content
513- }
514- 
515- 
516-def parse_generate_infos_by_loaded_block(loaded_block: LoadedBlockElement,
517- default_config: Dict[str, str],
518- env_dict: EnvDict) -> List[Dict]:
519- """根据根元素解析生成信息列表。"""
520- return invoke(
521- pipe(
522- partial(
523- map, pipe(
524- dispatch(
525- pipe(
526- extract_element_attrib,
527- partial(merge_dict, default_config),
528- partial(evaluate_info, loaded_block=loaded_block, env_dict=env_dict),
529- ),
530- partial(extract_generate_info_content, env_dict=env_dict),
531- ),
532- star_apply(merge_dict),
533- )
534- ),
535- list,
536- ),
537- loaded_block.root_ele.findall('generate_info')
538- )
539- 
540- 
541-def join_pkg_inner_softlink(link_str_list: List[str]) -> str:
542- """合并pkg_inner_softlink"""
543- path = "/".join(link_str_list)
544- return os.path.normpath(path)
545- 
546- 
547-def check_contain_asterisk(value: str) -> bool:
548- """检查串是否包含星号。"""
549- if '*' in value:
550- return True
551- return False
552- 
553- 
554-def check_value(value: str,
555- package_check: bool,
556- package_attr: PackageAttr):
557- """检查元素value属性。"""
558- if package_check and package_attr.get('suffix') == 'run':
559- if check_contain_asterisk(value):
560- raise ContainAsteriskError(value)
561- 
562- 
563-def get_dst_prefix(file_info: FileInfo, env: ParseEnv) -> str:
564- """获取文件的前缀。"""
565- return os.path.join(env.delivery_dir, file_info['dst_path'])
566- 
567- 
568-def get_dst_target(file_info: FileInfo, env: ParseEnv) -> str:
569- """获取文件的实际路径。"""
570- dst_prefix = get_dst_prefix(file_info, env)
571- return os.path.join(dst_prefix, os.path.basename(file_info.get('value')))
572- 
573- 
574-def make_hash(filepath: str) -> str:
575- """计算文件的hash(sha256)值。"""
576- sha256_hash = hashlib.sha256()
577- with open(filepath, "rb") as file:
578- sha256_hash.update(file.read())
579- 
580- return sha256_hash.hexdigest()
581- 
582- 
583-def config_hash(parsed_result: FileInfoParsedResult, env: ParseEnv):
584- """配置hash值。"""
585- file_info = parsed_result.file_info
586- # 如果配置了configurable,需要计算文件的hash值
587- if file_info and file_info['configurable'] == 'TRUE':
588- src_target = get_dst_target(file_info, env)
589- hash_value = make_hash(src_target)
590- file_info['hash'] = hash_value
591- return parsed_result
592- 
593- 
594-def apply_func(func: Callable[[str], str],
595- value: Union[List[str], Set[str], str]
596- ) -> Union[List[str], Set[str], str]:
597- """对一个字符串,或字符串序列,应用函数。"""
598- # 如:pkg_softlink列表
599- if isinstance(value, list):
600- return list(map(func, value))
601- # 如:feature集合
602- if isinstance(value, set):
603- return set(map(func, value))
604- return func(value)
605- 
606- 
607-REAL_PREFIX = 'real:'
608- 
609- 
610-def join_dst_path(base: str, other: str) -> str:
611- """联结dst_path。"""
612- if other.startswith('real:'):
613- other = other[len(REAL_PREFIX):]
614- return other
615- return os.path.join(base, other)
616- 
617- 
618-def evaluate_info(info: Dict[str, str],
619- loaded_block: LoadedBlockElement,
620- env_dict: EnvDict) -> Dict[str, str]:
621- """info元素求值。"""
622- dst_keys = ('dst_path',)
623- 
624- replace_env_func = partial(replace_env, env_dict)
625- add_dst_path_func = partial(join_dst_path, loaded_block.dst_path)
626- 
627- def split_pkg_softlink(key: str, value: str) -> Tuple[str, str]:
628- if key == 'pkg_softlink':
629- return key, value.split(';')
630- return key, value
631- 
632- def upper_value(key: str, value: str) -> Tuple[str, str]:
633- if key == 'configurable':
634- return key, value.upper()
635- return key, value
636- 
637- def add_dst_path(key: str, value: str) -> Tuple[str, str]:
638- if key in dst_keys:
639- return key, apply_func(add_dst_path_func, value)
640- return key, value
641- 
642- def replace_pkg_inner_softlink(key: str, value: str) -> Tuple[str, str]:
643- if key == 'pkg_inner_softlink':
644- return key, 'NA'
645- return key, value
646- 
647- def merge_feature(key: str, value: str) -> Tuple[str, Set[str]]:
648- if key in ('chip', 'feature'):
649- config_features = config_feature_to_set(value, key)
650- return key, config_features | getattr(loaded_block, f'{key}s')
651- return key, value
652- 
653- def eval_value(_key: str, value: str) -> str:
654- if value is not None:
655- return apply_func(replace_env_func, value)
656- 
657- eval_value_func = star_pipe(
658- split_pkg_softlink, upper_value, add_dst_path, replace_pkg_inner_softlink,
659- merge_feature, eval_value,
660- )
661- 
662- return {
663- key: eval_value_func(key, value)
664- for key, value in
665- itertools.chain(
666- # 默认值配置
667- [
668- ('dst_path', ''), ('configurable', 'FALSE'),
669- ('chip', None), ('feature', None), ('pkg_feature', None),
670- ],
671- info.items()
672- )
673- }
674- 
675- 
676-def parse_dir_info_elements(loaded_block: LoadedBlockElement,
677- default_config: Dict[str, str],
678- package_attr: PackageAttr,
679- env: ParseEnv) -> List[Dict[str, str]]:
680- """解析dir_info元素。"""
681- dir_info_elements: List[ET.Element] = loaded_block.root_ele.findall('dir_info')
682- dir_infos = []
683- for item in dir_info_elements:
684- dir_config = default_config.copy()
685- dir_config.update(item.attrib)
686- dir_config['module'] = dir_config.get('value')
687- for sub_item in list(item):
688- dir_info = dir_config.copy()
689- dir_info.update(sub_item.attrib)
690- dir_info = evaluate_info(dir_info, loaded_block, env.env_dict)
691- check_value(
692- dir_info['value'], env.parse_option.package_check, package_attr
693- )
694- dir_infos.append(dir_info)
695- 
696- return dir_infos
697- 
698- 
699-def get_target_name(target_conf) -> str:
700- """获取目标名。"""
701- rename = target_conf.get('rename')
702- if rename:
703- return rename
704- 
705- value_list = target_conf.get('value').split('/')
706- target_name = value_list[-1] if value_list[-1] else value_list[-2]
707- return target_name
708- 
709- 
710-def expand_dir(file_info: FileInfo, get_dst_target_func: Callable[[FileInfo], str], env: ParseEnv):
711- """
712- 如果file_info中配置的路径是文件夹,需要展开到文件
713- """
714- file_info_list = []
715- dir_info_list = []
716- dst_target = get_dst_target_func(file_info)
717- 
718- value_list = file_info.get('value').split('/')
719- target_name = value_list[-1] if value_list[-1] else value_list[-2]
720- 
721- # 这里把当前目录也加入到dir_info_list中
722- dir_info_copy = file_info.copy()
723- dir_info_copy['module'] = file_info.get('value')
724- dir_info_copy['value'] = os.path.join(
725- file_info.get('install_path', ''), target_name
726- )
727- 
728- # 子目录的权限按照xml中subdir_mod配置,如果没有配置subdir_mod按照install_mod配置
729- subdir_mod = file_info.get("subdir_mod", None)
730- if subdir_mod is not None:
731- dir_info_copy['install_mod'] = subdir_mod
732- # 被展开的当前目录不需要设置softlink
733- dir_info_copy['install_softlink'] = ''
734- dir_info_copy['pkg_inner_softlink'] = ''
735- dir_info_list.append(dir_info_copy)
736- 
737- for root, dirs, files in os.walk(dst_target, followlinks=True):
738- # 不同操作系统上,os.walk遍历的结果顺序会略有不同,这里按字母排序,保证不同系统一致
739- dirs.sort()
740- files.sort()
741- 
742- dirs_to_remove = []
743- for name in dirs:
744- dirname = os.path.join(root, name)
745- # 如果是指向目录的软连接,则按照文件处理,无需在安装时创建目录,只需要卸载时删除就行
746- if os.path.islink(dirname) and not need_dereference(file_info):
747- copy_file_info = create_file_info(dirname, dst_target, file_info, name, target_name)
748- # 被展开的子文件不需要设置softlink
749- copy_file_info['install_softlink'] = ''
750- copy_file_info['pkg_inner_softlink'] = ''
751- deal_with_copy_all(copy_file_info, env.package_attr)
752- file_info_list.append(copy_file_info)
753- dirs_to_remove.append(name)
754- continue
755- relative_dirname = os.path.relpath(dirname, dst_target)
756- dir_info_copy = file_info.copy()
757- dir_info_copy['module'] = file_info.get('value')
758- dir_info_copy['value'] = os.path.join(
759- file_info.get('install_path', ''), target_name, relative_dirname
760- )
761- # 被展开的子目录不需要设置softlink
762- dir_info_copy['install_softlink'] = ''
763- dir_info_copy['pkg_inner_softlink'] = ''
764- # 子目录的权限按照xml中subdir_mod配置,如果没有配置subdir_mod按照install_mod配置
765- subdir_mod = file_info.get("subdir_mod", None)
766- if subdir_mod is not None:
767- dir_info_copy['install_mod'] = subdir_mod
768- dir_info_list.append(dir_info_copy)
769- for name in files:
770- filename = os.path.join(root, name)
771- copy_file_info = create_file_info(filename, dst_target, file_info, name, target_name)
772- deal_with_copy_all(copy_file_info, env.package_attr)
773- file_info_list.append(copy_file_info)
774- 
775- for name in dirs_to_remove:
776- dirs.remove(name)
777- return file_info_list, dir_info_list
778- 
779- 
780-def create_file_info(dirname, dst_target, file_info, name, target_name):
781- relative_filename = os.path.relpath(dirname, dst_target)
782- relative_dir_name = os.path.split(relative_filename)[0]
783- copy_file_info = file_info.copy()
784- copy_file_info['value'] = name
785- copy_file_info['src_path'] = os.path.join(
786- file_info['src_path'], file_info['value'], relative_dir_name
787- )
788- copy_file_info['dst_path'] = os.path.join(
789- file_info['dst_path'], target_name, relative_dir_name
790- )
791- copy_file_info['install_path'] = os.path.join(
792- file_info.get('install_path', ''), target_name, relative_dir_name
793- )
794- return copy_file_info
795- 
796- 
797-def expand_file_info_asterisk(parsed_result: FileInfoParsedResult,
798- env: ParseEnv) -> Iterator[FileInfoParsedResult]:
799- """展开FileInfoParsedResult中的星号。"""
800- file_info = parsed_result.file_info
801- if check_contain_asterisk(file_info.get('value', '')):
802- dst_prefix = get_dst_prefix(file_info, env)
803- dst_targets = sorted(glob.glob(get_dst_target(file_info, env)))
804- if 'exclude' in file_info:
805- exclude = list(map(methodcaller('strip'), file_info['exclude'].split(';')))
806- else:
807- exclude = []
808- for dst_target in dst_targets:
809- value = os.path.relpath(dst_target, dst_prefix)
810- if value in exclude:
811- continue
812- new_file_info = file_info.copy()
813- new_file_info['value'] = value
814- if 'pkg_inner_softlink' in new_file_info:
815- # pkg_inner_softlink中的特殊变量$(FILE)替换为展开后的文件名
816- pkg_inner_softlink = new_file_info['pkg_inner_softlink']
817- new_file_info['pkg_inner_softlink'] = pkg_inner_softlink.replace(
818- '$(FILE)', os.path.basename(dst_target)
819- )
820- yield parsed_result._replace(file_info=new_file_info)
821- else:
822- yield parsed_result
823- 
824- 
825-def trans_to_stream(item: Any) -> Iterator[Any]:
826- """转换为流。"""
827- yield item
828- 
829- 
830-def need_dereference(file_info: FileInfo) -> bool:
831- """是否需要解引用。"""
832- if 'dereference' in file_info:
833- return True
834- return False
835- 
836- 
837-def need_expand(file_info: FileInfo, get_dst_target_func: Callable[[FileInfo], str]) -> bool:
838- """是否需要展开子目录。"""
839- if file_info.get('entity') == 'true':
840- return False
841- dst_target = get_dst_target_func(file_info)
842- if os.path.isdir(dst_target):
843- if need_dereference(file_info):
844- return True
845- if os.path.islink(dst_target):
846- return False
847- return True
848- return False
849- 
850- 
851-def deal_with_copy_all(file_info: FileInfo, package_attr: PackageAttr):
852- """处理copy_all选项。"""
853- if not package_attr.get('copy_all'):
854- return
855- if file_info.get('pkg_inner_softlink'):
856- raise PkgInnerSoftlinkNotAllowed(file_info)
857- file_info['dst_path'] = file_info['install_path']
858- if file_info.get('install_softlink'):
859- file_info['pkg_softlink'] = file_info['install_softlink'].split(';')
860- if file_info.get('install_mod'):
861- file_info['pkg_mod'] = file_info['install_mod']
862- 
863- 
864-def expand_file_info(parsed_result: FileInfoParsedResult,
865- use_move: bool,
866- get_dst_target_func: Callable[[FileInfo], str],
867- env: ParseEnv) -> FileInfoParsedResult:
868- """展开FileInfoParsedResult中的目录。"""
869- deal_with_copy_all(parsed_result.file_info, env.package_attr)
870- 
871- file_info = parsed_result.file_info
872- if need_expand(file_info, get_dst_target_func):
873- # 如果当前是文件夹,需要展开计算
874- expand_infos, dir_infos = expand_dir(file_info, get_dst_target_func, env)
875- # 实测发现,对于opp包,整体目录cp的安装速度要快于目录中各文件mv
876- # 可能的原因是,cp遍历目录的速度较快,并且目录中的文件都比较小。mv依赖shell迭代目录中的所有文件。
877- return FileInfoParsedResult(
878- merge_dict(file_info, {'is_dir': True}), [], dir_infos, expand_infos
879- )
880- 
881- if use_move:
882- return FileInfoParsedResult(
883- {}, [file_info], parsed_result.dir_infos, parsed_result.expand_infos
884- )
885- 
886- return parsed_result
887- 
888- 
889-def trans_file_info_to_result(file_info: FileInfo) -> FileInfoParsedResult:
890- """file_info转换为FileInfoParsedResult。"""
891- return FileInfoParsedResult(file_info, [], [], [])
892- 
893- 
894-def parse_file_element(file_ele: ET.Element,
895- file_config: Dict[str, str],
896- loaded_block: LoadedBlockElement,
897- package_attr: PackageAttr,
898- env: ParseEnv) -> Iterator[FileInfoParsedResult]:
899- """解析file元素。"""
900- file_info = merge_dict(file_config, file_ele.attrib)
901- file_info = evaluate_info(file_info, loaded_block, env.env_dict)
902- 
903- if package_attr.get('expand_asterisk', False):
904- expand_asterisk_func = partial(expand_file_info_asterisk, env=env)
905- else:
906- expand_asterisk_func = trans_to_stream
907- 
908- if 'install_path' not in file_info:
909- file_info['install_path'] = ''
910- 
911- trans_file_info_func = pipe(
912- trans_file_info_to_result,
913- expand_asterisk_func,
914- partial(map, partial(config_hash, env=env)),
915- partial(
916- map,
917- partial(
918- expand_file_info,
919- use_move=loaded_block.use_move,
920- get_dst_target_func=partial(get_dst_target, env=env),
921- env=env
922- )
923- ),
924- )
925- 
926- yield from trans_file_info_func(file_info)
927- 
928- 
929-def parse_file_info_elements(loaded_block: LoadedBlockElement,
930- default_config: Dict[str, str],
931- package_attr: PackageAttr,
932- env: ParseEnv) -> Iterator[FileInfoParsedResult]:
933- """解析file_info元素。"""
934- file_info_elements: List[ET.Element] = loaded_block.root_ele.findall('file_info')
935- for file_info_ele in file_info_elements:
936- file_config = merge_dict(
937- default_config,
938- file_info_ele.attrib,
939- {'module': file_info_ele.attrib.get('value')}
940- )
941- 
942- for sub_item in list(file_info_ele):
943- yield from parse_file_element(
944- sub_item, file_config, loaded_block, package_attr, env
945- )
946- 
947- 
948-def get_path_infos(pkg_ele: ET.Element,
949- default_config: Dict[str, str],
950- loaded_block: LoadedBlockElement,
951- env: ParseEnv) -> List[Dict[str, str]]:
952- """获取路径信息。"""
953- return invoke(
954- pipe(
955- list,
956- partial(map, attrgetter('attrib')),
957- partial(map, partial(merge_dict, default_config)),
958- partial(
959- map,
960- partial(evaluate_info, loaded_block=loaded_block, env_dict=env.env_dict)
961- ),
962- ),
963- pkg_ele
964- )
965- 
966- 
967-def get_path_infos_by_eles(pkg_eles: List[ET.Element],
968- default_config: Dict[str, str],
969- loaded_block: LoadedBlockElement,
970- env: ParseEnv) -> List[Dict[str, str]]:
971- """根据节点列表获取路径信息。"""
972- return flatten([
973- get_path_infos(pkg_ele, default_config, loaded_block, env)
974- for pkg_ele in pkg_eles
975- ])
976- 
977- 
978-def parse_pkg_mods(path_infos: List[Dict[str, str]]) -> List[PkgMod]:
979- """解析pkg元素。"""
980- return [
981- PkgMod(
982- path_info['value'],
983- int(path_info['pkg_mod'], 8)
984- )
985- for path_info in path_infos
986- ]
987- 
988- 
989-def parse_pkg_softlinks(path_infos: List[Dict[str, str]]) -> List[PkgSoftlink]:
990- """解析pkg_softlink元素。"""
991- return [
992- PkgSoftlink(
993- dst_path=path_info['value'],
994- src_path=path_info['src_path'],
995- )
996- for path_info in path_infos
997- ]
998- 
999- 
1000-def parse_paths_element(root_ele: ET.Element,
1001- tag_name: str,
1002- ex: PackageError,
1003- parse_func: Callable[[List[ET.Element]], List]) -> List:
1004- """解析路径列表元素。"""
1005- tag_eles = root_ele.findall(tag_name)
1006- if len(tag_eles) > 1:
1007- raise ex
1008- 
1009- return parse_func(tag_eles)
1010- 
1011- 
1012-def unique_infos(infos: Iterable) -> List[Dict[str, str]]:
1013- """infos去重。"""
1014- cache: Set[str] = set()
1015- new_infos = []
1016- for info in infos:
1017- if info['value'] in cache:
1018- continue
1019- cache.add(info['value'])
1020- new_infos.append(info)
1021- 
1022- return new_infos
1023- 
1024- 
1025-def parse_block_config(loaded_block: LoadedBlockElement,
1026- package_attr: PackageAttr,
1027- parse_env: ParseEnv):
1028- """解析块配置。"""
1029- default_config = copy.copy(loaded_block.attrs)
1030- default_config.update(loaded_block.root_ele.attrib)
1031- 
1032- dir_infos = parse_dir_info_elements(loaded_block, default_config,
1033- package_attr, parse_env,
1034- )
1035- file_info_results = list(
1036- chain(
1037- parse_file_info_elements(loaded_block, default_config,
1038- package_attr, parse_env,
1039- )
1040- )
1041- )
1042- 
1043- get_path_infos_func = partial(
1044- get_path_infos_by_eles,
1045- default_config=default_config,
1046- loaded_block=loaded_block,
1047- env=parse_env
1048- )
1049- 
1050- parse_pkg_mods_func = pipe(get_path_infos_func, parse_pkg_mods)
1051- pkg_mods = parse_paths_element(
1052- loaded_block.root_ele,
1053- 'pkg_mod',
1054- MultiPkgModError(),
1055- # tar.gz才处理pkg_mod元素
1056- lambda x: parse_pkg_mods_func(x) if package_attr.get('suffix') == 'tar.gz' else [],
1057- )
1058- 
1059- parse_pkg_softlinks_func = pipe(get_path_infos_func, parse_pkg_softlinks)
1060- pkg_softlinks = parse_paths_element(
1061- loaded_block.root_ele,
1062- 'pkg_softlink',
1063- MultiPkgSoftlinkError(),
1064- parse_pkg_softlinks_func,
1065- )
1066- 
1067- generate_infos = parse_generate_infos_by_loaded_block(
1068- loaded_block, default_config, parse_env.env_dict
1069- )
1070- 
1071- return BlockConfig(
1072- unique_infos(
1073- itertools.chain(dir_infos,
1074- flatten(result.dir_infos for result in file_info_results)
1075- )
1076- ),
1077- list(flatten(map(attrgetter('move_infos'), file_info_results))),
1078- list(flatten(map(attrgetter('expand_infos'), file_info_results))),
1079- [result.file_info for result in file_info_results if result.file_info],
1080- pkg_mods,
1081- pkg_softlinks,
1082- generate_infos,
1083- )
1084- 
1085- 
1086-def make_loaded_block_element(root_ele: ET.Element,
1087- dst_path: str = '') -> LoadedBlockElement:
1088- """创建加载后的块配置。"""
1089- return LoadedBlockElement(root_ele, False, dst_path, set(), set(), {})
1090- 
1091- 
1092-def parse_block_element(block_ele: ET.Element,
1093- block_info_attr: Dict[str, str]) -> BlockElement:
1094- """解析单个块配置。"""
1095- 
1096- def filter_attrs(attrs: Dict[str, str]) -> Dict[str, str]:
1097- # block属性中过滤掉dst_path与block_conf_path
1098- # dst_path由单独的参数传递
1099- # block中不需要block_conf_path
1100- return {
1101- key: value
1102- for key, value in attrs.items()
1103- if key not in ('dst_path', 'block_conf_path')
1104- }
1105- 
1106- def with_merged_attrs(attrs: Dict[str, str]) -> BlockElement:
1107- name = attrs.get('name')
1108- block_conf_path = attrs.get('block_conf_path')
1109- 
1110- if not name:
1111- raise BlockConfigError("block's name is not set!")
1112- 
1113- if not block_conf_path:
1114- raise BlockConfigError("block's conf_path is not set!")
1115- 
1116- return BlockElement(
1117- name=name,
1118- block_conf_path=block_conf_path,
1119- dst_path=attrs.get('dst_path', ''),
1120- chips=config_feature_to_set(attrs.get('chip'), 'chip'),
1121- features=config_feature_to_set(attrs.get('feature'), 'feature'),
1122- attrs=filter_attrs(attrs)
1123- )
1124- 
1125- return with_merged_attrs(merge_dict(block_info_attr, block_ele.attrib))
1126- 
1127- 
1128-def parse_block_info(block_info: ET.Element) -> List[BlockElement]:
1129- """解析块配置。"""
1130- def parse_block_elements(block_elements: List[ET.Element]) -> List[BlockElement]:
1131- return [
1132- parse_block_element(block_ele, block_info.attrib) for block_ele in block_elements
1133- ]
1134- 
1135- return parse_block_elements(list(block_info))
1136- 
1137- 
1138-def get_block_filepath(block_element: BlockElement) -> str:
1139- """获取块配置路径。"""
1140- return os.path.join(
1141- pkg_utils.TOP_SOURCE_DIR, BLOCK_CONFIG_PATH, block_element.block_conf_path,
1142- f'{block_element.name}.xml'
1143- )
1144- 
1145- 
1146-def load_block_element(package_attr: PackageAttr,
1147- block_element: BlockElement) -> LoadedBlockElement:
1148- """加载块配置。"""
1149- def with_filepath(block_xml: str):
1150- if not os.path.exists(block_xml):
1151- raise BlockConfigError(f"block's config xml {block_xml} does not exist!")
1152- 
1153- try:
1154- return LoadedBlockElement(
1155- root_ele=ET.parse(block_xml).getroot(),
1156- use_move=package_attr.get('use_move', False),
1157- **{
1158- name: getattr(block_element, name)
1159- for name in BLOCK_ELEMENT_PASS_THROUGH_ARGS
1160- }
1161- )
1162- except Exception:
1163- raise BlockConfigError(f"dependent block configuration {block_xml} parse failed!")
1164- 
1165- return with_filepath(get_block_filepath(block_element))
1166- 
1167- 
1168-def parse_blocks(root_ele: ET.Element,
1169- package_attr: PackageAttr,
1170- parse_env: ParseEnv) -> List[BlockConfig]:
1171- """解析块列表。"""
1172- return [
1173- parse_block_config(
1174- loaded_block, package_attr, parse_env
1175- )
1176- for loaded_block in itertools.chain(
1177- [make_loaded_block_element(root_ele)],
1178- map(
1179- partial(load_block_element, package_attr),
1180- chain.from_iterable(
1181- map(parse_block_info, root_ele.findall("block_info"))
1182- )
1183- )
1184- )
1185- ]
1186- 
1187- 
1188-def read_version_info(delivery_dir: str, package_attr: PackageAttr) -> Tuple[str, str]:
1189- if 'install_script' not in package_attr:
1190- raise InstallScriptNotInPackageInfo()
1191- install_script = package_attr['install_script']
1192- install_script_paths = install_script.split('/')
1193- if len(install_script_paths) < 2:
1194- raise InstallScriptFormatError()
1195- 
1196- version_path = os.path.join(delivery_dir, *install_script_paths[:-2], "version.info")
1197- if not os.path.isfile(version_path):
1198- raise VersionInfoNotExist()
1199- 
1200- with open(version_path, 'r') as file:
1201- line1 = file.readline().strip()
1202- line2 = file.readline().strip()
1203- version = line1.split("=")[1]
1204- version_dir = line2.split("=")[1]
1205- m = re.match(r'[.a-zA-Z0-9]+$', version) or re.match(r'[-a-zA-Z.0-9]+$', version)
1206- if not m:
1207- raise VersionFormatNotMatch()
1208- 
1209- return version, version_dir
1210- 
1211- 
1212-def parse_xml_config(filepath: str,
1213- delivery_dir: str,
1214- parse_option: ParseOption,
1215- args: Namespace) -> XmlConfig:
1216- """解析打包xml配置。"""
1217- try:
1218- tree = ET.parse(filepath)
1219- xml_root = tree.getroot()
1220- except ET.ParseError as ex:
1221- CommLog.cilog_error("xml parse %s failed: %s!", filepath, ex)
1222- sys.exit(FAIL)
1223- 
1224- default_config = xml_root.attrib.copy()
1225- 
1226- package_attr = parse_package_attr(xml_root, args)
1227- try:
1228- version, version_dir = read_version_info(delivery_dir, package_attr)
1229- except InstallScriptNotInPackageInfo:
1230- CommLog.cilog_error("The install_script is not configured in the package_info in %s!", filepath)
1231- sys.exit(FAIL)
1232- except InstallScriptFormatError:
1233- CommLog.cilog_error("The install_script format is illegel in %s! More directory levels are needed.", filepath)
1234- sys.exit(FAIL)
1235- except VersionInfoNotExist as ex:
1236- CommLog.cilog_error("The version.info file %s does not exist in %s!", str(ex), filepath)
1237- sys.exit(FAIL)
1238- if args.disable_multi_version:
1239- version_dir = None
1240- timestamp = get_timestamp(args)
1241- try:
1242- env_dict = parse_env_dict(
1243- parse_option.os_arch, package_attr, version, version_dir, timestamp
1244- )
1245- except ParseOsArchError:
1246- CommLog.cilog_error(
1247- "os_arch %s is not correctly configured: %s!",
1248- parse_option.os_arch, filepath
1249- )
1250- sys.exit(FAIL)
1251- 
1252- parse_env = ParseEnv(
1253- env_dict, parse_option, delivery_dir, pkg_utils.TOP_SOURCE_DIR, package_attr
1254- )
1255- 
1256- blocks = parse_blocks(
1257- xml_root, package_attr, parse_env
1258- )
1259- 
1260- if is_multi_version(version_dir):
1261- fill_is_common_path_func = partial(
1262- fill_is_common_path, target_env=env_dict.get('TARGET_ENV')
1263- )
1264- else:
1265- fill_is_common_path_func = iter
1266- 
1267- return XmlConfig(
1268- default_config, package_attr, None, blocks, version, None,
1269- PackerConfig(fill_is_common_path_func)
1270- )
Dscripts/package/common/py/utils/comm_log.py+0-63
@@ -1,63 +0,0 @@
1-#!/usr/bin/env python3
2-# -*- coding: UTF-8 -*-
3-# ----------------------------------------------------------------------------
4-# Copyright (c) 2025 Huawei Technologies Co., Ltd.
5-# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
6-# CANN Open Software License Agreement Version 2.0 (the "License").
7-# Please refer to the License for details. You may not use this file except in compliance with the License.
8-# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
9-# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
10-# See LICENSE in the root of the software repository for the full text of the License.
11-# ----------------------------------------------------------------------------
12- 
13-import time
14-import inspect
15-import logging
16- 
17-logging.basicConfig(format='[%(asctime)s] [%(levelname)s] [%(pathname)s] [line:%(lineno)d] %(message)s',
18- level=logging.INFO)
19- 
20- 
21-class CommLog:
22- @staticmethod
23- def cilog_get_timestamp():
24- return time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
25- 
26- @staticmethod
27- def cilog_print_element(cilog_element):
28- print("["+cilog_element+"]", end=' ')
29- return
30- 
31- @staticmethod
32- def cilog_logmsg(log_level, filename, line_no, log_msg, *log_paras):
33- log_timestamp = CommLog.cilog_get_timestamp()
34- CommLog.cilog_print_element(log_timestamp)
35- CommLog.cilog_print_element(log_level)
36- CommLog.cilog_print_element(filename)
37- CommLog.cilog_print_element(str(line_no))
38- print(log_msg % log_paras[0])
39- return
40- 
41- @staticmethod
42- def cilog_error(log_msg, *log_paras):
43- frame = inspect.currentframe().f_back
44- line_no = frame.f_lineno
45- filename = frame.f_code.co_filename
46- CommLog.cilog_logmsg("ERROR", filename, line_no, log_msg, log_paras)
47- return
48- 
49- @staticmethod
50- def cilog_warning(log_msg, *log_paras):
51- frame = inspect.currentframe().f_back
52- line_no = frame.f_lineno
53- filename = frame.f_code.co_filename
54- CommLog.cilog_logmsg("WARNING", filename, line_no, log_msg, log_paras)
55- return
56- 
57- @staticmethod
58- def cilog_info(log_msg, *log_paras):
59- frame = inspect.currentframe().f_back
60- line_no = frame.f_lineno
61- filename = frame.f_code.co_filename
62- CommLog.cilog_logmsg("INFO", filename, line_no, log_msg, log_paras)
63- return
Dscripts/package/common/py/utils/funcbase.py+0-89
@@ -1,89 +0,0 @@
1-#!/usr/bin/env python3
2-# -*- coding: UTF-8 -*-
3-# ----------------------------------------------------------------------------
4-# Copyright (c) 2025 Huawei Technologies Co., Ltd.
5-# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
6-# CANN Open Software License Agreement Version 2.0 (the "License").
7-# Please refer to the License for details. You may not use this file except in compliance with the License.
8-# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
9-# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
10-# See LICENSE in the root of the software repository for the full text of the License.
11-# ----------------------------------------------------------------------------
12- 
13-"""函数基础库。"""
14- 
15-import operator
16-from typing import Callable, Iterator, TypeVar
17- 
18- 
19-A = TypeVar('A')
20- 
21- 
22-def constant(value: A) -> Callable[..., A]:
23- """常量值。"""
24- def constant_inner(*_args, **_kwargs) -> A:
25- return value
26- 
27- return constant_inner
28- 
29- 
30-def dispatch(*funcs):
31- """分派应用。"""
32- def dispatch_inner(*args, **kwargs) -> Iterator:
33- return (func(*args, **kwargs) for func in funcs)
34- 
35- return dispatch_inner
36- 
37- 
38-def pipe(*funcs):
39- """串联多个函数。"""
40- def pipe_func(*args, **k_args):
41- result = funcs[0](*args, **k_args)
42- for func in funcs[1:]:
43- result = func(result)
44- return result
45- 
46- return pipe_func
47- 
48- 
49-def identity(value: A) -> A:
50- """同一。"""
51- return value
52- 
53- 
54-def invoke(func, *args, **kwargs):
55- """调用。"""
56- return func(*args, **kwargs)
57- 
58- 
59-def side_effect(*funcs):
60- """调用函数,产生副作用,但不影响管道结果。"""
61- def side_effect_func(arg):
62- for func in funcs:
63- # 不保留结果
64- func(arg)
65- return arg
66- 
67- return side_effect_func
68- 
69- 
70-def star_apply(func):
71- """列表展开再应用。"""
72- def star_apply_func(arg):
73- return func(*arg)
74- 
75- return star_apply_func
76- 
77- 
78-def any_(*funcs) -> Callable:
79- """高阶any。
80- 注意,any有短路效果。"""
81- return pipe(
82- dispatch(*funcs),
83- any,
84- )
85- 
86- 
87-def not_(func) -> Callable:
88- """高阶not。"""
89- return pipe(func, operator.not_)
Dscripts/package/common/py/utils/pkg_utils.py+0-202
@@ -1,202 +0,0 @@
1-#!/usr/bin/env python3
2-# -*- coding: UTF-8 -*-
3-# ----------------------------------------------------------------------------
4-# Copyright (c) 2025 Huawei Technologies Co., Ltd.
5-# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
6-# CANN Open Software License Agreement Version 2.0 (the "License").
7-# Please refer to the License for details. You may not use this file except in compliance with the License.
8-# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
9-# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
10-# See LICENSE in the root of the software repository for the full text of the License.
11-# ----------------------------------------------------------------------------
12- 
13-"""基础构件。"""
14- 
15-import os
16-from functools import partial
17-from itertools import chain, tee
18-from operator import methodcaller
19-from pathlib import Path
20-from typing import Callable, Dict, Iterator, List, Optional, TypeVar, Set
21- 
22-TOP_DIR = str(Path(__file__).resolve().parents[5])
23-TOP_SOURCE_DIR = TOP_DIR + '/scripts/'
24-DELIVERY_PATH = "build/_CPack_Packages/makeself_staging"
25-CONFIG_SCRIPT_PATH = 'package'
26-BLOCK_CONFIG_PATH = 'package/module'
27- 
28-SUCC = 0
29-FAIL = -1
30- 
31- 
32-A = TypeVar('A')
33- 
34- 
35-class PackageError(Exception):
36- """打包异常基类。"""
37- 
38- 
39-class PackageConfigError(PackageError):
40- """打包配置错误异常。"""
41- 
42- 
43-class BlockConfigError(PackageError):
44- """块配置错误异常。"""
45- 
46- 
47-class MultiPkgModError(PackageError):
48- """多个pkg_mod元素报错。"""
49- 
50- 
51-class MultiPkgSoftlinkError(PackageError):
52- """多个pkg_softlink元素报错。"""
53- 
54- 
55-class ParseOsArchError(PackageError):
56- """解析os_arch失败异常。"""
57- 
58- 
59-class EnvNotSupported(PackageError):
60- """环境变量不支持异常。"""
61- 
62- 
63-class ContainAsteriskError(PackageError):
64- """包含星号异常。"""
65- 
66- def __init__(self, value: str):
67- super().__init__()
68- self.value = value
69- 
70- 
71-class FilelistError(PackageError):
72- """文件列表异常。"""
73- 
74- 
75-class UnknownOperateTypeError(PackageError):
76- """未知的操作类型。"""
77- 
78- 
79-class PkgInnerSoftlinkNotAllowed(PackageError):
80- """不允许使用pkg_inner_softlink。"""
81- 
82- 
83-class PackageNameEmptyError(PackageError):
84- """包名为空错误。"""
85- 
86- 
87-class GenerateFilelistError(PackageError):
88- """生成文件列表文件异常。"""
89- 
90- def __init__(self, filename: str):
91- super().__init__()
92- self.filename = filename
93- 
94- 
95-class IllegalVersionDir(PackageError):
96- """version_dir配置错误。"""
97- 
98- 
99-class CompressError(PackageError):
100- """打包错误。"""
101- 
102- def __init__(self, package_name: Optional[str]):
103- super().__init__(package_name)
104- self.package_name = package_name
105- 
106- 
107-class InstallScriptNotInPackageInfo(PackageError):
108- """package_info中没有配置install_script。"""
109- 
110- 
111-class InstallScriptFormatError(PackageError):
112- """install_script配置格式错误。"""
113- 
114- 
115-class VersionInfoNotExist(PackageError):
116- """version.info文件不存在。"""
117- 
118- 
119-def flatten(list_of_lists):
120- """Flatten one level of nesting"""
121- return chain.from_iterable(list_of_lists)
122- 
123- 
124-def merge_dict(base: Dict, *news: Dict):
125- """合并两个字典。"""
126- result = base.copy()
127- for new in news:
128- result.update(new)
129- return result
130- 
131- 
132-def star_pipe(*funcs):
133- """串联多个函数。解包结果。"""
134- def pipe_func(*args, **k_args):
135- result = funcs[0](*args, **k_args)
136- for func in funcs[1:]:
137- # 解包元组或列表结果
138- result = func(*result)
139- return result
140- 
141- return pipe_func
142- 
143- 
144-def swap_args(func):
145- """交换函数前两个参数。"""
146- def inner(fst, snd, *args, **k_args):
147- return func(snd, fst, *args, **k_args)
148- return inner
149- 
150- 
151-def conditional_apply(predicate, func):
152- """条件下应用函数。"""
153- def conditional_apply_func(arg):
154- if predicate(arg):
155- return func(arg)
156- return arg
157- 
158- return conditional_apply_func
159- 
160- 
161-def pairwise(iterable):
162- """s -> (s0,s1), (s1,s2), (s2, s3), ..."""
163- a, b = tee(iterable)
164- next(b, None)
165- return zip(a, b)
166- 
167- 
168-def path_join(base: Optional, *others: str) -> Optional:
169- """路径联合。"""
170- if base is None:
171- return None
172- return os.path.join(base, *others)
173- 
174- 
175-def yield_if(data, predicate: Callable) -> Iterator:
176- """条件满足则产生。"""
177- if predicate(data):
178- yield data
179- 
180- 
181-def config_feature_to_set(feature_str: str, feature_type: str = 'feature') -> Set[str]:
182- """配置feature转换为集合。"""
183- if feature_str is None:
184- return set()
185- 
186- if isinstance(feature_str, set):
187- return feature_str
188- 
189- if feature_str == '':
190- raise PackageConfigError(f"Not allow to config {feature_type} empty.")
191- 
192- features = set(feature_str.split(';'))
193- if 'all' in features:
194- raise PackageConfigError(f"Not allow to config {feature_type} all.")
195- return features
196- 
197- 
198-def config_feature_to_string(features: Set[str]) -> str:
199- """配置feature集合转换为字符串。"""
200- if not features:
201- return 'all'
202- return ';'.join(sorted(features))
Dscripts/package/common/py/version_info.py+0-432
@@ -1,432 +0,0 @@
1-#!/usr/bin/env python3
2-# -*- coding: UTF-8 -*-
3-# --------------------------------------------------------------------------------
4-# Copyright (c) 2025 Huawei Technologies Co., Ltd.
5-# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
6-# CANN Open Software License Agreement Version 2.0 (the "License").
7-# Please refer to the License for details. You may not use this file except in compliance with the License.
8-# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
9-# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
10-# See LICENSE in the root of the software repository for the full text of the License.
11-# --------------------------------------------------------------------------------
12- 
13-import os
14-import re
15-import sys
16-import xml.etree.ElementTree as ET
17-from functools import total_ordering
18-from pathlib import Path
19-from typing import Dict, List, NamedTuple, Optional, Tuple, Union
20-from .utils.comm_log import CommLog
21- 
22- 
23-class VersionInfoError(Exception):
24- """版本信息异常基类。"""
25- 
26- 
27-class VersionFormatNotMatch(VersionInfoError):
28- """版本格式未匹配。"""
29- 
30- 
31-class IntervalFormatNotMatch(VersionInfoError):
32- """区间格式未匹配。"""
33- 
34- 
35-class DuplicatedPkgConfig(VersionInfoError):
36- """解析版本配置失败。重复的包配置。"""
37- 
38- def __init__(self, pkg_name):
39- super().__init__(pkg_name)
40- self.pkg_name = pkg_name
41- 
42- 
43-class ParseVersionFailed(VersionInfoError):
44- """解析版本失败。"""
45- 
46- 
47-class CollectRequiresFailed(VersionInfoError):
48- """收集包需求失败。"""
49- 
50- def __init__(self, pkg_name, version_str, msg):
51- super().__init__(pkg_name, version_str, msg)
52- self.pkg_name = pkg_name
53- self.version_str = version_str
54- self.msg = msg
55- 
56- 
57-@total_ordering
58-class Version:
59- """版本号。"""
60- 
61- def __init__(self, version):
62- self.version = version
63- 
64- @classmethod
65- def match(cls, input_str):
66- """输入字符串是否匹配版本号模式。"""
67- m = re.match(r'[.a-zA-Z0-9]+$', input_str)
68- return bool(m)
69- 
70- @classmethod
71- def parse(cls, input_str):
72- """解析版本号。"""
73- if not cls.match(input_str):
74- raise VersionFormatNotMatch()
75- 
76- return cls(input_str)
77- 
78- @classmethod
79- def try_convert_to_int_list(cls, str_list):
80- """尝试转换为int数组。"""
81- for idx, item in enumerate(str_list):
82- try:
83- int_item = int(item)
84- str_list[idx] = int_item
85- except ValueError:
86- pass
87- 
88- def to_required_list(self):
89- """转换为版本需求字符串列表。"""
90- return [self.version]
91- 
92- def __eq__(self, other):
93- """等于。"""
94- if not isinstance(other, self.__class__):
95- return False
96- return self.version == other.version
97- 
98- def __lt__(self, other):
99- """小于。"""
100- if not isinstance(other, self.__class__):
101- return True
102- 
103- self_list = self.version.split('.')
104- other_list = other.version.split('.')
105- 
106- self.try_convert_to_int_list(self_list)
107- self.try_convert_to_int_list(other_list)
108- 
109- self_tuple = tuple(self_list)
110- other_tuple = tuple(other_list)
111- 
112- return self_tuple < other_tuple
113- 
114- def __str__(self):
115- return self.version
116- 
117- def __repr__(self):
118- return repr(self.version)
119- 
120- 
121-class Point(NamedTuple):
122- """区间端点。"""
123- type_: int # 类型,0为闭区间,1为开区间
124- value: Version
125- 
126- 
127-class Interval(NamedTuple):
128- """版本号区间。"""
129- low: Point
130- high: Point
131- 
132- @classmethod
133- def match(cls, input_str: str) -> bool:
134- """输入字符串是否匹配区间模式。"""
135- if not input_str.startswith('(') and not input_str.startswith('['):
136- return False
137- if not input_str.endswith(')') and not input_str.endswith(']'):
138- return False
139- input_str = input_str[1:-1]
140- if input_str.count(',') > 1:
141- return False
142- return True
143- 
144- @classmethod
145- def parse(cls, input_str):
146- """解析版本号区间。"""
147- if not cls.match(input_str):
148- raise IntervalFormatNotMatch()
149- 
150- if input_str[0] == '[':
151- low_type = 0
152- elif input_str[0] == '(':
153- low_type = 1
154- else:
155- assert False, 'should not go here.'
156- 
157- if input_str[-1] == ']':
158- high_type = 0
159- elif input_str[-1] == ')':
160- high_type = 1
161- else:
162- assert False, 'should not go here.'
163- 
164- input_str = input_str[1:-1]
165- input_list = input_str.split(',')
166- low = input_list[0].strip()
167- if len(input_list) > 1:
168- high = input_list[1].strip()
169- else:
170- high = None
171- 
172- if low:
173- low_version = Point(low_type, Version(low))
174- else:
175- low_version = None
176- 
177- if high:
178- high_version = Point(high_type, Version(high))
179- else:
180- high_version = None
181- 
182- return cls(low=low_version, high=high_version)
183- 
184- def to_required_list(self):
185- """转换为版本需求字符串列表。"""
186- result = []
187- 
188- if self.low:
189- if self.low.type_ == 0:
190- operator = '>='
191- else:
192- operator = '>'
193- required_str = '{0}{1}'.format(operator, self.low.value.version)
194- result.append(required_str)
195- 
196- if self.high:
197- if self.high.type_ == 0:
198- operator = '<='
199- else:
200- operator = '<'
201- required_str = '{0}{1}'.format(operator, self.high.value.version)
202- result.append(required_str)
203- 
204- return result
205- 
206- 
207-class Require(NamedTuple):
208- """包需求。"""
209- pkg_name: str
210- versions: List
211- 
212- @classmethod
213- def _sort_key(cls, item) -> Tuple:
214- """排序键。"""
215- if isinstance(item, Interval):
216- # 如果存在区间左值,则左值参与排序。
217- if item.low:
218- return item.low.value, item.low.type_
219- # 否则使用区间右值,由于开区间更小,所以type_取负。
220- return item.high.value, -item.high.type_
221- 
222- return item, 0
223- 
224- @classmethod
225- def _sort_versions(cls, versions: List) -> bool:
226- """排序版本序列。"""
227- versions.sort(key=cls._sort_key)
228- return True
229- 
230- @classmethod
231- def _to_required_list(cls, versions: List) -> List[str]:
232- """转换为版本需求字符串列表。"""
233- result = []
234- for version in versions:
235- requires = version.to_required_list()
236- result.extend(requires)
237- 
238- return result
239- 
240- @classmethod
241- def _to_required_str(cls, versions: List) -> str:
242- """转换为版本需求字符串。"""
243- requires = cls._to_required_list(versions)
244- required_str = ', '.join(requires)
245- 
246- return required_str
247- 
248- def sort_versions(self) -> bool:
249- """排序版本序列。"""
250- return self._sort_versions(self.versions)
251- 
252- def to_required_full_str(self) -> str:
253- """转换为版本需求字符串。"""
254- required_str = self._to_required_str(self.versions)
255- required_full_str = 'required_package_{0}_version="{1}"'.format(self.pkg_name, required_str)
256- return required_full_str
257- 
258- 
259-class ItemElement(NamedTuple):
260- """item元素。"""
261- name: str
262- version: str
263- 
264- @classmethod
265- def parse(cls, item_ele: ET.Element, cur_ver: str):
266- """解析item元素。"""
267- name = item_ele.attrib['name']
268- version = item_ele.attrib['version'].replace("$(CUR_VER)", cur_ver)
269- return cls(name=name, version=version)
270- 
271- @classmethod
272- def skip(cls, item_ele: ET.Element):
273- """是否跳过item元素。"""
274- version = item_ele.attrib['version']
275- return version.strip() == ''
276- 
277- 
278-class CompatibleElement(NamedTuple):
279- """compatible元素。"""
280- items: List
281- 
282- @classmethod
283- def parse(cls, compatible_ele: ET.Element, cur_ver: str):
284- """解析compatible元素。"""
285- items = []
286- for item_ele in compatible_ele.findall("./item"):
287- if ItemElement.skip(item_ele):
288- continue
289- item = ItemElement.parse(item_ele, cur_ver)
290- items.append(item)
291- return cls(items=items)
292- 
293- 
294-def is_version_number(version: str) -> bool:
295- """字符串是否为版本号。"""
296- has_slash = '/' in version
297- return not has_slash and len(version.split(".")) >= 3
298- 
299- 
300-class VersionXml(NamedTuple):
301- """版本配置。"""
302- release_version: str
303- version_dir: str
304- packages: Dict
305- 
306- @classmethod
307- def match(cls, filepath: Union[Path, str]) -> bool:
308- """文件路径是否匹配版本信息文件。"""
309- return str(filepath).endswith('.xml')
310- 
311- @classmethod
312- def parse_version(cls, version_str: str):
313- """解析版本配置。"""
314- ret = Interval.match(version_str)
315- if ret:
316- result = Interval.parse(version_str)
317- return result
318- 
319- ret = Version.match(version_str)
320- if ret:
321- result = Version.parse(version_str)
322- return result
323- 
324- raise ParseVersionFailed()
325- 
326- def get_release_version(self):
327- """获取发布版本号。"""
328- return self.release_version
329- 
330- def get_version_dir(self):
331- """获取多版本目录。"""
332- return self.version_dir
333- 
334- def collect_requires(self, package: str) -> List[Require]:
335- """收集对应包的包需求列表。"""
336- requires = {}
337- 
338- if package not in self.packages:
339- return []
340- 
341- compatible = self.packages[package]
342- 
343- for item in compatible.items:
344- pkg_name = item.name
345- if pkg_name not in requires:
346- requires[pkg_name] = Require(pkg_name=pkg_name, versions=[])
347- 
348- version_str = item.version
349- try:
350- version = self.parse_version(version_str)
351- except ParseVersionFailed as ex:
352- msg = f'parse pkg {pkg_name} version {version_str} failed'
353- raise CollectRequiresFailed(pkg_name, version_str, msg) from ex
354- 
355- requires[pkg_name].versions.append(version)
356- 
357- result = []
358- for pkg_name in sorted(requires.keys()):
359- requires[pkg_name].sort_versions()
360- result.append(requires[pkg_name])
361- 
362- return result
363- 
364- 
365-def get_version_dir(version_xml: Optional[VersionXml],
366- disable_multi_version: bool,
367- version_dir: Optional[str]) -> Optional[str]:
368- """获取版本目录名。"""
369- if disable_multi_version:
370- return None
371- 
372- if version_dir:
373- return version_dir
374- 
375- # 支持从version.xml中获取version_dir
376- if version_xml and version_xml.get_version_dir():
377- return version_xml.get_version_dir()
378- 
379- return None
380- 
381- 
382-def is_multi_version(version_dir: str) -> bool:
383- """是否多版本。"""
384- return bool(version_dir)
385- 
386- 
387-class VersionInfo(NamedTuple):
388- """版本信息。"""
389- install_version_info: bool
390- install_version_info_attrib: Optional[Dict[str, str]]
391- itf_versions: List[str]
392- version: str
393- version_xml: Optional[VersionXml]
394- timestamp: Optional[str]
395- 
396- 
397-class VersionInfoFile(NamedTuple):
398- """生成的版本配置。"""
399- version: str
400- itf_version_info: Optional[str] = None
401- requires: Optional[List[Require]] = None
402- version_dir: Optional[str] = None
403- timestamp: Optional[str] = None
404- 
405- def _get_content(self) -> str:
406- """获取版本配置内容。"""
407- lines = ['Version={0}'.format(self.version)]
408- if self.version_dir:
409- lines.append('version_dir={0}'.format(self.version_dir))
410- if self.timestamp:
411- lines.append('timestamp={0}'.format(self.timestamp))
412- if self.itf_version_info:
413- lines.append(self.itf_version_info)
414- 
415- if self.requires:
416- requires_str = [require.to_required_full_str() for require in self.requires]
417- lines.extend(requires_str)
418- 
419- lines.append('')
420- 
421- return '\n'.join(lines)
422- 
423- def save(self, target_path: Union[Path, str]):
424- """保存版本配置。"""
425- content = self._get_content()
426- 
427- target_dir = os.path.dirname(target_path)
428- if not os.path.exists(target_dir):
429- os.makedirs(target_dir)
430- 
431- with open(target_path, 'w') as file:
432- file.write(content)
Dscripts/package/common/sh/check_version_required.awk+0-271
@@ -1,271 +0,0 @@
1-function strip(input) {
2- sub("^ +", "", input)
3- sub(" +$", "", input)
4- return input
5-}
6- 
7-function check_compatible_le(version_arr, len_version_arr, require_arr, len_require_arr, i) {
8- for (i = 1; i <= len_require_arr; i++) {
9- if (require_arr[i] == "") {
10- continue
11- }
12- # len_version_arr lt len_require_arr
13- if (i > len_version_arr) {
14- return 1
15- }
16- if (version_arr[i] < require_arr[i]) {
17- return 1
18- }
19- if (version_arr[i] > require_arr[i]) {
20- return 0
21- }
22- }
23- if (len_version_arr > len_require_arr) {
24- return 1
25- }
26- # len_version_arr eq len_require_arr
27- return 1
28-}
29- 
30-function check_compatible_lt(version_arr, len_version_arr, require_arr, len_require_arr, i) {
31- for (i = 1; i <= len_require_arr; i++) {
32- if (require_arr[i] == "") {
33- continue
34- }
35- # len_version_arr lt len_require_arr
36- if (i > len_version_arr) {
37- return 1
38- }
39- if (version_arr[i] < require_arr[i]) {
40- return 1
41- }
42- if (version_arr[i] > require_arr[i]) {
43- return 0
44- }
45- }
46- if (len_version_arr > len_require_arr) {
47- return 0
48- }
49- # len_version_arr eq len_require_arr
50- return 0
51-}
52- 
53-function check_compatible_ge(version_arr, len_version_arr, require_arr, len_require_arr, i) {
54- for (i = 1; i <= len_require_arr; i++) {
55- if (require_arr[i] == "") {
56- continue
57- }
58- # len_version_arr lt len_require_arr
59- if (i > len_version_arr) {
60- return 0
61- }
62- if (version_arr[i] < require_arr[i]) {
63- return 0
64- }
65- if (version_arr[i] > require_arr[i]) {
66- return 1
67- }
68- }
69- if (len_version_arr > len_require_arr) {
70- return 1
71- }
72- # len_version_arr eq len_require_arr
73- return 1
74-}
75- 
76-function check_compatible_gt(version_arr, len_version_arr, require_arr, len_require_arr, i) {
77- for (i = 1; i <= len_require_arr; i++) {
78- if (require_arr[i] == "") {
79- continue
80- }
81- # len_version_arr lt len_require_arr
82- if (i > len_version_arr) {
83- return 0
84- }
85- if (version_arr[i] < require_arr[i]) {
86- return 0
87- }
88- if (version_arr[i] > require_arr[i]) {
89- return 1
90- }
91- }
92- if (len_version_arr > len_require_arr) {
93- return 1
94- }
95- # len_version_arr eq len_require_arr
96- return 0
97-}
98- 
99-function check_compatible_eq(version_arr, len_version_arr, require_arr, len_require_arr, i) {
100- for (i = 1; i <= len_require_arr; i++) {
101- if (require_arr[i] == "") {
102- continue
103- }
104- # len_version_arr lt len_require_arr
105- if (i > len_version_arr) {
106- return 0
107- }
108- if (version_arr[i] != require_arr[i]) {
109- return 0
110- }
111- }
112- if (len_version_arr > len_require_arr) {
113- return 1
114- }
115- # len_version_arr eq len_require_arr
116- return 1
117-}
118- 
119-function check_compatible(version_arr, len_version_arr, require, require_arr, len_require_arr, pos) {
120- len_require_arr = split(require, require_arr, ".")
121- 
122- pos = match(require_arr[1], /^>=/)
123- if (pos != 0) {
124- require_arr[1] = substr(require_arr[1], pos + RLENGTH)
125- return check_compatible_ge(version_arr, len_version_arr, require_arr, len_require_arr)
126- }
127- 
128- pos = match(require_arr[1], /^>/)
129- if (pos != 0) {
130- require_arr[1] = substr(require_arr[1], pos + RLENGTH)
131- return check_compatible_gt(version_arr, len_version_arr, require_arr, len_require_arr)
132- }
133- 
134- pos = match(require_arr[1], /^<=/)
135- if (pos != 0) {
136- require_arr[1] = substr(require_arr[1], pos + RLENGTH)
137- return check_compatible_le(version_arr, len_version_arr, require_arr, len_require_arr)
138- }
139- 
140- pos = match(require_arr[1], /^</)
141- if (pos != 0) {
142- require_arr[1] = substr(require_arr[1], pos + RLENGTH)
143- return check_compatible_lt(version_arr, len_version_arr, require_arr, len_require_arr)
144- }
145- 
146- return check_compatible_eq(version_arr, len_version_arr, require_arr, len_require_arr)
147-}
148- 
149-BEGIN {
150- len_all_required_arr = split(all_required, all_required_arr, ",")
151- 
152- compated = 0
153- 
154- in_gt = 0
155- matched_gt = 0
156- 
157- len_version_arr = split(version, version_arr, ".")
158- 
159- for (i = 1; i <= len_all_required_arr; i++) {
160- all_required_arr[i] = strip(all_required_arr[i])
161- one_compated = check_compatible(version_arr, len_version_arr, all_required_arr[i])
162- 
163- pos = match(all_required_arr[i], /^>/)
164- if (pos != 0) {
165- gt_require = 1
166- lt_require = 0
167- eq_require = 0
168- } else {
169- pos = match(all_required_arr[i], /^</)
170- if (pos != 0) {
171- gt_require = 0
172- lt_require = 1
173- eq_require = 0
174- } else {
175- gt_require = 0
176- lt_require = 0
177- eq_require = 1
178- }
179- }
180- 
181- if (matched_gt) {
182- if (one_compated) {
183- # gt after gt, all compated.
184- if (gt_require) {
185- matched_gt = 1
186- in_gt = 1
187- continue
188- }
189- # lt after gt, all compated.
190- if (lt_require) {
191- compated = 1
192- matched_gt = 0
193- in_gt = 0
194- break
195- }
196- # eq after gt, all compated.
197- if (eq_require) {
198- # eq compated, go.
199- compated = 1
200- break
201- }
202- } else {
203- # miscompated.
204- # gt after gt, compated first gt. miscompated second gt.
205- if (gt_require) {
206- matched_gt = 0
207- in_gt = 1
208- continue
209- }
210- # lt after gt, compated first gt. miscompated second lt.
211- if (lt_require) {
212- matched_gt = 0
213- in_gt = 0
214- continue
215- }
216- # eq after gt, compated first gt. miscompated second eq.
217- if (eq_require) {
218- continue
219- }
220- }
221- } else {
222- if (one_compated) {
223- if (gt_require) {
224- matched_gt = 1
225- in_gt = 1
226- continue
227- }
228- if (lt_require) {
229- if (in_gt) {
230- matched_gt = 0
231- in_gt = 0
232- continue
233- } else {
234- compated = 1
235- break
236- }
237- }
238- if (eq_require) {
239- # eq compated, go.
240- compated = 1
241- break
242- }
243- } else {
244- # miscompated.
245- if (gt_require) {
246- matched_gt = 0
247- in_gt = 1
248- continue
249- }
250- if (lt_require) {
251- matched_gt = 0
252- in_gt = 0
253- continue
254- }
255- if (eq_require) {
256- continue
257- }
258- }
259- }
260- }
261- 
262- if (matched_gt) {
263- compated = 1
264- }
265- 
266- if (compated == 0) {
267- printf("F")
268- } else {
269- printf("T")
270- }
271-}
Dscripts/package/common/sh/cleanup.sh+0-14
@@ -1,14 +0,0 @@
1-#!/bin/sh
2-# --------------------------------------------------------------------------------
3-# Copyright (c) 2025 Huawei Technologies Co., Ltd.
4-# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
5-# CANN Open Software License Agreement Version 2.0 (the "License").
6-# Please refer to the License for details. You may not use this file except in compliance with the License.
7-# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
8-# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
9-# See LICENSE in the root of the software repository for the full text of the License.
10-# --------------------------------------------------------------------------------
11- 
12-set -e
13- 
14-rm -rf $(pwd)
Dscripts/package/common/sh/common_func.inc+0-1236
@@ -1,1236 +0,0 @@
1-#!/bin/sh
2-# ----------------------------------------------------------------------------
3-# Copyright (c) 2025 Huawei Technologies Co., Ltd.
4-# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
5-# CANN Open Software License Agreement Version 2.0 (the "License").
6-# Please refer to the License for details. You may not use this file except in compliance with the License.
7-# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
8-# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
9-# See LICENSE in the root of the software repository for the full text of the License.
10-# ----------------------------------------------------------------------------
11- 
12-## module log
13- 
14-if [ "$(id -u)" != "0" ]; then
15- COMM_LOG_DIR="${HOME}/var/log/ascend_seclog"
16-else
17- COMM_LOG_DIR="/var/log/ascend_seclog"
18-fi
19- 
20-COMM_OPERATION_LOGFILE="${COMM_LOG_DIR}/operation.log"
21-COMM_LOGFILE="${COMM_LOG_DIR}/ascend_install.log"
22-COMM_USERNAME="$(id -un)"
23-COMM_USERGROUP="$(id -gn)"
24- 
25-LOG_PKG_NAME="Common"
26-LOG_FILE="/dev/null"
27-LOG_STYLE="normal"
28- 
29-# 初始化日志系统
30-comm_init_log() {
31- if [ ! -d "$COMM_LOG_DIR" ]; then
32- mkdir -p "$COMM_LOG_DIR"
33- fi
34- if [ $(id -u) -ne 0 ]; then
35- chmod 740 "$COMM_LOG_DIR"
36- else
37- chmod 750 "$COMM_LOG_DIR"
38- fi
39- if [ ! -f "$COMM_LOGFILE" ]; then
40- touch "$COMM_LOGFILE"
41- fi
42- chmod 640 "$COMM_LOGFILE"
43-}
44- 
45-# 组合格式化后的日志信息
46-# _outvar : [输出变量] 格式化后的日志信息
47-# _log_type : 日志级别
48-# _msg : 日志信息
49-_comm_compose_log_msg() {
50- local _outvar="$1"
51- local _log_type="$2"
52- local _msg="$3"
53- local _cur_date="$(date +'%Y-%m-%d %H:%M:%S')"
54- local _result
55- 
56- if [ "${LOG_STYLE}" = "no-colon" ]; then
57- _result="[${LOG_PKG_NAME}] [${_cur_date}] [${_log_type}]${_msg}"
58- else
59- _result="[${LOG_PKG_NAME}] [${_cur_date}] [${_log_type}]: ${_msg}"
60- fi
61- eval "${_outvar}=\"${_result}\""
62-}
63- 
64-# 输出格式化后的消息
65-_comm_echo_log_msg() {
66- local log_type_="$1"
67- local log_format_="$2"
68- 
69- if [ $log_type_ = "INFO" ]; then
70- echo "${log_format_}"
71- elif [ $log_type_ = "WARNING" ]; then
72- echo "${log_format_}"
73- elif [ $log_type_ = "ERROR" ]; then
74- echo "${log_format_}"
75- elif [ $log_type_ = "DEBUG" ]; then
76- :
77- fi
78-}
79- 
80-# 输出
81-comm_echo() {
82- local log_type="$1"
83- local msg="$2"
84- local log_msg
85- 
86- _comm_compose_log_msg "log_msg" "$log_type" "$msg"
87- _comm_echo_log_msg "$log_type" "$log_msg"
88-}
89- 
90-# 写日志
91-comm_log() {
92- local log_type="$1"
93- local msg="$2"
94- local log_msg
95- 
96- _comm_compose_log_msg "log_msg" "$log_type" "$msg"
97- _comm_echo_log_msg "$log_type" "$log_msg"
98- echo "$log_msg" >> "${LOG_FILE}"
99-}
100- 
101-# 设置日志参数
102-set_comm_log() {
103- local pkg_name="$1"
104- local log_file="$2"
105- 
106- LOG_PKG_NAME="${pkg_name}"
107- if [ "$log_file" != "" ]; then
108- LOG_FILE="${log_file}"
109- fi
110-}
111- 
112-# 安全日志
113-comm_log_operation() {
114- local cur_date="$(date +'%Y-%m-%d %H:%M:%S')"
115- local operation="$1"
116- local runfilename="$2"
117- local result="$3"
118- local installmode="$4"
119- local all_parma="$5"
120- local level=""
121- if [ "${operation}" = "Install" ]; then
122- level="SUGGESTION"
123- elif [ "${operation}" = "Upgrade" ]; then
124- level="MINOR"
125- elif [ "${operation}" = "Uninstall" ]; then
126- level="MAJOR"
127- else
128- level="UNKNOWN"
129- fi
130- 
131- if [ ! -f "${COMM_OPERATION_LOGFILE}" ]; then
132- touch "${COMM_OPERATION_LOGFILE}"
133- chmod 640 "${COMM_OPERATION_LOGFILE}"
134- fi
135- 
136- echo "${operation} ${level} ${COMM_USERNAME} ${cur_date} 127.0.0.1 ${runfilename} ${result} installmode=${installmode}; cmdlist=${all_parma}" >> "${COMM_OPERATION_LOGFILE}"
137-}
138- 
139-## end module
140- 
141-# 转换--install-for-all参数下文件权限
142-comm_set_install_for_all_mod() {
143- local _outvar="$1"
144- local _mod="$2"
145- local _new_mod
146- 
147- # ${parameter%word} Remove matching suffix pattern.
148- local _new_mod="${_mod%?}"
149- # ${parameter#word} Remove matching prefix pattern.
150- local _new_mod="${_new_mod}${_new_mod#${_new_mod%?}}"
151- 
152- eval "${_outvar}=\"${_new_mod}\""
153-}
154- 
155-# 创建文件
156-comm_create_file() {
157- local path="$1"
158- local mod="$2"
159- local own="$3"
160- local install_for_all="$4"
161- 
162- if [ -d "$path" ]; then
163- comm_log "WARNING" "remove existed dir $path before create file."
164- rm -rf "$path"
165- fi
166- touch "$path"
167- if [ $? -ne 0 ]; then
168- comm_log "WARNING" "create file $path failed."
169- return 1
170- fi
171- 
172- if [ "$install_for_all" = "true" ] || [ "$install_for_all" = "y" ]; then
173- comm_set_install_for_all_mod "mod" "$mod"
174- fi
175- chmod "$mod" "$path"
176- if [ $? -ne 0 ]; then
177- comm_log "WARNING" "chmod path $path $mod failed."
178- return 1
179- fi
180- 
181- chown "$own" "$path"
182- if [ $? -ne 0 ]; then
183- comm_log "WARNING" "chown path $path $own failed."
184- return 1
185- fi
186- return 0
187-}
188- 
189-# 创建目录
190-comm_create_dir() {
191- local path="$1"
192- local mod="$2"
193- local own="$3"
194- local install_for_all="$4"
195- 
196- if [ "$path" = "" ]; then
197- comm_log "WARNING" "dir path is empty"
198- return 1
199- fi
200- 
201- if [ ! -d "$path" ]; then
202- mkdir -p "$path"
203- if [ $? -ne 0 ]; then
204- comm_log "WARNING" "create dir $path failed."
205- return 1
206- fi
207- fi
208- 
209- if [ "$install_for_all" = "true" ] || [ "$install_for_all" = "y" ]; then
210- comm_set_install_for_all_mod "mod" "$mod"
211- fi
212- chmod "$mod" "$path"
213- if [ $? -ne 0 ]; then
214- comm_log "WARNING" "chmod path $path $mod failed."
215- return 1
216- fi
217- 
218- chown -f "$own" "$path"
219- if [ $? -ne 0 ]; then
220- comm_log "WARNING" "chown path $path $own failed."
221- return 1
222- fi
223-}
224- 
225-# 创建子包目录
226-comm_create_package_dir() {
227- local package_dir="$1"
228- local own="$2"
229- local install_for_all="$3"
230- if [ ! -d "$package_dir" ]; then
231- comm_create_dir "$package_dir" "755" "$own" "$install_for_all"
232- fi
233-}
234- 
235-# 创建install_info文件
236-comm_create_install_info_by_path() {
237- local install_info="$1"
238- local own="$2"
239- if [ ! -f "$install_info" ]; then
240- comm_create_file "$install_info" "640" "$own" "false"
241- fi
242-}
243- 
244-# 更新安装参数
245-comm_update_install_param() {
246- local key="$1"
247- local val="$2"
248- local file="$3"
249- local param=""
250- if [ ! -f "${file}" ]; then
251- return 1
252- fi
253- param="$(grep -i "^${key}=" "${file}")"
254- if [ "${param}" = "" ]; then
255- echo "${key}=${val}" >> "${file}"
256- else
257- sed -i "/^${key}=/Ic ${key}=${val}" "${file}"
258- fi
259-}
260- 
261-# 获取安装参数
262-# _outvar : [输出变量],安装参数值
263-# _file : install.info文件路径
264-# _key : 参数名
265-comm_get_install_param() {
266- local _outvar="$1"
267- local _file="$2"
268- local _key="$3"
269- local _result
270- 
271- if [ ! -f "${_file}" ]; then
272- comm_log "WARNING" "file ${_file} doesn't exist in get install param."
273- return 1
274- fi
275- _result="$(grep -i "^${_key}=" "${_file}" | cut -d"=" -f2-)"
276- eval "${_outvar}=\"${_result}\""
277-}
278- 
279-# 更新安装参数文件
280-comm_update_install_info() {
281- local install_path_param=""
282- local install_type=""
283- local username=""
284- local usergroup=""
285- local feature_type=""
286- local package install_info
287- 
288- while true
289- do
290- case "$1" in
291- --install-path-param=*)
292- install_path_param="$(echo "$1" | cut -d"=" -f2-)"
293- shift
294- ;;
295- --install-type=*)
296- install_type="$(echo "$1" | cut -d"=" -f2-)"
297- shift
298- ;;
299- --username=*)
300- username="$(echo "$1" | cut -d"=" -f2-)"
301- shift
302- ;;
303- --usergroup=*)
304- usergroup="$(echo "$1" | cut -d"=" -f2-)"
305- shift
306- ;;
307- --feature-type=*)
308- feature_type="$(echo "$1" | cut -d"=" -f2-)"
309- shift
310- ;;
311- *)
312- break
313- ;;
314- esac
315- done
316- 
317- if [ $# -lt 2 ]; then
318- return 1
319- fi
320- 
321- package="$1"
322- install_info="$2"
323- 
324- if [ "$install_path_param" != "" ]; then
325- comm_update_install_param "${package}_Install_Path_Param" "$install_path_param" "$install_info"
326- fi
327- if [ "$install_type" != "" ]; then
328- comm_update_install_param "${package}_Install_Type" "$install_type" "$install_info"
329- fi
330- if [ "$username" != "" ]; then
331- comm_update_install_param "${package}_UserName" "$username" "$install_info"
332- fi
333- if [ "$usergroup" != "" ]; then
334- comm_update_install_param "${package}_UserGroup" "$usergroup" "$install_info"
335- fi
336- if [ "$feature_type" != "" ]; then
337- comm_update_install_param "${package}_Feature_Type" "$feature_type" "$install_info"
338- fi
339-}
340- 
341-# 开始安装前打印开始信息
342-comm_start_log() {
343- local all_parma="$@"
344- local cur_date="$(date +'%Y-%m-%d %H:%M:%S')"
345- comm_log "INFO" "Start time:$cur_date"
346- comm_log "INFO" "LogFile:${COMM_LOGFILE}"
347- comm_log "INFO" "InputParams:$all_parma"
348- comm_log "INFO" "OperationLogFile:${COMM_OPERATION_LOGFILE}"
349-}
350- 
351-# 安装结束退出前打印结束信息
352-comm_exit_log() {
353- local cur_date="$(date +'%Y-%m-%d %H:%M:%S')"
354- comm_log "INFO" "End time:${cur_date}"
355- exit "$1"
356-}
357- 
358-comm_print_usage() {
359- local runfilename="$1"
360- comm_log "INFO" "Please input this command for help: ./${runfilename} --help"
361-}
362- 
363-# 判断安装路径是否合法
364-comm_judgmentpath() {
365- local install_path="$1"
366- local pkg_name="$2"
367- check_install_path_valid "$install_path"
368- if [ $? -ne 0 ]; then
369- comm_log "ERROR" "The $pkg_name install_path $install_path is invalid, only characters in [a-z,A-Z,0-9,-,_] are supported!"
370- exit 1
371- fi
372-}
373- 
374-# 标准化安装路径
375-# _outvar : [输出变量],处理后的安装路径
376-# _install_path : 安装路径
377-comm_normalize_install_path() {
378- local _outvar="$1"
379- local _install_path="$2"
380- local _slashes_num _result
381- _slashes_num=$(echo "$_install_path" | grep -o '/' | wc -l)
382- if [ "$_slashes_num" -gt 1 ]; then
383- _result=$(echo "$_install_path" | sed "s/\/*$//g")
384- else
385- _result="$_install_path"
386- fi
387- eval "${_outvar}=\"${_result}\""
388-}
389- 
390-# 解析安装路径
391-# _outvar : [输出变量],处理后的安装路径
392-# _install_path : 安装路径
393-# _pkg_name : 包名
394-comm_parse_install_path() {
395- local _outvar="$1"
396- local _install_path="$2"
397- local _pkg_name="$3"
398- 
399- comm_judgmentpath "$_install_path" "$_pkg_name"
400- comm_normalize_install_path "$_outvar" "$_install_path"
401-}
402- 
403-############### 错误函数 ###############
404-# 文件没有找到
405-comm_err_file_or_directory_not_exist() {
406- comm_log "ERROR" "The file or directory doesn't exist, $1"
407- comm_exit_log 1
408-}
409- 
410-os_name() {
411- if [ ! -f "$1" ];then
412- HostOsName=unknown
413- HostOsFullName=unknown
414- HostOsVersion=unknown
415- return
416- fi
417- 
418- HostOsName=$(cat "$1" | grep ^NAME= | awk -F "[\" ]" '{print $2}')
419- HostOsFullName=$(cat "$1" | grep ^NAME= | awk -F "\"" '{print $2}')
420- 
421- if [ x"$HostOsName" = "x" ];then
422- HostOsName=$(cat "$1" | grep ^NAME= | awk -F "[=]" '{print $2}')
423- HostOsFullName=$(cat "$1" | grep ^NAME= | awk -F "\"" '{print $2}')
424- fi
425- if [ x"$HostOsName" = "x" ];then
426- HostOsName=unknown
427- HostOsFullName=unknown
428- fi
429- HostOsVersion=$(cat "$1" | grep ^VERSION_ID= | awk -F "\"" '{print $2}')
430- if [ x"$HostOsVersion" = "x" ];then
431- HostOsVersion=unknown
432- fi
433- return
434-}
435- 
436-get_os_info() {
437- if [ -f /etc/os-release ];then
438- os_name /etc/os-release
439- elif [ -f /etc/centos-release ];then
440- HostOsName=CentOS
441- HostOsFullName="CentOS Linux"
442- HostOsVersion=$(cat /etc/centos-release | awk '{print $4}')
443- else
444- which lsb_release >/dev/null 2>&1
445- if [ $? -eq 0 ];then
446- HostOsName=$(lsb_release -si)
447- HostOsFullName="${HostOsName}"
448- HostOsVersion=$(lsb_release -sr)
449- else
450- os_name /etc/issue
451- fi
452- fi
453- return
454-}
455- 
456-get_system_info() {
457- get_os_info
458- HostArch=$(uname -m)
459- KernelVersion=$(uname -r)
460-}
461- 
462-version_gt() {
463- if [ "$2"x = "x" ];then
464- return 0
465- else
466- test "$(echo "$@" | tr " " "\n" | sort -V | head -n 1)" != "$1"
467- fi
468-}
469-version_le() {
470- if [ "$2"x = "x" ];then
471- return 0
472- else
473- test "$(echo "$@" | tr " " "\n" | sort -V | head -n 1)" = "$1"
474- fi
475-}
476-version_lt() {
477- if [ "$2"x = "x" ];then
478- return 0
479- else
480- test "$(echo "$@" | tr " " "\n" | sort -rV | head -n 1)" != "$1"
481- fi
482-}
483-version_ge() {
484- if [ "$2"x = "x" ];then
485- return 0
486- else
487- test "$(echo "$@" | tr " " "\n" | sort -rV | head -n 1)" = "$1"
488- fi
489-}
490- 
491-version_vaild() {
492- local ver_range="$1" #version range
493- local ver="$2" #version provider
494- local clean_ver_range=$(echo "$ver_range" | sed 's/"//g') # clean ver_range,去除引号啥的
495- local new_ver_range=$(echo "$clean_ver_range" | sed 's/\[//' | sed 's/\]//' | sed 's/(//' | sed 's/)//') #处理过的version range,去除[],() 得到类似 1.0,2.0
496- 
497- local start=$(echo $new_ver_range | awk -F ',' '{print $1}')
498- local end=$(echo $new_ver_range | awk -F ',' '{print $2}')
499- 
500- if echo $clean_ver_range | grep -Eq "^\[.*\]$" ; then
501- # 闭合区间 [a,b]={x|a<=x<=b}
502- if version_ge $ver $start && version_le $ver $end ; then
503- # pass
504- return 0
505- fi
506- elif echo $clean_ver_range | grep -Eq "^\[.*\)$"; then
507- # 左闭右开 [a,b)={x|a<=x<b}
508- if version_ge $ver $start && version_lt $ver $end ; then
509- return 0
510- fi
511- elif echo $clean_ver_range | grep -Eq "^\(.*\]$"; then
512- # 左开右闭 (a,b]={x|a<x<=b}
513- if version_gt $ver $start && version_le $ver $end ; then
514- return 0
515- fi
516- elif echo $clean_ver_range | grep -Eq "^\(.*\)$"; then
517- # 开区间 (a,b)={x|a<x<b}
518- if version_gt $ver $start && version_lt $ver $end ; then
519- return 0
520- fi
521- else
522- # 兼容老版本,ver_range=require_ver,ver >= require_ver 结果pass 否则 nopass。即依赖包的当前版本大于等于需求
523- if version_ge $ver $clean_ver_range ;then
524- return 0
525- fi
526- fi
527- #not pass
528- return 1
529-}
530- 
531-# 检查包接口版本。
532-# 输出VerCheckStatus和ver_check_status变量。
533-check_pkg_ver_deps() {
534- ver_path="$1"
535- req_pkg="$2"
536- req_ver_path="$3"
537- 
538- ver_info_list=$(awk -F '[_=]' '$1=="required" && $2=="'$req_pkg'" {print $1":"$2":"$3":"$4":"$5}' "$ver_path")
539- 
540- for line in ${ver_info_list}
541- do
542- itf=`echo $line | cut -d":" -f 3`
543- ver=`echo $line | cut -d":" -f 5`
544- req_ver=`awk -F '=' '$1=="'$itf'_version" {print $2}' "$req_ver_path"`
545- if ! version_vaild $ver $req_ver; then
546- VerCheckStatus=FAIL
547- ver_check_status=FAIL
548- return
549- fi
550- done
551- VerCheckStatus=SUCC
552- ver_check_status=SUCC
553-}
554- 
555-# 检查安装路径是否合法。
556-# install_path : 安装路径
557-check_install_path_valid() {
558- local install_path="$1"
559- # 黑名单设置,不允许//,...这样的路径
560- if echo "${install_path}" | grep -Eq '/{2,}|\.{3,}'; then
561- return 1
562- fi
563- # 白名单设置,只允许常见字符
564- if echo "${install_path}" | grep -Eq '^~?[a-zA-Z0-9./_-]*$'; then
565- return 0
566- else
567- return 1
568- fi
569-}
570- 
571-# 创建相对软链接
572-# mdc的release包场景,ln命令不支持-r参数,需要手动实现相对软链功能
573-# 入参需要是规范(绝对)路径,完整路径。例如:目标路径不能是一个目录,否则相对软链计算结果不正确
574-# src_path : 源路径
575-# dst_path : 目标路径
576-create_softlink_icp_relative() {
577- local src_path="$1"
578- local dst_path="$2"
579- local source="top${src_path}"
580- local target="top${dst_path}"
581- 
582- # 若变量内容从尾向前的数据符合,则将符合的最短数据删除
583- local common="${target%/*}"
584- # 若变量内容从头开始的数据符合,则将符合的最短数据删除
585- local forward="${source#"$common"/}"
586- 
587- local result=""
588- 
589- while [ "${forward}" = "${source}" ]; do
590- common="$(dirname "$common")"
591- forward="${source#"$common"/}"
592- result="../${result}"
593- done
594- 
595- result="${result}${forward}"
596- 
597- ln -sfn "${result}" "${dst_path}"
598- if [ $? -ne 0 ]; then
599- log "ERROR" "create softlink relative from ${src_path} to ${dst_path} failed!"
600- return 1
601- fi
602- return 0
603-}
604- 
605-# 创建软链接
606-create_softlink() {
607- local _src_dir="$1"
608- local _dst_dir="$2"
609- local _sub_dir_src="$3"
610- local _sub_dir_dst="$4"
611- local _sub_dir_dst_new _dst_path ret total_ret=0
612- 
613- # 注意_sub_dir_src可能有通配符
614- for _src_path in "${_src_dir}"/${_sub_dir_src}
615- do
616- if [ -z "$_sub_dir_dst" ]; then
617- _sub_dir_dst_new="$(basename "${_src_path}")"
618- else
619- _sub_dir_dst_new="${_sub_dir_dst}"
620- fi
621- _dst_path="${_dst_dir}/${_sub_dir_dst_new}"
622- if [ -L "${_dst_path}" ]; then
623- rm -f "${_dst_path}"
624- fi
625- # 目标取规范化路径
626- _dst_path="$(readlink -f "${_dst_path}")"
627- if [ -d "${_dst_path}" ]; then
628- # 如果目标为目录,拼接上源文件名
629- _dst_path="${_dst_path}/$(basename "${_src_path}")"
630- fi
631- 
632- # 源取规范化路径
633- _src_path="$(readlink -f "${_src_path}")"
634- 
635- create_softlink_icp_relative "${_src_path}" "${_dst_path}"
636- ret="$?" && [ $ret -ne 0 ] && total_ret=$ret
637- done
638- 
639- return $total_ret
640-}
641- 
642-# 创建软链接,调用ln -r命令
643-create_softlink_by_relative_ln() {
644- local _src_dir="$1"
645- local _dst_dir="$2"
646- local _sub_dir_src="$3"
647- local _sub_dir_dst="$4"
648- local _src_path="$_src_dir/$_sub_dir_src"
649- # 注意_src_path可能有通配符,处理流程中需要考虑通配场景。
650- local _dst_path
651- if [ -z "$_sub_dir_dst" ]; then
652- _sub_dir_dst=$(basename $_src_path)
653- fi
654- _dst_path="$_dst_dir/$_sub_dir_dst"
655- if [ -L "${_dst_path}" ]; then
656- rm -f "${_dst_path}"
657- fi
658- ln -sr $_src_path $_dst_path
659-}
660- 
661-# 如果源文件存在,则创建软链接
662-create_softlink_if_exists() {
663- local _src_dir="$1"
664- local _sub_dir_src="$3"
665- local _src_path="$_src_dir/$_sub_dir_src"
666- 
667- if [ -e $_src_path ]; then
668- create_softlink "$@"
669- fi
670-}
671- 
672-# 目录是否为空
673-# 空目录返回0
674-# 目录不存在返回1
675-# 目录非空或无权限访问返回2
676-is_dir_empty() {
677- # 如果目录不存在,则返回
678- if [ ! -d "$1" ]; then
679- return 1
680- fi
681- # 否则检查目录是否为空
682- # 2>&1重定向无权限访问报错信息
683- if [ "$(ls -A "$1" 2>&1)" != "" ]; then
684- return 2
685- fi
686- return 0
687-}
688- 
689-# 删除空目录
690-remove_dir_if_empty() {
691- local dirpath="$1"
692- 
693- if is_dir_empty "${dirpath}"; then
694- rm -rf "${dirpath}"
695- fi
696- return 0
697-}
698- 
699-# 删除目录
700-remove_dir() {
701- local dirpath="$1"
702- 
703- if [ -e "${dirpath}" ]; then
704- chmod u+w -R "${dirpath}"
705- fi
706- rm -rf "${dirpath}"
707- 
708- return 0
709-}
710- 
711-# 删除软链接
712-remove_softlink() {
713- local path="$1"
714- [ -L "${path}" ] && rm -f "${path}"
715-}
716- 
717-# 处理pre_check
718-# pkg_name : 包名
719-# pre_check_func : pre_check函数
720-# standalone : 是否为独立的pre_check命令[y/n]
721-process_pre_check() {
722- local pkg_name="$1"
723- local pre_check_func="$2"
724- local standalone="$3"
725- 
726- log "INFO" "${pkg_name} do pre_check started."
727- ${pre_check_func}
728- ret=$?
729- if [ $ret -ne 0 ]; then
730- log "WARNING" "${pkg_name} do pre check failed."
731- fi
732- log "INFO" "${pkg_name} do pre_check finished."
733- 
734- if [ "${standalone}" = "y" ]; then
735- if [ $ret -ne 0 ]; then
736- exitInstallLog 1
737- fi
738- exitInstallLog 0
739- fi
740-}
741- 
742-# 包是否在环境上安装
743-# install_path : 安装路径
744-# pkgname : 包名
745-# varname : [输出变量],包是否在环境上安装
746-does_pkg_installed() {
747- local install_path="$1"
748- local pkgname="$2"
749- local varname="$3"
750- local result="false"
751- local pkg_path="${install_path}/share/info/${pkgname}"
752- 
753- if [ -d "${pkg_path}" ]; then
754- result="true"
755- fi
756- 
757- read -r "$varname" <<EOF
758-$result
759-EOF
760-}
761- 
762- 
763-# 获取驱动包安装路径
764-# 搜索顺序:
765-# 1. 查看同级目录下是否存在driver包
766-# 2. 查看/etc/ascend_install.info中配置的路径
767-# _outvar : [输出变量],driver包安装路径
768-# _install_path : 安装路径
769-get_driver_install_path() {
770- local _outvar="$1"
771- local _install_path="$2"
772- 
773- local _install_info="/etc/ascend_install.info"
774- local _ascend_hal_name="libascend_hal.so"
775- local _old_ifs
776- local _var
777- local _driver_install_path
778- local _ldconfig_result
779- local _ldconfig_cnt
780- 
781- if [ -d "${_install_path}/driver" ]; then
782- eval "${_outvar}=\"${_install_path}\""
783- return 0
784- fi
785- 
786- # 第一种方案
787- if [ -f ${_install_info} ]; then
788- . ${_install_info}
789- if [ ! -z ${Driver_Install_Path_Param} ]; then
790- _driver_install_path="${Driver_Install_Path_Param}"
791- eval "${_outvar}=\"${_driver_install_path}\""
792- return 0
793- fi
794- fi
795- 
796- eval "${_outvar}=\"\""
797-}
798- 
799-# 执行卸载脚本
800-# install_path : 安装路径
801-# pkgname : 包名
802-run_uninstall_script() {
803- local install_path="$1"
804- local pkgname="$2"
805- local uninstall_path="${install_path}/${pkgname}/script/uninstall.sh"
806- 
807- if [ -f "${uninstall_path}" ]; then
808- "${uninstall_path}"
809- return $?
810- else
811- return 1
812- fi
813-}
814- 
815-# 获取包版本。
816-get_package_version() {
817- local _outvar="$1"
818- local _version_info_path="$2"
819- local _result
820- 
821- if [ ! -f "${_version_info_path}" ]; then
822- eval "${_outvar}=\"\""
823- return 1
824- fi
825- 
826- _result="$(grep "^Version=" "${_version_info_path}" | cut -d= -f2-)"
827- eval "${_outvar}=\"${_result}\""
828-}
829- 
830-# 获取包版本
831-# _outvar : [输出变量],包版本
832-# _version_info_path : version.info文件路径
833-get_version() {
834- get_package_version "$@"
835-}
836- 
837-# 获取包版本目录
838-# _outvar : [输出变量],包版本目录
839-# _version_info_path : version.info文件路径
840-get_version_dir() {
841- local _outvar="$1"
842- local _version_info_path="$2"
843- local _result
844- 
845- if [ ! -f "${_version_info_path}" ]; then
846- eval "${_outvar}=\"\""
847- return 1
848- fi
849- 
850- _result="$(grep "^version_dir=" "${_version_info_path}" | cut -d= -f2-)"
851- eval "${_outvar}=\"${_result}\""
852-}
853- 
854-# 获取包架构
855-# _outvar : [输出变量],包架构
856-# _scene_info_path : scene.info文件路径
857-get_arch() {
858- local _outvar="$1"
859- local _scene_info_path="$2"
860- local _result
861- 
862- if [ ! -f "${_scene_info_path}" ]; then
863- eval "${_outvar}=\"\""
864- return 1
865- fi
866- 
867- _result="$(grep "^arch=" "${_scene_info_path}" | cut -d= -f2-)"
868- eval "${_outvar}=\"${_result}\""
869-}
870- 
871-# 获取包目录
872-# _outvar : [输出变量],包目录
873-# _version_info_path : version.info文件路径
874-# _install_path : 安装路径
875-# _pkg_name : 包名
876-get_install_package_dir() {
877- local _outvar="$1"
878- local _version_info_path="$2"
879- local _install_path="$3"
880- local _pkg_name="$4"
881- local _pkg_is_multi_version _pkg_version_dir _result
882- 
883- is_multi_version_pkg "_pkg_is_multi_version" "$_version_info_path"
884- get_version_dir "_pkg_version_dir" "$_version_info_path"
885- 
886- if [ "$_pkg_is_multi_version" = "true" ]; then
887- _result="$_install_path/$_pkg_version_dir/$_pkg_name"
888- else
889- _result="$_install_path/$_pkg_name"
890- fi
891- eval "${_outvar}=\"${_result}\""
892-}
893- 
894-# 包名转换为日志输出的包名(首字母大写)
895-_package_to_log_pkg_name() {
896- local _outvar="$1"
897- local _pkg_name="$2"
898- 
899- local _result
900- 
901- _result="$(awk -v pkg_name="${_pkg_name}" '
902- BEGIN {print toupper(substr(pkg_name, 1, 1)) substr(pkg_name, 2)}
903- ')"
904- 
905- eval "${_outvar}=\"${_result}\""
906-}
907- 
908-# 获取首字母大字的包名
909-get_titled_package_name() {
910- _package_to_log_pkg_name "$@"
911-}
912- 
913-# 安装前处理。
914-# 返回码:
915-# 0: 成功
916-# 1: 版本兼容性检查失败
917-# 调用方式:
918-# preinstall_process --install-path=<install_path> --script-dir=<script-dir> --package=<package> --logfile=<logfile> [ --logstyle=<logstyle> ] [ --docker-root=<docker_root> ]
919-#
920-# --install-path=<install_path> : 安装路径
921-# --script-dir=<script-dir> : 当前脚本目录路径
922-# --package=<package> : 当前包名
923-# --logfile=<logfile> : ascend_install.log日志文件路径
924-# --logstyle=<logstyle> : 日志风格
925-# --docker-root=<docker_root> : docker根路径
926-preinstall_process() {
927- local install_path=""
928- local docker_root=""
929- local script_dir=""
930- local package=""
931- local logfile=""
932- local real_install_path
933- local version_info_path
934- local ret
935- 
936- # 参数列表末尾添加--参数
937- eval set -- "$@ --"
938- 
939- while true
940- do
941- case "$1" in
942- --install-path=*)
943- install_path="$(echo "$1" | cut -d"=" -f2-)"
944- shift
945- ;;
946- --docker-root=*)
947- docker_root="$(echo "$1" | cut -d"=" -f2-)"
948- shift
949- ;;
950- --script-dir=*)
951- script_dir="$(echo "$1" | cut -d"=" -f2-)"
952- shift
953- ;;
954- --package=*)
955- package="$(echo "$1" | cut -d"=" -f2-)"
956- shift
957- ;;
958- --logfile=*)
959- logfile="$(echo "$1" | cut -d"=" -f2-)"
960- shift
961- ;;
962- --logstyle=*)
963- LOG_STYLE="$(echo "$1" | cut -d"=" -f2-)"
964- shift
965- ;;
966- *)
967- break
968- ;;
969- esac
970- done
971- 
972- if [ "${install_path}" = "" ]; then
973- echo "error: install_path is needed!"
974- return 1
975- fi
976- if [ "${script_dir}" = "" ]; then
977- echo "error: script_dir is needed!"
978- return 1
979- fi
980- 
981- if [ "${package}" = "" ]; then
982- echo "error: package is needed!"
983- return 1
984- fi
985- if [ "${logfile}" = "" ]; then
986- echo "error: logfile is needed!"
987- return 1
988- fi
989- 
990- LOG_FILE="${logfile}"
991- _package_to_log_pkg_name "LOG_PKG_NAME" "${package}"
992- 
993- if [ "${docker_root}" = "" ]; then
994- real_install_path="${install_path}"
995- else
996- real_install_path="${docker_root}/${install_path}"
997- fi
998- 
999- _check_version_compatiable "$package" "${real_install_path}" "${script_dir}"
1000- if [ $? -ne 0 ]; then
1001- return 1
1002- fi
1003- 
1004- return 0
1005-}
1006- 
1007-# 安装前检查。
1008-# 返回码:
1009-# 0: 成功
1010-# 1: 版本兼容性检查失败
1011-# 调用方式:
1012-# preinstall_check --install-path=<install_path> --script-dir=<script-dir> --package=<package> --logfile=<logfile> [ --logstyle=<logstyle> ] [ --docker-root=<docker_root> ]
1013-#
1014-# --install-path=<install_path> : 安装路径
1015-# --script-dir=<script-dir> : 当前脚本目录路径
1016-# --package=<package> : 当前包名
1017-# --logfile=<logfile> : ascend_install.log日志文件路径
1018-# --logstyle=<logstyle> : 日志风格
1019-# --docker-root=<docker_root> : docker根路径
1020-preinstall_check() {
1021- preinstall_process "$@"
1022-}
1023- 
1024-# 是否为多版本包
1025-# 读取version.info文件,检查本包是否为多版本包
1026-# _outvar : [输出变量],是否为多版本包
1027-# _filepath : version.info文件路径
1028-is_multi_version_pkg() {
1029- local _outvar="$1"
1030- local _filepath="$2"
1031- local _ret="false"
1032- 
1033- if [ -f "${_filepath}" ]; then
1034- grep "^version_dir=" "${_filepath}" > /dev/null
1035- [ $? -eq 0 ] && _ret="true"
1036- fi
1037- eval "${_outvar}=\"${_ret}\""
1038-}
1039- 
1040-# 文件列表的某一列
1041-_filelist_column() {
1042- local _filepath="$1"
1043- local _idx="$2"
1044- tail -n +2 "$_filepath" | cut -d, -f${_idx} | sort | uniq
1045-}
1046- 
1047-# 移除comm特性
1048-_remove_comm_feature() {
1049- local _outvar="$1"
1050- local _feature="$2"
1051- local _result
1052- 
1053- _result="$(echo "$_feature" | sed 's/comm//g' | sed 's/,,/,/g' | sed 's/^,//g' | sed 's/,$//g')"
1054- eval "${_outvar}=\"${_result}\""
1055-}
1056- 
1057-# 特性列表转换为正则表达式
1058-_feature_to_regex() {
1059- local _outvar="$1"
1060- local _feature="$2"
1061- local _result_ftr
1062- _result_ftr="\b(($(echo "$_feature" | sed 's/,/)|(/g')))\b"
1063- eval "${_outvar}=\"${_result_ftr}\""
1064-}
1065- 
1066-# 是否包含指定feature
1067-# 读取filelist.csv文件,检查是否包含指定feature
1068-# _outvar : [输出变量](true/false),是否包含指定feature
1069-# _feature : 指定feature,可指定多个,以“,”分隔,如:dvpp,audio
1070-# _filepath : filelist.csv文件路径
1071-contain_feature() {
1072- local _outvar="$1"
1073- local _feature="$2"
1074- local _filepath="$3"
1075- local _feature_regex _feature_cf
1076- 
1077- # 输入的comm不作为特性,也就是说不支持--feature=comm
1078- _remove_comm_feature "_feature_cf" "$_feature"
1079- if [ "$_feature_cf" = "" ]; then
1080- eval "${_outvar}=\"false\""
1081- return 0
1082- fi
1083- 
1084- _feature_to_regex "_feature_regex" "$_feature_cf"
1085- _filelist_column "$_filepath" 10 | grep -E "$_feature_regex" > /dev/null 2>&1
1086- if [ $? -eq 0 ]; then
1087- eval "${_outvar}=\"true\""
1088- else
1089- eval "${_outvar}=\"false\""
1090- fi
1091-}
1092- 
1093-# 向filelist.csv中写入1个条目
1094-# filepath : filelist.csv文件路径
1095-# operation : 操作类型
1096-# path_in_pkg : 包内路径
1097-# install_path : 安装路径
1098-# permission : 文件权限
1099-# owner_group : 文件属主,支持默认值DEFAULT
1100-# install_type : 安装类型
1101-# softlink : 软链接
1102-# block : 所属块
1103-# feature : 所属特性,支持默认值DEFAULT
1104-# chip : 所属芯片,支持默认值DEFAULT
1105-# arch : 包架构
1106-add_fileitem() {
1107- local filepath="$1"
1108- local operation="$2"
1109- local path_in_pkg="$3"
1110- local install_path="$4"
1111- local permission="$5"
1112- local owner_group="$6"
1113- local install_type="$7"
1114- local softlink="$8"
1115- local pkg_inner_softlink="$9"
1116- local block="${10}"
1117- local feature="${11}"
1118- local chip="${12}"
1119- local arch="${13}"
1120- local is_common_path="N"
1121- 
1122- if [ "$owner_group" = "DEFAULT" ]; then
1123- owner_group="\\\\\$username:\\\\\$usergroup"
1124- fi
1125- 
1126- if [ "$feature" = "DEFAULT" ]; then
1127- feature="all"
1128- fi
1129- 
1130- if [ "$chip" = "DEFAULT" ]; then
1131- chip="all"
1132- fi
1133- 
1134- if echo "$install_path" | grep "^${arch}-linux/" > /dev/null; then
1135- is_common_path="Y"
1136- fi
1137- 
1138- echo "NA,${operation},${path_in_pkg},${install_path},FALSE,${permission},${owner_group},${install_type},${softlink},${feature},${is_common_path},FALSE,NA,${block},${pkg_inner_softlink},${chip}" >> ${filepath}
1139-}
1140- 
1141-# 设置公共变量
1142-set_global_vars() {
1143- local arch
1144- local scene_filepath="${curpath}/../scene.info"
1145- 
1146- if [ -f "${scene_filepath}" ]; then
1147- get_scene_arch "arch" "${scene_filepath}"
1148- if [ "${arch}" != "" ]; then
1149- PKG_ARCH="${arch}"
1150- fi
1151- fi
1152-}
1153- 
1154-# 获取并发进程数
1155-get_thread_num() {
1156- local _outvar="$1"
1157- local _thread_num="$(cat /proc/cpuinfo | grep "^processor" | wc -l)"
1158- 
1159- # 未取得CPU核数时,默认为1
1160- if [ "$_thread_num" = "" ] || [ "$_thread_num" = "0" ]; then
1161- _thread_num="1"
1162- fi
1163- 
1164- # CPU核数2倍
1165- eval "${_outvar}=\"$((_thread_num*2))\""
1166-}
1167- 
1168-# 初始化fifo,用于并发控制
1169-init_fifo() {
1170- local _outvar="$1"
1171- local _thread_num="$2"
1172- local _tmppath_if tmp
1173- local _skip_msg="skip init fifo"
1174- 
1175- # 并发数为空时,默认为1
1176- if [ "$_thread_num" = "" ]; then
1177- _thread_num="1"
1178- comm_log "WARNING" "thread number is empty, use ${_thread_num}."
1179- fi
1180- # 并发数<=0时,默认为1
1181- if [ $_thread_num -le 0 ]; then
1182- comm_log "WARNING" "thread number is ${_thread_num}, use 1."
1183- _thread_num="1"
1184- fi
1185- 
1186- get_tmp_file _tmppath_if "fifo"
1187- if [ -e "$_tmppath_if" ]; then
1188- rm -rf "$_tmppath_if"
1189- check_ret_warning "$?" "remove old file $_tmppath_if failed, ${_skip_msg}."
1190- ret="$?" && [ $ret -ne 0 ] && return $ret
1191- fi
1192- 
1193- mkfifo "$_tmppath_if"
1194- check_ret_warning "$?" "make fifo $_tmppath_if failed, ${_skip_msg}."
1195- ret="$?" && [ $ret -ne 0 ] && return $ret
1196- 
1197- # 使用文件描述符8作并发控制
1198- exec 8<> "$_tmppath_if" # 创建文件描述符8
1199- check_ret_warning "$?" "open fifo $_tmppath_if file descriptor failed, ${_skip_msg}."
1200- ret="$?" && [ $ret -ne 0 ] && return $ret
1201- 
1202- rm -f "$_tmppath_if"
1203- check_ret_warning "$?" "remove fifo $_tmppath_if failed, ${_skip_msg}."
1204- ret="$?" && [ $ret -ne 0 ] && return $ret
1205- 
1206- for tmp in $(seq $_thread_num); do
1207- echo >&8
1208- check_ret_warning "$?" "echo fifo $_tmppath_if failed, ${_skip_msg}."
1209- ret="$?" && [ $ret -ne 0 ] && return $ret
1210- done
1211- 
1212- eval "${_outvar}=\"${_tmppath_if}\""
1213-}
1214- 
1215-# 根据参数执行
1216-exec_with_param() {
1217- local param="$1"
1218- shift 1
1219- local exec_mode fifo_path="$PARALLEL_FIFO" tmp
1220- extract_1st "exec_mode" "$param"
1221- if [ "$exec_mode" = "concurrency" ]; then
1222- if [ "$fifo_path" = "none" ] || [ "$fifo_path" = "" ]; then
1223- "$@" &
1224- else
1225- read tmp <&8
1226- {
1227- "$@"
1228- ret="$?"
1229- echo >&8
1230- exit $ret
1231- } &
1232- fi
1233- else
1234- "$@"
1235- fi
1236-}
Dscripts/package/common/sh/common_func_v2.inc+0-1730
@@ -1,1730 +0,0 @@
1-#!/bin/sh
2-# --------------------------------------------------------------------------------
3-# Copyright (c) 2025 Huawei Technologies Co., Ltd.
4-# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
5-# CANN Open Software License Agreement Version 2.0 (the "License").
6-# Please refer to the License for details. You may not use this file except in compliance with the License.
7-# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
8-# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
9-# See LICENSE in the root of the software repository for the full text of the License.
10-# --------------------------------------------------------------------------------
11- 
12-# 公共函数库
13-# 总setenv文件权限
14-SETENV_MOD="550"
15-# 总setenv文件可写状态权限
16-SETENV_WRITEABLE_MOD="600"
17-# 默认特性配置
18-DEFAULT_FEATURE_PARAM="all n all"
19-#export PS4='+ ${FUNCNAME[0]:+${FUNCNAME[0]}():} ${BASH_SOURCE}:${LINENO}: '
20-#set -x
21-RESET_MOD="750"
22- 
23-# db.info文件格式如下
24-# ---
25-# CommonLib|atc,fwkacllib
26-# Compiler|atc,fwkacllib
27-# ---
28-# 使用"|"分隔模块名和包名列表,包名列表使用","分隔
29-# 文件中块名保持以升序排序
30-PKG_DB_INFO_RELPATH="var/ascend_package_db.info"
31-# 缓存mod文件名
32-STASH_MOD_PATH="stash_mod.txt"
33-# stash_mod文件权限
34-STASH_FILE_MOD="600"
35- 
36-# 写日志
37-log() {
38- local cur_date_="$(date +"%Y-%m-%d %H:%M:%S")"
39- local log_type_="${1}"
40- local msg_="${2}"
41- local log_format_="[Common] [${cur_date_}] [${log_type_}]: ${msg_}"
42- if [ "${log_type_}" = "INFO" ]; then
43- echo "${log_format_}"
44- elif [ "${log_type_}" = "WARNING" ]; then
45- echo "${log_format_}"
46- elif [ "${log_type_}" = "ERROR" ]; then
47- echo "${log_format_}"
48- elif [ "${log_type_}" = "DEBUG" ]; then
49- echo "${log_format_}" 1> /dev/null
50- fi
51-}
52- 
53-# 返回列表长度
54-__length_list() {
55- local list="$1"
56- local var="$2"
57- local list_item
58- local cnt=0
59- 
60- for list_item in ${list}; do
61- cnt=$((cnt+1))
62- done
63- 
64- eval "${var}=\"${cnt}\""
65-}
66- 
67-# 获取列表索引值
68-__index_list() {
69- local list="$1"
70- shift
71- local list_item
72- local cnt=0
73- 
74- if [ $# -eq 0 ]; then
75- return 0
76- fi
77- 
78- for list_item in ${list}; do
79- if [ ${1} -eq ${cnt} ]; then
80- eval "${2}=\"${list_item}\""
81- shift 2
82- if [ $# -eq 0 ]; then
83- return 0
84- fi
85- fi
86- cnt=$((cnt+1))
87- done
88- 
89- return 0
90-}
91- 
92-# 从列表中移除一项
93-__remove_item_in_list() {
94- local to_removed="$1"
95- shift
96- local list="$*"
97- local list_item
98- local new_list
99- 
100- for list_item in ${list}; do
101- if [ "${to_removed}" != "${list_item}" ]; then
102- if [ "${new_list}" = "" ]; then
103- new_list="${list_item}"
104- else
105- new_list="${new_list} ${list_item}"
106- fi
107- fi
108- done
109- echo "${new_list}"
110-}
111- 
112-# 元素是否在列表中
113-__item_in_list() {
114- local _outvar="$1"
115- local _item="$2"
116- shift 2
117- local _list_item
118- local _matched="false"
119- 
120- for _list_item in $*; do
121- if [ "${_item}" = "${_list_item}" ]; then
122- _matched="true"
123- break
124- fi
125- done
126- eval "${_outvar}=\"${_matched}\""
127-}
128- 
129-# 反转列表
130-__reverse_list() {
131- local _outvar="$1"
132- local _list="$2"
133- local _new_list=""
134- local _list_item
135- 
136- for _list_item in ${_list}; do
137- if [ "${_new_list}" = "" ]; then
138- _new_list="${_list_item}"
139- else
140- _new_list="${_list_item} ${_new_list}"
141- fi
142- done
143- eval "${_outvar}=\"${_new_list}\""
144-}
145- 
146-# 修改各文件及目录的属性
147-change_own() {
148- local recursive="$3"
149- local option=""
150- local username="${USERNAME}"
151- local usergroup="${USERGROUP}"
152- if [ "$2" != "NA" ]; then
153- if [ "${recursive}" = "true" ]; then
154- option="-R"
155- fi
156- eval chown ${option} -h \"$2\" \"$1\"
157- if [ $? -ne 0 ]; then
158- log "ERROR" "$1 chown failed!"
159- return 1
160- fi
161- fi
162-}
163- 
164-# 获取install_for_all文件权限
165-get_install_for_all_mod() {
166- local _outvar="$1"
167- local _mod="$2"
168- local _new_mod _other_mod
169- 
170- _new_mod="${_mod%?}"
171- _other_mod="${_new_mod#${_new_mod%?}}"
172- _other_mod="$(($_other_mod & 5))" # other权限位移除写权限,仅支持普通用户运行
173- 
174- eval "${_outvar}=\"${_new_mod}${_other_mod}\""
175-}
176- 
177-# 修改各文件及目录的权限
178-change_mod() {
179- local mod="$2"
180- local install_for_all="$3"
181- local recursive="$4"
182- local option="" new_mod
183- # 对于软连接,可能目标文件还没有拷贝进来,导致无法修改mod,这里过滤掉软连接
184- if [ -L "$1" ]; then
185- return 0
186- fi
187- if [ "$2" != "NA" ]; then
188- if [ "${recursive}" = "true" ]; then
189- option="-R"
190- fi
191- # 如果设置了install_for_all,则安装时other权限跟group权限对齐
192- if [ "${install_for_all}" = "y" ]; then
193- get_install_for_all_mod new_mod "$mod"
194- chmod ${option} "${new_mod}" "$1"
195- else
196- chmod ${option} "$2" "$1"
197- fi
198- if [ $? -ne 0 ]; then
199- log "ERROR" "$1 chmod failed!"
200- return 1
201- fi
202- fi
203- return 0
204-}
205- 
206-# 获取文件权限
207-get_file_mod() {
208- local _outvar="$1"
209- local _options="" _ret
210- shift
211- 
212- while true; do
213- case "$1" in
214- -L|--dereference)
215- _options="${_options} $1"
216- shift
217- ;;
218- *)
219- break
220- ;;
221- esac
222- done
223- 
224- local _path="$1"
225- local _result
226- 
227- _result="$(stat ${_options} -c %a "${_path}")"
228- _ret="$?" && [ $_ret -ne 0 ] && return $_ret
229- eval "${_outvar}=\"${_result}\""
230-}
231- 
232-# 检查路径是否为绝对路径
233-__check_abs_path() {
234- local path="$1"
235- 
236- if [ "${path#/}" != "${path}" ]; then
237- is_abs_path="true"
238- else
239- is_abs_path="false"
240- fi
241-}
242- 
243-__set_abs_path() {
244- local install_path="$1"
245- local path="$2"
246- local varname="$3"
247- local is_abs_path
248- 
249- __check_abs_path "${path}"
250- if [ "${is_abs_path}" != "true" ]; then
251- eval "${varname}=\"${install_path}/${path}\""
252- else
253- eval "${varname}=\"${path}\""
254- fi
255-}
256- 
257-# 创建目录
258-make_dir() {
259- local path="$1"
260- mkdir -p "${path}"
261- if [ $? -ne 0 ]; then
262- log "ERROR" "${path} mkdir failed!"
263- exit 1
264- fi
265- return 0
266-}
267- 
268-# 检查install_path在docker_root之中
269-check_install_path_in_docker_root() {
270- local install_path="$1"
271- local docker_root="$2"
272- 
273- echo "${install_path}" | grep "^${docker_root}" > /dev/null 2>&1
274- if [ $? -ne 0 ]; then
275- log "ERROR" "check install path ${install_path} in docker root ${docker_root} failed!"
276- return 1
277- fi
278- return 0
279-}
280- 
281-# 移除路径右侧斜线(/)
282-rstrip_path() {
283- local _outvar="$1"
284- local _path="$2"
285- 
286- _path="$(echo "${_path}" | sed "s/\/\+\$//g")"
287- eval "${_outvar}=\"${_path}\""
288-}
289- 
290-# 包是否在tools目录下
291-is_package_under_tools() {
292- local _outvar="$1"
293- local _package="$2"
294- local _result_iput="false"
295- 
296- if [ "${_package}" = "aoe" ] || [ "${_package}" = "nca" ] || [ "${_package}" = "amct_acl" ] || [ "${_package}" = "ncs" ]; then
297- _result_iput="true"
298- fi
299- 
300- eval "${_outvar}=\"${_result_iput}\""
301-}
302- 
303-# 获取包目录名
304-get_package_dir() {
305- local _outvar="$1"
306- local _package="$2"
307- local _is_under_tools _result
308- 
309- if [ "$USE_SHARE_INFO" = "y"]; then
310- _result="share/info"
311- else
312- is_package_under_tools "_is_under_tools" "${_package}"
313- if [ "${_is_under_tools}" = "true" ]; then
314- _result="tools"
315- else
316- _result=""
317- fi
318- fi
319- eval "${_outvar}=\"${_result}\""
320-}
321- 
322-# 获取包目录路径
323-get_package_dirpath() {
324- local _outvar="$1"
325- local _package="$2"
326- local _is_under_tools _result
327- 
328- if [ "$USE_SHARE_INFO" = "y" ]; then
329- _result="share/info/$_package"
330- else
331- is_package_under_tools "_is_under_tools" "${_package}"
332- if [ "${_is_under_tools}" = "true" ]; then
333- _result="tools/${_package}"
334- else
335- _result="${_package}"
336- fi
337- fi
338- eval "${_outvar}=\"${_result}\""
339-}
340- 
341-# 获取包ascend_install.info路径
342-get_package_install_info() {
343- local _outvar="$1"
344- local _install_path="$2"
345- local _version_dir="$3"
346- local _package="$4"
347- local _package_dirpath=""
348- 
349- eval "${_outvar}=\"\""
350- 
351- get_package_dirpath "_package_dirpath" "${_package}"
352- 
353- eval "${_outvar}=\"${_install_path}/${_version_dir}/${_package_dirpath}/ascend_install.info\""
354-}
355- 
356-# 获取包version.info路径
357-get_package_version_info() {
358- local _outvar="$1"
359- local _install_path="$2"
360- local _version_dir="$3"
361- local _package="$4"
362- local _package_dirpath=""
363- 
364- eval "${_outvar}=\"\""
365- 
366- get_package_dirpath "_package_dirpath" "${_package}"
367- 
368- eval "${_outvar}=\"${_install_path}/${_version_dir}/${_package_dirpath}/version.info\""
369-}
370- 
371-# 获取包filelist.csv路径
372-get_package_filelist() {
373- local _outvar="$1"
374- local _install_path="$2"
375- local _version_dir="$3"
376- local _package="$4"
377- local _package_dirpath=""
378- 
379- eval "${_outvar}=\"\""
380- 
381- get_package_dirpath "_package_dirpath" "${_package}"
382- 
383- eval "${_outvar}=\"${_install_path}/${_version_dir}/${_package_dirpath}/script/filelist.csv\""
384-}
385- 
386-# 获取包install_common_parser.sh路径
387-get_package_install_common_parser() {
388- local _outvar="$1"
389- local _install_path="$2"
390- local _version_dir="$3"
391- local _package="$4"
392- local _package_dirpath=""
393- 
394- eval "${_outvar}=\"\""
395- 
396- get_package_dirpath "_package_dirpath" "${_package}"
397- 
398- eval "${_outvar}=\"${_install_path}/${_version_dir}/${_package_dirpath}/script/install_common_parser.sh\""
399-}
400- 
401-# 获取latest_manager的install_common_parser.sh路径
402-get_latest_manager_install_common_parser() {
403- local _outvar="$1"
404- local _install_path="$2"
405- local _latest_dir="$3"
406- 
407- eval "${_outvar}=\"${_install_path}/${_latest_dir}/var/manager/install_common_parser.sh\""
408-}
409- 
410-# 获取包script目录路径
411-get_package_script_dirpath() {
412- local _outvar="$1"
413- local _install_path="$2"
414- local _version_dir="$3"
415- local _package="$4"
416- local _package_dirpath=""
417- 
418- eval "${_outvar}=\"\""
419- 
420- get_package_dirpath "_package_dirpath" "${_package}"
421- 
422- eval "${_outvar}=\"${_install_path}/${_version_dir}/${_package_dirpath}/script\""
423-}
424- 
425-# 检查参数不为空
426-check_param_not_empty() {
427- local name="$1"
428- local error_msg="$2"
429- local value
430- 
431- eval "value=\"\${${name}}\""
432- 
433- if [ "${value}" = "" ]; then
434- comm_log "ERROR" "$2"
435- return 1
436- fi
437- 
438- return 0
439-}
440- 
441-# 检查文件存在
442-check_file_exists() {
443- local path="$1"
444- local error_msg="$2"
445- 
446- if [ ! -f "${path}" ]; then
447- comm_log "ERROR" "$2"
448- return 1
449- fi
450- 
451- return 0
452-}
453- 
454-# 检查返回值是否为0
455-check_ret_error() {
456- local ret="$1"
457- local msg="$2"
458- 
459- if [ ${ret} -ne 0 ]; then
460- comm_log "ERROR" "${msg}"
461- return ${ret}
462- fi
463- 
464- return 0
465-}
466- 
467-# 检查返回值是否为0
468-check_ret_warning() {
469- local ret="$1"
470- local msg="$2"
471- 
472- if [ ${ret} -ne 0 ]; then
473- comm_log "WARNING" "${msg}"
474- return ${ret}
475- fi
476- 
477- return 0
478-}
479- 
480-# 获取真实路径
481-get_realpath() {
482- local _outvar="$1"
483- local _path_gr="$2"
484- 
485- _path_gr="$(readlink -f "${_path_gr}")"
486- eval "${_outvar}=\"${_path_gr}\""
487-}
488- 
489-# 检查返回值是否为0
490-cleanup_if_error() {
491- local ret="$1"
492- local cleanup="$2"
493- 
494- if [ ${ret} -ne 0 ]; then
495- eval "${cleanup}"
496- return ${ret}
497- fi
498- 
499- return 0
500-}
501- 
502-# 获取临时目录
503-get_tmp_root() {
504- local _outvar="$1"
505- local _result_gtr
506- 
507- if [ -d "${HOME}" ]; then
508- _result_gtr="${HOME}"
509- elif [ $(id -u) -eq 0 ] && [ -d "/root" ]; then
510- _result_gtr="/root"
511- else
512- _result_gtr="${PWD}"
513- fi
514- 
515- eval "${_outvar}=\"${_result_gtr}\""
516- return 0
517-}
518- 
519-# 获取临时文件
520-get_tmp_file() {
521- local _outvar="$1"
522- local _filename="$2"
523- local _tmp_file_gtf _result
524- 
525- get_tmp_root "_tmp_file_gtf"
526- 
527- _result=$(mktemp "$_tmp_file_gtf/${_filename}_XXXXXX")
528- check_ret_warning "$?" "mktemp $_tmp_file_gtf/${_filename}_XXXXXX failed."
529- ret="$?" && [ $ret -ne 0 ] && return $ret
530- 
531- eval "${_outvar}=\"${_result}\""
532- return 0
533-}
534- 
535-# 获取包架构
536-get_scene_arch() {
537- local _outvar="$1"
538- local _scene_filepath="$2"
539- local _result
540- 
541- _result="$(grep "^arch=" "${_scene_filepath}" | cut -d= -f2-)"
542- eval "${_outvar}=\"${_result}\""
543-}
544- 
545-# 打包feature参数
546-pack_feature_param() {
547- local _outvar="$1"
548- local _feature_type="$2"
549- local _feature_exclude_all="$3"
550- local _chip="$4"
551- 
552- eval "${_outvar}=\"${_feature_type} ${_feature_exclude_all} ${_chip}\""
553-}
554- 
555-# 解包feature参数
556-# 注意,调用解包时参数不可加引号
557-unpack_feature_param() {
558- local _feature_type_var="$1"
559- local _feature_exclude_all_var="$2"
560- local _chip_var="$3"
561- shift 3
562- eval "${_feature_type_var}=\"${1}\""
563- eval "${_feature_exclude_all_var}=\"${2}\""
564- eval "${_chip_var}=\"${3}\""
565-}
566- 
567-# 展开version.info文件中参数
568-expand_version_file() {
569- if [ "${VERSION_FILE}" = "" ]; then
570- return 0
571- fi
572- get_version "VERSION" "${VERSION_FILE}"
573- get_version_dir "VERSION_DIR" "${VERSION_FILE}"
574-}
575- 
576-# 提取第一项
577-extract_1st() {
578- local _outvar="$1"
579- eval "${_outvar}=\"$2\""
580-}
581- 
582-# 提取第二项
583-extract_2nd() {
584- local _outvar="$1"
585- eval "${_outvar}=\"$3\""
586-}
587- 
588-# 路径转为sed正则表达式
589-path_to_regex() {
590- local _outvar="$1"
591- local _path="$2"
592- local _reslut_ptr="$(echo "${_path}" | sed "s#\/#\\\/#g")"
593- 
594- eval "${_outvar}=\"${_reslut_ptr}\""
595-}
596- 
597-# 设置默认值
598-set_default() {
599- local _outvar="$1"
600- local _input="$2"
601- local _default="$3"
602- 
603- if [ "${_input}" = "" ]; then
604- eval "${_outvar}=\"${_default}\""
605- else
606- eval "${_outvar}=\"${_input}\""
607- fi
608-}
609- 
610-# 标准化feature参数
611-# 输入的feature参数中,如果有all字段(表示安装所有feature)
612-# 则将feature参数重置为all,在后续流程中安装所有feature
613-# 卸载流程中,feature与chip强制为all,保证卸载掉block中的所有文件
614-normalize_feature() {
615- local _outvar="$1"
616- local _feature_nf="$2"
617- local _operation="$3"
618- 
619- if [ "$_operation" = "uninstall" ]; then
620- eval "${_outvar}=\"all\""
621- return 0
622- fi
623- 
624- if [ "$_feature_nf" = "" ]; then
625- eval "${_outvar}=\"all\""
626- return 0
627- fi
628- 
629- case "${_feature_nf}" in
630- all,*)
631- _feature_nf="all"
632- ;;
633- *,all)
634- _feature_nf="all"
635- ;;
636- *,all,*)
637- _feature_nf="all"
638- ;;
639- esac
640- 
641- eval "${_outvar}=\"${_feature_nf}\""
642-}
643- 
644-# 删除软连接
645-remove_softlink_icp() {
646- local softlink="$1"
647- 
648- if [ "${softlink}" = "" ]; then
649- return 0
650- fi
651- 
652- if [ "${softlink}" != "NA" ] && [ -L "${softlink}" ]; then
653- rm -f "${softlink}"
654- if [ $? -ne 0 ]; then
655- log "ERROR" "remove ${softlink} failed!"
656- return 1
657- fi
658- fi
659- return 0
660-}
661- 
662-#移除文件
663-remove_file() {
664- local target="$1"
665- local softlink="$2"
666- local ret
667- if [ -e "${target}" ] || [ -L "${target}" ]; then
668- rm -f "${target}"
669- if [ $? -ne 0 ]; then
670- log "ERROR" "remove ${target} failed!"
671- return 1
672- fi
673- fi
674- remove_softlink_icp "${softlink}"
675- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
676- 
677- return 0
678-}
679- 
680-# 创建文件夹
681-create_folder() {
682- local install_path="$1"
683- local target="$2"
684- local softlinks_str="$3"
685- local ret target_abs
686- 
687- __set_abs_path "${install_path}" "${target}" "target_abs"
688- 
689- if [ ! -d "${target_abs}" ]; then
690- make_dir "${target_abs}"
691- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
692- fi
693- change_mod "${target_abs}" "${RESET_MOD}" ""
694- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
695- if [ "${softlinks_str}" != "NA" ]; then
696- create_softlink_by_install_path "${install_path}" "${target}" "${softlinks_str}"
697- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
698- fi
699- return 0
700-}
701- 
702-# 创建目录
703-create_dirs() {
704- local install_path="$1"
705- local line="$2"
706- local target
707- local target_abs
708- local softlinks_str
709- local ret
710- 
711- __index_list "${line}" 1 "target" 4 "softlinks_str"
712- 
713- __set_abs_path "${install_path}" "${target}" "target_abs"
714- 
715- if [ -L "${target_abs}" ] ; then
716- rm -f "${target_abs}"
717- log "WARNING" "${target_abs} is an existing soft-link, deleted."
718- fi
719- create_folder "${install_path}" "${target}" "${softlinks_str}"
720- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
721- return 0
722-}
723- 
724-# 修改目录的权限和属组
725-reset_mod_dirs() {
726- local install_path="$1"
727- local line="$2"
728- local mod
729- local target
730- local target_abs
731- local is_abs_path ret
732- 
733- __index_list "${line}" 1 "target"
734- 
735- __set_abs_path "${install_path}" "${target}" "target_abs"
736- 
737- # 目录不存在时跳过
738- if [ ! -d "${target_abs}" ]; then
739- return 0
740- fi
741- # 只处理目录,没有处理目录的软链接
742- change_mod "${target_abs}" "${RESET_MOD}" "" "false"
743- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
744- 
745- return 0
746-}
747- 
748-# 递归修改目录的权限和属组
749-reset_mod_dirs_recursive() {
750- local install_path="$1"
751- local line="$2"
752- local mod
753- local target
754- local target_abs
755- local is_abs_path ret
756- 
757- __index_list "${line}" 1 "target"
758- 
759- __set_abs_path "${install_path}" "${target}" "target_abs"
760- 
761- # 目录不存在时跳过
762- if [ ! -d "${target_abs}" ]; then
763- return 0
764- fi
765- # 只处理目录,没有处理目录的软链接
766- change_mod "${target_abs}" "${RESET_MOD}" "" "true"
767- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
768- 
769- return 0
770-}
771- 
772-__unpack_softlinks() {
773- local softlinks_str="$1"
774- local varname="$2"
775- local item
776- local temp
777- OLD_IFS="${IFS}"
778- IFS=";"
779- temp=""
780- for item in ${softlinks_str}; do
781- if [ "${temp}" = "" ]; then
782- temp="${item}"
783- else
784- temp="${temp} ${item}"
785- fi
786- done
787- IFS="${OLD_IFS}"
788- 
789- eval "${varname}=\"${temp}\""
790-}
791- 
792-#移除文件夹
793-remove_dir_icp() {
794- if [ -e "$1" ] || [ -L "$1" ]; then
795- rm -fr "$1"
796- if [ $? -ne 0 ]; then
797- log "ERROR" "$1 remove failed!"
798- return 1
799- fi
800- fi
801- return 0
802-}
803- 
804-# 创建软链时,移除存在的目录
805-remove_exists_dir_in_create_softlink() {
806- local dirpath="$1"
807- 
808- if [ -d "${dirpath}" ] && [ ! -L "${dirpath}" ]; then
809- log "WARNING" "${dirpath} is an existing directory in create softlink, deleted."
810- change_mod "${dirpath}" "700" "n" "true"
811- remove_dir_icp "${dirpath}"
812- fi
813-}
814- 
815-# 创建绝对软链接
816-create_softlink_icp_absolute() {
817- local src_path="$1"
818- local dst_path="$2"
819- 
820- remove_exists_dir_in_create_softlink "${dst_path}"
821- ln -sfn "${src_path}" "${dst_path}"
822- if [ $? -ne 0 ]; then
823- log "ERROR" "create softlink absolute from ${src_path} to ${dst_path} failed!"
824- return 1
825- fi
826- return 0
827-}
828- 
829-# 该函数与common_func.inc脚本中的相同
830-# install_common_parser.sh不一定能source到common_func.inc(原因见source common_func.inc的注释)
831-# 所以这里需要重复定义
832-create_softlink_icp_relative() {
833- local src_path="$1"
834- local dst_path="$2"
835- local source="top${src_path}"
836- local target="top${dst_path}"
837- 
838- # 若变量内容从尾向前的数据符合,则将符合的最短数据删除
839- local common="${target%/*}"
840- # 若变量内容从头开始的数据符合,则将符合的最短数据删除
841- local forward="${source#"$common"/}"
842- 
843- local result=""
844- 
845- while [ "${forward}" = "${source}" ]; do
846- common="$(dirname "$common")"
847- forward="${source#"$common"/}"
848- result="../${result}"
849- done
850- 
851- result="${result}${forward}"
852- 
853- remove_exists_dir_in_create_softlink "${dst_path}"
854- ln -sfn "${result}" "${dst_path}"
855- if [ $? -ne 0 ]; then
856- log "ERROR" "create softlink relative from ${src_path} to ${dst_path} failed!"
857- return 1
858- fi
859- return 0
860-}
861- 
862-# 创建软连接
863-create_softlink_by_install_path() {
864- local install_path="$1"
865- local target="$2"
866- local softlinks_str="$3"
867- local softlinks softlink
868- local target_abs
869- local softlink_abs
870- local is_abs_path
871- local ret
872- 
873- if [ "${softlinks_str}" = "NA" ]; then
874- return 0
875- fi
876- 
877- __unpack_softlinks "${softlinks_str}" "softlinks"
878- 
879- for softlink in ${softlinks}; do
880- __check_abs_path "${target}"
881- if [ "${is_abs_path}" = "true" ]; then
882- __set_abs_path "${install_path}" "${softlink}" "softlink_abs"
883- create_softlink_icp_absolute "${target}" "${softlink_abs}"
884- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
885- continue
886- fi
887- 
888- __check_abs_path "${softlink}"
889- if [ "${is_abs_path}" = "true" ]; then
890- __set_abs_path "${install_path}" "${target}" "target_abs"
891- create_softlink_icp_absolute "${target_abs}" "${softlink}"
892- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
893- continue
894- fi
895- 
896- create_softlink_icp_relative "${install_path}/${target}" "${install_path}/${softlink}"
897- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
898- done
899- 
900- return 0
901-}
902- 
903-# 创建软链接。icp后缀为避免重名
904-create_softlink_icp() {
905- local options=""
906- local is_relative="false"
907- 
908- while true; do
909- case "$1" in
910- -r|--relative)
911- is_relative="true"
912- shift
913- ;;
914- -*)
915- log "ERROR" "unsupported option $1 in create softlink icp!"
916- return 1
917- ;;
918- *)
919- break
920- ;;
921- esac
922- done
923- 
924- local src_path="$1"
925- local dst_path="$2"
926- 
927- if [ "${is_relative}" = "true" ]; then
928- create_softlink_icp_relative "${src_path}" "${dst_path}"
929- else
930- create_softlink_icp_absolute "${src_path}" "${dst_path}"
931- fi
932-}
933- 
934-# 修改权限和属组
935-change_mod_and_own(){
936- local target="$1"
937- local mod="$2"
938- local own="$3"
939- local install_for_all="$4"
940- local recursive="$5"
941- local ret
942- 
943- change_mod "${target}" "${mod}" "${install_for_all}" "${recursive}"
944- ret="$?" && [ $ret -ne 0 ] && return $ret
945- 
946- change_own "${target}" "${own}" "${recursive}"
947- ret="$?" && [ $ret -ne 0 ] && return $ret
948- 
949- return 0
950-}
951- 
952-# 修改目录的权限和属组
953-change_mod_and_own_dirs() {
954- local install_path="$1"
955- local line="$2"
956- local target
957- local mod
958- local own
959- local is_abs_path
960- 
961- __index_list "${line}" 1 "target" 2 "mod" 3 "own"
962- 
963- __check_abs_path "${target}"
964- if [ "${is_abs_path}" != "true" ]; then
965- target="${install_path}/${target}"
966- fi
967- if [ ! -d "${target}" ]; then
968- return 0
969- fi
970- # 只处理目录,没有处理目录的软链接
971- change_mod_and_own "${target}" "${mod}" "${own}" "${INSTALL_FOR_ALL}" "false"
972-}
973- 
974-# 创建stash_mod.txt文件
975-create_stash_mod() {
976- local install_path="$1"
977- rm -f "${install_path}/${STASH_MOD_PATH}"
978- touch "${install_path}/${STASH_MOD_PATH}"
979- chmod ${STASH_FILE_MOD} "${install_path}/${STASH_MOD_PATH}"
980-}
981- 
982-# 删除stash_mod.txt文件
983-remove_stash_mod() {
984- local install_path="$1"
985- rm -f "${install_path}/${STASH_MOD_PATH}"
986-}
987- 
988-# 修改目录的权限和属组
989-restore_stash_mod() {
990- local install_path="$1"
991- local line="$2"
992- local target
993- local target_abs
994- local mod
995- local is_abs_path ret
996- 
997- __index_list "${line}" 0 "target" 1 "mod"
998- 
999- __set_abs_path "${install_path}" "${target}" "target_abs"
1000- 
1001- # 目录不存在时跳过
1002- if [ ! -d "${target_abs}" ]; then
1003- return 0
1004- fi
1005- # 只处理目录,没有处理目录的软链接
1006- change_mod "${target_abs}" "${mod}" "${INSTALL_FOR_ALL}"
1007- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
1008- 
1009- return 0
1010-}
1011- 
1012-# 修改目录的权限,暂存原文件权限
1013-reset_mod_dirs_with_stash_mod() {
1014- local install_path="$1"
1015- local line="$2"
1016- local mod
1017- local target
1018- local target_abs
1019- local is_abs_path ret
1020- 
1021- __index_list "${line}" 1 "target"
1022- 
1023- __set_abs_path "${install_path}" "${target}" "target_abs"
1024- 
1025- # 目录不存在时跳过
1026- if [ ! -d "${target_abs}" ]; then
1027- return 0
1028- fi
1029- 
1030- get_file_mod "mod" "${target_abs}"
1031- echo "${target}:${mod}" >> "${install_path}/${STASH_MOD_PATH}"
1032- 
1033- # 只处理目录,没有处理目录的软链接
1034- change_mod "${target_abs}" "${RESET_MOD}" ""
1035- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
1036- 
1037- return 0
1038-}
1039- 
1040-# 删除软连接列表
1041-remove_softlinks() {
1042- local install_path="$1"
1043- local softlinks_str="$2"
1044- local softlinks softlink softlink_abs ret
1045- 
1046- __unpack_softlinks "${softlinks_str}" "softlinks"
1047- 
1048- for softlink in ${softlinks}; do
1049- __set_abs_path "${install_path}" "${softlink}" "softlink_abs"
1050- remove_softlink_icp "${softlink_abs}"
1051- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
1052- done
1053- return 0
1054-}
1055- 
1056-# 删除安装文件夹
1057-remove_install_dirs() {
1058- local install_path="$1"
1059- local line="$2"
1060- local target softlinks_str is_abs_path
1061- 
1062- __index_list "${line}" 1 "target" 4 "softlinks_str"
1063- 
1064- if [ "${target}" != "NA" ]; then
1065- __check_abs_path "${target}"
1066- if [ "${is_abs_path}" != "true" ]; then
1067- target="${install_path}/${target}"
1068- fi
1069- if [ -d "${target}" ]; then
1070- # 不同的blocks中,可能配置相同的dir_info。目录不为空时不删除。
1071- if [ "$(ls -A "${target}")" != "" ]; then
1072- return 0
1073- fi
1074- remove_dir_icp "${target}"
1075- ret="$?" && [ $ret -ne 0 ] && return $ret
1076- fi
1077- 
1078- if [ "${softlinks_str}" != "NA" ]; then
1079- remove_softlinks "${install_path}" "${softlinks_str}"
1080- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
1081- fi
1082- fi
1083- return 0
1084-}
1085- 
1086-# 获取所有的blocks
1087-get_blocks_info() {
1088- local _outvar="$1"
1089- local _install_path="$2"
1090- local _result _db_filepath="${_install_path}/${PKG_DB_INFO_RELPATH}"
1091- 
1092- if [ ! -f "${_db_filepath}" ]; then
1093- return 0
1094- fi
1095- _result="$(cut -d'|' -f1 "${_db_filepath}" | xargs)"
1096- eval "${_outvar}=\"${_result}\""
1097-}
1098- 
1099-# 列表转为正则表达式
1100-trans_list_to_regex() {
1101- local _outvar="$1"
1102- local _value="$2"
1103- # 移除右侧空白符,mawk无法编译右侧存在空白符时的正则表达式,如:^(EngineeringCommon|)$
1104- local _result="$(echo "$_value" | sed 's/\s\+$//g' | sed 's/ /|/g' | xargs printf "^(%s)$")"
1105- eval "${_outvar}=\"${_result}\""
1106-}
1107- 
1108-parse_filelist_core() {
1109- awk -F, '{ print $3,$4,$6,$7,$9,$12,$13,$14,$15 }'
1110-}
1111- 
1112-# 过滤公共路径条目
1113-filter_common_dirs() {
1114- awk -F, '
1115- $11 == "Y" {
1116- print $0
1117- }
1118- $11 == "YY" {
1119- print $1 "," $2 "," $3 "," $4 "," $5 "," $6 "," $7 "," $8 ",NA," $10 "," $11 "," $12 "," $13 "," $14 "," $15 "," $16
1120- }
1121- '
1122-}
1123- 
1124-# 过滤包内软链
1125-filter_pkg_inner_softlink() {
1126- awk -F, '$15 != "NA" { print $0 }'
1127-}
1128- 
1129-# 过滤块
1130-filter_blocks() {
1131- local blocks="$1"
1132- local blocks_reg
1133- 
1134- trans_list_to_regex "blocks_reg" "$blocks"
1135- awk -F, "\$14 ~ \"$blocks_reg\" {print \$0}"
1136-}
1137- 
1138-# 过滤不匹配块
1139-filterfalse_blocks() {
1140- local blocks="$1"
1141- local blocks_reg
1142- 
1143- trans_list_to_regex "blocks_reg" "$blocks"
1144- awk -F, "\$14 !~ \"$blocks_reg\" {print \$0}"
1145-}
1146- 
1147-# 过滤操作类型条目
1148-filter_operate_type() {
1149- local operator_type="$1"
1150- local operator_type_reg
1151- 
1152- trans_list_to_regex "operator_type_reg" "$operator_type"
1153- 
1154- # ~为部分匹配,正则表达式需要匹配头尾
1155- awk -F, "\$2 ~ \"$operator_type_reg\" {print \$0}"
1156-}
1157- 
1158-# 过滤安装类型条目
1159-filter_install_type() {
1160- local install_type="$1"
1161- local install_type_reg
1162- 
1163- # 注意:特征串不能嵌套,否则会错误匹配
1164- if [ "$install_type" != "full" ] && [ "$install_type" != "debug" ]; then
1165- install_type_reg="(all)|($install_type)"
1166- else
1167- install_type_reg="(all)|(docker)|(devel)|(run)"
1168- fi
1169- 
1170- awk -F, "\$8 ~ \"$install_type_reg\" {print \$0}"
1171-}
1172- 
1173-# 过滤特性参数
1174-filter_feature_param() {
1175- local feature_param="$1"
1176- local feature_type
1177- local feature_exclude_all # feature是否为排除公共all文件
1178- local chip chip_list # 芯片类型
1179- local feature_list feature_type_list
1180- 
1181- unpack_feature_param "feature_type" "feature_exclude_all" "chip" ${feature_param}
1182- 
1183- if [ "$feature_type" != "all" ]; then
1184- feature_list="$(echo $feature_type | tr ',' ' ')"
1185- if [ "${feature_exclude_all}" = "y" ]; then
1186- feature_type_list="${feature_list}"
1187- else
1188- feature_type_list="comm ${feature_list}"
1189- fi
1190- else
1191- feature_type_list="all"
1192- fi
1193- 
1194- if [ "$chip" != "all" ]; then
1195- chip_list="all $(echo $chip | tr ',' ' ')"
1196- else
1197- chip_list="all"
1198- fi
1199- 
1200- # filelist中的feature为all,表示所有场景下都安装这个文件
1201- # 输入参数中的feature为all,表示安装所有的feature
1202- awk -v feature_type_list="${feature_type_list}" \
1203- -v chip_list="${chip_list}" '
1204- BEGIN {
1205- FS= ","
1206- split(feature_type_list, input_feature_type_arr, " ")
1207- split(chip_list, input_chip_arr, " ")
1208- }
1209- 
1210- function match_feature_type(features_str) {
1211- if (feature_type_list == "all") {
1212- return 1
1213- }
1214- matched_feature_type_tmp=0
1215- split(features_str, features, ";")
1216- for(i in features)
1217- {
1218- for(j in input_feature_type_arr) {
1219- if(input_feature_type_arr[j] == features[i]) {
1220- matched_feature_type_tmp = 1
1221- break;
1222- }
1223- }
1224- }
1225- return matched_feature_type_tmp
1226- }
1227- 
1228- function match_chip(chip_str) {
1229- if (chip_list == "all") {
1230- return 1
1231- }
1232- matched_chip_tmp=0
1233- split(chip_str, chips, ";")
1234- for(i in chips)
1235- {
1236- for(j in input_chip_arr) {
1237- if(input_chip_arr[j] == chips[i]) {
1238- matched_chip_tmp = 1
1239- break;
1240- }
1241- }
1242- }
1243- return matched_chip_tmp
1244- }
1245- 
1246- {
1247- matched_feature_type = match_feature_type($10);
1248- if (matched_feature_type == 0) next;
1249- 
1250- matched_chip = match_chip($16)
1251- if (matched_chip == 0) next
1252- 
1253- print $0
1254- }'
1255-}
1256- 
1257-# 读取fileist的第2行到最后行
1258-tail_filelist() {
1259- local filelist_path="$1"
1260- tail -n +2 "$filelist_path"
1261-}
1262- 
1263-# 解析filelist.csv脚本
1264-parse_filelist() {
1265- local install_type="$1"
1266- local operate_type="$2"
1267- local filelist_path="$3"
1268- local feature_param="$4"
1269- local filter_type="$5"
1270- local blocks="$6"
1271- 
1272- if [ ! -f "$filelist_path" ]; then
1273- log "ERROR" "filelist $filelist_path does not exist!"
1274- exit 1
1275- fi
1276- 
1277- # 注意:这里的filelist需要是全局变量。其它函数(如add_filelist_blocks_info)会引用这个变量。
1278- filelist=$(
1279- parse_filelist_v2 "$install_type" "$operate_type" "$filelist_path" "$feature_param" \
1280- "$filter_type" "$blocks"
1281- )
1282-}
1283- 
1284-# 解析filelist.csv脚本
1285-parse_filelist_v2() {
1286- local install_type="$1"
1287- local operate_type="$2"
1288- local filelist_path="$3"
1289- local feature_param="$4"
1290- local filter_type="$5"
1291- local blocks="$6"
1292- local filter_cmds=""
1293- 
1294- if [ "$operate_type" != "all" ]; then
1295- filter_cmds="$filter_cmds | filter_operate_type \"$operate_type\""
1296- fi
1297- if printf "%s" "$filter_type" | grep -Eq "\<filter_common_dirs\>"; then
1298- filter_cmds="$filter_cmds | filter_common_dirs"
1299- fi
1300- if printf "%s" "$filter_type" | grep -Eq "\<filter_by_pkg_inner_softlink\>"; then
1301- filter_cmds="$filter_cmds | filter_pkg_inner_softlink"
1302- fi
1303- if printf "%s" "$filter_type" | grep -Eq "\<filter_by_blocks\>|\<filterfalse_blocks\>" && [ "$blocks" != "" ]; then
1304- filter_cmds="$filter_cmds | filterfalse_blocks \"$blocks\""
1305- fi
1306- if printf "%s" "$filter_type" | grep -Eq "\<filter_blocks\>" && [ "$blocks" != "" ]; then
1307- filter_cmds="$filter_cmds | filter_blocks \"$blocks\""
1308- fi
1309- 
1310- tail_filelist "$filelist_path" \
1311- | filter_install_type "$install_type" \
1312- | filter_feature_param "$feature_param" \
1313- | eval cat $filter_cmds \
1314- | parse_filelist_core
1315-}
1316- 
1317- 
1318-pack_exec_params() {
1319- local _outvar="$1"
1320- local _exec_mode="$2"
1321- 
1322- eval "${_outvar}=\"${_exec_mode}\""
1323-}
1324- 
1325- 
1326-# 迭代filelist中的条目,执行操作
1327-foreach_filelist_exec() {
1328- local filelist="$1"
1329- local sort_filelist="$2"
1330- local exec_mode="$3"
1331- local exec_func="$4"
1332- local install_path="$5"
1333- local ret=0 tmp_ret exec_params
1334- shift 5
1335- 
1336- pack_exec_params "exec_params" "${exec_mode}"
1337- 
1338- if [ "${sort_filelist}" = "reverse" ]; then
1339- # 对第二列文件路径做倒序排列,保证先删除子文件夹,再删除父文件夹
1340- # 不可以使用echo "${filelist}"
1341- # 在dash中会消耗\\$username:\\$usergroup中的一个反斜线(bash与busybox的sh无此问题)
1342- # 需要使用here document,防止shell的解析过程
1343- filelist=$(sort -k2,2 -b -r <<EOF
1344-${filelist}
1345-EOF
1346- )
1347- elif [ "${sort_filelist}" = "no" ]; then
1348- # 不排序
1349- :
1350- else
1351- log "ERROR" "sort_filelist param wrong! sort_filelist is ${sort_filelist}, exec_func is ${exec_func}, parse_func is ${parse_func}"
1352- exit 1
1353- fi
1354- 
1355- if [ "${exec_mode}" = "concurrency" ] || [ "${exec_mode}" = "normal" ]; then
1356- # 并发模式或正常模式
1357- :
1358- else
1359- log "ERROR" "exec_mode param wrong! exec_mode is ${exec_mode}, exec_func is ${exec_func}, parse_func is ${parse_func}"
1360- exit 1
1361- fi
1362- 
1363- while read line; do
1364- array=${line}
1365- __length_list "${array}" "len_array"
1366- if [ ${len_array} -eq 0 ]; then
1367- continue
1368- fi
1369- "${exec_func}" "${install_path}" "${line}" "${exec_params}" "$@"
1370- tmp_ret="$?" && [ ${tmp_ret} -ne 0 ] && ret="${tmp_ret}"
1371- done << EOF
1372-${filelist}
1373-EOF
1374- 
1375- if [ "${exec_mode}" = "concurrency" ]; then
1376- wait
1377- fi
1378- 
1379- return $ret
1380-}
1381- 
1382-# 迭代遍历filelist中的条目,执行一些操作
1383-foreach_filelist() {
1384- local filter_type="$1"
1385- local exec_func="$2"
1386- local install_type="$3"
1387- local install_path="$4"
1388- local operate_type="$5"
1389- local filelist_path="$6"
1390- local feature_param="$7"
1391- local sort_filelist="$8" # 排序filelist,可选参数为[no,reverse]
1392- local exec_mode="$9" # 执行模式,可选参数为[normal,concurrency]
1393- shift 9
1394- local ret blocks
1395- 
1396- if [ "$filter_type" = "filter_by_blocks" ]; then
1397- get_blocks_info "blocks" "$install_path"
1398- fi
1399- 
1400- foreach_filelist_v2 "$exec_func" "$install_type" "$install_path" "$operate_type" "$filelist_path" \
1401- "$feature_param" "$filter_type" "$blocks" "$sort_filelist" "$exec_mode" "$@"
1402- ret="$?" && [ $ret -ne 0 ] && return $ret
1403- 
1404- return 0
1405-}
1406- 
1407-# 迭代遍历filelist中的条目,执行一些操作
1408-foreach_filelist_v2() {
1409- local exec_func="$1"
1410- local install_type="$2"
1411- local install_path="$3"
1412- local operate_type="$4"
1413- local filelist_path="$5"
1414- local feature_param="$6"
1415- local filter_type="$7"
1416- local blocks="$8"
1417- local sort_filelist="$9" # 排序filelist,可选参数为[no,reverse]
1418- local exec_mode="${10}" # 执行模式,可选参数为[normal,concurrency]
1419- shift 10
1420- local filter_cmds=""
1421- local ret
1422- 
1423- parse_filelist "${install_type}" "${operate_type}" "${filelist_path}" "${feature_param}" \
1424- "${filter_type}" "${blocks}"
1425- 
1426- foreach_filelist_exec "${filelist}" "${sort_filelist}" "${exec_mode}" "${exec_func}" "${install_path}" "$@"
1427- ret="$?" && [ $ret -ne 0 ] && return $ret
1428- 
1429- return 0
1430-}
1431- 
1432-# 解析stash_mod.txt
1433-parse_stashmod() {
1434- local stashmod_path="$1"
1435- 
1436- if [ ! -f "${stashmod_path}" ]; then
1437- log "ERROR" "stash mod ${stashmod_path} does not exist!"
1438- exit 1
1439- fi
1440- 
1441- stashmod_list=$(awk '
1442- BEGIN{
1443- FS=":"
1444- }
1445- {
1446- print $1,$2
1447- }' "${stashmod_path}")
1448-}
1449- 
1450-# 迭代遍历stash_mod中的条目,执行操作
1451-foreach_stashmod() {
1452- local exec_func="$1"
1453- local install_path="$2"
1454- local sort_type="$3" # 排序方式,可选参数为[no,reverse]
1455- local array
1456- local len_array
1457- local line
1458- local ret=0 tmp_ret
1459- 
1460- parse_stashmod "${install_path}/${STASH_MOD_PATH}"
1461- 
1462- if [ "${sort_type}" = "reverse" ]; then
1463- # 对第二列文件路径做倒序排列,保证先处理子文件夹,再处理父文件夹
1464- stashmod_list=$(echo "${stashmod_list}" | sort -k1,1 -r)
1465- elif [ "${sort_type}" = "no" ]; then
1466- # 不排序
1467- echo > /dev/null
1468- else
1469- log "ERROR" "sort_type param wrong! sort_type is ${sort_type}, exec_func is ${exec_func}"
1470- exit 1
1471- fi
1472- 
1473- while read line; do
1474- array=${line}
1475- __length_list "${array}" "len_array"
1476- if [ ${len_array} -eq 0 ]; then
1477- continue
1478- fi
1479- "${exec_func}" "${install_path}" "${line}"
1480- tmp_ret="$?" && [ ${tmp_ret} -ne 0 ] && ret="${tmp_ret}"
1481- done << EOF
1482-${stashmod_list}
1483-EOF
1484- 
1485- return $ret
1486-}
1487- 
1488-# 创建目录并且设置权限
1489-make_dir_with_permission() {
1490- local path="$1"
1491- local mod="$2"
1492- local username="$3"
1493- local usergroup="$4"
1494- local install_for_all="$5"
1495- local ret
1496- 
1497- make_dir "${path}"
1498- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
1499- 
1500- change_mod "${path}" "${mod}" "${install_for_all}"
1501- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
1502- 
1503- change_own "${path}" "${username}:${usergroup}"
1504- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
1505- 
1506- return 0
1507-}
1508- 
1509-# blocks转换为db.info配置
1510-blocks_to_db_item() {
1511- local package="$1"
1512- local version_dir="$2"
1513- 
1514- xargs printf "%s|[$package:$version_dir]\n"
1515-}
1516- 
1517-# 折叠第2个参数
1518-# 需要保证输入数据第1个参数已排序
1519-fold_2() {
1520- local fs="$1"
1521- local concat="$2"
1522- local options=""
1523- 
1524- if [ "$fs" != "" ]; then
1525- options="-F$fs"
1526- fi
1527- 
1528- awk $options -v CONCAT="$concat" '
1529- NR == 1 {
1530- field1 = $1
1531- field2 = $2
1532- }
1533- NR != 1 {
1534- if (field1 == $1) {
1535- field2 = field2 CONCAT $2
1536- } else {
1537- print field1 FS field2
1538- field1 = $1
1539- field2 = $2
1540- }
1541- }
1542- END {
1543- if (field1) {
1544- print field1 FS field2
1545- }
1546- }'
1547-}
1548- 
1549-# 折叠第3个参数,保持第2个参数
1550-# 需要保证输入数据第1个参数已排序
1551-fold_3_keep_2() {
1552- local fs="$1"
1553- local concat="$2"
1554- local options=""
1555- 
1556- if [ "$fs" != "" ]; then
1557- options="-F$fs"
1558- fi
1559- 
1560- awk $options -v CONCAT="$concat" '
1561- NR == 1 {
1562- field1 = $1
1563- field2 = $2
1564- field3 = $3
1565- }
1566- NR != 1 {
1567- if (field1 == $1) {
1568- field3 = field3 CONCAT $3
1569- } else {
1570- print field1 FS field2 FS field3
1571- field1 = $1
1572- field2 = $2
1573- field3 = $3
1574- }
1575- }
1576- END {
1577- if (field1) {
1578- print field1 FS field2 FS field3
1579- }
1580- }'
1581-}
1582- 
1583-# 根据第1列排序
1584-sort_1() {
1585- local sep="$1"
1586- local options=""
1587- if [ "$sep" != "" ]; then
1588- options="-t$sep"
1589- fi
1590- sort $options -k1,1 -s
1591-}
1592- 
1593-# 显示3,4列有差异的条目
1594-show_diff_3_4() {
1595- awk '$3 != $4 { print $0 }'
1596-}
1597- 
1598-# 显示具有最少列数的条目
1599-show_min_nf() {
1600- local min_nf="$1"
1601- awk -v MIN_NF="$min_nf" 'NF >= MIN_NF { print $0 }'
1602-}
1603- 
1604-# 选取第3,2,1列
1605-select_fields_3_2_1() {
1606- awk '{print $3, $2, $1}'
1607-}
1608- 
1609-# 选取第1列
1610-select_fields_1() {
1611- awk '{print $1}'
1612-}
1613- 
1614-# 删除db.info配置
1615-del_db_items() {
1616- local package="$1"
1617- local version_dir="$2"
1618- 
1619- sed "s/\\[$package:$version_dir\\]//g; /|\$/d"
1620-}
1621- 
1622-# 保留块最后一个配置
1623-# 输出:第1列(块),第2列(包名),第3列(版本目录)
1624-remain_db_last_item() {
1625- awk -F '[|:\\[\\]]+' '{ print $1, $(NF-2), $(NF-1) }'
1626-}
1627- 
1628-# 移除空白行
1629-remove_blank_line() {
1630- sed '/^$/d'
1631-}
1632- 
1633-# 保证文件权限
1634-ensure_permission() {
1635- local path="$1"
1636- local mod="$2"
1637- local username="$3"
1638- local usergroup="$4"
1639- local install_for_all="$5"
1640- 
1641- change_mod "$path" "$mod" "$install_for_all"
1642- ret=$? && [ $ret -ne 0 ] && return $ret
1643- 
1644- change_own "$path" "$username:$usergroup"
1645- ret=$? && [ $ret -ne 0 ] && return $ret
1646- 
1647- return 0
1648-}
1649- 
1650-# 创建文件
1651-touch_file() {
1652- local path="$1"
1653- local mod="$2"
1654- local username="$3"
1655- local usergroup="$4"
1656- local install_for_all="$5"
1657- local ret
1658- 
1659- touch "$path"
1660- ret=$? && [ $ret -ne 0 ] && return $ret
1661- 
1662- ensure_permission "$path" "$mod" "$username" "$usergroup" "$install_for_all"
1663- ret=$? && [ $ret -ne 0 ] && return $ret
1664- 
1665- return 0
1666-}
1667- 
1668-# 保证文件存在
1669-ensure_file() {
1670- local path="$1"
1671- local mod="$2"
1672- local username="$3"
1673- local usergroup="$4"
1674- local install_for_all="$5"
1675- local ret
1676- 
1677- if [ ! -f "$path" ]; then
1678- touch_file "$path" "$mod" "$username" "$usergroup" "$install_for_all"
1679- ret=$? && [ $ret -ne 0 ] && return $ret
1680- fi
1681- 
1682- return 0
1683-}
1684- 
1685-# 写文本文件
1686-write_text() {
1687- local content="$1"
1688- local filepath="$2"
1689- 
1690- printf "%s\n" "$content" > "$filepath"
1691-}
1692- 
1693-# 修改文件权限并操作
1694-with_chmod() {
1695- local path="$1"
1696- local mod="$2"
1697- local origin_mod ret
1698- shift 2
1699- 
1700- origin_mod="$(stat -L -c "%a" "$path")"
1701- chmod "$mod" "$path"
1702- "$@"
1703- ret="$?"
1704- chmod "$origin_mod" "$path"
1705- 
1706- return $ret
1707-}
1708- 
1709-# filelist中所有公共目录的块
1710-all_common_dirs_blocks_in_filelist() {
1711- local install_type="$1"
1712- local filelist_path="$2"
1713- local feature_param="$3"
1714- 
1715- parse_filelist_v2 "$install_type" "all" "$filelist_path" "$feature_param" "filter_common_dirs" "" \
1716- | cut -d' ' -f8 | sort | uniq
1717-}
1718- 
1719- 
1720-# 是否为版本目录
1721-is_version_dirpath() {
1722- local version_dirpath="$1"
1723- if [ -d "$version_dirpath/share/info" ]; then
1724- return 0
1725- fi
1726- if [ -f "$version_dirpath/var/ascend_package_db.info" ]; then
1727- return 0
1728- fi
1729- return 1
1730-}
Dscripts/package/common/sh/common_installer.inc+0-55
@@ -1,55 +0,0 @@
1-#!/bin/sh
2-# --------------------------------------------------------------------------------
3-# Copyright (c) 2025 Huawei Technologies Co., Ltd.
4-# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
5-# CANN Open Software License Agreement Version 2.0 (the "License").
6-# Please refer to the License for details. You may not use this file except in compliance with the License.
7-# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
8-# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
9-# See LICENSE in the root of the software repository for the full text of the License.
10-# --------------------------------------------------------------------------------
11- 
12-# 通知latest管理器创建版本软链
13-notify_latest_manager_create_version_softlink() {
14- local curpath install_path version_dir var_path
15- 
16- set_comm_log "Notifier"
17- 
18- curpath="$(dirname $(readlink -f "${BASH_SOURCE:-$0}"))"
19- install_path="$(readlink -f "$curpath/..")"
20- version_dir="$(basename "$curpath")"
21- var_path="$install_path/$LATEST_DIR/var"
22- 
23- if [ ! -f "$var_path/manager.sh" ]; then
24- comm_log "ERROR" "$var_path/manager.sh doesn't exist!"
25- exit 2
26- fi
27- 
28- if ! "$var_path/manager.sh" --version-dir "$version_dir" create_version_softlink; then
29- comm_log "ERROR" "create version softlink failed!"
30- exit 1
31- fi
32- return 0
33-}
34- 
35-# 通知latest管理器删除latest软链
36-notify_latest_manager_remove_latest_softlink() {
37- local curpath install_path var_path
38- 
39- set_comm_log "Notifier"
40- 
41- curpath="$(dirname $(readlink -f "${BASH_SOURCE:-$0}"))"
42- install_path="$(readlink -f "$curpath/..")"
43- var_path="$install_path/$LATEST_DIR/var"
44- 
45- if [ ! -f "$var_path/manager.sh" ]; then
46- comm_log "ERROR" "$var_path/manager.sh doesn't exist!"
47- exit 2
48- fi
49- 
50- if ! "$var_path/manager.sh" remove_latest_softlink; then
51- comm_log "ERROR" "remove latest softlink failed!"
52- exit 1
53- fi
54- return 0
55-}
Dscripts/package/common/sh/common_interface.bash+0-61
@@ -1,61 +0,0 @@
1-#!/bin/sh
2-# --------------------------------------------------------------------------------
3-# Copyright (c) 2025 Huawei Technologies Co., Ltd.
4-# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
5-# CANN Open Software License Agreement Version 2.0 (the "License").
6-# Please refer to the License for details. You may not use this file except in compliance with the License.
7-# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
8-# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
9-# See LICENSE in the root of the software repository for the full text of the License.
10-# --------------------------------------------------------------------------------
11- 
12-mk_custom_path() {
13- if [ $(id -u) -eq 0 ]; then
14- return 0
15- fi
16- local _custom_path_file="$1"
17- while read line || [ -n "$line" ]
18- do
19- local _custom_path="$(echo "$line" | cut --only-delimited -d= -f2)"
20- if [ -z "$_custom_path" ]; then
21- continue
22- fi
23- eval "_custom_path=$_custom_path"
24- if [ ! -d "$_custom_path" ]; then
25- mkdir -p "$_custom_path"
26- if [ $? -ne 0 ]; then
27- cur_date="$(date +"%Y-%m-%d %H:%M:%S")"
28- echo "[Common] [$cur_date] [ERROR]: create $_custom_path failed."
29- return 1
30- fi
31- fi
32- done < $_custom_path_file
33- return 0
34-}
35- 
36-py_version_check(){
37- local pyver_set="3.7 3.8 3.9 3.10 3.11"
38- local cur_date="$(date +"%Y-%m-%d %H:%M:%S")"
39- which python3 > /dev/null 2>&1
40- if [ $? -eq 0 ]; then
41- local python_version="$(python3 --version 2>&1 | head -n 1)"
42- local python3_version=$(echo "$python_version" | sed -n 's/.*[^\.0-9]\([0-9]\+\.[0-9]\+\).*/\1/p')
43- if [ "x$python3_version" != "x" ]; then
44- for ver in $pyver_set; do
45- if [ "x$ver" = "x$python3_version" ]; then
46- return 0
47- fi
48- done
49-
50- echo "[Common] [$cur_date] [WARNING]: $python_version is not in Python3.7.x, Python3.8.x, Python3.9.x, Python3.10.x, Python3.11.x."
51- return 1
52- else
53- echo "[Common] [$cur_date] [WARNING]: $python_version cannot be identified as a standard version, please check manually."
54- return 1
55- fi
56- else
57- echo "[Common] [$cur_date] [WARNING]: python3 is not found."
58- return 1
59- fi
60-}
61- 
Dscripts/package/common/sh/common_interface.csh+0-37
@@ -1,37 +0,0 @@
1-#!/bin/csh
2-# --------------------------------------------------------------------------------
3-# Copyright (c) 2025 Huawei Technologies Co., Ltd.
4-# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
5-# CANN Open Software License Agreement Version 2.0 (the "License").
6-# Please refer to the License for details. You may not use this file except in compliance with the License.
7-# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
8-# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
9-# See LICENSE in the root of the software repository for the full text of the License.
10-# --------------------------------------------------------------------------------
11- 
12-set func_name = "$1"
13-switch ( "$func_name" )
14- case "mk_custom_path":
15- if ( "`id -u`" == 0 ) then
16- exit 0
17- endif
18- set file_path = "$2"
19- foreach line ("` cat $file_path `")
20- set custom_path = "`echo '$line' | cut --only-delimited -d= -f2`"
21- if ( "$custom_path" == "" ) then
22- continue
23- endif
24- set custom_path = "` eval echo $custom_path `"
25- if ( ! -d "$custom_path" ) then
26- mkdir -p "$custom_path"
27- if ( $status != 0 ) then
28- set cur_date = "`date +'%Y-%m-%d %H:%M:%S'`"
29- echo "[Common] [$cur_date] [ERROR]: create $custom_path failed."
30- exit 1
31- endif
32- endif
33- end
34- breaksw
35- default:
36- breaksw
37-endsw
Dscripts/package/common/sh/install_common_parser.sh+0-2216
@@ -1,2216 +0,0 @@
1-#!/bin/sh
2-# --------------------------------------------------------------------------------
3-# Copyright (c) 2025 Huawei Technologies Co., Ltd.
4-# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
5-# CANN Open Software License Agreement Version 2.0 (the "License").
6-# Please refer to the License for details. You may not use this file except in compliance with the License.
7-# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
8-# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
9-# See LICENSE in the root of the software repository for the full text of the License.
10-# --------------------------------------------------------------------------------
11- 
12-# run包安装解析公共脚本
13-# 解析filelist.csv文件,完成目录创建,文件复制,权限设置,文件删除等操作。
14- 
15-# minirc场景下存在使用dash调用该脚本的情况
16-#export PS4='+ ${FUNCNAME[0]:+${FUNCNAME[0]}():} ${BASH_SOURCE}:${LINENO}: '
17-#set -x
18-# 总uninstall.sh文件权限
19-TOTAL_UNINSTALL_MOD="500"
20-# db.info文件权限
21-DB_INFO_MOD="640"
22-# 包架构
23-PKG_ARCH="UNKNOWN"
24-# 复制文件是否并发
25-PARALLEL=""
26-# 是否限制并发
27-PARALLEL_LIMIT=""
28-# 控制并发的fifo文件路径
29-PARALLEL_FIFO="none"
30-COPY_COMMAND="cp -rf"
31-MOVE_COMMAND="mv -f"
32- 
33-curpath="$(dirname $(readlink -f "${BASH_SOURCE:-$0}"))"
34- 
35-# 导入公共库
36-common_func_v2_path="${curpath}/common_func_v2.inc"
37-. "${common_func_v2_path}"
38- 
39-# 导入common_func.inc
40-# 不一定能source到common_func.inc
41-# 例如driver包中的install_common_parser.sh与common_func.inc,不在同一个目录下
42-# 需要子包整改,才将该if语句移除
43-common_func_path="${curpath}/common_func.inc"
44-if [ -f "${common_func_path}" ]; then
45- . "${common_func_path}"
46-fi
47- 
48-# 导入script_operator.inc
49-script_operator_path="${curpath}/script_operator.inc"
50-if [ -f "${script_operator_path}" ]; then
51- . "${script_operator_path}"
52-fi
53- 
54-# 导入version_cfg.inc
55-version_cfg_path="${curpath}/version_cfg.inc"
56-if [ -f "${version_cfg_path}" ]; then
57- . "${version_cfg_path}"
58-fi
59- 
60-# 导入multi_version.inc
61-multi_version_path="${curpath}/multi_version.inc"
62-if [ -f "${multi_version_path}" ]; then
63- . "${multi_version_path}"
64-fi
65- 
66-# 导入version_compatiable.inc
67-version_compatiable_path="${curpath}/version_compatiable.inc"
68-if [ -f "${version_compatiable_path}" ]; then
69- . "${version_compatiable_path}"
70-fi
71- 
72-# 导入config.inc
73-config_path="${curpath}/config.inc"
74-if [ -f "${config_path}" ]; then
75- . "${config_path}"
76-fi
77- 
78-# 导入cold_patch.sh
79-cold_patch_path="${curpath}/cold_patch.sh"
80-if [ -f "${cold_patch_path}" ]; then
81- . "${cold_patch_path}"
82-fi
83- 
84-# bash执行时需要展开别名
85-if [ -n "$BASH_SOURCE" ]; then
86- shopt -s expand_aliases
87-fi
88- 
89-__pkg_in_pkgs() {
90- local matched
91- __item_in_list "matched" "$@"
92- echo "${matched}"
93-}
94- 
95-__block_in_blocks() {
96- __item_in_list "matched" "$@"
97- echo "${matched}"
98-}
99- 
100-# 包列表中移除包
101-__remove_pkg_in_pkgs() {
102- __remove_item_in_list "$@"
103-}
104- 
105-# 冒泡排序,O(n**2)
106-__sort_blocks() {
107- local blocks="$*"
108- local block
109- local sorted_blocks=""
110- local max
111- local i=0
112- 
113- __length_list "${blocks}" "len_blocks"
114- 
115- while [ "$i" -lt ${len_blocks} ]; do
116- max=""
117- for block in ${blocks}; do
118- if [ "${block}" \> "${max}" ]; then
119- max="${block}"
120- fi
121- done
122- if [ "${sorted_blocks}" = "" ]; then
123- sorted_blocks="${max}"
124- else
125- sorted_blocks="${max} ${sorted_blocks}"
126- fi
127- blocks="$(__remove_item_in_list "${max}" "${blocks}")"
128- i=$((i+1))
129- done
130- 
131- echo "${sorted_blocks}"
132-}
133- 
134-__unpack_block_item() {
135- local line="$1"
136- echo "${line}" | tr '|' ' '
137-}
138- 
139-__pack_block_item() {
140- local block_name="$1"
141- local block_pkgs_str="$2"
142- echo "${block_name}|${block_pkgs_str}"
143-}
144- 
145-__unpack_block_pkgs() {
146- local block_pkgs_str="$1"
147- echo "${block_pkgs_str}" | tr ',' ' '
148-}
149- 
150-__pack_block_pkgs() {
151- echo "$*" | tr ' ' ','
152-}
153- 
154-check_install_path() {
155- local install_path="$1"
156- 
157- if [ "${install_path}" = "" ]; then
158- log "ERROR" "install_path is empty!"
159- return 1
160- fi
161-}
162- 
163-# 添加pkg对应的block_info
164-add_pkg_blocks_info() {
165- local install_path="$1"
166- local pkg="$2"
167- shift; shift
168- # blocks为待加入db.info的块列表
169- local blocks="$*"
170- local block_idx_value
171- 
172- local db_filepath="${install_path}/${PKG_DB_INFO_RELPATH}"
173- local db_dirpath="$(dirname "${db_filepath}")"
174- 
175- local db_filepath_new="${install_path}/${PKG_DB_INFO_RELPATH}~"
176- 
177- local block_list
178- local block_name
179- local block_pkgs_str
180- local block_pkgs
181- local len_block_pkgs
182- local block_idx=0
183- local len_blocks
184- 
185- __length_list "${blocks}" "len_blocks"
186- if [ ${len_blocks} -eq 0 ]; then
187- return 0
188- fi
189- 
190- blocks="$(__sort_blocks "${blocks}")"
191- 
192- if [ ! -d "${db_dirpath}" ]; then
193- mkdir -p "${db_dirpath}"
194- if [ $? -ne 0 ]; then
195- log "ERROR" "mkdir ${db_dirpath} failed!"
196- exit 1
197- fi
198- fi
199- 
200- if [ ! -f "${db_filepath}" ]; then
201- touch "${db_filepath}"
202- if [ $? -ne 0 ]; then
203- log "ERROR" "touch ${db_filepath} failed!"
204- exit 1
205- fi
206- change_mod_and_own "${db_filepath}" "${DB_INFO_MOD}" "\$username:\$usergroup" "${INSTALL_FOR_ALL}"
207- fi
208- 
209- rm -f "${db_filepath_new}"
210- if [ $? -ne 0 ]; then
211- log "ERROR" "delete ${db_filepath_new} failed!"
212- exit 1
213- fi
214- 
215- while read line; do
216- block_list="$(__unpack_block_item "${line}")"
217- __index_list "${block_list}" 0 "block_name"
218- __index_list "${block_list}" 1 "block_pkgs_str"
219- 
220- # 如果pkgs为空,则忽略该条目
221- if [ "${block_pkgs_str}" = "" ]; then
222- continue
223- fi
224- 
225- block_pkgs="$(__unpack_block_pkgs "${block_pkgs_str}")"
226- __length_list "${block_pkgs}" "len_block_pkgs"
227- if [ ${len_block_pkgs} -eq 0 ]; then
228- continue
229- fi
230- 
231- if [ ${block_idx} -lt ${len_blocks} ]; then
232- __index_list "${blocks}" ${block_idx} "block_idx_value"
233- fi
234- 
235- # blocks中的块名小于db中的当前块名,直接添加到db中。
236- # 注意不能continue,因为还要处理db的当前条目。
237- while [ ${block_idx} -lt ${len_blocks} ] && [ "${block_idx_value}" \< "${block_name}" ]; do
238- __pack_block_item "${block_idx_value}" "${pkg}" >> "${db_filepath_new}"
239- if [ $? -ne 0 ]; then
240- log "ERROR" "write ${db_filepath_new} failed!"
241- exit 1
242- fi
243- block_idx=$((block_idx+1))
244- __index_list "${blocks}" ${block_idx} "block_idx_value"
245- done
246- 
247- # 已经没有需要添加的块,保持db的当前条目。
248- if [ ${block_idx} -eq ${len_blocks} ]; then
249- __pack_block_item "${block_name}" "${block_pkgs_str}" >> "${db_filepath_new}"
250- if [ $? -ne 0 ]; then
251- log "ERROR" "write ${db_filepath_new} failed!"
252- exit 1
253- fi
254- continue
255- fi
256- 
257- # blocks中的块名大于db中的当前块名,保持db的当前条目。
258- if [ "${block_idx_value}" \> "${block_name}" ]; then
259- __pack_block_item "${block_name}" "${block_pkgs_str}" >> "${db_filepath_new}"
260- if [ $? -ne 0 ]; then
261- log "ERROR" "write ${db_filepath_new} failed!"
262- exit 1
263- fi
264- continue
265- fi
266- 
267- # blocks中的块名等于db中的当前块名,将pkg添加到当前条目pkg列表中。
268- in_pkgs="$(__pkg_in_pkgs "${pkg}" "${block_pkgs}")"
269- if [ "${in_pkgs}" = "true" ]; then
270- __pack_block_item "${block_name}" "${block_pkgs_str}" >> "${db_filepath_new}"
271- if [ $? -ne 0 ]; then
272- log "ERROR" "write ${db_filepath_new} failed!"
273- exit 1
274- fi
275- else
276- __pack_block_item "${block_name}" "${block_pkgs_str},${pkg}" >> "${db_filepath_new}"
277- if [ $? -ne 0 ]; then
278- log "ERROR" "write ${db_filepath_new} failed!"
279- exit 1
280- fi
281- fi
282- block_idx=$((block_idx+1))
283- done < "${db_filepath}"
284- 
285- # blocks中剩余的块添加到db中。
286- while [ ${block_idx} -lt ${len_blocks} ]; do
287- __index_list "${blocks}" ${block_idx} "block_idx_value"
288- echo "${block_idx_value}|${pkg}" >> "${db_filepath_new}"
289- block_idx=$((block_idx+1))
290- done
291- 
292- cp "${db_filepath_new}" "${db_filepath}"
293- if [ $? -ne 0 ]; then
294- log "ERROR" "replace ${db_filepath} failed!"
295- exit 1
296- fi
297- 
298- rm -f "${db_filepath_new}"
299- if [ $? -ne 0 ]; then
300- log "ERROR" "delete ${db_filepath_new} failed!"
301- exit 1
302- fi
303-}
304- 
305-# 删除block_info中的pkg信息
306-del_blocks_info_pkg() {
307- local install_path="$1"
308- local pkg="$2"
309- 
310- local db_filepath="${install_path}/${PKG_DB_INFO_RELPATH}"
311- local db_dirpath="$(dirname "${db_filepath}")"
312- 
313- local db_filepath_new="${install_path}/${PKG_DB_INFO_RELPATH}~"
314- 
315- local block_list
316- local block_name
317- local block_pkgs_str
318- local block_pkgs
319- local len_block_pkgs
320- 
321- if [ ! -f "${db_filepath}" ]; then
322- return 0
323- fi
324- 
325- rm -f "${db_filepath_new}"
326- if [ $? -ne 0 ]; then
327- log "ERROR" "delete ${db_filepath_new} failed!"
328- exit 1
329- fi
330- 
331- while read line; do
332- block_list="$(__unpack_block_item "${line}")"
333- 
334- __index_list "${block_list}" 0 "block_name"
335- __index_list "${block_list}" 1 "block_pkgs_str"
336- 
337- block_pkgs="$(__unpack_block_pkgs "${block_pkgs_str}")"
338- 
339- block_pkgs="$(__remove_pkg_in_pkgs "${pkg}" "${block_pkgs}")"
340- 
341- __length_list "${block_pkgs}" "len_block_pkgs"
342- 
343- if [ ${len_block_pkgs} -gt 0 ]; then
344- block_pkgs_str="$(__pack_block_pkgs "${block_pkgs}")"
345- __pack_block_item "${block_name}" "${block_pkgs_str}" >> "${db_filepath_new}"
346- if [ $? -ne 0 ]; then
347- log "ERROR" "write ${db_filepath_new} failed!"
348- exit 1
349- fi
350- fi
351- done < "${db_filepath}"
352- 
353- # 当前条目如果没有pkg使用,则删除。
354- if [ ! -f "${db_filepath_new}" ]; then
355- rm -f "${db_filepath}"
356- if [ $? -ne 0 ]; then
357- log "ERROR" "delete ${db_filepath} failed!"
358- exit 1
359- fi
360- else
361- mv -f "${db_filepath_new}" "${db_filepath}"
362- if [ $? -ne 0 ]; then
363- log "ERROR" "replace ${db_filepath} failed!"
364- exit 1
365- fi
366- fi
367- return 0
368-}
369- 
370-# 准备并且检查软链接路径
371-prepare_and_check_softlink_path() {
372- local softlink_abs="$1"
373- local softlink_dir="$(dirname "${softlink_abs}")"
374- 
375- if [ ! -d "${softlink_dir}" ]; then
376- mkdir -p "${softlink_dir}"
377- fi
378- 
379- # 如果目标路径是个软链接,则移除
380- if [ -L "${softlink_abs}" ]; then
381- rm -f "${softlink_abs}"
382- if [ $? -ne 0 ]; then
383- log "ERROR" "remove softlink ${softlink_abs} failed! (create relative softlink)"
384- exit 1
385- fi
386- fi
387- 
388- # 不允许目标路径已经是一个目录,防止软链接到错误的位置。
389- if [ -d "${softlink_abs}" ]; then
390- log "ERROR" "softlink existed dir ${softlink_abs}!"
391- exit 1
392- fi
393-}
394- 
395-get_file_owner_group() {
396- local _outvar="$1"
397- local _path="$2"
398- local _result
399- 
400- _result="$(stat -c %U "${_path}"):$(stat -c %G "${_path}")"
401- eval "${_outvar}=\"${_result}\""
402-}
403- 
404-# 根据pkg_inner_softlink创建软链接
405-create_pkg_inner_softlink() {
406- local install_path="$1"
407- local line="$2"
408- local array
409- local target
410- local pkg_inner_softlink
411- local pkg_inner_softlink_list
412- local is_abs_path
413- 
414- __index_list "${line}" 1 "target" 8 "pkg_inner_softlink"
415- 
416- if [ "${pkg_inner_softlink}" != "NA" ]; then
417- create_softlink_by_install_path "${install_path}" "${target}" "${pkg_inner_softlink}"
418- fi
419- return 0
420-}
421- 
422-# 处理软链路径为已存在目录的情况
423-# minirc场景,install_path目录下,存在include目录
424-# 并且filelist.csv中配置了(x86_64|aarch64)-linux/include目录
425-# 则将(x86_64|aarch64)-linux/include中的内容,软链接至include目录下
426-deal_with_existed_dir() {
427- local install_path="$1"
428- local folder="$2"
429- local filelist_path="$3"
430- local install_path_regex="^(x86_64|aarch64)-linux/${folder}$"
431- local install_sub_path_regex="^(x86_64|aarch64)-linux/${folder}/[^/]+$"
432- 
433- if [ -L "${install_path}/${folder}" ] || [ ! -d "${install_path}/${folder}" ]; then
434- return 0
435- fi
436- 
437- target_path=$(awk -v folder="${folder}" -v install_path_regex="${install_path_regex}" '
438- BEGIN{
439- FS= ","
440- }
441- {
442- if ($2 != "mkdir") next
443- 
444- if ($4 !~ install_path_regex) next
445- 
446- if ($9 != folder) next
447- 
448- print $4
449- }' "${filelist_path}")
450- 
451- if [ "${target_path}" = "" ]; then
452- return 0
453- fi
454- 
455- awk -v folder="${folder}" -v install_path_regex="${install_path_regex}" -v install_sub_path_regex="${install_sub_path_regex}" '
456- BEGIN{
457- FS = ","; OFS = ","
458- }
459- {
460- if ($2 != "mkdir" && $2 != "copy" && $2 != "move") {
461- print $0
462- next
463- }
464- if ($4 ~ install_path_regex) {
465- print $1, $2, $3, $4, $5, $6, $7, $8, "NA", $10, $11, $12, $13, $14, $15
466- next
467- }
468- if ($4 !~ install_sub_path_regex) {
469- print $0
470- next
471- }
472- softlink = $9
473- if (softlink != "NA") {
474- print $0
475- next
476- }
477- 
478- z = split($4, filepath_list, "/")
479- softlink = folder "/" filepath_list[z]
480- print $1, $2, $3, $4, $5, $6, $7, $8, softlink, $10, $11, $12, $13, $14, $15
481- }' "${filelist_path}" > "${filelist_path}.tmp"
482- if [ $? -ne 0 ]; then
483- log "ERROR" "modify filelist for ${install_path}/${folder} failed!"
484- return 1
485- fi
486- 
487- mv -f "${filelist_path}.tmp" "${filelist_path}"
488- if [ $? -ne 0 ]; then
489- log "ERROR" "replace filelist for ${install_path}/${folder} failed!"
490- return 1
491- fi
492-}
493- 
494-#执行创建目录动作
495-do_create_dirs() {
496- local action="$1"
497- local install_type="$2"
498- local install_path="$3"
499- local filelist_path="$4"
500- local package="$5"
501- local feature_param="$6"
502- local ret
503- 
504- if [ "${action}" = "resetmod" ]; then
505- if [ "${package}" != "" ]; then
506- del_blocks_info_pkg "${install_path}" "${package}"
507- ret="$?" && [ $ret -ne 0 ] && return $ret
508- 
509- create_stash_mod "${install_path}"
510- ret="$?" && [ $ret -ne 0 ] && return $ret
511- 
512- foreach_filelist "NA" "reset_mod_dirs_with_stash_mod" "${install_type}" "${install_path}" "mkdir" "${filelist_path}" "${feature_param}" "no" "normal"
513- ret="$?" && [ $ret -ne 0 ] && return $ret
514- else
515- foreach_filelist "NA" "reset_mod_dirs" "${install_type}" "${install_path}" "mkdir" "${filelist_path}" "${feature_param}" "no" "normal"
516- ret="$?" && [ $ret -ne 0 ] && return $ret
517- fi
518- # 设置copy_entity路径的权限,解决删除时权限问题
519- # filter_by_blocks过滤,防止变更不删除的文件(目录)的权限
520- foreach_filelist "filter_by_blocks" "reset_mod_dirs_recursive" "${install_type}" "${install_path}" "copy_entity" "${filelist_path}" "${feature_param}" "no" "normal"
521- ret="$?" && [ $ret -ne 0 ] && return $ret
522- elif [ "${action}" = "all" ] || [ "${action}" = "mkdir" ]; then
523- deal_with_existed_dir "$install_path" "include" "$filelist_path"
524- ret="$?" && [ $ret -ne 0 ] && return $ret
525- 
526- deal_with_existed_dir "$install_path" "lib64" "$filelist_path"
527- ret="$?" && [ $ret -ne 0 ] && return $ret
528- 
529- # 先重置目录权限配置,防止软链接时缺少权限
530- foreach_filelist "NA" "reset_mod_dirs" "$install_type" "$install_path" "mkdir" "$filelist_path" "${feature_param}" "no" "normal"
531- ret="$?" && [ $ret -ne 0 ] && return $ret
532- 
533- # 重置copy_entity权限,防止块复用场景,复制时缺少权限
534- foreach_filelist "NA" "reset_mod_dirs_recursive" "$install_type" "$install_path" "copy_entity" "$filelist_path" "${feature_param}" "no" "normal"
535- ret="$?" && [ $ret -ne 0 ] && return $ret
536- 
537- foreach_filelist "NA" "create_dirs" "$install_type" "$install_path" "mkdir" "$filelist_path" "${feature_param}" "no" "normal"
538- ret="$?" && [ $ret -ne 0 ] && return $ret
539- 
540- foreach_filelist "filter_by_pkg_inner_softlink" "create_pkg_inner_softlink" "$install_type" "$install_path" "mkdir" "$filelist_path" "${feature_param}" "no" "normal"
541- ret="$?" && [ $ret -ne 0 ] && return $ret
542- else
543- log "ERROR" "action wrong! action is ${action}"
544- return 1
545- fi
546- 
547- return 0
548-}
549- 
550-# 拷贝文件
551-copy_file() {
552- local install_path="$1"
553- local source="$2"
554- local target="$3"
555- local softlink="$4"
556- local pkg_inner_softlink="$5"
557- local target_abs
558- local target_dir
559- 
560- __set_abs_path "${install_path}" "${target}" "target_abs"
561- target_dir="$(dirname "${target_abs}")"
562- 
563- if [ ! -e "${source}" ]; then
564- log "ERROR" "copy file source file ${source} doesn't exist!"
565- return 1
566- fi
567- 
568- ${COPY_COMMAND} "${source}" "${target_dir}"
569- if [ $? -ne 0 ]; then
570- log "ERROR" "${source} copy failed!"
571- return 1
572- fi
573- if [ "${softlink}" != "NA" ]; then
574- create_softlink_by_install_path "${install_path}" "${target}" "${softlink}"
575- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
576- fi
577- if [ "${pkg_inner_softlink}" != "NA" ]; then
578- create_softlink_by_install_path "${install_path}" "${target}" "${pkg_inner_softlink}"
579- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
580- fi
581- return 0
582-}
583- 
584-# 移动文件
585-move_file() {
586- local install_path="$1"
587- local source="$2"
588- local target="$3"
589- local softlink="$4"
590- local pkg_inner_softlink="$5"
591- local target_abs
592- local target_dir
593- 
594- __set_abs_path "${install_path}" "${target}" "target_abs"
595- target_dir="$(dirname "${target_abs}")"
596- 
597- if [ ! -e "${source}" ] && [ ! -L "${source}" ]; then
598- log "ERROR" "move file source file ${source} doesn't exist!"
599- return 1
600- fi
601- 
602- ${MOVE_COMMAND} "${source}" "${target_dir}"
603- if [ $? -ne 0 ]; then
604- log "ERROR" "${source} move failed!"
605- return 1
606- fi
607- if [ "${softlink}" != "NA" ]; then
608- create_softlink_by_install_path "${install_path}" "${target}" "${softlink}"
609- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
610- fi
611- if [ "${pkg_inner_softlink}" != "NA" ]; then
612- create_softlink_by_install_path "${install_path}" "${target}" "${pkg_inner_softlink}"
613- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
614- fi
615- return 0
616-}
617- 
618-# 复制文件
619-copy_files() {
620- local install_path="$1"
621- local line="$2"
622- local exec_params="$3"
623- local src_dir="$4"
624- local src src_abs target target_abs softlink configurable pkg_inner_softlink tmpdir action
625- local ret
626- 
627- __index_list "${line}" 0 "src" 1 "target" 4 "softlink" 5 "configurable" 8 "pkg_inner_softlink"
628- if [ "${src_dir}" = "--move" ]; then
629- src_abs="${src}"
630- action="move_file"
631- elif [ "${src_dir}" != "" ]; then
632- __set_abs_path "${src_dir}" "${src}" "src_abs"
633- action="copy_file"
634- else
635- src_abs="${src}"
636- action="copy_file"
637- fi
638- __set_abs_path "${install_path}" "${target}" "target_abs"
639- 
640- tmpdir="$(dirname "${target_abs}")"
641- if [ ! -d "${tmpdir}" ]; then
642- mkdir -p "${tmpdir}"
643- fi
644- 
645- # 如果目标文件已经存在,而且是配置文件,则不执行覆盖操作
646- if [ -e "${target_abs}" ] && [ "${configurable}" = "TRUE" ]; then
647- return 0
648- fi
649- 
650- # 源文件不是软链,并且目标文件是软链
651- if [ ! -L "${src_abs}" ] && [ -L "${target_abs}" ] ; then
652- rm -f "${target_abs}"
653- log "WARNING" "${target_abs} is an existing softlink in copy files, deleted."
654- fi
655- 
656- exec_with_param "$exec_params" "$action" "${install_path}" "${src_abs}" "${target}" "${softlink}" "${pkg_inner_softlink}"
657- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
658- return 0
659-}
660- 
661-__set_total_uninstall_path() {
662- local install_path="$1"
663- local varname="$2"
664- 
665- eval "${varname}=\"${install_path}/cann_uninstall.sh\""
666-}
667- 
668-# 是否为hilinux环境
669-__is_hilinux() {
670- local varname="$1"
671- 
672- which lsattr > /dev/null 2>&1
673- if [ $? -ne 0 ]; then
674- eval "${varname}=\"true\""
675- else
676- eval "${varname}=\"false\""
677- fi
678-}
679- 
680-# 移除文件上的不可修改权限
681-__remove_immutable() {
682- local file="$1"
683- local is_hilinux ret
684- 
685- # 文件不存在则退出。
686- if [ ! -f "${file}" ]; then
687- return 0
688- fi
689- 
690- __is_hilinux "is_hilinux"
691- if [ "${is_hilinux}" = "true" ]; then
692- return 0
693- fi
694- 
695- attr="$(lsattr "${file}" | cut -d' ' -f1 | grep -o "i")"
696- if [ "${attr}" != "" ]; then
697- chattr -i "${file}"
698- ret="$?" && [ $ret -ne 0 ] && return $ret
699- fi
700- return 0
701-}
702- 
703-# 创建总uninstall.sh脚本
704-__create_uninstall() {
705- local file="$1"
706- 
707- if [ ! -f "${file}" ]; then
708- cat > ${file} <<EOF
709-#!/bin/sh
710- 
711-SHELL_DIR="\$(dirname "\${BASH_SOURCE:-\$0}")"
712-INSTALL_PATH="\$(cd "\${SHELL_DIR}" && pwd)"
713-TOTAL_RET="0"
714- 
715-uninstall_package() {
716- local path="\$1"
717- local cur_date ret
718- 
719- if [ ! -d "\${INSTALL_PATH}/\${path}" ]; then
720- cur_date=\$(date +"%Y-%m-%d %H:%M:%S")
721- echo "[\$cur_date] [ERROR]: \${INSTALL_PATH}/\${path}: No such file or directory"
722- TOTAL_RET="1"
723- return 1
724- fi
725- 
726- cd "\${INSTALL_PATH}/\${path}"
727- ./uninstall.sh
728- ret="\$?" && [ \${ret} -ne 0 ] && TOTAL_RET="1"
729- return \${ret}
730-}
731- 
732-if [ ! "\$*" = "" ]; then
733- cur_date=\$(date +"%Y-%m-%d %H:%M:%S")
734- echo "[\$cur_date] [ERROR]: \$*, parameter is not supported."
735- exit 1
736-fi
737- 
738-exit \${TOTAL_RET}
739-EOF
740- fi
741-}
742- 
743-# 添加uninstall.sh文件中的uninstall_package函数调用
744-__add_uninstall_package() {
745- local file="$1"
746- local script_dirpath="$2"
747- 
748- sed -i "/^exit /i uninstall_package \"${script_dirpath}\"" "${file}"
749-}
750- 
751-# 删除uninstall.sh文件中的uninstall_package函数调用
752-__remove_uninstall_package() {
753- local file="$1"
754- local script_dirpath="$2"
755- local path_regex
756- 
757- path_to_regex "path_regex" "${script_dirpath}"
758- 
759- if [ -f "${file}" ]; then
760- sed -i "/uninstall_package \"${path_regex}\"/d" "${file}"
761- if [ $? -ne 0 ]; then
762- log "ERROR" "remove ${file} uninstall_package command failed!"
763- return 1
764- fi
765- fi
766- return 0
767-}
768- 
769-# 删除uninstall.sh文件,如果已经没有uninstall_package调用
770-__remove_uninstall_file_if_no_content() {
771- local file="$1"
772- local num
773- 
774- if [ ! -f "${file}" ]; then
775- return 0
776- fi
777- 
778- # 注意uninstall_package后有一个空格,表示函数调用
779- num=$(grep "^uninstall_package " ${file} | wc -l)
780- if [ ${num} -eq 0 ]; then
781- rm -f "${file}" > /dev/null 2>&1
782- if [ $? -ne 0 ]; then
783- log "WARNING" "Delete file:${file} failed, please delete it by yourself."
784- fi
785- fi
786- return 0
787-}
788- 
789-# 向cann_uninstall.sh文件中添加uninstall_package命令
790-add_cann_uninstall_script_dir() {
791- local install_path="$1"
792- local script_dir="$2"
793- local username="$3"
794- local usergroup="$4"
795- local install_for_all="$5"
796- local oldmod="" ret
797- 
798- __set_total_uninstall_path "${install_path}" "total_uninstall_path"
799- 
800- if [ -f "${install_path}/${script_dir}/uninstall.sh" ]; then
801- if [ -f "${total_uninstall_path}" ]; then
802- get_file_mod "oldmod" "${total_uninstall_path}"
803- else
804- __create_uninstall "${total_uninstall_path}"
805- fi
806- ret="$?" && [ $ret -ne 0 ] && return $ret
807- 
808- __remove_immutable "${total_uninstall_path}"
809- ret="$?" && [ $ret -ne 0 ] && return $ret
810- 
811- change_own "${total_uninstall_path}" "${username}:${usergroup}"
812- ret="$?" && [ $ret -ne 0 ] && return $ret
813- 
814- change_mod "${total_uninstall_path}" "${SETENV_WRITEABLE_MOD}" ""
815- ret="$?" && [ $ret -ne 0 ] && return $ret
816- 
817- __add_uninstall_package "${total_uninstall_path}" "${script_dir}"
818- ret="$?" && [ $ret -ne 0 ] && return $ret
819- 
820- if [ "${oldmod}" = "" ]; then
821- change_mod "${total_uninstall_path}" "${TOTAL_UNINSTALL_MOD}" "${install_for_all}"
822- else
823- change_mod "${total_uninstall_path}" "${oldmod}" ""
824- fi
825- ret="$?" && [ $ret -ne 0 ] && return $ret
826- fi
827- 
828- return 0
829-}
830- 
831-# 向cann_uninstall.sh文件中添加子包命令
832-add_cann_uninstall_package() {
833- local install_path="$1"
834- local package="$2"
835- local username="$3"
836- local usergroup="$4"
837- local install_for_all="$5"
838- local ret package_dirpath script_dir
839- 
840- get_package_dirpath "package_dirpath" "${package}"
841- script_dir="${package_dirpath}/script"
842- 
843- # pto_kernel包存在同时安装多种芯片包的场景
844- # 确保cann_uninstall.sh脚本中只有一个pto_kernel的uninstall_package
845- del_cann_uninstall_script_dir "${install_path}" "${script_dir}"
846- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
847- 
848- add_cann_uninstall_script_dir "${install_path}" "${script_dir}" "${username}" "${usergroup}" "${install_for_all}"
849- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
850- 
851- return 0
852-}
853- 
854-# cann_uninstall.sh文件中添加命令(命令入口)
855-do_add_cann_uninstall() {
856- local install_path="$1"
857- local script_dir="$2"
858- local username="$3"
859- local usergroup="$4"
860- local install_for_all="$5"
861- local ret
862- 
863- check_param_not_empty "install_path" "need set package parameter in add cann uninstall!"
864- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
865- 
866- check_param_not_empty "script_dir" "need set script_dir parameter in add cann uninstall!"
867- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
868- 
869- check_param_not_empty "username" "need set username parameter in add cann uninstall!"
870- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
871- 
872- check_param_not_empty "usergroup" "need set usergroup parameter in add cann uninstall!"
873- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
874- 
875- # 删除cann_uninstall.sh文件中已存在的uninstall_package命令
876- del_cann_uninstall_script_dir "${install_path}" "${script_dir}"
877- add_cann_uninstall_script_dir "${install_path}" "${script_dir}" "${username}" "${usergroup}" "${install_for_all}"
878- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
879- 
880- return 0
881-}
882- 
883-# 删除cann_uninstall.sh文件中uninstall_package命令
884-del_cann_uninstall_script_dir() {
885- local install_path="$1"
886- local script_dir="$2"
887- local oldmod="" ret
888- 
889- __set_total_uninstall_path "${install_path}" "total_uninstall_path"
890- 
891- if [ -f "${total_uninstall_path}" ]; then
892- __remove_immutable "${total_uninstall_path}"
893- ret="$?" && [ $ret -ne 0 ] && return $ret
894- 
895- get_file_mod "oldmod" "${total_uninstall_path}"
896- ret="$?" && [ $ret -ne 0 ] && return $ret
897- 
898- change_mod "${total_uninstall_path}" "${SETENV_WRITEABLE_MOD}" ""
899- ret="$?" && [ $ret -ne 0 ] && return $ret
900- 
901- __remove_uninstall_package "${total_uninstall_path}" "${script_dir}"
902- ret="$?" && [ $ret -ne 0 ] && return $ret
903- 
904- change_mod "${total_uninstall_path}" "${oldmod}" ""
905- ret="$?" && [ $ret -ne 0 ] && return $ret
906- 
907- __remove_uninstall_file_if_no_content "${total_uninstall_path}"
908- ret="$?" && [ $ret -ne 0 ] && return $ret
909- fi
910- return 0
911-}
912- 
913-# 删除cann_uninstall.sh文件中子包命令
914-del_cann_uninstall_package() {
915- local install_path="$1"
916- local package="$2"
917- local ret package_dirpath script_dir
918- 
919- get_package_dirpath "package_dirpath" "${package}"
920- script_dir="${package_dirpath}/script"
921- 
922- del_cann_uninstall_script_dir "${install_path}" "${script_dir}"
923- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
924- 
925- return 0
926-}
927- 
928-# cann_uninstall.sh文件中删除命令(命令入口)
929-do_del_cann_uninstall() {
930- local install_path="$1"
931- local script_dir="$2"
932- local ret
933- 
934- check_param_not_empty "install_path" "need set package parameter in del cann uninstall!"
935- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
936- 
937- check_param_not_empty "script_dir" "need set script_dir parameter in del cann uninstall!"
938- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
939- 
940- del_cann_uninstall_script_dir "${install_path}" "${script_dir}"
941- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
942- 
943- return 0
944-}
945- 
946-# 删除ascend_install.info文件
947-del_ascend_install_info() {
948- local install_path="$1"
949- local package="$2"
950- local package_dirpath
951- 
952- rm -f "$curpath/../ascend_install.info"
953-}
954- 
955-# 执行拷贝动作
956-do_copy_files() {
957- local install_type="$1"
958- local install_path="$2"
959- local filelist_path="$3"
960- local package="$4"
961- local feature_param="$5"
962- local total_uninstall_path exec_mode ret
963- 
964- if [ "$COPY_ALL" = "y" ]; then
965- cp -af * "$install_path"
966- ret="$?" && [ $ret -ne 0 ] && return $ret
967- else
968- if [ "$PARALLEL" = "true" ]; then
969- exec_mode="concurrency"
970- else
971- exec_mode="normal"
972- fi
973- if [ "${USE_MOVE}" = "true" ]; then
974- foreach_filelist "NA" "copy_files" "$install_type" "$install_path" "move" "$filelist_path" "${feature_param}" \
975- "no" "$exec_mode" "--move"
976- foreach_filelist "NA" "copy_files" "$install_type" "$install_path" "copy copy_entity" "$filelist_path" "${feature_param}" \
977- "no" "$exec_mode"
978- else
979- foreach_filelist "NA" "copy_files" "$install_type" "$install_path" "copy copy_entity move" "$filelist_path" "${feature_param}" \
980- "no" "$exec_mode"
981- fi
982- ret="$?" && [ $ret -ne 0 ] && return $ret
983- fi
984- 
985- if [ "${package}" != "" ] && [ "${SET_CANN_UNINSTALL}" = "y" ]; then
986- add_cann_uninstall_package "${install_path}" "${package}" "${USERNAME}" "${USERGROUP}" "${INSTALL_FOR_ALL}"
987- fi
988-}
989- 
990-# 修改文件的权限和属组
991-change_mod_and_own_files() {
992- local install_path="$1"
993- local line="$2"
994- local exec_params="$3"
995- local target mod own is_abs_path
996- 
997- __index_list "${line}" 1 "target" 2 "mod" 3 "own"
998- __check_abs_path "${target}"
999- if [ "${is_abs_path}" != "true" ]; then
1000- target="${install_path}/${target}"
1001- fi
1002- if [ -d "${target}" ]; then
1003- return 0
1004- fi
1005- # 只处理文件,没有处理文件的软链接
1006- exec_with_param "$exec_params" change_mod_and_own "${target}" "${mod}" "${own}" "${INSTALL_FOR_ALL}" "false"
1007-}
1008- 
1009-# 递归修改文件的权限和属组
1010-change_mod_and_own_files_recursive() {
1011- local install_path="$1"
1012- local line="$2"
1013- local exec_params="$3"
1014- local target mod own is_abs_path
1015- 
1016- __index_list "${line}" 1 "target" 2 "mod" 3 "own"
1017- __check_abs_path "${target}"
1018- if [ "${is_abs_path}" != "true" ]; then
1019- target="${install_path}/${target}"
1020- fi
1021- if [ ! -e "${target}" ]; then
1022- return 0
1023- fi
1024- 
1025- exec_with_param "$exec_params" change_mod_and_own "${target}" "${mod}" "${own}" "${INSTALL_FOR_ALL}" "true"
1026-}
1027- 
1028-# filelist中的使用到的blocks,添加到db.info中
1029-add_filelist_blocks_info() {
1030- local install_type="$1"
1031- local install_path="$2"
1032- local filelist_path="$3"
1033- local feature_param="$4"
1034- local package="$5"
1035- local blocks=""
1036- 
1037- parse_filelist "${install_type}" "copy copy_entity del mkdir move" "${filelist_path}" "${feature_param}" "NA" ""
1038- filelist="$(echo "${filelist}" | cut -d' ' -f8 | sort | uniq)"
1039- 
1040- while read line; do
1041- block_name="${line}"
1042- if [ "${block_name}" = "" ]; then
1043- continue
1044- fi
1045- if [ "${blocks}" = "" ]; then
1046- blocks="${block_name}"
1047- else
1048- blocks="${blocks} ${block_name}"
1049- fi
1050- done << EOF
1051-${filelist}
1052-EOF
1053- add_pkg_blocks_info "${install_path}" "${package}" "${blocks}"
1054-}
1055- 
1056-# 修改文件和目录的权限
1057-do_chmod_file_dir() {
1058- local install_type="$1"
1059- local install_path="$2"
1060- local filelist_path="$3"
1061- local feature_param="$4"
1062- local package="$5"
1063- local ret
1064- 
1065- if [ "$COPY_ALL" != "y" ] || [ "$INSTALL_FOR_ALL" = "y" ]; then
1066- foreach_filelist "NA" "change_mod_and_own_files" "$install_type" "$install_path" "copy del move" "$filelist_path" "${feature_param}" "no" "concurrency"
1067- ret="$?" && [ $ret -ne 0 ] && return $ret
1068- fi
1069- 
1070- foreach_filelist "NA" "change_mod_and_own_files_recursive" "$install_type" "$install_path" "copy_entity" "$filelist_path" "${feature_param}" "no" "concurrency"
1071- ret="$?" && [ $ret -ne 0 ] && return $ret
1072- 
1073- foreach_filelist "NA" "change_mod_and_own_dirs" "$install_type" "$install_path" "mkdir" "$filelist_path" "${feature_param}" "reverse" "normal"
1074- ret="$?" && [ $ret -ne 0 ] && return $ret
1075- 
1076- if [ "${package}" != "" ]; then
1077- add_filelist_blocks_info "$install_type" "$install_path" "$filelist_path" "${feature_param}" "$package"
1078- ret="$?" && [ $ret -ne 0 ] && return $ret
1079- fi
1080- 
1081- return 0
1082-}
1083- 
1084-# 删除安装生成的pkg_inner_softlinks
1085-remove_install_pkg_inner_softlinks() {
1086- local install_path="$1"
1087- local line="$2"
1088- local target
1089- local pkg_inner_softlink
1090- local pkg_inner_softlink_list
1091- local pkg_inner_softlink_abs
1092- local ret
1093- 
1094- __index_list "${line}" 1 "target" 8 "pkg_inner_softlink"
1095- if [ "${target}" != "NA" ]; then
1096- if [ "${pkg_inner_softlink}" != "NA" ]; then
1097- remove_softlinks "${install_path}" "${pkg_inner_softlink}"
1098- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
1099- fi
1100- fi
1101- return 0
1102-}
1103- 
1104-# 删除安装文件,入参为$1: install_type, $2: install_path $3:filelist_path
1105-remove_install_files() {
1106- local install_path="$1"
1107- local line="$2"
1108- local exec_params="$3"
1109- local target softlink configurable hash_value is_abs_path
1110- 
1111- __index_list "${line}" 1 "target" 4 "softlink" 5 "configurable" 6 "hash_value"
1112- 
1113- if [ "${target}" != "NA" ]; then
1114- __check_abs_path "${target}"
1115- if [ "${is_abs_path}" != "true" ]; then
1116- target="${install_path}/${target}"
1117- fi
1118- if [ -d "${target}" ] && [ ! -L "${target}" ]; then
1119- return 0
1120- fi
1121- if [ "${softlink}" != "NA" ]; then
1122- __check_abs_path "${softlink}"
1123- if [ "${is_abs_path}" != "true" ]; then
1124- softlink="${install_path}/${softlink}"
1125- fi
1126- fi
1127- # 配置文件不删除
1128- if [ "${configurable}" = "TRUE" ]; then
1129- echo "${hash_value} ${target}" | sha256sum --check > /dev/null 2>&1
1130- if [ $? -ne 0 ]; then
1131- log "WARNING" "${target} user configuration file has been modified, skip deleting."
1132- return 0
1133- fi
1134- fi
1135- exec_with_param "$exec_params" remove_file "${target}" "${softlink}"
1136- fi
1137- return 0
1138-}
1139- 
1140-# 递归删除安装文件
1141-remove_install_files_recursive() {
1142- local install_path="$1"
1143- local line="$2"
1144- local exec_params="$3"
1145- local target configurable hash_value is_abs_path
1146- 
1147- __index_list "${line}" 1 "target" 5 "configurable" 6 "hash_value"
1148- 
1149- if [ "${target}" != "NA" ]; then
1150- __check_abs_path "${target}"
1151- if [ "${is_abs_path}" != "true" ]; then
1152- target="${install_path}/${target}"
1153- fi
1154- # 配置文件不删除
1155- if [ "${configurable}" = "TRUE" ]; then
1156- echo "${hash_value} ${target}" | sha256sum --check > /dev/null 2>&1
1157- if [ $? -ne 0 ]; then
1158- log "WARNING" "${target} user configuration file has been modified, skip deleting."
1159- return 0
1160- fi
1161- fi
1162- exec_with_param "$exec_params" remove_dir_icp "${target}"
1163- fi
1164- return 0
1165-}
1166- 
1167-# 删除安装文件夹生成的软链接
1168-remove_install_softlink() {
1169- local install_path="$1"
1170- local line="$2"
1171- local target
1172- local softlinks_str
1173- local is_abs_path
1174- 
1175- __index_list "${line}" 1 "target" 4 "softlinks_str"
1176- 
1177- if [ "$target" != "NA" ]; then
1178- if [ "${softlinks_str}" != "NA" ]; then
1179- remove_softlinks "${install_path}" "${softlinks_str}"
1180- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
1181- fi
1182- fi
1183- return 0
1184-}
1185- 
1186-# 删除安装文件与目录等
1187-do_remove() {
1188- local install_type="$1"
1189- local install_path="$2"
1190- local filelist_path="$3"
1191- local package="$4"
1192- local feature_param="$5"
1193- local tmp_filelist_path
1194- local total_uninstall_path
1195- local oldmod
1196- local func_before_remove
1197- local func_after_remove_01
1198- local func_after_remove_02
1199- local ret
1200- 
1201- get_tmp_file tmp_filelist_path "filelist"
1202- cp -f "$filelist_path" "$tmp_filelist_path"
1203- 
1204- if [ $? -ne 0 ]; then
1205- log "ERROR" "cp -f $filelist_path $tmp_filelist_path failed!"
1206- return 1
1207- fi
1208- 
1209- if [ "${package}" != "" ]; then
1210- # root帐户--uninstall时,存在不调用restoremod,直接调用remove的场景。(toolkit包)
1211- func_before_remove="del_blocks_info_pkg \"${install_path}\" \"${package}\""
1212- if [ -f "${install_path}/${STASH_MOD_PATH}" ]; then
1213- # 删除文件后,恢复目录权限配置
1214- func_after_remove_01="foreach_stashmod \"restore_stash_mod\" \"${install_path}\" \"reverse\""
1215- func_after_remove_02="remove_stash_mod \"${install_path}\""
1216- fi
1217- 
1218- del_cann_uninstall_package "${install_path}" "${package}"
1219- ret="$?" && [ $ret -ne 0 ] && return $ret
1220- 
1221- if [ "$REMOVE_INSTALL_INFO" = "y" ]; then
1222- del_ascend_install_info
1223- fi
1224- fi
1225- 
1226- eval "${func_before_remove}"
1227- ret="$?" && [ $ret -ne 0 ] && return $ret
1228- 
1229- # del元素不会出现pkg_inner_softlink
1230- foreach_filelist "filter_by_pkg_inner_softlink" "remove_install_pkg_inner_softlinks" "$install_type" "$install_path" "mkdir copy copy_entity move" "$tmp_filelist_path" "${feature_param}" "no" "normal"
1231- ret="$?" && [ $ret -ne 0 ] && return $ret
1232- 
1233- foreach_filelist "filter_by_blocks" "remove_install_softlink" "$install_type" "$install_path" "mkdir copy copy_entity move" "$tmp_filelist_path" "${feature_param}" "no" "normal"
1234- ret="$?" && [ $ret -ne 0 ] && return $ret
1235- 
1236- foreach_filelist "filter_by_blocks" "remove_install_files" "$install_type" "$install_path" "copy del move" "$tmp_filelist_path" "${feature_param}" "no" "concurrency"
1237- ret="$?" && [ $ret -ne 0 ] && return $ret
1238- 
1239- foreach_filelist "filter_by_blocks" "remove_install_files_recursive" "$install_type" "$install_path" "copy_entity" "$tmp_filelist_path" "${feature_param}" "no" "concurrency"
1240- ret="$?" && [ $ret -ne 0 ] && return $ret
1241- 
1242- foreach_filelist "filter_by_blocks" "remove_install_dirs" "$install_type" "$install_path" "mkdir" "$tmp_filelist_path" "${feature_param}" "reverse" "normal"
1243- ret="$?" && [ $ret -ne 0 ] && return $ret
1244- 
1245- eval "${func_after_remove_01}"
1246- ret="$?" && [ $ret -ne 0 ] && return $ret
1247- 
1248- eval "${func_after_remove_02}"
1249- ret="$?" && [ $ret -ne 0 ] && return $ret
1250- 
1251- rm -f "$tmp_filelist_path"
1252- ret="$?" && [ $ret -ne 0 ] && return $ret
1253- 
1254- return 0
1255-}
1256- 
1257-# 打印安装信息
1258-print_install_content() {
1259- local install_type="$1"
1260- local install_path="$2"
1261- local operate_type="$3"
1262- local filelist_path="$4"
1263- local feature_param="$5"
1264- local blocks
1265- 
1266- get_blocks_info "blocks" "$install_path"
1267- parse_filelist "$install_type" "$operate_type" "$filelist_path" "$feature_param" "filter_by_blocks" "$blocks"
1268- 
1269- # bash与dash的echo有差异,需要使用系统的/bin/echo
1270- /bin/echo "$filelist"
1271- exit 0
1272-}
1273- 
1274-# 包名是否存在于db.info
1275-pkg_in_dbinfo() {
1276- local install_path="$1"
1277- local pkg="$2"
1278- 
1279- local db_filepath="${install_path}/${PKG_DB_INFO_RELPATH}"
1280- 
1281- if [ -z "${install_path}" ]; then
1282- echo "false"
1283- exit 1
1284- fi
1285- 
1286- if [ -z "${pkg}" ]; then
1287- echo "false"
1288- exit 2
1289- fi
1290- 
1291- if [ ! -f "${db_filepath}" ]; then
1292- echo "false"
1293- return 0
1294- fi
1295- 
1296- awk -v input_pkg="${pkg}" '
1297- BEGIN {
1298- FS = "|"
1299- found = "false"
1300- }
1301- {
1302- split($2, pkg_list, ",")
1303- 
1304- for (i in pkg_list) {
1305- if (input_pkg == pkg_list[i]) {
1306- found = "true"
1307- exit
1308- }
1309- }
1310- }
1311- END {
1312- print found
1313- }' "${db_filepath}"
1314-}
1315- 
1316-# 展开自定义选项
1317-expand_custom_options() {
1318- local _outvar="$1"
1319- local _custom_options="$2"
1320- 
1321- eval "${_outvar}=\"$(echo "${_custom_options}" | tr "," " ")\""
1322-}
1323- 
1324-# 调用子包自定义脚本
1325-package_custom_script() {
1326- local script_name="$1"
1327- local install_path="$2"
1328- local version_dir="$3"
1329- local custom_options="$4"
1330- local ret install_options=""
1331- 
1332- if [ ! -f "${curpath}/${script_name}" ]; then
1333- return 0
1334- fi
1335- 
1336- install_options="--install-path=${install_path}"
1337- 
1338- if [ "${version_dir}" != "" ]; then
1339- install_options="${install_options} --version-dir=${version_dir}"
1340- fi
1341- 
1342- expand_custom_options "custom_options" "${custom_options}"
1343- 
1344- chmod u+x ${curpath}/${script_name}
1345- ${curpath}/${script_name} ${install_options} ${custom_options}
1346- ret="$?" && [ $ret -ne 0 ] && return $ret
1347- 
1348- return 0
1349-}
1350- 
1351-# 调用子包自定义安装脚本
1352-package_custom_install() {
1353- local package="$1"
1354- local install_path="$2"
1355- local version_dir="$3"
1356- local custom_options="$4"
1357- local ret
1358- 
1359- package_custom_script "${package}_custom_install.sh" "${install_path}" "${version_dir}" "${custom_options}"
1360- check_ret_error "$?" "Run ${package} custom install failed!"
1361- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
1362- 
1363- return 0
1364-}
1365- 
1366-# 调用子包自定义卸载脚本
1367-package_custom_uninstall() {
1368- local package="$1"
1369- local install_path="$2"
1370- local version_dir="$3"
1371- local custom_options="$4"
1372- local ret
1373- 
1374- package_custom_script "${package}_custom_uninstall.sh" "${install_path}" "${version_dir}" "${custom_options}"
1375- check_ret_error "$?" "Run ${package} custom uninstall failed!"
1376- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
1377- 
1378- return 0
1379-}
1380- 
1381-# 版本安装流程函数
1382-version_install() {
1383- local install_type="$1"
1384- local install_path="$2"
1385- local filelist_path="$3"
1386- local package="$4"
1387- local feature_param="$5"
1388- local version_dir="$6"
1389- local username="$7"
1390- local usergroup="$8"
1391- local setenv="$9"
1392- local is_upgrade="${10}"
1393- local docker_root="${11}"
1394- local custom_options="${12}"
1395- local is_simple="${13}"
1396- local install_path_full="" ret
1397- local package_real
1398- 
1399- if [ "${version_dir}" != "" ]; then
1400- install_path_full="${install_path}/${version_dir}"
1401- else
1402- install_path_full="${install_path}"
1403- fi
1404- 
1405- if [ "${is_simple}" = "y" ]; then
1406- package_real=""
1407- else
1408- package_real="${package}"
1409- fi
1410- 
1411- # 创建目录
1412- do_create_dirs "mkdir" "${install_type}" "${install_path_full}" "${filelist_path}" "${package_real}" "${feature_param}"
1413- if [ $? -ne 0 ]; then
1414- log "ERROR" "failed to create folder."
1415- return 1
1416- fi
1417- 
1418- # 拷贝目录与文件
1419- do_copy_files "${install_type}" "${install_path_full}" "${filelist_path}" "${package_real}" "${feature_param}"
1420- if [ $? -ne 0 ]; then
1421- log "ERROR" "failed to copy files."
1422- return 1
1423- fi
1424- 
1425- if [ "${is_simple}" != "y" ]; then
1426- # set env
1427- add_setenv "${install_path_full}" "${package_real}" "${setenv}" "${username}" "${usergroup}" "false" "${docker_root}"
1428- ret="$?" && [ $ret -ne 0 ] && return $ret
1429- 
1430- # set prereq_check
1431- add_prereq_check "${install_path_full}" "${package_real}" "${username}" "${usergroup}" "${docker_root}"
1432- ret="$?" && [ $ret -ne 0 ] && return $ret
1433- 
1434- # 调用组件自定义安装流程
1435- package_custom_install "${package_real}" "${install_path}" "${version_dir}" "${custom_options}"
1436- ret="$?" && [ $ret -ne 0 ] && return $ret
1437- fi
1438- 
1439- # 文件与目录赋权
1440- do_chmod_file_dir "${install_type}" "${install_path_full}" "${filelist_path}" "${feature_param}" "${package_real}"
1441- if [ $? -ne 0 ]; then
1442- log "ERROR" "failed to chown files."
1443- return 1
1444- fi
1445- 
1446- return 0
1447-}
1448- 
1449-# 版本卸载流程函数
1450-version_uninstall() {
1451- local install_type="$1"
1452- local install_path="$2"
1453- local filelist_path="$3"
1454- local package="$4"
1455- local feature_param="$5"
1456- local version_dir="$6"
1457- local username="$7"
1458- local docker_root="$8"
1459- local custom_options="$9"
1460- local is_simple="${10}"
1461- local install_path_full="" ret total_ret=0
1462- local package_real
1463- 
1464- if [ "${version_dir}" != "" ]; then
1465- install_path_full="${install_path}/${version_dir}"
1466- else
1467- install_path_full="${install_path}"
1468- fi
1469- 
1470- if [ "${is_simple}" = "y" ]; then
1471- package_real=""
1472- else
1473- package_real="${package}"
1474- fi
1475- 
1476- # 恢复权限
1477- do_create_dirs "resetmod" "${install_type}" "${install_path_full}" "${filelist_path}" "${package_real}" "${feature_param}"
1478- if [ $? -ne 0 ]; then
1479- log "ERROR" "failed to resetmod chmod."
1480- return 1
1481- fi
1482- 
1483- if [ "${is_simple}" != "y" ]; then
1484- # unset env
1485- del_setenv "${install_path_full}" "${package_real}" "${username}" "${docker_root}"
1486- ret="$?" && [ $ret -ne 0 ] && return $ret
1487- 
1488- # unset prereq_check
1489- del_prereq_check "${install_path_full}" "${package_real}" "${docker_root}"
1490- ret="$?" && [ $ret -ne 0 ] && return $ret
1491- 
1492- # 调用组件自定义卸载流程,失败流程不中断
1493- package_custom_uninstall "${package_real}" "${install_path}" "${version_dir}" "${custom_options}"
1494- ret="$?" && [ $ret -ne 0 ] && total_ret="$ret"
1495- fi
1496- 
1497- # 删除文件和目录
1498- do_remove "${install_type}" "${install_path_full}" "${filelist_path}" "${package_real}" "${feature_param}"
1499- if [ $? -ne 0 ]; then
1500- log "ERROR" "failed to remove files and dirs."
1501- return 1
1502- fi
1503- 
1504- return ${total_ret}
1505-}
1506- 
1507-# 版本安装函数
1508-do_install() {
1509- local install_type="$1"
1510- local install_path="$2"
1511- local filelist_path="$3"
1512- local package="$4"
1513- local feature_param="$5"
1514- local docker_root="$6"
1515- local is_simple="$7"
1516- local install_path_real ret
1517- 
1518- check_param_not_empty "package" "need set package parameter in install!"
1519- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
1520- 
1521- _package_to_log_pkg_name "LOG_PKG_NAME" "${package}"
1522- check_ret_error "$?" "Set log package name failed in install!"
1523- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
1524- 
1525- expand_version_file
1526- 
1527- # 获取filelist.csv文件真实路径
1528- get_realpath "filelist_path" "${filelist_path}"
1529- # 获取install_path真实路径
1530- get_realpath "install_path" "${install_path}"
1531- 
1532- if [ "${docker_root}" != "" ]; then
1533- if [ "${WITH_DOCKER_ROOT_PREFIX}" = "y" ]; then
1534- install_path_real="${install_path}"
1535- else
1536- install_path_real="${docker_root}${install_path}"
1537- fi
1538- else
1539- install_path_real="${install_path}"
1540- fi
1541- 
1542- if [ "${VERSION}" != "" ] && [ "${VERSION_DIR}" != "" ]; then
1543- multi_version_install "${install_type}" "${install_path_real}" "${filelist_path}" "${package}" "${feature_param}" \
1544- "${VERSION}" "${VERSION_DIR}" "${USERNAME}" "${USERGROUP}" "${SETENV}" "${IS_UPGRADE}" "${docker_root}" "${CUSTOM_OPTIONS}" \
1545- "${INSTALL_FOR_ALL}"
1546- ret="$?" && [ $ret -ne 0 ] && return $ret
1547- else
1548- version_install "${install_type}" "${install_path_real}" "${filelist_path}" "${package}" "${feature_param}" \
1549- "" "${USERNAME}" "${USERGROUP}" "${SETENV}" "${IS_UPGRADE}" "${docker_root}" "${CUSTOM_OPTIONS}" "${is_simple}"
1550- ret="$?" && [ $ret -ne 0 ] && return $ret
1551- fi
1552- return 0
1553-}
1554- 
1555-# 版本卸载函数
1556-do_uninstall() {
1557- local install_type="$1"
1558- local install_path="$2"
1559- local filelist_path="$3"
1560- local package="$4"
1561- local feature_param="$5"
1562- local docker_root="$6"
1563- local is_simple="$7"
1564- local install_path_real ret
1565- 
1566- check_param_not_empty "package" "need set package parameter in uninstall!"
1567- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
1568- 
1569- _package_to_log_pkg_name "LOG_PKG_NAME" "${package}"
1570- check_ret_error "$?" "Set log package name failed in uninstall!"
1571- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
1572- 
1573- expand_version_file
1574- 
1575- # 获取filelist.csv文件真实路径
1576- get_realpath "filelist_path" "${filelist_path}"
1577- # 获取install_path真实路径
1578- get_realpath "install_path" "${install_path}"
1579- 
1580- if [ "${docker_root}" != "" ]; then
1581- if [ "${WITH_DOCKER_ROOT_PREFIX}" = "y" ]; then
1582- install_path_real="${install_path}"
1583- else
1584- install_path_real="${docker_root}${install_path}"
1585- fi
1586- else
1587- install_path_real="${install_path}"
1588- fi
1589- 
1590- if [ "${VERSION}" != "" ] && [ "${VERSION_DIR}" != "" ]; then
1591- multi_version_uninstall "${install_type}" "${install_path_real}" "${filelist_path}" "${package}" "${feature_param}" \
1592- "${VERSION}" "${VERSION_DIR}" "${USERNAME}" "${USERGROUP}" "${docker_root}" "${CUSTOM_OPTIONS}" "${IS_RECREATE_SOFTLINK}"
1593- ret="$?" && [ $ret -ne 0 ] && return $ret
1594- else
1595- version_uninstall "${install_type}" "${install_path_real}" "${filelist_path}" "${package}" "${feature_param}" \
1596- "" "${USERNAME}" "${docker_root}" "${CUSTOM_OPTIONS}" "${is_simple}"
1597- ret="$?" && [ $ret -ne 0 ] && return $ret
1598- fi
1599- return 0
1600-}
1601- 
1602-help_info() {
1603- echo "Usage:"
1604- echo ""
1605- echo " ----------------------------------------------------------------------------------------------------"
1606- echo ""
1607- echo " $0 {--install,--uninstall,--mkdir,--makedir,--copy,--chmoddir,--restoremod,--remove}"
1608- echo ""
1609- echo " --install : Install package."
1610- echo " --uninstall : Uninstall package."
1611- echo " --mkdir : (Deprecated) Use --install instead. Create the install directories."
1612- echo " --makedir : (Deprecated) Use --install instead. Create the install directories."
1613- echo " --copy : (Deprecated) Use --install instead. Copy install files."
1614- echo " --chmoddir : (Deprecated) Use --install instead. Set installed file's right."
1615- echo " --restoremod : (Deprecated) Use --uninstall instead. Restore directories right."
1616- echo " --remove : (Deprecated) Use --uninstall instead. Remove installed files."
1617- echo ""
1618- echo " Group options:"
1619- echo " --install-path=<install-path> : Specify install path."
1620- echo " --username=<username> : Specify install username."
1621- echo " --usergroup=<usergroup> : Specify install usergroup."
1622- echo " --install_for_all : Install for all users."
1623- echo " Usually specified in root user install scene."
1624- echo " --docker-root=<docker-root> : Specify docker root path."
1625- echo " Install path is not contained docker root path."
1626- echo " --chip=<chip> : Specify chip."
1627- echo " Install files which chip matched or chip is all."
1628- echo " --feature=<feature> : Specify feature. Default is all."
1629- echo " Install files which feature matched or feature is comm."
1630- echo " --feature-exclude-all : Switch feature mode."
1631- echo " Install files which feature matched."
1632- echo ""
1633- echo " $0 --install [options] <install_type> <install_path> <filelist_path>"
1634- echo ""
1635- echo " Command options:"
1636- echo " --package=<package> : Specify package name."
1637- echo " --version=<version> : Specify version. --version-file is recommended."
1638- echo " --version-dir=<version-dir> : Specify version directory. --version-file is recommended."
1639- echo " --version-file=<version-file> : Specify version info file path."
1640- echo " Common script will parse version and version_dir from version_file."
1641- echo " If you specified this option, --version and --version-dir options"
1642- echo " no longer need to be specified."
1643- echo " --custom-options=<args> : Specify custom options for package custom script."
1644- echo " --setenv : Add \"source setenv.\${shell_type}\" to rcfile."
1645- echo " --set-cann-uninstall : Add uninstall command into cann_uninstall.sh."
1646- echo " Always specified this in multi-version install."
1647- echo ""
1648- echo " $0 --uninstall [options] <install_type> <install_path> <filelist_path>"
1649- echo ""
1650- echo " Command options:"
1651- echo " --package=<package> : Specify package name."
1652- echo " --version=<version> : Specify version. --version-file is recommended."
1653- echo " --version-dir=<version-dir> : Specify version directory. --version-file is recommended."
1654- echo " --version-file=<version-file> : Specify version info file path."
1655- echo " Common script will parse version and version_dir from version_file."
1656- echo " If you specified this option, --version and --version-dir options"
1657- echo " no longer need to be specified."
1658- echo " --custom-options=<args> : Specify custom options for package custom script."
1659- echo ""
1660- echo " ----------------------------------------------------------------------------------------------------"
1661- echo ""
1662- echo " $0 {--add-cann-uninstall,--del-cann-uninstall}"
1663- echo ""
1664- echo " --add-cann-uninstall : Add uninstall command in cann_uninstall.sh."
1665- echo " --del-cann-uninstall : Del uninstall command in cann_uninstall.sh."
1666- echo ""
1667- echo " Group options:"
1668- echo " --install-path=<install-path> : Specify install path."
1669- echo ""
1670- echo " $0 --add-cann-uninstall [options] <script_dir>"
1671- echo ""
1672- echo " Command options:"
1673- echo " --username=<username> : Specify install username."
1674- echo " --usergroup=<usergroup> : Specify install usergroup."
1675- echo " --install_for_all : Install for all users."
1676- echo " Usually specified in root user install scene."
1677- echo ""
1678- echo " $0 --del-cann-uninstall [options] <script_dir>"
1679- echo ""
1680- echo " ----------------------------------------------------------------------------------------------------"
1681- echo ""
1682- echo " $0 {--add-env-rc,--del-env-rc}"
1683- echo ""
1684- echo " --add-env-rc : Add \"source setenv.\${shell_type}\" to rcfile."
1685- echo " --del-env-rc : Del \"source setenv.\${shell_type}\" in rcfile."
1686- echo ""
1687- echo " Group options:"
1688- echo " --username=<username> : Specify install username."
1689- echo " --usergroup=<usergroup> : Specify install usergroup."
1690- echo " --docker-root=<docker-root> : Specify docker root path."
1691- echo " Install path is not contained docker root path."
1692- echo ""
1693- echo " $0 --add-env-rc [options] <install_path> <setenv_filepath> <shell_type>"
1694- echo ""
1695- echo " Command options:"
1696- echo " --package=<package> : Specify package name."
1697- echo " --setenv : Add \"source setenv.\${shell_type}\" to rcfile."
1698- echo ""
1699- echo " $0 --del-env-rc [options] <install_path> <setenv_filepath> <shell_type>"
1700- echo ""
1701- echo " ----------------------------------------------------------------------------------------------------"
1702- echo ""
1703- echo " $0 --pkg-in-dbinfo <install_path>"
1704- echo ""
1705- echo " Does package in ascend_package_db.info. Echo true or false."
1706- echo ""
1707- echo " Command options:"
1708- echo " --package=<package> : Specify package name."
1709- echo ""
1710- echo " ----------------------------------------------------------------------------------------------------"
1711- echo ""
1712- echo " $0 --help"
1713- echo ""
1714- echo " Print help messages."
1715-}
1716- 
1717-# env rc相关命令
1718-env_rc_commands() {
1719- local install_path
1720- local setenv_filepath
1721- local shell_type
1722- 
1723- case "${OPERATE_TYPE}" in
1724- "add-env-rc")
1725- install_path="$1"
1726- setenv_filepath="$2"
1727- shell_type="$3"
1728- add_env_rc "${install_path}" "${setenv_filepath}" "${PACKAGE}" "${shell_type}" "${SETENV}" "${USERNAME}" "${USERGROUP}" "false" "${DOCKER_ROOT}"
1729- exit 0
1730- ;;
1731- "del-env-rc")
1732- install_path="$1"
1733- setenv_filepath="$2"
1734- shell_type="$3"
1735- del_env_rc "${install_path}" "${setenv_filepath}" "${shell_type}" "${USERNAME}" "${DOCKER_ROOT}"
1736- exit 0
1737- ;;
1738- esac
1739-}
1740- 
1741-# db.info相关命令
1742-dbinfo_commands() {
1743- local install_path
1744- 
1745- case "${OPERATE_TYPE}" in
1746- "pkg-in-dbinfo")
1747- install_path="$1"
1748- pkg_in_dbinfo "${install_path}" "${PACKAGE}"
1749- exit 0
1750- esac
1751-}
1752- 
1753-# spc相关命令
1754-spc_commands() {
1755- local install_path="$1"
1756- local filelist_path="$2"
1757- local filelist_spc_path="$3"
1758- local version_dir="$4"
1759- local ret
1760- 
1761- case "${OPERATE_TYPE}" in
1762- "spc_install")
1763- install_patch "${install_path}" "${filelist_path}" "${filelist_spc_path}" "${version_dir}"
1764- exit $?
1765- ;;
1766- "spc_rollback")
1767- rollback_patch "${install_path}" "${filelist_path}" "${version_dir}"
1768- exit 0
1769- ;;
1770- "spc_uninstall")
1771- uninstall_patch "${install_path}" "${filelist_path}" "${version_dir}"
1772- exit 0
1773- ;;
1774- esac
1775-}
1776- 
1777-# cann_uninstall总卸载脚本相关命令
1778-cann_uninstall_commands() {
1779- local script_dir="$1"
1780- local ret
1781- 
1782- case "${OPERATE_TYPE}" in
1783- "add-cann-uninstall")
1784- do_add_cann_uninstall "${INSTALL_PATH}" "${script_dir}" "${USERNAME}" "${USERGROUP}" "${INSTALL_FOR_ALL}"
1785- ret="$?" && [ ${ret} -ne 0 ] && exit 1
1786- exit 0
1787- ;;
1788- "del-cann-uninstall")
1789- do_del_cann_uninstall "${INSTALL_PATH}" "${script_dir}"
1790- ret="$?" && [ ${ret} -ne 0 ] && exit 1
1791- exit 0
1792- ;;
1793- esac
1794-}
1795- 
1796-# 多版本创建相关命令
1797-multi_version_commands() {
1798- case "${OPERATE_TYPE}" in
1799- "notify_create_softlink")
1800- notify_latest_manager_create_softlink "$INSTALL_PATH/$LATEST_DIR/var" "$PACKAGE" "$VERSION" "$VERSION_DIR" \
1801- "$INSTALL_FOR_ALL" "$DOCKER_ROOT"
1802- ret="$?" && [ $ret -ne 0 ] && exit 1
1803- exit 0
1804- ;;
1805- "notify_remove_softlink")
1806- notify_latest_manager_remove_softlink "$INSTALL_PATH/$LATEST_DIR/var" "$PACKAGE" "$VERSION" "$VERSION_DIR" \
1807- "$INSTALL_FOR_ALL" "$DOCKER_ROOT"
1808- ret="$?" && [ $ret -ne 0 ] && exit 1
1809- exit 0
1810- ;;
1811- esac
1812-}
1813- 
1814-# 正式命令
1815-formal_commands() {
1816- local install_type="$1"
1817- local install_path="$2"
1818- local filelist_path="$3"
1819- local input_feature input_chip
1820- local ret docker_root
1821- local feature_param thread_num
1822- 
1823- if [ "$FEATURE" != "all" ]; then
1824- normalize_feature "input_feature" "$FEATURE" "${OPERATE_TYPE}"
1825- else
1826- if [ $# = 3 ]; then
1827- input_feature="all"
1828- else
1829- normalize_feature "input_feature" "$4" "${OPERATE_TYPE}"
1830- fi
1831- fi
1832- normalize_feature "input_chip" "$CHIP" "${OPERATE_TYPE}"
1833- 
1834- pack_feature_param "feature_param" "${input_feature}" "${FEATURE_EXCLUDE_ALL}" "${input_chip}"
1835- 
1836- # 移除docker_root右侧的/
1837- rstrip_path "docker_root" "${DOCKER_ROOT}"
1838- 
1839- if [ "$PARALLEL_LIMIT" = "true" ]; then
1840- get_thread_num "thread_num"
1841- init_fifo "PARALLEL_FIFO" "$thread_num"
1842- fi
1843- 
1844- case "${OPERATE_TYPE}" in
1845- "install")
1846- do_install "${install_type}" "${install_path}" "${filelist_path}" "${PACKAGE}" "${feature_param}" "${docker_root}" "n"
1847- ret="$?" && [ $ret -ne 0 ] && return $ret
1848- ;;
1849- "uninstall")
1850- # 统一使用full模式卸载文件。两个包共用部分block(如opp和opp_kernel),以不同的模式安装时(如:run/full)
1851- # full模式的包先卸载,run模式的包后卸载,确保能够完整卸载。
1852- do_uninstall "full" "${install_path}" "${filelist_path}" "${PACKAGE}" "${feature_param}" "${docker_root}"
1853- ret="$?" && [ $ret -ne 0 ] && return $ret
1854- ;;
1855- "simple_install")
1856- # 简化安装模式
1857- do_install "${install_type}" "${install_path}" "${filelist_path}" "${PACKAGE}" "${feature_param}" "" "y"
1858- ret="$?" && [ $ret -ne 0 ] && return $ret
1859- ;;
1860- "simple_uninstall")
1861- # 简化卸载模式
1862- do_uninstall "full" "${install_path}" "${filelist_path}" "${PACKAGE}" "${feature_param}" "" "y"
1863- ret="$?" && [ $ret -ne 0 ] && return $ret
1864- ;;
1865- "mkdir")
1866- do_create_dirs "all" "$1" "$2" "$3" "${PACKAGE}" "${feature_param}"
1867- ret="$?" && [ $ret -ne 0 ] && return $ret
1868- ;;
1869- "makedir")
1870- do_create_dirs "mkdir" "$1" "$2" "$3" "${PACKAGE}" "${feature_param}"
1871- ret="$?" && [ $ret -ne 0 ] && return $ret
1872- ;;
1873- "copy")
1874- do_copy_files "$1" "$2" "$3" "${PACKAGE}" "${feature_param}"
1875- ret="$?" && [ $ret -ne 0 ] && return $ret
1876- ;;
1877- "chmoddir")
1878- do_chmod_file_dir "$install_type" "$2" "$3" "${feature_param}" "${PACKAGE}"
1879- ret="$?" && [ $ret -ne 0 ] && return $ret
1880- ;;
1881- "restoremod")
1882- do_create_dirs "resetmod" "$1" "$2" "$3" "${PACKAGE}" "${feature_param}"
1883- ret="$?" && [ $ret -ne 0 ] && return $ret
1884- ;;
1885- "remove")
1886- do_remove "$install_type" "$2" "$filelist_path" "${PACKAGE}" "${feature_param}"
1887- ret="$?" && [ $ret -ne 0 ] && return $ret
1888- ;;
1889- "print")
1890- print_install_content "$1" "$2" "${PRINT_OPERATE_TYPE}" "$3" "${feature_param}"
1891- ret="$?" && [ $ret -ne 0 ] && return $ret
1892- ;;
1893- esac
1894- 
1895- return 0
1896-}
1897- 
1898-# 正式命令并加锁
1899-formal_commands_with_lock() {
1900- local install_path="$2"
1901- local lockfile docker_root install_path_real ret
1902- 
1903- if [ "${OPERATE_TYPE}" = "install" ] || [ "${OPERATE_TYPE}" = "uninstall" ]; then
1904- if [ "${WITH_DOCKER_ROOT_PREFIX}" = "y" ]; then
1905- install_path_real="${install_path}"
1906- else
1907- # 移除docker_root右侧的/
1908- rstrip_path "docker_root" "${DOCKER_ROOT}"
1909- install_path_real="${docker_root}${install_path}"
1910- fi
1911- else
1912- install_path_real="${install_path}"
1913- fi
1914- lockfile="${install_path_real}/ascend.lock"
1915- 
1916- if [ ! -d "${install_path_real}" ]; then
1917- # 兼容覆盖安装场景,包安装目录被删除情况
1918- mkdir -p "${install_path_real}"
1919- # 安装目录设置合适的权限
1920- change_mod "${install_path_real}" "750" "${INSTALL_FOR_ALL}"
1921- check_ret_error "$?" "Change mod ${install_path_real} failed!"
1922- ret="$?" && [ $ret -ne 0 ] && return $ret
1923- fi
1924- 
1925- # 使用文件描述符9作为并发锁
1926- (
1927- flock -n 9
1928- if [ $? -ne 0 ]; then
1929- log "ERROR" "Get ${install_path_real} lockfile failed! There may be another process also installing in the directory."
1930- exit 1
1931- fi
1932- formal_commands "$@"
1933- ) 9> "${lockfile}"
1934- ret="$?"
1935- rm -f "${lockfile}"
1936- 
1937- if [ $ret -ne 0 ]; then
1938- exit 1
1939- fi
1940-}
1941- 
1942-# 第一个参数为source,标记为source场景,不执行主流程
1943-[ "$1" = "source" ] && return 0
1944- 
1945-# 全局变量
1946-# change_mod_and_own_files与change_mod_and_own_dirs函数中会使用
1947-INSTALL_FOR_ALL=""
1948-SETENV=""
1949-SET_CANN_UNINSTALL=""
1950-IS_UPGRADE=""
1951-IS_RECREATE_SOFTLINK=""
1952-WITH_DOCKER_ROOT_PREFIX=""
1953-FEATURE_EXCLUDE_ALL="n"
1954-REMOVE_INSTALL_INFO="n" # 卸载时移除ascend_install.info文件
1955-USE_SHARE_INFO="n"
1956-CHIP="all"
1957-FEATURE="all"
1958-INCREMENT="n" # 增量安装
1959- 
1960-OPERATE_TYPE=""
1961-PACKAGE=""
1962-USERNAME=""
1963-USERGROUP=""
1964-VERSION=""
1965-VERSION_DIR=""
1966-VERSION_FILE=""
1967-CUSTOM_OPTIONS=""
1968-# 宿主机打docker包场景,配置docker文件系统根路径
1969-DOCKER_ROOT=""
1970-# 安装路径
1971-INSTALL_PATH=""
1972-# 输入的latest_dir
1973-INPUT_LATEST_DIR=""
1974-COPY_ALL=""
1975- 
1976-while true; do
1977- case "$1" in
1978- --copy_all)
1979- COPY_ALL="y"
1980- shift
1981- ;;
1982- --spc-install | -i)
1983- OPERATE_TYPE="spc_install"
1984- shift
1985- ;;
1986- --spc-rollback | -l)
1987- OPERATE_TYPE="spc_rollback"
1988- shift
1989- ;;
1990- --spc-uninstall | -u)
1991- OPERATE_TYPE="spc_uninstall"
1992- shift
1993- ;;
1994- --install)
1995- OPERATE_TYPE="install"
1996- shift
1997- ;;
1998- --uninstall)
1999- OPERATE_TYPE="uninstall"
2000- shift
2001- ;;
2002- --simple-install)
2003- OPERATE_TYPE="simple_install"
2004- shift
2005- ;;
2006- --simple-uninstall)
2007- OPERATE_TYPE="simple_uninstall"
2008- shift
2009- ;;
2010- --copy | -c)
2011- OPERATE_TYPE="copy"
2012- shift
2013- ;;
2014- --mkdir | -m)
2015- OPERATE_TYPE="mkdir"
2016- shift
2017- ;;
2018- --makedir | -d)
2019- OPERATE_TYPE="makedir"
2020- shift
2021- ;;
2022- --chmoddir | -o)
2023- OPERATE_TYPE="chmoddir"
2024- shift
2025- ;;
2026- --restoremod | -e)
2027- OPERATE_TYPE="restoremod"
2028- shift
2029- ;;
2030- --remove | -r)
2031- OPERATE_TYPE="remove"
2032- shift
2033- ;;
2034- --add-cann-uninstall)
2035- OPERATE_TYPE="add-cann-uninstall"
2036- shift
2037- ;;
2038- --del-cann-uninstall)
2039- OPERATE_TYPE="del-cann-uninstall"
2040- shift
2041- ;;
2042- --add-env-rc)
2043- OPERATE_TYPE="add-env-rc"
2044- shift
2045- ;;
2046- --del-env-rc)
2047- OPERATE_TYPE="del-env-rc"
2048- shift
2049- ;;
2050- --pkg-in-dbinfo)
2051- OPERATE_TYPE="pkg-in-dbinfo"
2052- shift
2053- ;;
2054- --add-pkg-blocks)
2055- OPERATE_TYPE="add-pkg-blocks"
2056- shift
2057- ;;
2058- --del-pkg-blocks)
2059- OPERATE_TYPE="del-pkg-blocks"
2060- shift
2061- ;;
2062- --create-latest-softlink)
2063- OPERATE_TYPE="notify_create_softlink"
2064- shift
2065- ;;
2066- --create-package-latest-softlink)
2067- OPERATE_TYPE="notify_create_softlink"
2068- shift
2069- ;;
2070- --remove-latest-softlink)
2071- OPERATE_TYPE="notify_remove_softlink"
2072- shift
2073- ;;
2074- --remove-package-latest-softlink)
2075- OPERATE_TYPE="notify_remove_softlink"
2076- shift
2077- ;;
2078- --print-blocks)
2079- OPERATE_TYPE="print-blocks"
2080- shift
2081- ;;
2082- --print=*)
2083- OPERATE_TYPE="print"
2084- PRINT_OPERATE_TYPE="$(echo "$1" | cut -d"=" -f2)"
2085- shift
2086- ;;
2087- --print| -p)
2088- OPERATE_TYPE="print"
2089- PRINT_OPERATE_TYPE="copy mkdir move"
2090- shift
2091- ;;
2092- --install-path=*)
2093- INSTALL_PATH=$(echo "$1" | cut -d"=" -f2)
2094- shift
2095- ;;
2096- --version=*)
2097- VERSION=$(echo "$1" | cut -d"=" -f2)
2098- shift
2099- ;;
2100- --version-dir=*)
2101- VERSION_DIR=$(echo "$1" | cut -d"=" -f2)
2102- shift
2103- ;;
2104- --version-file=*)
2105- VERSION_FILE=$(echo "$1" | cut -d"=" -f2)
2106- shift
2107- ;;
2108- --latest-dir=*)
2109- INPUT_LATEST_DIR=$(echo "$1" | cut -d"=" -f2)
2110- shift
2111- ;;
2112- --username=*)
2113- USERNAME=$(echo "$1" | cut -d"=" -f2)
2114- shift
2115- ;;
2116- --usergroup=*)
2117- USERGROUP=$(echo "$1" | cut -d"=" -f2)
2118- shift
2119- ;;
2120- --custom-options=*)
2121- CUSTOM_OPTIONS=$(echo "$1" | cut -d"=" -f2-)
2122- shift
2123- ;;
2124- --arch=*)
2125- PKG_ARCH=$(echo "$1" | cut -d"=" -f2-)
2126- shift
2127- if [ "$PKG_ARCH" = "" ]; then
2128- log "ERROR" "The --arch option should not be an empty string."
2129- exit 1
2130- fi
2131- ;;
2132- --upgrade)
2133- IS_UPGRADE="y"
2134- shift
2135- ;;
2136- --recreate-softlink)
2137- IS_RECREATE_SOFTLINK="y"
2138- shift
2139- ;;
2140- --install_for_all)
2141- INSTALL_FOR_ALL="y"
2142- shift
2143- ;;
2144- --setenv)
2145- SETENV="y"
2146- shift
2147- ;;
2148- --set-cann-uninstall)
2149- SET_CANN_UNINSTALL="y"
2150- shift
2151- ;;
2152- --package=*)
2153- PACKAGE=$(echo "$1" | cut -d"=" -f2)
2154- shift
2155- ;;
2156- --docker-root=*)
2157- DOCKER_ROOT=$(echo "$1" | cut -d"=" -f2)
2158- shift
2159- ;;
2160- --chip=*)
2161- CHIP="$(echo "$1" | cut -d"=" -f2)"
2162- shift
2163- ;;
2164- --feature=*)
2165- FEATURE="$(echo "$1" | cut -d"=" -f2)"
2166- shift
2167- ;;
2168- --with-docker-root-prefix)
2169- WITH_DOCKER_ROOT_PREFIX="y"
2170- shift
2171- ;;
2172- --feature-exclude-all)
2173- FEATURE_EXCLUDE_ALL="y"
2174- shift
2175- ;;
2176- --remove-install-info)
2177- REMOVE_INSTALL_INFO="y"
2178- shift
2179- ;;
2180- --use-share-info)
2181- USE_SHARE_INFO="y"
2182- shift
2183- ;;
2184- --increment)
2185- INCREMENT="y"
2186- shift
2187- ;;
2188- -h | --help)
2189- help_info
2190- exit 0
2191- ;;
2192- -*)
2193- echo Unrecognized input options : "$1"
2194- help_info
2195- exit 1
2196- ;;
2197- *)
2198- break
2199- ;;
2200- esac
2201-done
2202- 
2203-set_global_vars
2204-env_rc_commands "$@"
2205-dbinfo_commands "$@"
2206-spc_commands "$@"
2207-cann_uninstall_commands "$@"
2208-multi_version_commands "$@"
2209- 
2210-if [ $# -lt 3 ]; then
2211- log "ERROR" "It's too few input params: $*"
2212- exit 1
2213-fi
2214- 
2215-formal_commands_with_lock "$@"
2216-exit $?
Dscripts/package/common/sh/multi_version.inc+0-305
@@ -1,305 +0,0 @@
1-#!/bin/sh
2-# --------------------------------------------------------------------------------
3-# Copyright (c) 2025 Huawei Technologies Co., Ltd.
4-# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
5-# CANN Open Software License Agreement Version 2.0 (the "License").
6-# Please refer to the License for details. You may not use this file except in compliance with the License.
7-# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
8-# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
9-# See LICENSE in the root of the software repository for the full text of the License.
10-# --------------------------------------------------------------------------------
11- 
12-# 多版本函数库
13-# 创建版本目录
14-create_version_dir() {
15- local install_path="$1"
16- local version_dir="$2"
17- local username="$3"
18- local usergroup="$4"
19- local install_for_all="$5"
20- local ret
21- 
22- if [ ! -d "${install_path}/${version_dir}" ]; then
23- make_dir "${install_path}/${version_dir}"
24- ret="$?" && [ $ret -ne 0 ] && return $ret
25- fi
26- 
27- # 约束版本号目录权限
28- change_mod "${install_path}/${version_dir}" "750" "${install_for_all}"
29- ret="$?" && [ $ret -ne 0 ] && return $ret
30- 
31- change_own "${install_path}/${version_dir}" "${username}:${usergroup}"
32- ret="$?" && [ $ret -ne 0 ] && return $ret
33- 
34- return 0
35-}
36- 
37-# 检查版本包安装结果
38-check_version_install() {
39- local install_path="$1"
40- local version_dir="$2"
41- local package="$3"
42- local version_info_path
43- 
44- get_package_version_info "version_info_path" "$install_path" "$version_dir" "$package"
45- 
46- if [ ! -f "$version_info_path" ]; then
47- comm_log "ERROR" "$version_info_path doesn't exist in check version install!"
48- return 1
49- fi
50- return 0
51-}
52- 
53-# 安装latest管理器
54-install_latest_manager() {
55- local var_path="$1"
56- shift 1
57- sh latest_manager/install.sh --install-path="$var_path" "$@"
58-}
59- 
60-# 卸载latest管理器
61-uninstall_latest_manager_for_upgrade() {
62- local var_path="$1"
63- ${var_path}/manager/uninstall.sh --upgrade
64-}
65- 
66-# 升级latest管理器
67-upgrade_latest_manager() {
68- local var_path="$1"
69- local latest_version_info latest_manager_version local_manager_version
70- 
71- latest_version_info="$var_path/manager/version.info"
72- if [ -f "$latest_version_info" ]; then
73- get_version latest_manager_version "$latest_version_info"
74- get_version local_manager_version "latest_manager/version.info"
75- 
76- set_default "latest_manager_version" "$latest_manager_version" "0"
77- set_default "local_manager_version" "$local_manager_version" "0"
78- 
79- if [ "$latest_manager_version" -lt "$local_manager_version" ]; then
80- uninstall_latest_manager_for_upgrade "$var_path"
81- install_latest_manager "$var_path" "--upgrade"
82- fi
83- else
84- install_latest_manager "$var_path"
85- fi
86-}
87- 
88-get_install_for_all_param() {
89- local _outvar="$1"
90- local _install_for_all="$2"
91- local _result=""
92- 
93- if [ "$_install_for_all" = "y" ]; then
94- _result="--install-for-all"
95- fi
96- 
97- eval "${_outvar}=\"${_result}\""
98-}
99- 
100-get_docker_root_param() {
101- local _outvar="$1"
102- local _docker_root="$2"
103- local _result=""
104- 
105- if [ "$_docker_root" != "" ]; then
106- _result="--docker-root \\\"$_docker_root\\\""
107- fi
108- 
109- eval "${_outvar}=\"${_result}\""
110-}
111- 
112-# 通知latest管理器
113-notify_latest_manager() {
114- local var_path="$1"
115- local package="$2"
116- local version="$3"
117- local version_dir="$4"
118- local install_for_all="$5"
119- local docker_root="$6"
120- local ext_params="$7"
121- local operation="$8"
122- local ret package_dir install_for_all_param docker_root_param
123- 
124- if [ ! -f "$var_path/manager.sh" ]; then
125- return 1
126- fi
127- 
128- if [ "$USE_SHARE_INFO" = "y" ]; then
129- ext_params="$ext_params --use-share-info"
130- fi
131- 
132- get_package_dir package_dir "$package"
133- get_install_for_all_param "install_for_all_param" "$install_for_all"
134- get_docker_root_param "docker_root_param" "$docker_root"
135- 
136- eval "\"$var_path/manager.sh\"" --version "\"$version\"" --version-dir "\"$version_dir\"" \
137- --package "\"$package\"" --package-dir "\"$package_dir\"" \
138- "$install_for_all_param" "$docker_root_param" "$ext_params" "$operation"
139- ret="$?" && [ $ret -ne 0 ] && return $ret
140- 
141- return 0
142-}
143- 
144-# 通知latest管理器安装完成
145-notify_latest_manager_installed() {
146- local local_manager_version
147- local ext_params
148- 
149- get_version local_manager_version "latest_manager/version.info"
150- set_default "local_manager_version" "$local_manager_version" "0"
151- 
152- if [ "$INCREMENT" = "y" ]; then
153- ext_params="--serial $local_manager_version --increment"
154- else
155- ext_params="--serial $local_manager_version"
156- fi
157- 
158- notify_latest_manager "$@" "$ext_params" "package_installed"
159-}
160- 
161-# 通知latest管理器创建软链
162-# 老版本全部卸载后,latest回滚到新版本时,会用老版本的install_common_parser.sh,
163-# 调用新版本的--create-package-latest-softlink,会走到这个流程。
164-notify_latest_manager_create_softlink() {
165- notify_latest_manager "$@" "" "package_create_softlink"
166-}
167- 
168-# 通知latest管理器删除软链
169-notify_latest_manager_remove_softlink() {
170- notify_latest_manager "$@" "" "package_remove_softlink"
171-}
172- 
173-# 通知latest管理器准备卸载
174-notify_latest_manager_pre_uninstall() {
175- notify_latest_manager "$@" "" "package_pre_uninstall"
176-}
177- 
178-# 通知latest管理器卸载完成
179-notify_latest_manager_uninstalled() {
180- local is_recreate_softlink="$1"
181- local recreate_softlink=""
182- local ret
183- shift 1
184- 
185- if [ "$is_recreate_softlink" = "y" ]; then
186- recreate_softlink="--recreate-softlink"
187- fi
188- 
189- notify_latest_manager "$@" "$recreate_softlink" "package_uninstalled"
190- ret="$?" && [ $ret -ne 0 ] && return $ret
191- 
192- return 0
193-}
194- 
195-# 多版本安装流程
196-multi_version_install() {
197- local install_type="$1"
198- local install_path="$2"
199- local filelist_path="$3"
200- local package="$4"
201- local feature_param="$5"
202- local version="$6"
203- local version_dir="$7"
204- local username="$8"
205- local usergroup="$9"
206- local setenv="${10}"
207- local is_upgrade="${11}"
208- local docker_root="${12}"
209- local custom_options="${13}"
210- local install_for_all="${14}"
211- local ret total_ret="0" pkg_running_version version_pair_arr last_version last_version_dir
212- 
213- create_version_dir "${install_path}" "${version_dir}" "${username}" "${usergroup}" "${install_for_all}"
214- ret="$?" && [ $ret -ne 0 ] && return $ret
215- 
216- version_install "${install_type}" "${install_path}" "${filelist_path}" "${package}" "${feature_param}" \
217- "${version_dir}" "${username}" "${usergroup}" "${setenv}" "${is_upgrade}" "${docker_root}" "${custom_options}"
218- ret="$?" && [ $ret -ne 0 ] && return $ret
219- 
220- check_version_install "$install_path" "$version_dir" "$package"
221- ret="$?" && [ $ret -ne 0 ] && return $ret
222- 
223- return ${total_ret}
224-}
225- 
226-# 删除latest下空目录
227-del_empty_dirs_in_latest() {
228- local install_type="$1"
229- local install_path="$2"
230- local latest_dir="$3"
231- local filelist_path="$4"
232- local feature_param="$5"
233- local ret
234- 
235- create_stash_mod "${install_path}/${latest_dir}"
236- ret="$?" && [ $ret -ne 0 ] && return $ret
237- 
238- foreach_filelist "filter_common_dirs" "reset_mod_dirs_with_stash_mod" "${install_type}" "${install_path}/${latest_dir}" "mkdir" \
239- "${filelist_path}" "${feature_param}" "no" "normal"
240- ret="$?" && [ $ret -ne 0 ] && return $ret
241- 
242- foreach_filelist "filter_common_dirs" "remove_install_dirs" "${install_type}" "${install_path}/${latest_dir}" "mkdir" \
243- "${filelist_path}" "${feature_param}" "reverse" "normal"
244- ret="$?" && [ $ret -ne 0 ] && return $ret
245- 
246- foreach_stashmod "restore_stash_mod" "${install_path}/${latest_dir}" "reverse"
247- ret="$?" && [ $ret -ne 0 ] && return $ret
248- 
249- remove_stash_mod "${install_path}/${latest_dir}"
250- ret="$?" && [ $ret -ne 0 ] && return $ret
251- 
252- return 0
253-}
254- 
255-# 多版本卸载流程
256-multi_version_uninstall() {
257- local install_type="$1"
258- local install_path="$2"
259- local filelist_path="$3"
260- local package="$4"
261- local feature_param="$5"
262- local version="$6"
263- local version_dir="$7"
264- local username="$8"
265- local usergroup="$9"
266- local docker_root="${10}"
267- local custom_options="${11}"
268- local is_recreate_softlink="${12}"
269- local tmp_root tmp_filelist_path
270- local ret total_ret=0 install_path_full="" is_running="" is_upgrade
271- local running_packages is_final_running="false"
272- 
273- check_param_not_empty "usergroup" "need set usergroup parameter in multi version uninstall!"
274- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
275- 
276- get_tmp_root "tmp_root"
277- tmp_filelist_path=$(mktemp "$tmp_root/filelist_XXXXXX" || exit 1)
278- cp -f "${filelist_path}" "${tmp_filelist_path}"
279- 
280- if [ $? -ne 0 ]; then
281- log "ERROR" "cp -f ${filelist_path} ${tmp_filelist_path} failed!"
282- exit 1
283- fi
284- 
285- del_tmp_filelist="rm -f \"${tmp_filelist_path}\""
286- 
287- version_uninstall "${install_type}" "${install_path}" "${filelist_path}" "${package}" "${feature_param}" \
288- "${version_dir}" "${username}" "${docker_root}" "${custom_options}"
289- if [ $? -ne 0 ]; then
290- eval "${del_tmp_filelist}"
291- return 1
292- fi
293- 
294- # 删除临时文件tmp_filelist_path
295- eval "${del_tmp_filelist}"
296- 
297- # 删除版本空目录
298- is_dir_empty "${install_path}/${version_dir}"
299- if [ $? -eq 0 ]; then
300- remove_dir_icp "${install_path}/${version_dir}"
301- ret="$?" && [ $ret -ne 0 ] && total_ret="1"
302- fi
303- 
304- return ${total_ret}
305-}
Dscripts/package/common/sh/script_operator.inc+0-517
@@ -1,517 +0,0 @@
1-#!/bin/sh
2-# --------------------------------------------------------------------------------
3-# Copyright (c) 2025 Huawei Technologies Co., Ltd.
4-# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
5-# CANN Open Software License Agreement Version 2.0 (the "License").
6-# Please refer to the License for details. You may not use this file except in compliance with the License.
7-# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
8-# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
9-# See LICENSE in the root of the software repository for the full text of the License.
10-# --------------------------------------------------------------------------------
11- 
12-# 公共脚本操作库
13-ENV_SHELL_TYPES="bash csh fish"
14- 
15-# 检查参数。
16-__check_param() {
17- local name="$1"
18- local value
19- 
20- value="$(eval echo \${${name}})"
21- 
22- if [ "${value}" = "" ]; then
23- log "ERROR" "need set ${name} parameter!"
24- exit 1
25- fi
26-}
27- 
28-# 设置用户家目录路径
29-__set_userpath() {
30- local username="$1"
31- local docker_root="$2"
32- 
33- if [ -z "${docker_root}" ] || [ "${docker_root}" = "/" ]; then
34- userpath="$(eval echo "~${username}")"
35- else
36- if [ "${username}" = "root" ]; then
37- userpath="${docker_root}/root"
38- else
39- userpath="${docker_root}/home/${username}"
40- fi
41- fi
42-}
43- 
44-# 获取setenv.[shell]路径正则
45-get_setenv_path_regex() {
46- local _outvar="$1"
47- local _package="$2"
48- local _shell_type="$3"
49- # setenv传入路径正则规则
50- # 必须以/开头(绝对路径)
51- # 必须以/${package}/bin/setenv.${shell_type}结尾
52- local _path_regex="\/\(.\+\/\)\?${_package}\/bin\/setenv.${_shell_type}"
53- 
54- eval "${_outvar}=\"${_path_regex}\""
55-}
56- 
57-# 创建setenv文件
58-__create_setenv_file() {
59- local file="$1"
60- local shell_type="$2"
61- local add_multi_version_param="$3"
62- local install_path="$4"
63- local ret
64- 
65- if [ ! -f "${file}" ]; then
66- echo "#!/usr/bin/env ${shell_type}" > ${file}
67- ret="$?" && [ $ret -ne 0 ] && return $ret
68- 
69- if [ "${add_multi_version_param}" = "true" ]; then
70- if [ "${shell_type}" = "bash" ]; then
71- echo "export ASCEND_HOME_PATH=\"${install_path}\"" >> "${file}"
72- ret="$?" && [ $ret -ne 0 ] && return $ret
73- elif [ "${shell_type}" = "fish" ]; then
74- echo "set -gx ASCEND_HOME_PATH \"${install_path}\"" >> "${file}"
75- ret="$?" && [ $ret -ne 0 ] && return $ret
76- elif [ "${shell_type}" = "csh" ]; then
77- echo "setenv ASCEND_HOME_PATH \"${install_path}\"" >> "${file}"
78- ret="$?" && [ $ret -ne 0 ] && return $ret
79- fi
80- fi
81- fi
82- return 0
83-}
84- 
85-# 是否存在ASCEND_HOME_PATH环境变量
86-has_ascend_home_path_env() {
87- local file="$1"
88- [ ! -f "${file}" ] && return 0
89- grep "\<ASCEND_HOME_PATH\>" "${file}" > /dev/null 2>&1
90-}
91- 
92-# 添加ASCEND_HOME_PATH环境变量
93-add_ascend_home_path_env() {
94- local file="$1"
95- local shell_type="$2"
96- local install_path="$3"
97- 
98- if [ "${shell_type}" = "bash" ]; then
99- sed -i "1aexport ASCEND_HOME_PATH=\"${install_path}\"" "${file}"
100- elif [ "${shell_type}" = "fish" ]; then
101- sed -i "1aset -gx ASCEND_HOME_PATH \"${install_path}\"" "${file}"
102- elif [ "${shell_type}" = "csh" ]; then
103- sed -i "1asetenv ASCEND_HOME_PATH \"${install_path}\"" "${file}"
104- fi
105-}
106- 
107-# 删除rcfile中的source <path>
108-__remove_path_regex() {
109- local path_regex="$1"
110- local rcfile="$2"
111- 
112- if [ -f "${rcfile}" ]; then
113- sed -i "/source ${path_regex}\( \"multi_version\"\)\?$/d" "${rcfile}"
114- if [ $? -ne 0 ]; then
115- log "ERROR" "remove ${rcfile} source command failed!"
116- exit 1
117- fi
118- fi
119-}
120- 
121-# 获取setenv.${shell_type}文件路径
122-get_setenv_filepath() {
123- local _outvar="$1"
124- local _install_path="$2"
125- local _shell_type="$3"
126- 
127- eval "${_outvar}=\"${_install_path}/bin/setenv.${_shell_type}\""
128-}
129- 
130-# setenv.[shell]总脚本中添加一条语句
131-add_setenv_cmd() {
132- local install_path="$1"
133- local setenv_filepath="$2"
134- local package="$3"
135- local shell_type="$4"
136- local username="$5"
137- local usergroup="$6"
138- local add_multi_version_param="$7"
139- local path_regex config_path multi_version_param ret
140- 
141- get_setenv_path_regex "path_regex" "${package}" "${shell_type}"
142- 
143- get_setenv_filepath "config_path" "$install_path" "$shell_type"
144- __create_setenv_file "${config_path}" "${shell_type}" "${add_multi_version_param}" "${install_path}"
145- ret="$?" && [ $ret -ne 0 ] && return $ret
146- 
147- change_own "${config_path}" "${username}:${usergroup}"
148- ret="$?" && [ $ret -ne 0 ] && return $ret
149- 
150- change_mod "${config_path}" "${SETENV_WRITEABLE_MOD}" ""
151- ret="$?" && [ $ret -ne 0 ] && return $ret
152- 
153- __remove_path_regex "${path_regex}" "${config_path}"
154- ret="$?" && [ $ret -ne 0 ] && return $ret
155- 
156- if [ "${add_multi_version_param}" = "true" ]; then
157- multi_version_param=" \"multi_version\""
158- fi
159- 
160- if [ "${shell_type}" = "bash" ] || [ "${shell_type}" = "fish" ]; then
161- echo "source ${setenv_filepath}${multi_version_param}" >> "${config_path}"
162- ret="$?" && [ $ret -ne 0 ] && return $ret
163- elif [ "${shell_type}" = "csh" ]; then
164- echo "set argv=(\"${setenv_filepath}\"${multi_version_param}); source ${setenv_filepath}" >> "${config_path}"
165- ret="$?" && [ $ret -ne 0 ] && return $ret
166- fi
167- change_mod "${config_path}" "${SETENV_MOD}" "${INSTALL_FOR_ALL}"
168- ret="$?" && [ $ret -ne 0 ] && return $ret
169- 
170- return 0
171-}
172- 
173-# 修改路径的own
174-__chown_path() {
175- local path="$1"
176- local username="$2"
177- local usergroup="$3"
178- 
179- if [ "${username}" != "" ] && [ "${usergroup}" != "" ]; then
180- chown -h "${username}:${usergroup}" "${path}"
181- if [ $? -ne 0 ]; then
182- log "ERROR" "${path} chown failed!"
183- exit 1
184- fi
185- fi
186- 
187- return 0
188-}
189- 
190-# 创建配置所在目录
191-__create_config_dir() {
192- local dir="$1"
193- local username="$2"
194- local usergroup="$3"
195- local ret
196- 
197- if [ ! -d "${dir}" ]; then
198- mkdir -p "${dir}"
199- ret="$?" && [ $ret -ne 0 ] && return $ret
200- 
201- __chown_path "${dir}" "${username}" "${usergroup}"
202- ret="$?" && [ $ret -ne 0 ] && return $ret
203- fi
204- 
205- return 0
206-}
207- 
208-# 创建配置文件
209-__create_config_file() {
210- local file="$1"
211- local username="$2"
212- local usergroup="$3"
213- local ret
214- 
215- if [ ! -f "${file}" ]; then
216- touch "${file}"
217- ret="$?" && [ $ret -ne 0 ] && return $ret
218- 
219- __chown_path "${file}" "${username}" "${usergroup}"
220- ret="$?" && [ $ret -ne 0 ] && return $ret
221- fi
222- 
223- return 0
224-}
225- 
226-# 添加source setenv.[shell]脚本到rc文件中
227-add_env_rc() {
228- local install_path="$1"
229- local setenv_filepath="$2"
230- local package="$3"
231- local shell_type="$4"
232- local setenv="$5"
233- local username="$6"
234- local usergroup="$7"
235- local add_multi_version_param="$8"
236- local docker_root="$9"
237- local path_suffix="${package}/bin/setenv.${shell_type}"
238- local path_regex
239- local matched
240- local userpath
241- local config_path
242- local ret
243- 
244- __check_param "package"
245- __check_param "username"
246- __check_param "usergroup"
247- 
248- echo "${shell_type}" | grep -E "^(bash|fish|csh)$" > /dev/null
249- if [ $? -ne 0 ]; then
250- log "ERROR" "shell type ${shell_type} not support!"
251- exit 1
252- fi
253- 
254- get_setenv_path_regex "path_regex" "${package}" "${shell_type}"
255- 
256- matched="$(echo "${setenv_filepath}" | sed -n "/^${path_regex}$/p")"
257- if [ -z "${matched}" ]; then
258- log "ERROR" "setenv filepath is illegal, should endswith ${path_suffix}"
259- exit 1
260- fi
261- 
262- if [ "${setenv}" = "y" ]; then
263- __set_userpath "${username}" "${docker_root}"
264- 
265- if [ "${shell_type}" = "bash" ]; then
266- config_path="${userpath}/.bashrc"
267- __create_config_dir "${userpath}" "${username}" "${usergroup}"
268- ret="$?" && [ $ret -ne 0 ] && return $ret
269- 
270- __create_config_file "${config_path}" "${username}" "${usergroup}"
271- ret="$?" && [ $ret -ne 0 ] && return $ret
272- 
273- __remove_path_regex "${path_regex}" "${config_path}"
274- ret="$?" && [ $ret -ne 0 ] && return $ret
275- 
276- echo "source ${setenv_filepath}" >> "${config_path}"
277- ret="$?" && [ $ret -ne 0 ] && return $ret
278- elif [ "${shell_type}" = "fish" ]; then
279- config_path="${userpath}/.config/fish/config.fish"
280- __create_config_dir "${userpath}" "${username}" "${usergroup}"
281- ret="$?" && [ $ret -ne 0 ] && return $ret
282- 
283- __create_config_dir "${userpath}/.config" "${username}" "${usergroup}"
284- ret="$?" && [ $ret -ne 0 ] && return $ret
285- 
286- __create_config_dir "${userpath}/.config/fish" "${username}" "${usergroup}"
287- ret="$?" && [ $ret -ne 0 ] && return $ret
288- 
289- __create_config_file "${config_path}" "${username}" "${usergroup}"
290- ret="$?" && [ $ret -ne 0 ] && return $ret
291- 
292- __remove_path_regex "${path_regex}" "${config_path}"
293- ret="$?" && [ $ret -ne 0 ] && return $ret
294- 
295- echo "source ${setenv_filepath}" >> "${config_path}"
296- ret="$?" && [ $ret -ne 0 ] && return $ret
297- elif [ "${shell_type}" = "csh" ]; then
298- config_path="${userpath}/.cshrc"
299- __create_config_dir "${userpath}" "${username}" "${usergroup}"
300- ret="$?" && [ $ret -ne 0 ] && return $ret
301- 
302- __create_config_file "${config_path}" "${username}" "${usergroup}"
303- ret="$?" && [ $ret -ne 0 ] && return $ret
304- 
305- __remove_path_regex "${path_regex}" "${config_path}"
306- ret="$?" && [ $ret -ne 0 ] && return $ret
307- 
308- echo "set argv=(\"${setenv_filepath}\"); source ${setenv_filepath}" >> "${config_path}"
309- ret="$?" && [ $ret -ne 0 ] && return $ret
310- fi
311- fi
312- 
313- if [ ! -d "${install_path}/bin" ]; then
314- # 如果bin目录不存在,则不处理
315- return 0
316- fi
317- 
318- add_setenv_cmd "${install_path}" "${setenv_filepath}" "${package}" "${shell_type}" "${username}" "${usergroup}" "${add_multi_version_param}"
319-}
320- 
321-# 删除setenv文件,如果已经没有setenv内容
322-__remove_setenv_file_if_no_content() {
323- local file="$1"
324- local shell_type="$2"
325- local num
326- 
327- if [ ! -f "${file}" ]; then
328- return 0
329- fi
330- 
331- num=$(grep "setenv.${shell_type}" ${file} | wc -l)
332- if [ ${num} -eq 0 ]; then
333- rm -f "${file}" > /dev/null 2>&1
334- if [ $? -ne 0 ]; then
335- log "WARNING" "Delete file:${file} failed, please delete it by yourself."
336- fi
337- fi
338-}
339- 
340-# 从rc文件中删除source setenv.[shell]
341-del_env_rc() {
342- local install_path="$1"
343- local setenv_filepath="$2"
344- local shell_type="$3"
345- local username="$4"
346- local docker_root="$5"
347- local path_regex
348- local userpath
349- local config_path
350- local oldmod
351- 
352- __check_param "username"
353- 
354- echo "${shell_type}" | grep -E "^(bash|fish|csh)$" > /dev/null
355- if [ $? -ne 0 ]; then
356- log "ERROR" "shell type ${shell_type} not support!"
357- exit 1
358- fi
359- 
360- __set_userpath "${username}" "${docker_root}"
361- 
362- # 将路径中的/转换为\/
363- path_to_regex "path_regex" "${setenv_filepath}"
364- 
365- if [ "${shell_type}" = "bash" ]; then
366- __remove_path_regex "${path_regex}" "${userpath}/.bashrc"
367- ret="$?" && [ $ret -ne 0 ] && return $ret
368- elif [ "${shell_type}" = "fish" ]; then
369- __remove_path_regex "${path_regex}" "${userpath}/.config/fish/config.fish"
370- ret="$?" && [ $ret -ne 0 ] && return $ret
371- elif [ "${shell_type}" = "csh" ]; then
372- __remove_path_regex "${path_regex}" "${userpath}/.cshrc"
373- ret="$?" && [ $ret -ne 0 ] && return $ret
374- fi
375- 
376- get_setenv_filepath "config_path" "$install_path" "$shell_type"
377- 
378- if [ ! -f "${config_path}" ]; then
379- # 如果文件不存在,则不处理
380- return 0
381- fi
382- 
383- get_file_mod "oldmod" "${config_path}"
384- change_mod "${config_path}" "${SETENV_WRITEABLE_MOD}" ""
385- ret="$?" && [ $ret -ne 0 ] && return $ret
386- 
387- __remove_path_regex "${path_regex}" "${config_path}"
388- ret="$?" && [ $ret -ne 0 ] && return $ret
389- 
390- change_mod "${config_path}" "${oldmod}" ""
391- ret="$?" && [ $ret -ne 0 ] && return $ret
392- 
393- __remove_setenv_file_if_no_content "${config_path}" "${shell_type}"
394-}
395- 
396-# 移除路径中的docker_root
397-remove_path_docker_root() {
398- local _outvar="$1"
399- local _path="$2"
400- local _docker_root="$3"
401- local _path_rpdr="$(echo "${_path}" | sed "s/^.\{${#_docker_root}\}//")"
402- 
403- eval "${_outvar}=\"${_path_rpdr}\""
404-}
405- 
406-# 获取脚本路径与脚本真实路径(docker_root)
407-get_shell_path_and_shell_path_real() {
408- local _outvar="$1"
409- local _install_path="$2"
410- local _package="$3"
411- local _shell_filename="$4"
412- local _docker_root="$5"
413- local _package_dirpath _shell_path _shell_path_real
414- 
415- get_package_dirpath "_package_dirpath" "${_package}"
416- _shell_path_real="${_install_path}/${_package_dirpath}/bin/${_shell_filename}"
417- 
418- if [ "${_docker_root}" != "" ]; then
419- # 移除脚本路径中的docker_root前缀
420- remove_path_docker_root "_shell_path" "${_shell_path_real}" "${_docker_root}"
421- else
422- _shell_path="${_shell_path_real}"
423- fi
424- 
425- eval "${_outvar}=\"${_shell_path} ${_shell_path_real}\""
426-}
427- 
428-# 增加prereq_check
429-add_prereq_check() {
430- local install_path="$1"
431- local package="$2"
432- local username="$3"
433- local usergroup="$4"
434- local docker_root="$5"
435- local ret
436- 
437- for type in ${ENV_SHELL_TYPES}; do
438- local shell_path_pair shell_path shell_path_real
439- 
440- get_shell_path_and_shell_path_real "shell_path_pair" "${install_path}" "${package}" "prereq_check.${type}" "${docker_root}"
441- __index_list "${shell_path_pair}" 0 "shell_path" 1 "shell_path_real"
442- [ ! -f "${shell_path_real}" ] && continue
443- 
444- local common_path=${install_path}/bin/prereq_check.${type}
445- local path_regex="\/\(.\+\/\)\?${package}\/bin\/prereq_check.${type}"
446- if [ -f "${common_path}" ]; then
447- chmod u+w ${common_path}
448- 
449- sed -i "/^${path_regex}$/d" ${common_path}
450- ret="$?" && [ $ret -ne 0 ] && return $ret
451- 
452- echo "${shell_path}" >> ${common_path}
453- ret="$?" && [ $ret -ne 0 ] && return $ret
454- 
455- chmod u-w ${common_path}
456- else
457- echo "#!/usr/bin/env ${type}" > ${common_path}
458- ret="$?" && [ $ret -ne 0 ] && return $ret
459- 
460- echo "${shell_path}" >> ${common_path}
461- ret="$?" && [ $ret -ne 0 ] && return $ret
462- 
463- change_mod "${common_path}" "${SETENV_MOD}" "${INSTALL_FOR_ALL}"
464- ret="$?" && [ $ret -ne 0 ] && return $ret
465- fi
466- change_own "${common_path}" "${username}:${usergroup}"
467- ret="$?" && [ $ret -ne 0 ] && return $ret
468- done
469- 
470- return 0
471-}
472- 
473-# 删除prereq_check
474-del_prereq_check() {
475- local install_path="$1"
476- local package="$2"
477- local docker_root="$3"
478- local ret
479- 
480- for type in ${ENV_SHELL_TYPES}; do
481- local shell_path_pair shell_path shell_path_real
482- 
483- get_shell_path_and_shell_path_real "shell_path_pair" "${install_path}" "${package}" "prereq_check.${type}" "${docker_root}"
484- __index_list "${shell_path_pair}" 0 "shell_path" 1 "shell_path_real"
485- 
486- local common_path="${install_path}/bin/prereq_check.${type}"
487- 
488- [ ! -f "${common_path}" ] && continue
489- 
490- chmod u+w "${common_path}"
491- local path_regex="\/\(.\+\/\)\?${package}\/bin\/prereq_check.${type}"
492- 
493- sed -i "/^${path_regex}$/d" "${common_path}"
494- ret="$?" && [ $ret -ne 0 ] && return $ret
495- 
496- chmod u-w "${common_path}"
497- 
498- local num=$(grep -r "prereq_check.${type}" "${common_path}" | wc -l)
499- if [ "${num}" -eq 0 ]; then
500- rm -f "${common_path}"
501- fi
502- done
503- 
504- return 0
505-}
506- 
507-# 设置环境变量
508-add_setenv() {
509- 
510- return 0
511-}
512- 
513-# 删除环境变量
514-del_setenv() {
515- 
516- return 0
517-}
Dscripts/package/common/sh/version_cfg.inc+0-1027
@@ -1,1027 +0,0 @@
1-#!/bin/sh
2-# --------------------------------------------------------------------------------
3-# Copyright (c) 2025 Huawei Technologies Co., Ltd.
4-# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
5-# CANN Open Software License Agreement Version 2.0 (the "License").
6-# Please refer to the License for details. You may not use this file except in compliance with the License.
7-# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
8-# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
9-# See LICENSE in the root of the software repository for the full text of the License.
10-# --------------------------------------------------------------------------------
11- 
12-# 版本状态配置文件处理
13-# 版本状态配置文件格式为
14-# # version: 1.0
15-# runtime_running_version=[1.80:CANN-1.80]
16-# compiler_running_version=[1.80:CANN-1.80]
17-# toolkit_running_version=[1.80:CANN-1.80]
18-# fwkplugin_running_version=[1.80:CANN-1.80]
19-# runtime_installed_version=[1.79:CANN-1.79][1.80:CANN-1.80]
20-# compiler_installed_version=[1.79:CANN-1.79][1.80:CANN-1.80][1.81:CANN-1.81]
21-# toolkit_installed_version=[1.79:CANN-1.79][1.80:CANN-1.80][1.81:CANN-1.81]
22-# fwkplugin_installed_version=[1.79:CANN-1.79][1.80:CANN-1.80][1.81:CANN-1.81]
23- 
24-# 子包会source本文件,本文件中调用的函数,都需要在文件内找到定义
25- 
26-# latest目录名
27-LATEST_DIR="cann"
28- 
29-# 配置版本号
30-_CFG_VERSION="1.0"
31- 
32-# 获取列表索引值
33-__index_list_by_cut() {
34- local _list_ilbc="$1"
35- local _cnt_ilbc _value_ilbc
36- shift
37- 
38- while true; do
39- if [ $# -eq 0 ]; then
40- return 0
41- fi
42- _cnt_ilbc="${1}" && _cnt_ilbc=$((_cnt_ilbc+1))
43- _value_ilbc="$(echo "${_list_ilbc}" | cut -d' ' -f"${_cnt_ilbc}")"
44- eval "${2}=\"${_value_ilbc}\""
45- shift 2
46- done
47- 
48- return 0
49-}
50- 
51-# 获取版本状态配置路径
52-get_version_cfg_path() {
53- local _outvar="$1"
54- local _dirpath="$2"
55- eval "${_outvar}=\"${_dirpath}/version.cfg\""
56-}
57- 
58-# 获取版本状态配置新路径
59-get_version_cfg_new_path() {
60- local _outvar="$1"
61- local _dirpath="$2"
62- eval "${_outvar}=\"${_dirpath}/version.cfg.new\""
63-}
64- 
65-# 解析配置行
66-parse_cfg_line() {
67- local _outvar="$1"
68- local _cfg_line="$2"
69- local _cfg_items_pcl
70- 
71- _cfg_items_pcl=$(echo "${_cfg_line}" | awk '
72- function join(items, len, sep, result, i) {
73- if (len >= 1) {
74- result = items[1]
75- for (i = 2; i <= len; i++) {
76- result = sprintf("%s%s%s", result, sep, items[i])
77- }
78- return result
79- }
80- return ""
81- }
82- 
83- /^[A-Za-z0-9_-]+_version=/ {
84- split($0, line_tokens, "=")
85- len_attr_tokens = split(line_tokens[1], attr_tokens, "_")
86- len_package_names = 0
87- for (i = 1; i < len_attr_tokens - 1; i++) {
88- len_package_names++
89- package_names[len_package_names] = attr_tokens[i]
90- }
91- package_name = join(package_names, len_package_names, "_")
92- attr_type = attr_tokens[len_attr_tokens-1]
93- value = line_tokens[2]
94- 
95- printf("%s %s %s", package_name, attr_type, value)
96- }
97- ')
98- eval "${_outvar}=\"${_cfg_items_pcl}\""
99-}
100- 
101-# 解析配置版本号列表
102-unpack_versions() {
103- local _outvar="$1"
104- local _value="$2"
105- 
106- local _versions="$(echo "${_value}" | sed 's/\[//g')"
107- _versions="$(echo "${_versions}" | sed 's/\]/ /g')"
108- _versions="$(echo ${_versions})" # strip
109- eval "${_outvar}=\"${_versions}\""
110-}
111- 
112-# 打包配置版本号列表
113-pack_versions() {
114- local _outvar="$1"
115- local _versions="$2"
116- 
117- local _value="$(echo "${_versions}" | sed 's/ /\]\[/g')"
118- eval "${_outvar}=\"[${_value}]\""
119-}
120- 
121-# 解析版本序对
122-unpack_version_pair() {
123- local _outvar="$1"
124- local _value="$2"
125- 
126- eval "${_outvar}=\"$(echo "${_value}" | tr ':' ' ')\""
127-}
128- 
129-# 打包运行版本配置
130-pack_running_version() {
131- local _outvar="$1"
132- local _package="$2"
133- local _version="$3"
134- local _version_dir="$4"
135- 
136- local _version_str
137- pack_versions "_version_str" "${_version}:${_version_dir}"
138- eval "${_outvar}=\"${_package}_running_version=${_version_str}\""
139-}
140- 
141-# 打包升级版本配置
142-pack_upgrade_version() {
143- local _outvar="$1"
144- local _package="$2"
145- local _version="$3"
146- local _version_dir="$4"
147- 
148- local _version_str
149- pack_versions "_version_str" "${_version}:${_version_dir}"
150- eval "${_outvar}=\"${_package}_upgrade_version=${_version_str}\""
151-}
152- 
153-# 检查配置版本
154-check_cfg_version() {
155- local filepath="$1"
156- local version
157- 
158- if [ ! -f "${filepath}" ]; then
159- return 0
160- fi
161- 
162- version=$(sed -n '1p' "${filepath}" | cut -d" " -f3)
163- if [ "${version}" != "${_CFG_VERSION}" ]; then
164- return 1
165- fi
166- return 0
167-}
168- 
169-# 输出配置版本错误
170-error_check_cfg_version() {
171- local filepath="$1"
172- log "ERROR" "check ${filepath} version failed!"
173-}
174- 
175-# 检查配置版本并输出报错日志
176-check_cfg_version_with_log() {
177- local filepath="$1"
178- local ret
179- 
180- check_cfg_version "${filepath}"
181- ret="$?"
182- if [ ${ret} -ne 0 ]; then
183- error_check_cfg_version "${filepath}"
184- return ${ret}
185- fi
186- return 0
187-}
188- 
189-# 添加配置版本注释
190-add_cfg_version_comment() {
191- local filepath="$1"
192- sed -i "1i # version: ${_CFG_VERSION}" "${filepath}"
193-}
194- 
195-# 移除子包版本
196-unset_package_version() {
197- local dirpath="$1"
198- local package="$2"
199- local target_type="$3"
200- local filepath filepath_new line cfg_items package_name attr_type
201- local cleanup="" ret mod
202- 
203- get_version_cfg_path "filepath" "${dirpath}"
204- get_version_cfg_new_path "filepath_new" "${dirpath}"
205- 
206- # 版本状态配置文件不存在则正常退出
207- if [ ! -f "${filepath}" ]; then
208- return 0
209- fi
210- 
211- check_cfg_version_with_log "${filepath}"
212- if [ $? -ne 0 ]; then
213- return 1
214- fi
215- 
216- # 保存当前目录的权限
217- get_file_mod "mod" "${dirpath}"
218- 
219- # 添加目录写权限
220- chmod u+w "${dirpath}"
221- ret="$?" && [ $ret -ne 0 ] && return $ret
222- 
223- # 恢复权限
224- cleanup="chmod ${mod} \"${dirpath}\""
225- 
226- rm -f "${filepath_new}"
227- if [ $? -ne 0 ]; then
228- log "ERROR" "delete ${filepath_new} failed!"
229- eval "${cleanup}"
230- return 1
231- fi
232- 
233- while read line; do
234- parse_cfg_line "cfg_items" "${line}"
235- [ "${cfg_items}" = "" ] && continue
236- 
237- __index_list_by_cut "${cfg_items}" 0 "package_name" 1 "attr_type"
238- 
239- if [ "${attr_type}" = "${target_type}" ]; then
240- if [ "${package_name}" != "${package}" ]; then
241- echo "${line}" >> "${filepath_new}"
242- fi
243- else
244- echo "${line}" >> "${filepath_new}"
245- fi
246- done < "${filepath}"
247- 
248- if [ -f "${filepath_new}" ]; then
249- add_cfg_version_comment "${filepath_new}"
250- mv -f "${filepath_new}" "${filepath}"
251- if [ $? -ne 0 ]; then
252- log "ERROR" "replace ${filepath} failed!"
253- eval "${cleanup}"
254- return 1
255- fi
256- else
257- rm -f "${filepath}"
258- if [ $? -ne 0 ]; then
259- log "ERROR" "delete ${filepath} failed!"
260- eval "${cleanup}"
261- return 1
262- fi
263- fi
264- 
265- eval "${cleanup}"
266- ret="$?" && [ $ret -ne 0 ] && return $ret
267- 
268- return 0
269-}
270- 
271-# 获取子包版本
272-get_package_version_version_cfg() {
273- local _outvar="$1"
274- local _dirpath="$2"
275- local _package="$3"
276- local _target_type="$4"
277- local _filepath _version_gpv
278- 
279- get_version_cfg_path "_filepath" "${_dirpath}"
280- 
281- if [ ! -f "${_filepath}" ]; then
282- eval "${_outvar}=\"\""
283- return 0
284- fi
285- 
286- check_cfg_version "${_filepath}"
287- if [ $? -ne 0 ]; then
288- eval "${_outvar}=\"\""
289- return 1
290- fi
291- 
292- _version_gpv="$(grep "^${_package}_${_target_type}_version=" "${_filepath}" | cut -d= -f2-)"
293- if [ "${_version_gpv}" != "" ]; then
294- unpack_versions "_version_gpv" "${_version_gpv}"
295- fi
296- 
297- eval "${_outvar}=\"${_version_gpv}\""
298- return 0
299-}
300- 
301-# 设置运行子包版本
302-set_running_package_version() {
303- local dirpath="$1"
304- local package="$2"
305- local version="$3"
306- local version_dir="$4"
307- local filepath filepath_new line cfg_items package_name attr_type running_version
308- local setted="false" cleanup="" ret mod
309- 
310- get_version_cfg_path "filepath" "${dirpath}"
311- get_version_cfg_new_path "filepath_new" "${dirpath}"
312- 
313- check_cfg_version_with_log "${filepath}"
314- if [ $? -ne 0 ]; then
315- return 1
316- fi
317- 
318- pack_running_version "running_version" "${package}" "${version}" "${version_dir}"
319- 
320- # 保存当前目录的权限
321- get_file_mod "mod" "${dirpath}"
322- 
323- # 添加目录写权限
324- chmod u+w "${dirpath}"
325- ret="$?" && [ $ret -ne 0 ] && return $ret
326- 
327- # 恢复权限
328- cleanup="chmod ${mod} \"${dirpath}\""
329- 
330- rm -f "${filepath_new}"
331- if [ $? -ne 0 ]; then
332- log "ERROR" "delete ${filepath_new} failed!"
333- eval "${cleanup}"
334- return 1
335- fi
336- 
337- touch "${filepath}"
338- while read line; do
339- parse_cfg_line "cfg_items" "${line}"
340- [ "${cfg_items}" = "" ] && continue
341- 
342- __index_list_by_cut "${cfg_items}" 0 "package_name" 1 "attr_type"
343- 
344- if [ "${attr_type}" = "running" ]; then
345- if [ "${package_name}" = "${package}" ]; then
346- if [ "${setted}" = "false" ]; then
347- echo "${running_version}" >> "${filepath_new}"
348- setted="true"
349- fi
350- else
351- echo "${line}" >> "${filepath_new}"
352- fi
353- elif [ "${attr_type}" = "upgrade" ]; then
354- if [ "${setted}" = "false" ]; then
355- echo "${running_version}" >> "${filepath_new}"
356- setted="true"
357- fi
358- echo "${line}" >> "${filepath_new}"
359- elif [ "${attr_type}" = "installed" ]; then
360- if [ "${setted}" = "false" ]; then
361- echo "${running_version}" >> "${filepath_new}"
362- setted="true"
363- fi
364- echo "${line}" >> "${filepath_new}"
365- else
366- echo "${line}" >> "${filepath_new}"
367- fi
368- done < "${filepath}"
369- 
370- if [ "${setted}" = "false" ]; then
371- echo "${running_version}" >> "${filepath_new}"
372- setted="true"
373- fi
374- 
375- add_cfg_version_comment "${filepath_new}"
376- mv -f "${filepath_new}" "${filepath}"
377- if [ $? -ne 0 ]; then
378- log "ERROR" "replace ${filepath} failed!"
379- eval "${cleanup}"
380- return 1
381- fi
382- 
383- eval "${cleanup}"
384- ret="$?" && [ $ret -ne 0 ] && return $ret
385- 
386- return 0
387-}
388- 
389-# 移除子包运行版本
390-unset_running_package() {
391- local dirpath="$1"
392- local package="$2"
393- 
394- unset_package_version "${dirpath}" "${package}" "running"
395-}
396- 
397-# 获取子包运行版本
398-get_running_package_version() {
399- local _outvar="$1"
400- local _dirpath="$2"
401- local _package="$3"
402- 
403- get_package_version_version_cfg "${_outvar}" "${_dirpath}" "${_package}" "running"
404-}
405- 
406-# 设置子包升级版本
407-set_upgrade_package_version() {
408- local dirpath="$1"
409- local package="$2"
410- local version="$3"
411- local version_dir="$4"
412- local filepath filepath_new line cfg_items package_name attr_type upgrade_version
413- local setted="false" cleanup="" ret mod
414- 
415- get_version_cfg_path "filepath" "${dirpath}"
416- get_version_cfg_new_path "filepath_new" "${dirpath}"
417- 
418- check_cfg_version_with_log "${filepath}"
419- if [ $? -ne 0 ]; then
420- return 1
421- fi
422- 
423- pack_upgrade_version "upgrade_version" "${package}" "${version}" "${version_dir}"
424- 
425- # 保存当前目录的权限
426- get_file_mod "mod" "${dirpath}"
427- 
428- # 添加目录写权限
429- chmod u+w "${dirpath}"
430- ret="$?" && [ $ret -ne 0 ] && return $ret
431- 
432- # 恢复权限
433- cleanup="chmod ${mod} \"${dirpath}\""
434- 
435- rm -f "${filepath_new}"
436- if [ $? -ne 0 ]; then
437- log "ERROR" "delete ${filepath_new} failed!"
438- eval "${cleanup}"
439- return 1
440- fi
441- 
442- touch "${filepath}"
443- while read line; do
444- parse_cfg_line "cfg_items" "${line}"
445- [ "${cfg_items}" = "" ] && continue
446- 
447- __index_list_by_cut "${cfg_items}" 0 "package_name" 1 "attr_type"
448- 
449- if [ "${attr_type}" = "upgrade" ]; then
450- if [ "${package_name}" = "${package}" ]; then
451- if [ "${setted}" = "false" ]; then
452- echo "${upgrade_version}" >> "${filepath_new}"
453- setted="true"
454- fi
455- else
456- echo "${line}" >> "${filepath_new}"
457- fi
458- elif [ "${attr_type}" = "installed" ]; then
459- if [ "${setted}" = "false" ]; then
460- echo "${upgrade_version}" >> "${filepath_new}"
461- setted="true"
462- fi
463- echo "${line}" >> "${filepath_new}"
464- else
465- echo "${line}" >> "${filepath_new}"
466- fi
467- done < "${filepath}"
468- 
469- if [ "${setted}" = "false" ]; then
470- echo "${upgrade_version}" >> "${filepath_new}"
471- setted="true"
472- fi
473- 
474- add_cfg_version_comment "${filepath_new}"
475- mv -f "${filepath_new}" "${filepath}"
476- if [ $? -ne 0 ]; then
477- log "ERROR" "replace ${filepath} failed!"
478- eval "${cleanup}"
479- return 1
480- fi
481- 
482- eval "${cleanup}"
483- ret="$?" && [ $ret -ne 0 ] && return $ret
484- 
485- return 0
486-}
487- 
488-# 移除子包升级版本
489-unset_upgrade_package() {
490- local dirpath="$1"
491- local package="$2"
492- unset_package_version "${dirpath}" "${package}" "upgrade"
493-}
494- 
495-# 获取子包升级版本
496-get_upgrade_package_version() {
497- local _outvar="$1"
498- local _dirpath="$2"
499- local _package="$3"
500- 
501- get_package_version_version_cfg "${_outvar}" "${_dirpath}" "${_package}" "upgrade"
502-}
503- 
504-# 包版本是否为某状态
505-is_package_version_status() {
506- local _outvar="$1"
507- local _latest_path="$2"
508- local _package="$3"
509- local _version="$4"
510- local _version_dir="$5"
511- local _target_type="$6"
512- local _pkg_status_version
513- local _ret _result_ipvs=""
514- 
515- eval "${_outvar}=\"\""
516- 
517- get_package_version_version_cfg "_pkg_status_version" "${_latest_path}" "${_package}" "${_target_type}"
518- _ret="$?" && [ $_ret -ne 0 ] && return $_ret
519- 
520- if [ "${_pkg_status_version}" = "${_version}:${_version_dir}" ]; then
521- _result_ipvs="true"
522- else
523- _result_ipvs="false"
524- fi
525- 
526- eval "${_outvar}=\"${_result_ipvs}\""
527-}
528- 
529-# 包版本是否running
530-is_package_version_running() {
531- local _outvar="$1"
532- local _latest_path="$2"
533- local _package="$3"
534- local _version="$4"
535- local _version_dir="$5"
536- 
537- is_package_version_status "${_outvar}" "${_latest_path}" "${_package}" "${_version}" "${_version_dir}" "running"
538-}
539- 
540-# 包版本是否upgrade
541-is_package_version_upgrade() {
542- local _outvar="$1"
543- local _latest_path="$2"
544- local _package="$3"
545- local _version="$4"
546- local _version_dir="$5"
547- 
548- is_package_version_status "${_outvar}" "${_latest_path}" "${_package}" "${_version}" "${_version_dir}" "upgrade"
549-}
550- 
551-# 添加安装子包版本
552-add_installed_package_version() {
553- local dirpath="$1"
554- local package="$2"
555- local version="$3"
556- local version_dir="$4"
557- local filepath filepath_new line cfg_items package_name attr_type value versions matched version_str
558- local setted="false"
559- 
560- get_version_cfg_path "filepath" "${dirpath}"
561- get_version_cfg_new_path "filepath_new" "${dirpath}"
562- 
563- check_cfg_version_with_log "${filepath}"
564- if [ $? -ne 0 ]; then
565- return 1
566- fi
567- 
568- rm -f "${filepath_new}"
569- if [ $? -ne 0 ]; then
570- log "ERROR" "delete ${filepath_new} failed!"
571- return 1
572- fi
573- 
574- pack_versions "version_str" "${version}:${version_dir}"
575- 
576- touch "${filepath}"
577- while read line; do
578- parse_cfg_line "cfg_items" "${line}"
579- [ "${cfg_items}" = "" ] && continue
580- 
581- __index_list_by_cut "${cfg_items}" 0 "package_name" 1 "attr_type" 2 "value"
582- 
583- if [ "${attr_type}" = "installed" ] && [ "${package_name}" = "${package}" ]; then
584- unpack_versions "versions" "${value}"
585- __item_in_list "matched" "${version}:${version_dir}" ${versions}
586- if [ "${matched}" = "true" ]; then
587- echo "${line}" >> "${filepath_new}"
588- else
589- echo "${line}${version_str}" >> "${filepath_new}"
590- fi
591- setted="true"
592- else
593- echo "${line}" >> "${filepath_new}"
594- fi
595- done < "${filepath}"
596- 
597- if [ "${setted}" = "false" ]; then
598- echo "${package}_installed_version=${version_str}" >> "${filepath_new}"
599- setted="true"
600- fi
601- 
602- add_cfg_version_comment "${filepath_new}"
603- mv -f "${filepath_new}" "${filepath}"
604- if [ $? -ne 0 ]; then
605- log "ERROR" "replace ${filepath} failed!"
606- return 1
607- fi
608-}
609- 
610-# 删除安装子包版本
611-del_installed_package_version() {
612- local dirpath="$1"
613- local package="$2"
614- local version="$3"
615- local version_dir="$4"
616- local filepath filepath_new line cfg_items package_name attr_type value versions len_versions
617- 
618- get_version_cfg_path "filepath" "${dirpath}"
619- get_version_cfg_new_path "filepath_new" "${dirpath}"
620- 
621- # 版本状态配置文件不存在则正常退出
622- if [ ! -f "${filepath}" ]; then
623- return 0
624- fi
625- 
626- check_cfg_version_with_log "${filepath}"
627- if [ $? -ne 0 ]; then
628- return 1
629- fi
630- 
631- rm -f "${filepath_new}"
632- if [ $? -ne 0 ]; then
633- log "ERROR" "delete ${filepath_new} failed!"
634- return 1
635- fi
636- 
637- while read line; do
638- parse_cfg_line "cfg_items" "${line}"
639- [ "${cfg_items}" = "" ] && continue
640- 
641- __index_list_by_cut "${cfg_items}" 0 "package_name" 1 "attr_type" 2 "value"
642- 
643- if [ "${attr_type}" = "installed" ] && [ "${package_name}" = "${package}" ]; then
644- unpack_versions "versions" "${value}"
645- versions="$(__remove_item_in_list "${version}:${version_dir}" "${versions}")"
646- __length_list "${versions}" "len_versions"
647- if [ ${len_versions} -gt 0 ]; then
648- pack_versions "value" "${versions}"
649- echo "${package}_installed_version=${value}" >> "${filepath_new}"
650- fi
651- else
652- echo "${line}" >> "${filepath_new}"
653- fi
654- done < "${filepath}"
655- 
656- if [ -f "${filepath_new}" ]; then
657- add_cfg_version_comment "${filepath_new}"
658- mv -f "${filepath_new}" "${filepath}"
659- if [ $? -ne 0 ]; then
660- log "ERROR" "replace ${filepath} failed!"
661- return 1
662- fi
663- else
664- rm -f "${filepath}"
665- if [ $? -ne 0 ]; then
666- log "ERROR" "delete ${filepath} failed!"
667- return 1
668- fi
669- fi
670- return 0
671-}
672- 
673-# 获取子包版本
674-get_installed_package_versions() {
675- local _outvar="$1"
676- local _dirpath="$2"
677- local _package="$3"
678- local _filepath _value _new_versions_gipv
679- 
680- get_version_cfg_path "_filepath" "${_dirpath}"
681- 
682- if [ ! -f "${_filepath}" ]; then
683- eval "${_outvar}=\"\""
684- return 0
685- fi
686- 
687- check_cfg_version "${_filepath}"
688- if [ $? -ne 0 ]; then
689- eval "${_outvar}=\"\""
690- return 1
691- fi
692- 
693- _value="$(grep "^${_package}_installed_version=" "${_filepath}" | cut -d= -f2-)"
694- unpack_versions "_new_versions_gipv" "${_value}"
695- __reverse_list "_new_versions_gipv" "${_new_versions_gipv}"
696- 
697- eval "${_outvar}=\"${_new_versions_gipv}\""
698-}
699- 
700-# 获取子包最后一次的安装版本
701-get_package_last_installed_version() {
702- local _outvar="$1"
703- local _install_path="$2"
704- local _package="$3"
705- local _versions_gpliv="" _version_gpliv="" _ret
706- 
707- eval "${_outvar}=\"\""
708- 
709- get_installed_package_versions "_versions_gpliv" "${_install_path}/${LATEST_DIR}" "${_package}"
710- _ret="$?" && [ ${_ret} -ne 0 ] && return ${_ret}
711- 
712- [ "${_versions_gpliv}" = "" ] && return 0
713- 
714- __index_list_by_cut "${_versions_gpliv}" 0 "_version_gpliv"
715- 
716- eval "${_outvar}=\"${_version_gpliv}\""
717-}
718- 
719- 
720-# 获取子包最后一次的安装版本目录
721-# 如果不存在上次安装版本,则返回空字符串
722-get_package_last_installed_version_dir() {
723- local _outvar="$1"
724- local _install_path="$2"
725- local _package="$3"
726- local _version_pair _version_pair_arr _version_dir_gplivd="" _ret
727- 
728- eval "${_outvar}=\"\""
729- 
730- get_package_last_installed_version "_version_pair" "${_install_path}" "${_package}"
731- _ret="$?" && [ ${_ret} -ne 0 ] && return ${_ret}
732- 
733- unpack_version_pair "_version_pair_arr" "${_version_pair}"
734- __index_list_by_cut "${_version_pair_arr}" 1 "_version_dir_gplivd"
735- 
736- eval "${_outvar}=\"${_version_dir_gplivd}\""
737-}
738- 
739-# 获取子包最后一次安装的install.info路径
740-# 如果不存在上次安装版本,则返回空字符串
741-get_package_last_install_info() {
742- local _outvar="$1"
743- local _install_path="$2"
744- local _package="$3"
745- local _version_dir="" _install_info="" _ret
746- 
747- eval "${_outvar}=\"\""
748- 
749- get_package_last_installed_version_dir "_version_dir" "${_install_path}" "${_package}"
750- _ret="$?" && [ ${_ret} -ne 0 ] && return ${_ret}
751- 
752- [ "${_version_dir}" = "" ] && return 0
753- 
754- get_package_install_info "_install_info" "${_install_path}" "${_version_dir}" "${_package}"
755- 
756- eval "${_outvar}=\"${_install_info}\""
757-}
758- 
759-get_package_version_dir() {
760- local _outvar="$1"
761- local _install_path="$2"
762- local _package="$3"
763- local _target_type="$4"
764- local _version_pair _version_pair_arr _version_dir_gprvd="" _ret
765- 
766- eval "${_outvar}=\"\""
767- 
768- get_package_version_version_cfg "_version_pair" "${_install_path}/${LATEST_DIR}" "${_package}" "${_target_type}"
769- _ret="$?" && [ ${_ret} -ne 0 ] && return ${_ret}
770- 
771- if [ "${_version_pair}" = "" ] && [ "${_target_type}" = "upgrade" ]; then
772- get_package_version_version_cfg "_version_pair" "${_install_path}/${LATEST_DIR}" "${_package}" "running"
773- _ret="$?" && [ ${_ret} -ne 0 ] && return ${_ret}
774- fi
775- 
776- [ "${_version_pair}" = "" ] && return 0
777- 
778- unpack_version_pair "_version_pair_arr" "${_version_pair}"
779- __index_list_by_cut "${_version_pair_arr}" 1 "_version_dir_gprvd"
780- 
781- eval "${_outvar}=\"${_version_dir_gprvd}\""
782-}
783- 
784-get_package_install_message() {
785- local _outvar="$1"
786- local _install_path="$2"
787- local _package="$3"
788- local _target_type="$4"
789- local _version_dir_gpim="" _install_info_gpim="" _ret
790- 
791- eval "${_outvar}=\"\""
792- 
793- get_package_version_dir "_version_dir_gpim" "${_install_path}" "${_package}" "${_target_type}"
794- _ret="$?" && [ ${_ret} -ne 0 ] && return ${_ret}
795- 
796- [ "${_version_dir_gpim}" = "" ] && return 0
797- 
798- get_package_install_info "_install_info_gpim" "${_install_path}" "${_version_dir_gpim}" "${_package}"
799- 
800- eval "${_outvar}=\"${_install_info_gpim}\""
801-}
802- 
803-# 获取子包的运行版本目录
804-# 如果不存在运行版本,则返回空字符串
805-get_package_running_version_dir() {
806- local _outvar="$1"
807- local _install_path="$2"
808- local _package="$3"
809- 
810- get_package_version_dir "${_outvar}" "${_install_path}" "${_package}" "running"
811-}
812- 
813-# 获取子包运行版本的install.info路径
814-# 如果不存在运行版本,则返回空字符串
815-get_package_running_install_info() {
816- local _outvar="$1"
817- local _install_path="$2"
818- local _package="$3"
819- 
820- get_package_install_message "${_outvar}" "${_install_path}" "${_package}" "running"
821-}
822- 
823-# 获取子包的升级版本目录
824-# 如果不存在升级版本,则返回运行版本目录
825-# 如果也不存在运行版本,则返回空字符串
826-get_package_upgrade_version_dir() {
827- local _outvar="$1"
828- local _install_path="$2"
829- local _package="$3"
830- 
831- get_package_version_dir "${_outvar}" "${_install_path}" "${_package}" "upgrade"
832-}
833- 
834-# 获取子包升级版本的install.info路径
835-# 如果不存在升级版本,则返回运行版本的install.info路径
836-# 如果也不存在运行版本,则返回空字符串
837-get_package_upgrade_install_info() {
838- local _outvar="$1"
839- local _install_path="$2"
840- local _package="$3"
841- 
842- get_package_install_message "${_outvar}" "${_install_path}" "${_package}" "upgrade"
843-}
844- 
845-# 获取所有运行包
846-get_running_packages() {
847- local _outvar="$1"
848- local _dirpath="$2"
849- local _filepath _line _cfg_items _package_name_grp _attr_type_grp _packages=""
850- 
851- eval "${_outvar}=\"\""
852- 
853- get_version_cfg_path "_filepath" "${_dirpath}"
854- 
855- if [ ! -f "${_filepath}" ]; then
856- return 0
857- fi
858- 
859- check_cfg_version "${_filepath}"
860- if [ $? -ne 0 ]; then
861- return 1
862- fi
863- 
864- while read _line; do
865- parse_cfg_line "_cfg_items" "${_line}"
866- [ "${_cfg_items}" = "" ] && continue
867- 
868- __index_list_by_cut "${_cfg_items}" 0 "_package_name_grp" 1 "_attr_type_grp"
869- 
870- if [ "${_attr_type_grp}" = "running" ]; then
871- if [ "${_packages}" = "" ]; then
872- _packages="${_package_name_grp}"
873- else
874- _packages="${_packages} ${_package_name_grp}"
875- fi
876- fi
877- done < "${_filepath}"
878- 
879- eval "${_outvar}=\"${_packages}\""
880-}
881- 
882-# 根据版本目录获取版本号
883-get_version_by_version_dir() {
884- local _outvar="$1"
885- local _dirpath="$2"
886- local _version_dir="$3"
887- local _filepath _line _cfg_items _attr_type_gvbvd _value_gvbvd
888- local _version_pairs _version_pair _version_pair_arr _version_gvbvd _version_dir_gvbvd
889- 
890- eval "${_outvar}=\"\""
891- 
892- get_version_cfg_path "_filepath" "${_dirpath}"
893- if [ ! -f "${_filepath}" ]; then
894- return 1
895- fi
896- 
897- check_cfg_version "${_filepath}"
898- if [ $? -ne 0 ]; then
899- return 1
900- fi
901- 
902- while read _line; do
903- parse_cfg_line "_cfg_items" "${_line}"
904- [ "${_cfg_items}" = "" ] && continue
905- 
906- __index_list_by_cut "${_cfg_items}" 1 "_attr_type_gvbvd" 2 "_value_gvbvd"
907- 
908- if [ "${_attr_type_gvbvd}" = "installed" ]; then
909- unpack_versions "_version_pairs" "${_value_gvbvd}"
910- for _version_pair in ${_version_pairs}; do
911- unpack_version_pair "_version_pair_arr" "${_version_pair}"
912- __index_list_by_cut "${_version_pair_arr}" 0 "_version_gvbvd" 1 "_version_dir_gvbvd"
913- if [ "${_version_dir_gvbvd}" = "${_version_dir}" ]; then
914- eval "${_outvar}=\"${_version_gvbvd}\""
915- return 0
916- fi
917- done
918- fi
919- done < "${_filepath}"
920- 
921- return 1
922-}
923- 
924-# 根据版本号与版本目录获取所有安装包
925-get_installed_packages_by_version_version_dir() {
926- local _outvar="$1"
927- local _dirpath="$2"
928- local _version="$3"
929- local _version_dir="$4"
930- local _filepath _line _cfg_items
931- local _package_gipbvvd _attr_type_gipbvvd _value_gipbvvd _version_pairs _in_list
932- local _packages_gipbvvd=""
933- 
934- eval "${_outvar}=\"\""
935- 
936- get_version_cfg_path "_filepath" "${_dirpath}"
937- if [ ! -f "${_filepath}" ]; then
938- return 1
939- fi
940- 
941- check_cfg_version "${_filepath}"
942- if [ $? -ne 0 ]; then
943- return 1
944- fi
945- 
946- while read _line; do
947- parse_cfg_line "_cfg_items" "${_line}"
948- [ "${_cfg_items}" = "" ] && continue
949- 
950- __index_list_by_cut "${_cfg_items}" 0 "_package_gipbvvd" 1 "_attr_type_gipbvvd" 2 "_value_gipbvvd"
951- 
952- if [ "${_attr_type_gipbvvd}" = "installed" ]; then
953- unpack_versions "_version_pairs" "${_value_gipbvvd}"
954- __item_in_list "_in_list" "${_version}:${_version_dir}" ${_version_pairs}
955- if [ "${_in_list}" = "true" ]; then
956- if [ "${_packages_gipbvvd}" = "" ]; then
957- _packages_gipbvvd="${_package_gipbvvd}"
958- else
959- _packages_gipbvvd="${_packages_gipbvvd} ${_package_gipbvvd}"
960- fi
961- fi
962- fi
963- done < "${_filepath}"
964- 
965- eval "${_outvar}=\"${_packages_gipbvvd}\""
966-}
967- 
968-# 获取所有安装包
969-get_installed_packages() {
970- local _outvar="$1"
971- local _dirpath="$2"
972- local _filepath _line _cfg_items _package_name_gip _attr_type_gip _packages=""
973- 
974- eval "${_outvar}=\"\""
975- 
976- get_version_cfg_path "_filepath" "${_dirpath}"
977- 
978- if [ ! -f "${_filepath}" ]; then
979- return 0
980- fi
981- 
982- check_cfg_version "${_filepath}"
983- if [ $? -ne 0 ]; then
984- return 1
985- fi
986- 
987- while read _line; do
988- parse_cfg_line "_cfg_items" "${_line}"
989- [ "${_cfg_items}" = "" ] && continue
990- 
991- __index_list_by_cut "${_cfg_items}" 0 "_package_name_gip" 1 "_attr_type_gip"
992- 
993- if [ "${_attr_type_gip}" = "installed" ]; then
994- if [ "${_packages}" = "" ]; then
995- _packages="${_package_name_gip}"
996- else
997- _packages="${_packages} ${_package_name_gip}"
998- fi
999- fi
1000- done < "${_filepath}"
1001- 
1002- eval "${_outvar}=\"${_packages}\""
1003-}
1004- 
1005-# version.cfg文件是否存在
1006-# 返回值0为真,1为假
1007-version_cfg_exists() {
1008- local dirpath="$1"
1009- local filepath
1010- 
1011- get_version_cfg_path "filepath" "$dirpath"
1012- if [ -f "$filepath" ]; then
1013- return 0
1014- fi
1015- return 1
1016-}
1017- 
1018-# 显示version.cfg文件
1019-show_version_cfg() {
1020- local dirpath="$1"
1021- local filepath
1022- 
1023- get_version_cfg_path "filepath" "$dirpath"
1024- if [ -f "$filepath" ]; then
1025- cat "$filepath"
1026- fi
1027-}
Dscripts/package/common/sh/version_compatiable.inc+0-174
@@ -1,174 +0,0 @@
1-#!/bin/sh
2-# ----------------------------------------------------------------------------
3-# Copyright (c) 2025 Huawei Technologies Co., Ltd.
4-# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
5-# CANN Open Software License Agreement Version 2.0 (the "License").
6-# Please refer to the License for details. You may not use this file except in compliance with the License.
7-# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
8-# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
9-# See LICENSE in the root of the software repository for the full text of the License.
10-# ----------------------------------------------------------------------------
11- 
12-# 获取版本号,用于版本兼容性对比
13-get_package_compat_version() {
14- local _outvar="$1"
15- local _version_info_path="$2"
16- local _result
17- 
18- if [ ! -f "${_version_info_path}" ]; then
19- read -r "$_outvar" <<EOF
20-EOF
21- return 1
22- fi
23- 
24- read -r "$_outvar" <<EOF
25-$(grep "^Version=" "$_version_info_path" | cut -d= -f2- | cut -d- -f1 | cut -d. -f-3)
26-EOF
27-}
28- 
29-# 获取正式包名
30-get_formal_package_name() {
31- local _outvar="$1"
32- local _package="$2"
33- read -r "$_outvar" <<EOF
34-$(echo "$_package" | sed 's/_/-/g')
35-EOF
36-}
37- 
38-# 获取非正式包名
39-get_informal_package_name() {
40- local _outvar="$1"
41- local _package="$2"
42- read -r "$_outvar" <<EOF
43-$(echo "$_package" | sed 's/-/_/g')
44-EOF
45-}
46- 
47-# 获取需求包列表。
48-_get_required_packages() {
49- local _version_info_path="$1"
50- 
51- if [ ! -f "$_version_info_path" ]; then
52- return 0
53- fi
54- 
55- awk '
56- function join(items, len, sep, result, i) {
57- if (len >= 1) {
58- result = items[1]
59- for (i = 2; i <= len; i++) {
60- result = sprintf("%s%s%s", result, sep, items[i])
61- }
62- return result
63- }
64- return ""
65- }
66- 
67- /^required_package_.+_version=/ {
68- split($0, line_tokens, "=")
69- len_require_tokens = split(line_tokens[1], require_tokens, "_")
70- len_package_names = 0
71- for (i = 3; i < len_require_tokens; i++) {
72- len_package_names++
73- package_names[len_package_names] = require_tokens[i]
74- }
75- package_name = join(package_names, len_package_names, "_")
76- print package_name
77- }
78- ' "${_version_info_path}"
79-}
80- 
81-# 获取需求包信息。
82-_get_required_package_info() {
83- local _outvar="$1"
84- local _version_info_path="$2"
85- local _pkg_name="$3"
86- local _required_pkg_name
87- local _required_value
88- 
89- if [ ! -f "${_version_info_path}" ]; then
90- eval "${_outvar}=\"\""
91- return 1
92- fi
93- 
94- _required_pkg_name="required_package_${_pkg_name}_version"
95- _required_value="$(grep "^${_required_pkg_name}=" "${_version_info_path}" | cut -d= -f2-)"
96- # _required_value取得的值带有双引号,eval时不可以在外侧再添加双引号
97- 
98- eval "${_outvar}=${_required_value}"
99-}
100- 
101-# 检查版本与需求版本兼容性。
102-# 兼容返回0,不兼容返回1
103-_check_version_required() {
104- local version="$1"
105- local require="$2"
106- local script_dir="$3"
107- local src_pkg="$4"
108- local dst_pkg="$5"
109- local result
110- 
111- result=$(awk -f "${script_dir}/check_version_required.awk" -v version="${version}" -v all_required="${require}")
112- if [ "${result}" = "T" ]; then
113- return 0
114- fi
115- 
116- echo "Version compatibility check failed, $src_pkg required $dst_pkg version $require, but $dst_pkg version is $version!"
117- return 1
118-}
119- 
120-# 检查版本兼容性。
121-_check_version_compatiable() {
122- local package="$1"
123- local install_path="$2"
124- local script_dir="$3"
125- local version_info_path="$script_dir/../version.info"
126- local err_msgs package_formal self_version src_pkg src_pkg_formal dst_pkg dst_pkg_formal
127- local installed_version_info pkg_version_info_path
128- 
129- get_formal_package_name "package_formal" "$package"
130- get_package_compat_version "self_version" "$version_info_path"
131- 
132- if [ -d "$install_path/share/info" ]; then
133- err_msgs="$(
134- ls "$install_path/share/info" | grep -v "^${package}$" | while read src_pkg; do
135- get_formal_package_name "src_pkg_formal" "$package"
136- installed_version_info="$install_path/share/info/$src_pkg/version.info"
137- grep "^required_package_${package_formal}_version=" "$installed_version_info" | cut -d= -f2- | tr -d '"' | while read required; do
138- _check_version_required "$self_version" "$required" "$script_dir" "$src_pkg_formal" "$package_formal"
139- done
140- done
141- )"
142- if [ "$err_msgs" != "" ]; then
143- while read err_msg; do
144- comm_log "ERROR" "$err_msg"
145- done <<EOF
146-$err_msgs
147-EOF
148- return 1
149- fi
150- fi
151- err_msgs="$(
152- _get_required_packages "${version_info_path}" | while read dst_pkg_formal; do
153- get_informal_package_name "dst_pkg" "$dst_pkg_formal"
154- pkg_version_info_path="$install_path/share/info/$dst_pkg_formal/version.info"
155- if [ ! -f "$pkg_version_info_path" ]; then
156- pkg_version_info_path="$install_path/share/info/$dst_pkg/version.info"
157- fi
158- if [ -f "$pkg_version_info_path" ]; then
159- get_package_compat_version "pkg_version" "$pkg_version_info_path"
160- _get_required_package_info "required" "$version_info_path" "$dst_pkg_formal"
161- _check_version_required "$pkg_version" "$required" "$script_dir" "$package_formal" "$dst_pkg_formal"
162- fi
163- done
164- )"
165- if [ "$err_msgs" != "" ]; then
166- while read err_msg; do
167- comm_log "ERROR" "$err_msg"
168- done <<EOF
169-$err_msgs
170-EOF
171- return 1
172- fi
173- return 0
174-}
Dscripts/package/latest_manager/scripts/filelist.csv+0-16
@@ -1,16 +0,0 @@
1-module,operation,relative_path_in_pkg,relative_install_path,is_in_docker,permission,owner:group,install_type,softlink,feature,is_common_path,configurable,hash,block,pkg_inner_softlink,chip
2-NA,mkdir,NA,manager,TRUE,550,\\$username:\\$usergroup,all,NA,all,N,FALSE,NA,latest_manager,NA,all
3-NA,copy,latest_manager/filelist.csv,manager/filelist.csv,TRUE,440,\\$username:\\$usergroup,all,NA,all,N,FALSE,NA,latest_manager,NA,all
4-NA,copy,latest_manager/install.sh,manager/install.sh,TRUE,440,\\$username:\\$usergroup,all,NA,all,N,FALSE,NA,latest_manager,NA,all
5-NA,copy,latest_manager/manager.sh,manager.sh,TRUE,550,\\$username:\\$usergroup,all,NA,all,N,FALSE,NA,latest_manager,NA,all
6-NA,copy,latest_manager/manager_func.sh,manager/manager_func.sh,TRUE,440,\\$username:\\$usergroup,all,NA,all,N,FALSE,NA,latest_manager,NA,all
7-NA,copy,latest_manager/script_operator.inc,manager/script_operator.inc,TRUE,440,\\$username:\\$usergroup,all,NA,all,N,FALSE,NA,latest_manager,NA,all
8-NA,copy,latest_manager/uninstall.sh,manager/uninstall.sh,TRUE,550,\\$username:\\$usergroup,all,NA,all,N,FALSE,NA,latest_manager,NA,all
9-NA,copy,latest_manager/version.info,manager/version.info,TRUE,440,\\$username:\\$usergroup,all,NA,all,N,FALSE,NA,latest_manager,NA,all
10-NA,copy,latest_manager/version_compatiable.inc,manager/version_compatiable.inc,TRUE,440,\\$username:\\$usergroup,all,NA,all,N,FALSE,NA,latest_manager,NA,all
11-NA,copy,latest_manager/check_version_required.awk,manager/check_version_required.awk,TRUE,440,\\$username:\\$usergroup,all,NA,all,N,FALSE,NA,latest_manager,NA,all
12-NA,copy,latest_manager/common_func_v2.inc,manager/common_func_v2.inc,TRUE,440,\\$username:\\$usergroup,all,NA,all,N,FALSE,NA,latest_manager,NA,all
13-NA,copy,latest_manager/common_func.inc,manager/common_func.inc,TRUE,440,\\$username:\\$usergroup,all,NA,all,N,FALSE,NA,latest_manager,NA,all
14-NA,copy,latest_manager/common_installer.inc,manager/common_installer.inc,TRUE,440,\\$username:\\$usergroup,all,NA,all,N,FALSE,NA,latest_manager,NA,all
15-NA,copy,latest_manager/install_common_parser.sh,manager/install_common_parser.sh,TRUE,440,\\$username:\\$usergroup,all,NA,all,N,FALSE,NA,latest_manager,NA,all
16-NA,copy,latest_manager/version_cfg.inc,manager/version_cfg.inc,TRUE,440,\\$username:\\$usergroup,all,NA,all,N,FALSE,NA,latest_manager,NA,all
Dscripts/package/latest_manager/scripts/install.sh+0-59
@@ -1,59 +0,0 @@
1-#!/bin/bash
2-# --------------------------------------------------------------------------------
3-# Copyright (c) 2025 Huawei Technologies Co., Ltd.
4-# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
5-# CANN Open Software License Agreement Version 2.0 (the "License").
6-# Please refer to the License for details. You may not use this file except in compliance with the License.
7-# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
8-# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
9-# See LICENSE in the root of the software repository for the full text of the License.
10-# --------------------------------------------------------------------------------
11- 
12-CURPATH=$(dirname $(readlink -f "$0"))
13-USERNAME=$(id -un)
14-USERGROUP=$(id -gn)
15-common_func_path="${CURPATH}/common_func.inc"
16- 
17-. "$common_func_path"
18- 
19-INSTALL_PATH=""
20-IS_UPGRADE="n"
21- 
22-set_comm_log "Latest_manager" "$COMM_LOGFILE"
23- 
24-while true
25-do
26- case "$1" in
27- --install-path=*)
28- INSTALL_PATH="$(echo "$1" | cut -d"=" -f2-)"
29- shift
30- ;;
31- --upgrade)
32- IS_UPGRADE="y"
33- shift
34- ;;
35- -*)
36- comm_log "ERROR" "Unsupported parameters : $1"
37- exit 1
38- ;;
39- *)
40- break
41- ;;
42- esac
43-done
44- 
45-if [ "$INSTALL_PATH" = "" ]; then
46- comm_log "ERROR" "--install-path parameter is required!"
47- exit 1
48-fi
49- 
50-if ! sh "$CURPATH/install_common_parser.sh" --package="latest_manager" --install --username="$USERNAME" --usergroup="$USERGROUP" \
51- --simple-install "full" "$INSTALL_PATH" "$CURPATH/filelist.csv" "all"; then
52- comm_log "ERROR" "install failed!"
53- exit 1
54-fi
55- 
56-if [ "$IS_UPGRADE" = "y" ] && ! "$INSTALL_PATH/manager.sh" "migrate_latest_data"; then
57- comm_log "ERROR" "migrate latest data failed!"
58- exit 1
59-fi
Dscripts/package/latest_manager/scripts/manager.sh+0-128
@@ -1,128 +0,0 @@
1-#!/bin/sh
2-# --------------------------------------------------------------------------------
3-# Copyright (c) 2025 Huawei Technologies Co., Ltd.
4-# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
5-# CANN Open Software License Agreement Version 2.0 (the "License").
6-# Please refer to the License for details. You may not use this file except in compliance with the License.
7-# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
8-# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
9-# See LICENSE in the root of the software repository for the full text of the License.
10-# --------------------------------------------------------------------------------
11- 
12-CURPATH=$(dirname "$(readlink -f "$0")")
13-VAR_PATH="$CURPATH"
14-common_func_path="$CURPATH/manager/common_func.inc"
15-version_compatiable_path="$CURPATH/manager/version_compatiable.inc"
16-common_func_v2_path="$CURPATH/manager/common_func_v2.inc"
17-version_cfg_path="$CURPATH/manager/version_cfg.inc"
18-script_operator_path="$CURPATH/manager/script_operator.inc"
19-manager_func_path="$CURPATH/manager/manager_func.sh"
20- 
21-. "$common_func_path"
22-. "$version_compatiable_path"
23-. "$common_func_v2_path"
24-. "$version_cfg_path"
25-. "$script_operator_path"
26-. "$manager_func_path"
27- 
28-set_comm_log "Latest_manager" "$COMM_LOGFILE"
29- 
30-VERSION=""
31-VERSION_DIR=""
32-PACKAGE=""
33-DOCKER_ROOT=""
34-INTPUT_INSTALL_FOR_ALL="n"
35-IS_RECREATE_SOFTLINK="n"
36-INCREMENT="n"
37-USE_SHARE_INFO="n"
38- 
39-while true
40-do
41- case "$1" in
42- --version)
43- VERSION="$2"
44- shift 2
45- ;;
46- --version-dir)
47- VERSION_DIR="$2"
48- shift 2
49- ;;
50- --package)
51- PACKAGE="$2"
52- shift 2
53- ;;
54- --package-dir)
55- shift 2
56- ;;
57- --serial)
58- shift 2
59- ;;
60- --install-type)
61- shift 2
62- ;;
63- --feature)
64- shift 2
65- ;;
66- --chip)
67- shift 2
68- ;;
69- --filelist)
70- shift 2
71- ;;
72- --docker-root)
73- DOCKER_ROOT="$2"
74- shift 2
75- ;;
76- --recreate-softlink)
77- IS_RECREATE_SOFTLINK="y"
78- shift 1
79- ;;
80- --install-for-all)
81- INTPUT_INSTALL_FOR_ALL="y"
82- shift 1
83- ;;
84- --increment)
85- INCREMENT="y"
86- shift 1
87- ;;
88- --use-share-info)
89- USE_SHARE_INFO="y"
90- shift
91- ;;
92- -*)
93- comm_log "ERROR" "Unsupported parameters : $1"
94- exit 1
95- ;;
96- *)
97- break
98- ;;
99- esac
100-done
101- 
102-if [ "$PACKAGE" != "" ]; then
103- get_titled_package_name "TITLED_PACKAGE" "$PACKAGE"
104- set_comm_log "$TITLED_PACKAGE" "$COMM_LOGFILE"
105-fi
106- 
107-OPERATION="$1"
108- 
109-if [ "$OPERATION" = "package_installed" ]; then
110- package_installed "$CURPATH" "$VERSION" "$VERSION_DIR" "$PACKAGE" \
111- "$INTPUT_INSTALL_FOR_ALL" "$DOCKER_ROOT"
112-elif [ "$OPERATION" = "package_pre_uninstall" ]; then
113- package_pre_uninstall "$CURPATH" "$VERSION" "$VERSION_DIR" "$PACKAGE" \
114- "$INTPUT_INSTALL_FOR_ALL" "$DOCKER_ROOT"
115-elif [ "$OPERATION" = "package_uninstalled" ]; then
116- package_uninstalled "$CURPATH" "$VERSION" "$VERSION_DIR" "$PACKAGE" "$IS_RECREATE_SOFTLINK" \
117- "$DOCKER_ROOT"
118-elif [ "$OPERATION" = "package_create_softlink" ]; then
119- package_create_softlink "$CURPATH" "$VERSION" "$VERSION_DIR" "$PACKAGE" "$DOCKER_ROOT"
120-elif [ "$OPERATION" = "package_remove_softlink" ]; then
121- package_remove_softlink "$CURPATH" "$VERSION" "$VERSION_DIR" "$PACKAGE" "$DOCKER_ROOT"
122-elif [ "$OPERATION" = "create_version_softlink" ]; then
123- create_version_softlink "$CURPATH" "$VERSION_DIR"
124-elif [ "$OPERATION" = "remove_latest_softlink" ]; then
125- remove_latest_softlink "$CURPATH"
126-elif [ "$OPERATION" = "migrate_latest_data" ]; then
127- migrate_latest_data "$CURPATH"
128-fi
Dscripts/package/latest_manager/scripts/manager_func.sh+0-1719
@@ -1,1719 +0,0 @@
1-#!/bin/sh
2-# --------------------------------------------------------------------------------
3-# Copyright (c) 2025 Huawei Technologies Co., Ltd.
4-# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
5-# CANN Open Software License Agreement Version 2.0 (the "License").
6-# Please refer to the License for details. You may not use this file except in compliance with the License.
7-# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
8-# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
9-# See LICENSE in the root of the software repository for the full text of the License.
10-# --------------------------------------------------------------------------------
11- 
12-USERNAME="$(id -un)"
13-USERGROUP="$(id -gn)"
14- 
15-CREATE_VERSION_SOFTLINK="create_version_softlink.sh"
16-REMOVE_LATEST_SOFTLINK="remove_latest_softlink.sh"
17- 
18-MANAGER_INFO="$VAR_PATH/manager.info"
19- 
20-# 获取manager引用计数
21-get_manager_refs() {
22- local _outvar="$1"
23- local _var_path="$2"
24- local _result
25- 
26- if [ -f "$_var_path/manager_refs" ]; then
27- _result="$(cat "$_var_path/manager_refs")"
28- if [ "$_result" = "" ]; then
29- _result="0"
30- fi
31- else
32- _result="0"
33- fi
34- eval "${_outvar}=\"${_result}\""
35-}
36- 
37-# manager引用计数是否存在
38-# 返回值0为真,1为假
39-manager_refs_exists() {
40- local var_path="$1"
41- 
42- if [ -f "$var_path/manager_refs" ]; then
43- return 0
44- fi
45- return 1
46-}
47- 
48-# 删除manager引用计数
49-remove_manager_refs() {
50- local var_path="$1"
51- rm -f "$var_path/manager_refs"
52-}
53- 
54-# 增加manager引用计数
55-inc_manager_refs() {
56- local var_path="$1"
57- if [ -f "$var_path/manager_refs" ]; then
58- xargs expr 1 + < "$var_path/manager_refs" | (read -r arg; echo "$arg" > "$var_path/manager_refs")
59- else
60- echo "1" > "$var_path/manager_refs"
61- fi
62-}
63- 
64-# 减少manager引用计数
65-dec_manager_refs() {
66- local var_path="$1"
67- local cnt
68- 
69- if [ -f "$var_path/manager_refs" ]; then
70- xargs expr -1 + < "$var_path/manager_refs" | (read -r arg; echo "$arg" > "$var_path/manager_refs")
71- 
72- get_manager_refs cnt "$var_path"
73- if [ "$cnt" -le 0 ]; then
74- rm -f "$var_path/manager_refs"
75- fi
76- fi
77-}
78- 
79-# 获取是否需要install_for_all
80-get_install_for_all() {
81- local _outvar="$1"
82- local _result
83- 
84- if [ ! -f "$MANAGER_INFO" ]; then
85- eval "${_outvar}=\"n\""
86- return 0
87- fi
88- 
89- _result="$(grep "^install_for_all=" "$MANAGER_INFO" | cut -d= -f2-)"
90- if [ "$_result" = "" ]; then
91- eval "${_outvar}=\"n\""
92- return 0
93- fi
94- 
95- eval "${_outvar}=\"${_result}\""
96-}
97- 
98-# 过滤配置文件参数
99-filter_info_param() {
100- local name="$1"
101- local value="$2"
102- 
103- awk -v name="$name" -v value="$value" -F= '
104- BEGIN {
105- MATCHED = 0
106- OFS = "="
107- }
108- $1 == name {
109- MATCHED = 1
110- print $1, value
111- }
112- $1 != name {
113- print $0
114- }
115- END {
116- if (MATCHED == 0) {
117- print name, value
118- }
119- }
120- '
121-}
122- 
123-# 修改配置文件参数
124-modify_info_param() {
125- local filepath="$1"
126- local name="$2"
127- local value="$3"
128- local content
129- 
130- if [ ! -f "$filepath" ]; then
131- return 1
132- fi
133- 
134- content="$(cat "$filepath" | filter_info_param "$name" "$value")"
135- with_chmod "$filepath" "700" write_text "$content" "$filepath"
136-}
137- 
138-# 包创建软链事件
139-package_create_softlink() {
140- local var_path="$1"
141- local version="$2"
142- local version_dir="$3"
143- local package="$4"
144- local docker_root="$5"
145- local ret install_path pkg_running_version install_for_all
146- 
147- install_path="$(dirname "$(dirname "$var_path")")"
148- 
149- get_running_package_version "pkg_running_version" "$install_path/$LATEST_DIR" "$package"
150- ret="$?" && [ $ret -ne 0 ] && return $ret
151- 
152- if [ "$pkg_running_version" != "" ]; then
153- unpack_version_pair "version_pair_arr" "$pkg_running_version"
154- __index_list "$version_pair_arr" 0 "last_version" 1 "last_version_dir"
155- compat_del_package_softlink_in_latest "$install_path" "$package" "$last_version" "$last_version_dir" \
156- "$LATEST_DIR" "$USERNAME" "$docker_root"
157- check_ret_error "$?" "delete $package softlink in latest failed in package create softlink!"
158- ret="$?" && [ $ret -ne 0 ] && return $ret
159- fi
160- 
161- get_install_for_all "install_for_all"
162- ret="$?" && [ $ret -ne 0 ] && return $ret
163- INSTALL_FOR_ALL="$install_for_all"
164- 
165- do_create_package_softlink_to_latest "$install_path" "$package" "$version" "$version_dir" "$LATEST_DIR" \
166- "$USERNAME" "$USERGROUP" "$install_for_all" "$docker_root"
167- ret="$?" && [ $ret -ne 0 ] && return $ret
168- 
169- return 0
170-}
171- 
172-# 包安装事件
173-package_installed() {
174- local var_path="$1"
175- local version="$2"
176- local version_dir="$3"
177- local package="$4"
178- local install_for_all="$5"
179- local docker_root="$6"
180- local ret install_path pkg_running_version version_pair_arr last_version last_version_dir
181- local config_install_for_all
182- 
183- if [ "$INCREMENT" != "y" ]; then
184- inc_manager_refs "$var_path"
185- fi
186- install_path="$(dirname "$(dirname "$var_path")")"
187- 
188- create_total_create_softlink_script "$var_path/manager" "$install_path" "$version_dir" "$USERNAME" "$USERGROUP"
189- check_ret_warning "$?" "$package create total create softlink script in $version_dir in failed!"
190- 
191- create_total_remove_softlink_script "$var_path/manager" "$install_path" "$LATEST_DIR" "$USERNAME" "$USERGROUP"
192- check_ret_warning "$?" "Create total remove softlink script in $LATEST_DIR in package installed failed!"
193- 
194- create_platform_ini "$install_path" "$LATEST_DIR" "$USERNAME" "$USERGROUP"
195- check_ret_warning "$?" "Create platform.ini in $LATEST_DIR in package installed failed!"
196- 
197- get_install_for_all "config_install_for_all"
198- ret="$?" && [ $ret -ne 0 ] && return $ret
199- 
200- if [ "$install_for_all" != "$config_install_for_all" ]; then
201- ensure_file "$MANAGER_INFO" "440" "$USERNAME" "$USERGROUP" "$install_for_all"
202- ret="$?" && [ $ret -ne 0 ] && return $ret
203- 
204- modify_info_param "$MANAGER_INFO" "install_for_all" "$install_for_all"
205- ret="$?" && [ $ret -ne 0 ] && return $ret
206- fi
207- 
208- package_create_softlink "$var_path" "$version" "$version_dir" "$package" "$docker_root"
209- ret="$?" && [ $ret -ne 0 ] && return $ret
210- 
211- add_installed_package_version "$install_path/$LATEST_DIR" "$package" "$version" "$version_dir"
212- check_ret_error "$?" "Add $package installed version $version $version_dir in multi version install failed!"
213- ret="$?" && [ $ret -ne 0 ] && return $ret
214- 
215- return 0
216-}
217- 
218-# 包删除软链事件
219-package_remove_softlink() {
220- local var_path="$1"
221- local version="$2"
222- local version_dir="$3"
223- local package="$4"
224- local docker_root="$5"
225- local ret install_path is_running install_for_all
226- 
227- install_path="$(dirname "$(dirname "$var_path")")"
228- 
229- # version参数可能为空
230- # 1. 新版本升级老版本场景,断开兼容性软链时,会使用老版本的install_common_parser.sh,
231- # 调用新版本的--remove-package-latest-softlink,但不会传version参数。
232- # 2. 新版本先装,老版本后装的场景,latest目录下remove_latest_softlink.sh为老版本,
233- # 调用remove_latest_softlink.sh时,不会传version参数。
234- if [ "$version" = "" ]; then
235- get_version_by_version_dir "version" "$install_path/$LATEST_DIR" "$version_dir"
236- fi
237- 
238- get_install_for_all "install_for_all"
239- ret="$?" && [ $ret -ne 0 ] && return $ret
240- INSTALL_FOR_ALL="$install_for_all"
241- 
242- is_package_version_running "is_running" "$install_path/$LATEST_DIR" "$package" "$version" "$version_dir"
243- ret="$?" && [ $ret -ne 0 ] && return $ret
244- 
245- if [ "$is_running" = "true" ]; then
246- do_del_package_softlink_in_latest "$install_path" "$package" "$version" "$version_dir" "$LATEST_DIR" \
247- "$USERNAME" "$docker_root"
248- ret="$?" && [ $ret -ne 0 ] && return $ret
249- fi
250- 
251- return 0
252-}
253- 
254-# 包卸载前事件
255-package_pre_uninstall() {
256- return 0
257-}
258- 
259-# 包卸载事件
260-package_uninstalled() {
261- local var_path="$1"
262- local version="$2"
263- local version_dir="$3"
264- local package="$4"
265- local is_recreate_softlink="$5"
266- local docker_root="$6"
267- local install_path is_upgrade running_packages
268- 
269- install_path="$(dirname "$(dirname "$var_path")")"
270- 
271- if ! is_package_version_upgrade "is_upgrade" "$install_path/$LATEST_DIR" "$package" "$version" "$version_dir"; then
272- return 1
273- fi
274- if [ "$is_upgrade" = "true" ]; then
275- # 移除version.cfg中的upgrade配置
276- if ! unset_upgrade_package "$install_path/$LATEST_DIR" "$package"; then
277- comm_log "ERROR" "Unset $package upgrade version in package uninstalled failed!"
278- return 1
279- fi
280- fi
281- 
282- if ! del_installed_package_version "$install_path/$LATEST_DIR" "$package" "$version" "$version_dir"; then
283- comm_log "ERROR" "Del $package installed version $version $version_dir in package uninstalled failed!"
284- return 1
285- fi
286- 
287- # 删除版本目录下创建软链总脚本
288- if ! del_total_create_softlink_script "$install_path" "$LATEST_DIR" "$version" "$version_dir"; then
289- comm_log "WARNING" "Del total create softlink script in $version_dir in package uninstalled failed!"
290- fi
291- 
292- if ! version_cfg_exists "$install_path/$LATEST_DIR"; then
293- # 删除latest目录下删除软链总脚本
294- del_total_remove_softlink_script "$install_path" "$LATEST_DIR"
295- check_ret_warning "$?" "Del total remove softlink script in $LATEST_DIR in package uninstalled failed!"
296- 
297- # 删除latest目录下platform.ini
298- del_platform_ini "$install_path" "$LATEST_DIR"
299- check_ret_warning "$?" "Del platform.ini in $LATEST_DIR in package uninstalled failed!"
300- fi
301- 
302- if [ "$is_recreate_softlink" = "y" ]; then
303- # 多版本卸载时检查版本兼容性
304- if ! recreate_compatiable_softlink_in_multi_version_uninstall "$install_path" "$LATEST_DIR" "$package" \
305- "$USERNAME" "$USERGROUP" "$docker_root"; then
306- comm_log "ERROR" "Recreate ${package} compatiable softlink in package uninstalled failed!"
307- return 1
308- fi
309- fi
310- 
311- dec_manager_refs "$var_path"
312- if ! manager_refs_exists "$var_path"; then
313- rm -f "$MANAGER_INFO"
314- sh "$var_path/manager/uninstall.sh"
315- fi
316- return 0
317-}
318- 
319-# 设置版本软链接
320-create_version_softlink() {
321- local var_path="$1"
322- local version_dir="$2"
323- local install_path
324- local total_ret="0" version packages package version_pair version_pair_arr
325- local ret del_version del_version_dir install_for_all
326- 
327- install_path="$(dirname "$(dirname "$var_path")")"
328- 
329- get_version_by_version_dir "version" "$install_path/$LATEST_DIR" "$version_dir"
330- ret="$?" && [ $ret -ne 0 ] && return $ret
331- 
332- get_installed_packages_by_version_version_dir "packages" "$install_path/$LATEST_DIR" "$version" "$version_dir"
333- if [ "$packages" = "" ]; then
334- return 1
335- fi
336- 
337- get_install_for_all "install_for_all"
338- ret="$?" && [ $ret -ne 0 ] && return $ret
339- INSTALL_FOR_ALL="$install_for_all"
340- 
341- for package in ${packages}; do
342- if ! get_running_package_version "version_pair" "$install_path/$LATEST_DIR" "$package"; then
343- total_ret=1
344- continue
345- fi
346- 
347- if [ "$version_pair" != "" ]; then
348- unpack_version_pair "version_pair_arr" "$version_pair"
349- __index_list "$version_pair_arr" 0 "del_version" 1 "del_version_dir"
350- 
351- if ! do_del_package_softlink_in_latest "$install_path" "$package" "$del_version" "$del_version_dir" "$LATEST_DIR" \
352- "$USERNAME" ""; then
353- total_ret=1
354- fi
355- fi
356- 
357- if ! do_create_package_softlink_to_latest "$install_path" "$package" "$version" "$version_dir" "$LATEST_DIR" \
358- "$USERNAME" "$USERGROUP" "$install_for_all" ""; then
359- total_ret=1
360- fi
361- done
362- 
363- return $total_ret
364-}
365- 
366-# 删除运行软链接
367-remove_latest_softlink() {
368- local var_path="$1"
369- local install_path
370- local total_ret="0" ret running_packages package version_pair version_pair_arr version version_dir
371- local install_for_all
372- 
373- install_path="$(dirname "$(dirname "$var_path")")"
374- 
375- get_running_packages "running_packages" "$install_path/$LATEST_DIR"
376- ret="$?" && [ $ret -ne 0 ] && return $ret
377- 
378- if [ "${running_packages}" = "" ]; then
379- return 0
380- fi
381- 
382- get_install_for_all "install_for_all"
383- ret="$?" && [ $ret -ne 0 ] && return $ret
384- INSTALL_FOR_ALL="$install_for_all"
385- 
386- for package in ${running_packages}; do
387- if ! get_running_package_version "version_pair" "$install_path/$LATEST_DIR" "$package"; then
388- total_ret=1
389- continue
390- fi
391- 
392- unpack_version_pair "version_pair_arr" "$version_pair"
393- __index_list "$version_pair_arr" 0 "version" 1 "version_dir"
394- 
395- if ! do_del_package_softlink_in_latest "$install_path" "$package" "$version" "$version_dir" "$LATEST_DIR" \
396- "$USERNAME" ""; then
397- total_ret=1
398- fi
399- done
400- 
401- return $total_ret
402-}
403- 
404-# 创建子包软链到latest目录下
405-do_create_package_softlink_to_latest() {
406- local install_path="$1"
407- local package="$2"
408- local version="$3"
409- local version_dir="$4"
410- local latest_dir="$5"
411- local username="$6"
412- local usergroup="$7"
413- local install_for_all="$8"
414- local docker_root="$9"
415- local ret total_ret="0" install_type feature_type feature_param filelist_path install_info_path
416- local chip_type
417- 
418- check_param_not_empty "package" "need set package parameter in create package softlink to latest!"
419- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
420- 
421- _package_to_log_pkg_name "LOG_PKG_NAME" "${package}"
422- check_ret_error "$?" "Set log package name failed in create package softlink to latest!"
423- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
424- 
425- if [ ! -d "${install_path}/${latest_dir}" ]; then
426- make_dir_with_permission "${install_path}/${latest_dir}" "750" "${username}" "${usergroup}" "${install_for_all}"
427- ret="$?" && [ $ret -ne 0 ] && return $ret
428- fi
429- 
430- get_package_filelist "filelist_path" "${install_path}" "${version_dir}" "${package}"
431- get_package_install_info "install_info_path" "${install_path}" "${version_dir}" "${package}"
432- 
433- check_file_exists "${install_info_path}" "${install_info_path} doesn't exist in create package softlink to latest!"
434- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
435- 
436- get_package_install_type "install_type" "${install_info_path}" "${package}"
437- check_ret_error "$?" "Get install_type from ascend_install.info failed in create package softlink to latest!"
438- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
439- 
440- get_package_feature_type "feature_type" "${install_info_path}" "${package}" "install"
441- check_ret_error "$?" "Get feature_type from ascend_install.info failed in create package softlink to latest!"
442- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
443- 
444- get_package_chip_type "chip_type" "${install_info_path}" "${package}" "install"
445- check_ret_error "$?" "Get chip_type from ascend_install.info failed in create package softlink to latest!"
446- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
447- 
448- pack_feature_param "feature_param" "${feature_type}" "n" "${chip_type}"
449- 
450- # 创建公共目录到latest目录下的软链接
451- create_common_dirs_softlink_to_latest "${install_type}" "${install_path}" "${package}" "${version_dir}" "${latest_dir}" \
452- "${filelist_path}" "${feature_param}" "${username}" "${usergroup}" "${install_for_all}"
453- ret="$?" && [ $ret -ne 0 ] && return $ret
454- 
455- # 创建版本包到latest目录下的软链接
456- create_package_dir_softlink_to_latest "${install_path}" "${version_dir}" "${latest_dir}" "${package}" \
457- "${username}" "${usergroup}" "${install_for_all}"
458- ret="$?" && [ $ret -ne 0 ] && return $ret
459- 
460- # latest目录下公共脚本添加条目
461- add_latest_common_script "${install_path}/${latest_dir}" "${package}" "${username}" "${usergroup}" "${docker_root}"
462- ret="$?" && [ $ret -ne 0 ] && return $ret
463- 
464- # 更新version.cfg中的running配置
465- set_running_package_version "${install_path}/${latest_dir}" "${package}" "${version}" "${version_dir}"
466- check_ret_error "$?" "Set ${package} running version ${version} ${version_dir} in create package softlink to latest failed!"
467- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
468- 
469- # 更新version.cfg中的upgrade配置
470- set_upgrade_package_version "${install_path}/${latest_dir}" "${package}" "${version}" "${version_dir}"
471- check_ret_error "$?" "Set ${package} upgrade version ${version} ${version_dir} in create package softlink to latest failed!"
472- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
473- 
474- return ${total_ret}
475-}
476- 
477- 
478-# [兼容老版本]创建子包软链到latest目录下
479-compat_create_package_softlink_to_latest() {
480- local install_path="$1"
481- local package="$2"
482- local version="$3"
483- local version_dir="$4"
484- local latest_dir="$5"
485- local username="$6"
486- local usergroup="$7"
487- local install_for_all="$8"
488- local docker_root="$9"
489- local ret installer_path
490- 
491- get_package_install_common_parser "installer_path" "${install_path}" "${version_dir}" "${package}"
492- check_file_exists "${installer_path}" "${installer_path} doesn't exist in create package softlink to latest!"
493- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
494- 
495- install_options="--create-latest-softlink"
496- install_options="${install_options} --install-path=\"${install_path}\" --package=\"${package}\" --version=\"${version}\" --version-dir=\"${version_dir}\""
497- install_options="${install_options} --latest-dir=\"${latest_dir}\" --username=\"${username}\" --usergroup=\"${usergroup}\""
498- if [ "${install_for_all}" = "y" ]; then
499- install_options="${install_options} --install_for_all"
500- fi
501- if [ "${docker_root}" != "" ]; then
502- install_options="${install_options} --docker-root=\"${docker_root}\""
503- fi
504- 
505- eval sh "${installer_path}" "${install_options}"
506- check_ret_error "$?" "Create ${version_dir} ${package} softlink to latest failed!"
507- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
508- 
509- return 0
510-}
511- 
512-# 删除latest目录下子包的软链接
513-do_del_package_softlink_in_latest() {
514- local install_path="$1"
515- local package="$2"
516- local version="$3"
517- local version_dir="$4"
518- local latest_dir="$5"
519- local username="$6"
520- local docker_root="$7"
521- local ret install_type feature_type feature_param package_dirpath filelist_path install_info_path
522- local chip_type
523- 
524- _package_to_log_pkg_name "LOG_PKG_NAME" "${package}"
525- check_ret_error "$?" "Set log package name failed in del package softlink in latest!"
526- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
527- 
528- if [ ! -d "${install_path}/${latest_dir}" ]; then
529- comm_log "ERROR" "${install_path}/${latest_dir} doesn't exist in del package softlink in latest!"
530- return 1
531- fi
532- 
533- get_package_dirpath "package_dirpath" "$package"
534- filelist_path="$install_path/$version_dir/$package_dirpath/script/filelist.csv"
535- install_info_path="$install_path/$version_dir/$package_dirpath/ascend_install.info"
536- 
537- check_file_exists "${install_info_path}" "${install_info_path} doesn't exist in del package softlink in latest!"
538- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
539- 
540- # 卸载时install_type强制为full
541- install_type="full"
542- 
543- get_package_feature_type "feature_type" "${install_info_path}" "${package}" "uninstall"
544- check_ret_error "$?" "Get feature_type from ascend_install.info failed in del package softlink in latest!"
545- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
546- 
547- get_package_chip_type "chip_type" "${install_info_path}" "${package}" "uninstall"
548- check_ret_error "$?" "Get chip_type from ascend_install.info failed in del package softlink to latest!"
549- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
550- 
551- pack_feature_param "feature_param" "${feature_type}" "n" "${chip_type}"
552- 
553- # latest目录下公共脚本删除条目
554- del_latest_common_script "${install_path}/${latest_dir}" "${package}" "${username}" "${docker_root}"
555- ret="$?" && [ $ret -ne 0 ] && return $ret
556- 
557- # 删除公共目录到latest目录下的软链接
558- del_common_dirs_softlink_from_latest "${install_type}" "${install_path}" "${package}" "${version_dir}" "${latest_dir}" \
559- "${filelist_path}" "${feature_param}"
560- ret="$?" && [ $ret -ne 0 ] && return $ret
561- 
562- # 删除版本包到latest目录下的软链接
563- del_package_dir_softlink_in_latest "${install_path}" "${latest_dir}" "${package}"
564- ret="$?" && [ $ret -ne 0 ] && return $ret
565- 
566- # 删除latest下tools空目录。toolkit包创建latest软链时,latest下存在软链tools/simulator -> ${arch}-linux/simulator
567- # 通过filelist.csv文件,无法删除tools目录
568- # 解决toolkit包安装卸载latest目录下残留tools目录问题
569- remove_dir_if_empty "${install_path}/${latest_dir}/tools"
570- 
571- # 移除version.cfg中的running配置
572- unset_running_package "${install_path}/${latest_dir}" "${package}"
573- check_ret_error "$?" "Unset ${package} running version in del package softlink in latest failed!"
574- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
575- 
576- return 0
577-}
578- 
579-# [兼容老版本]删除latest目录下子包的软链接
580-compat_del_package_softlink_in_latest() {
581- local install_path="$1"
582- local package="$2"
583- local version="$3"
584- local version_dir="$4"
585- local latest_dir="$5"
586- local username="$6"
587- local docker_root="$7"
588- local ret installer_path
589- 
590- get_package_install_common_parser "installer_path" "${install_path}" "${version_dir}" "${package}"
591- 
592- check_file_exists "${installer_path}" "${installer_path} doesn't exist in del package softlink in latest!"
593- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
594- 
595- # 更新version.cfg中的upgrade配置
596- set_upgrade_package_version "${install_path}/${latest_dir}" "${package}" "${version}" "${version_dir}"
597- check_ret_error "$?" "Set ${package} upgrade version ${version} ${version_dir} in create package softlink to latest failed!"
598- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
599- 
600- install_options="--remove-latest-softlink"
601- install_options="${install_options} --install-path=\"${install_path}\" --package=\"${package}\" --version-dir=\"${version_dir}\""
602- install_options="${install_options} --latest-dir=\"${latest_dir}\" --username=\"${username}\""
603- if [ "${docker_root}" != "" ]; then
604- install_options="${install_options} --docker-root=\"${docker_root}\""
605- fi
606- 
607- eval sh "${installer_path}" "${install_options}"
608- check_ret_error "$?" "Remove ${version_dir} ${package} softlink in latest failed!"
609- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
610- 
611- return 0
612-}
613- 
614-# 获取包安装类型
615-get_package_install_type() {
616- local _outvar="$1"
617- local _install_info="$2"
618- local _package="$3"
619- local _install_type_gpit=""
620- 
621- eval "${_outvar}=\"\""
622- 
623- if [ ! -f "${_install_info}" ]; then
624- return 1
625- fi
626- 
627- _install_type_gpit="$(grep -i "^\(${_package}_\)\?install_type=" "${_install_info}" | cut -d"=" -f2-)"
628- if [ "${_install_type_gpit}" = "" ]; then
629- return 1
630- fi
631- 
632- eval "${_outvar}=\"${_install_type_gpit}\""
633-}
634- 
635-# 获取包特性参数
636-get_package_feature_type() {
637- local _outvar="$1"
638- local _install_info="$2"
639- local _package="$3"
640- local _operation="$4"
641- local _feature_type_gpft=""
642- 
643- eval "${_outvar}=\"\""
644- 
645- if [ ! -f "${_install_info}" ]; then
646- return 1
647- fi
648- 
649- if [ "${_package}" = "pto_isa" ]; then
650- _feature_type_gpft="$(grep -i "^Pto_Install_Feature=" "${_install_info}" | cut -d"=" -f2-)"
651- else
652- _feature_type_gpft="$(grep -i "^\(${_package}_\)\?feature_type=" "${_install_info}" | cut -d"=" -f2-)"
653- fi
654- if [ "${_feature_type_gpft}" = "" ]; then
655- _feature_type_gpft="all"
656- fi
657- 
658- normalize_feature "_feature_type_gpft" "${_feature_type_gpft}" "${_operation}"
659- 
660- eval "${_outvar}=\"${_feature_type_gpft}\""
661-}
662- 
663-# 获取包芯片参数
664-get_package_chip_type() {
665- local _outvar="$1"
666- local _install_info="$2"
667- local _package="$3"
668- local _operation="$4"
669- local _cihp_gpct=""
670- 
671- eval "${_outvar}=\"\""
672- 
673- if [ ! -f "${_install_info}" ]; then
674- return 1
675- fi
676- 
677- if [ "${_package}" = "pto_isa" ]; then
678- _cihp_gpct="$(grep -i "^Pto_Install_Chip=" "${_install_info}" | cut -d"=" -f2-)"
679- else
680- _cihp_gpct="$(grep -i "^\(${_package}_\)\?chip_type=" "${_install_info}" | cut -d"=" -f2-)"
681- fi
682- if [ "${_cihp_gpct}" = "" ]; then
683- _cihp_gpct="all"
684- fi
685- 
686- normalize_feature "_cihp_gpct" "${_cihp_gpct}" "${_operation}"
687- 
688- eval "${_outvar}=\"${_cihp_gpct}\""
689-}
690- 
691-# 创建latest软链接
692-create_latest_softlink() {
693- local latest_dirpath="$1"
694- local line="$2"
695- local version_dirpath="$4"
696- local target latest_filepath version_filepath softlink
697- local ret
698- 
699- __index_list "${line}" 1 "target" 4 "softlink"
700- 
701- __set_abs_path "${latest_dirpath}" "${target}" "latest_filepath"
702- __set_abs_path "${version_dirpath}" "${target}" "version_filepath"
703- 
704- if [ ! -e "$version_filepath" ]; then
705- # 如果源路径不存在,则跳过软链创建
706- return 0
707- fi
708- 
709- if [ -d "${latest_filepath}" ]; then
710- remove_dir_icp "${latest_filepath}"
711- ret="$?" && [ $ret -ne 0 ] && return $ret
712- fi
713- 
714- create_softlink_icp "-r" "${version_filepath}" "${latest_filepath}"
715- ret="$?" && [ $ret -ne 0 ] && return $ret
716- 
717- if [ "${softlink}" != "NA" ]; then
718- create_softlink_by_install_path "${latest_dirpath}" "${target}" "${softlink}"
719- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
720- fi
721- 
722- return 0
723-}
724- 
725-# 删除latest软链接
726-del_latest_softlink() {
727- local latest_dirpath="$1"
728- local line="$2"
729- local target latest_filepath softlink
730- local ret
731- 
732- __index_list "${line}" 1 "target" 4 "softlink"
733- 
734- __set_abs_path "${latest_dirpath}" "${target}" "latest_filepath"
735- 
736- if [ "${softlink}" != "NA" ]; then
737- remove_softlinks "${install_path}" "${softlink}"
738- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
739- fi
740- 
741- remove_softlink_icp "${latest_filepath}" "NA"
742- ret="$?" && [ $ret -ne 0 ] && return $ret
743- return 0
744-}
745- 
746-# 迁移latest/conf实体目录下的文件到版本conf目录下
747-migrate_conf_files_from_latest_to_version() {
748- local install_type="$1"
749- local install_path="$2"
750- local version_dir="$3"
751- local latest_dir="$4"
752- local filelist_path="$5"
753- local feature_param="$6"
754- local latest_conf_path="${install_path}/${latest_dir}/conf"
755- local version_conf_path="${install_path}/${version_dir}/conf"
756- local ret
757- 
758- if [ -d "${latest_conf_path}" ] && [ ! -L "${latest_conf_path}" ] && [ -d "${version_conf_path}" ]; then
759- foreach_filelist "filter_common_dirs" "reset_mod_dirs" "${install_type}" "${install_path}/${version_dir}" "mkdir" \
760- "${filelist_path}" "${feature_param}" "no" "normal"
761- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
762- 
763- is_dir_empty "${latest_conf_path}"
764- # 如果目录不为空,则迁移数据
765- if [ $? -ne 0 ]; then
766- mv -f "${latest_conf_path}"/* "${version_conf_path}"
767- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
768- fi
769- 
770- remove_dir_icp "${latest_conf_path}"
771- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
772- 
773- foreach_filelist "filter_common_dirs" "change_mod_and_own_dirs" "${install_type}" "${install_path}/${version_dir}" "mkdir" \
774- "${filelist_path}" "${feature_param}" "reverse" "normal"
775- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
776- fi
777- 
778- return 0
779-}
780- 
781-# 按块创建公共目录软链接到latest目录下
782-create_common_dirs_softlink_cross_version() {
783- local install_path="$1"
784- local package="$2"
785- local version_dir="$3"
786- local latest_dir="$4"
787- local blocks="$5"
788- local filelist_path install_info_path install_type feature_type chip_type feature_param
789- 
790- get_package_filelist "filelist_path" "$install_path" "$version_dir" "$package"
791- get_package_install_info "install_info_path" "$install_path" "$version_dir" "$package"
792- 
793- check_file_exists "$install_info_path" "$install_info_path doesn't exist in create common dirs softlink cross version!"
794- ret="$?" && [ $ret -ne 0 ] && return $ret
795- 
796- get_package_install_type "install_type" "$install_info_path" "$package"
797- check_ret_error "$?" "Get install_type from ascend_install.info failed in create common dirs softlink cross version!"
798- ret="$?" && [ $ret -ne 0 ] && return $ret
799- 
800- get_package_feature_type "feature_type" "$install_info_path" "$package" "install"
801- get_package_chip_type "chip_type" "$install_info_path" "$package" "install"
802- pack_feature_param "feature_param" "$feature_type" "n" "$chip_type"
803- 
804- create_common_dirs_softlink_by_blocks "$install_type" "$install_path" "$package" "$version_dir" "$latest_dir" \
805- "$filelist_path" "$feature_param" "$blocks" ""
806- check_ret_error "$?" "Create common dirs softlink by blocks failed in create common dirs softlink cross version!"
807- ret="$?" && [ $ret -ne 0 ] && return $ret
808- 
809- return 0
810-}
811- 
812-# 按块创建公共目录软链接到latest目录下
813-create_common_dirs_softlink_by_blocks() {
814- local install_type="$1"
815- local install_path="$2"
816- local package="$3"
817- local version_dir="$4"
818- local latest_dir="$5"
819- local filelist_path="$6"
820- local feature_param="$7"
821- local blocks="$8"
822- local custom_create_softlink="$9"
823- local ret package_dirpath filelist_path
824- 
825- foreach_filelist_v2 "reset_mod_dirs" "${install_type}" "${install_path}/${latest_dir}" "mkdir" \
826- "${filelist_path}" "${feature_param}" "filter_common_dirs,filter_blocks" "$blocks" "no" "normal"
827- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
828- 
829- foreach_filelist_v2 "create_dirs" "${install_type}" "${install_path}/${latest_dir}" "mkdir" \
830- "${filelist_path}" "${feature_param}" "filter_common_dirs,filter_blocks" "$blocks" "no" "normal"
831- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
832- 
833- foreach_filelist_v2 "create_latest_softlink" "${install_type}" "${install_path}/${latest_dir}" "copy copy_entity move" \
834- "${filelist_path}" "${feature_param}" "filter_common_dirs,filter_blocks" "$blocks" "no" "normal" "${install_path}/${version_dir}"
835- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
836- 
837- # 调用脚本创建自定义内容软链接到latest目录下
838- if [ "$custom_create_softlink" != "" ] && [ -f "$custom_create_softlink" ]; then
839- chmod u+x "${custom_create_softlink}"
840- "${custom_create_softlink}" --install-path="${install_path}" --version-dir="${version_dir}" --latest-dir="${latest_dir}"
841- check_ret_error "$?" "Run ${package} custom create ${version_dir} softlink failed!"
842- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
843- fi
844- 
845- foreach_filelist_v2 "change_mod_and_own_dirs" "${install_type}" "${install_path}/${latest_dir}" "mkdir" \
846- "${filelist_path}" "${feature_param}" "filter_common_dirs,filter_blocks" "$blocks" "reverse" "normal"
847- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
848- 
849- return 0
850-}
851- 
852-# 创建公共目录软链接到latest目录下
853-create_common_dirs_softlink_to_latest() {
854- local install_type="$1"
855- local install_path="$2"
856- local package="$3"
857- local version_dir="$4"
858- local latest_dir="$5"
859- local filelist_path="$6"
860- local feature_param="$7"
861- local username="$8"
862- local usergroup="$9"
863- local install_for_all="${10}"
864- local custom_create_softlink="${install_path}/${version_dir}/${package}/script/${package}_custom_create_softlink.sh"
865- local db_info_path="$install_path/$latest_dir/var/ascend_package_db.info"
866- local ret db_info
867- 
868- migrate_conf_files_from_latest_to_version "${install_type}" "${install_path}" "${version_dir}" "${latest_dir}" \
869- "${filelist_path}" "${feature_param}"
870- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
871- 
872- ensure_file "$db_info_path" "440" "$username" "$usergroup" "$install_for_all"
873- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
874- 
875- db_info="$(
876- (
877- cat "$db_info_path"
878- printf "\n" # 防止db.info缺失末尾换行符
879- all_common_dirs_blocks_in_filelist "$install_type" "$filelist_path" "$feature_param" \
880- | blocks_to_db_item "$package" "$version_dir"
881- ) | remove_blank_line | sort_1 "|" | fold_2 "|" ""
882- )"
883- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
884- 
885- # 注意换行符
886- (
887- cat "$db_info_path" | remove_blank_line | remain_db_last_item
888- printf "%s\n" "$db_info" | remove_blank_line | remain_db_last_item
889- ) | sort_1 | fold_3_keep_2 "" " " | show_diff_3_4 | show_min_nf "4" | select_fields_3_2_1 | sort_1 | fold_3_keep_2 "" " " \
890- | (
891- total_ret=0
892- while read -r tmp_version_dir tmp_package tmp_blocks; do
893- del_common_dirs_softlink_cross_version "$install_path" "$tmp_package" "$tmp_version_dir" "$latest_dir" \
894- "EngineeringCommon $tmp_blocks"
895- ret="$?" && [ $ret -ne 0 ] && total_ret="1"
896- done
897- exit $total_ret
898- )
899- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
900- 
901- # 安装时总是创建全部块的软链接
902- create_common_dirs_softlink_by_blocks "$install_type" "$install_path" "$package" "$version_dir" "$latest_dir" \
903- "$filelist_path" "$feature_param" "" "$custom_create_softlink"
904- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
905- 
906- with_chmod "$db_info_path" "700" write_text "$db_info" "$db_info_path"
907- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
908- 
909- return 0
910-}
911- 
912- 
913-# 跨版本按块删除公共目录到latest目录下的软链接
914-del_common_dirs_softlink_cross_version() {
915- local install_path="$1"
916- local package="$2"
917- local version_dir="$3"
918- local latest_dir="$4"
919- local blocks="$5"
920- local filelist_path install_info_path install_type feature_type chip_type feature_param
921- 
922- get_package_filelist "filelist_path" "$install_path" "$version_dir" "$package"
923- get_package_install_info "install_info_path" "$install_path" "$version_dir" "$package"
924- 
925- check_file_exists "$install_info_path" "$install_info_path doesn't exist in del common dirs softlink cross version!"
926- ret="$?" && [ $ret -ne 0 ] && return $ret
927- 
928- get_package_install_type "install_type" "$install_info_path" "$package"
929- check_ret_error "$?" "Get install_type from ascend_install.info failed in del common dirs softlink cross version!"
930- ret="$?" && [ $ret -ne 0 ] && return $ret
931- 
932- get_package_feature_type "feature_type" "$install_info_path" "$package" "uninstall"
933- get_package_chip_type "chip_type" "$install_info_path" "$package" "uninstall"
934- pack_feature_param "feature_param" "$feature_type" "n" "$chip_type"
935- 
936- del_common_dirs_softlink_by_blocks "$install_type" "$install_path" "$package" "$version_dir" "$latest_dir" \
937- "$filelist_path" "$feature_param" "$blocks" ""
938- check_ret_error "$?" "Del common dirs softlink by blocks failed in del common dirs softlink cross version!"
939- ret="$?" && [ $ret -ne 0 ] && return $ret
940- 
941- return 0
942-}
943- 
944-# 按块删除公共目录到latest目录下的软链接
945-del_common_dirs_softlink_by_blocks() {
946- local install_type="$1"
947- local install_path="$2"
948- local package="$3"
949- local version_dir="$4"
950- local latest_dir="$5"
951- local filelist_path="$6"
952- local feature_param="$7"
953- local blocks="$8"
954- local custom_remove_softlink="$9"
955- local ret package_dirpath filelist_path
956- 
957- create_stash_mod "${install_path}/${latest_dir}"
958- ret="$?" && [ $ret -ne 0 ] && return $ret
959- 
960- foreach_filelist_v2 "reset_mod_dirs_with_stash_mod" "${install_type}" "${install_path}/${latest_dir}" "mkdir" \
961- "${filelist_path}" "${feature_param}" "filter_common_dirs,filter_blocks" "$blocks" "no" "normal"
962- ret="$?" && [ $ret -ne 0 ] && return $ret
963- 
964- # 调用脚本删除latest目录下自定义内容软链接
965- if [ "$custom_remove_softlink" != "" ] && [ -f "$custom_remove_softlink" ]; then
966- chmod u+x "${custom_remove_softlink}"
967- "${custom_remove_softlink}" --install-path="${install_path}" --version-dir="${version_dir}" --latest-dir="${latest_dir}"
968- check_ret_error "$?" "Run ${package} custom remove ${version_dir} softlink failed!"
969- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
970- fi
971- 
972- foreach_filelist_v2 "del_latest_softlink" "${install_type}" "${install_path}/${latest_dir}" "copy copy_entity move" \
973- "${filelist_path}" "${feature_param}" "filter_common_dirs,filter_blocks" "$blocks" "no" "normal"
974- ret="$?" && [ $ret -ne 0 ] && return $ret
975- 
976- foreach_filelist_v2 "remove_install_dirs" "${install_type}" "${install_path}/${latest_dir}" "mkdir" \
977- "${filelist_path}" "${feature_param}" "filter_common_dirs,filter_blocks" "$blocks" "reverse" "normal"
978- ret="$?" && [ $ret -ne 0 ] && return $ret
979- 
980- foreach_stashmod "restore_stash_mod" "${install_path}/${latest_dir}" "reverse"
981- ret="$?" && [ $ret -ne 0 ] && return $ret
982- 
983- remove_stash_mod "${install_path}/${latest_dir}"
984- ret="$?" && [ $ret -ne 0 ] && return $ret
985- 
986- return 0
987-}
988- 
989-# 删除公共目录到latest目录下的软链接
990-del_common_dirs_softlink_from_latest() {
991- local install_type="$1"
992- local install_path="$2"
993- local package="$3"
994- local version_dir="$4"
995- local latest_dir="$5"
996- local filelist_path="$6"
997- local feature_param="$7"
998- local custom_remove_softlink="${install_path}/${version_dir}/${package}/script/${package}_custom_remove_softlink.sh"
999- local db_info_path="$install_path/$latest_dir/var/ascend_package_db.info"
1000- local ret db_info_origin db_info diff_result blocks_to_remove
1001- 
1002- if [ -f "$db_info_path" ]; then
1003- db_info_origin="$(cat "$db_info_path")""\n"
1004- else
1005- db_info_origin=""
1006- fi
1007- 
1008- db_info="$(printf "$db_info_origin" | del_db_items "$package" "$version_dir")"
1009- ret="$?" && [ $ret -ne 0 ] && return $ret
1010- 
1011- # 注意换行符和次序
1012- diff_result="$(
1013- (
1014- printf "%s\n" "$db_info" | remove_blank_line | remain_db_last_item
1015- printf "$db_info_origin" | remove_blank_line | remain_db_last_item
1016- ) | sort_1 | fold_3_keep_2 "" " " | show_diff_3_4
1017- )"
1018- blocks_to_remove="$(printf "%s\n" "$diff_result" | select_fields_1 | xargs)"
1019- 
1020- # blocks_to_remove可以为空
1021- del_common_dirs_softlink_by_blocks "$install_type" "$install_path" "$package" "$version_dir" "$latest_dir" \
1022- "$filelist_path" "$feature_param" "EngineeringCommon $blocks_to_remove" "$custom_remove_softlink"
1023- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
1024- 
1025- printf "%s\n" "$diff_result" | show_min_nf "4" | select_fields_3_2_1 | sort_1 | fold_3_keep_2 "" " " \
1026- | (
1027- total_ret=0
1028- while read -r tmp_version_dir tmp_package tmp_blocks; do
1029- create_common_dirs_softlink_cross_version "$install_path" "$tmp_package" "$tmp_version_dir" "$latest_dir" \
1030- "EngineeringCommon $tmp_blocks"
1031- ret="$?" && [ $ret -ne 0 ] && total_ret="1"
1032- done
1033- exit $total_ret
1034- )
1035- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
1036- 
1037- if [ "$db_info" != "" ]; then
1038- with_chmod "$db_info_path" "700" write_text "$db_info" "$db_info_path"
1039- else
1040- rm -f "$db_info_path"
1041- fi
1042- ret="$?" && [ $ret -ne 0 ] && return $ret
1043- 
1044- return 0
1045-}
1046- 
1047-# latest目录下公共脚本添加条目
1048-add_latest_common_script() {
1049- local latest_path="$1"
1050- local package="$2"
1051- local username="$3"
1052- local usergroup="$4"
1053- local docker_root="$5"
1054- local ret mod
1055- 
1056- # 保存bin目录的权限
1057- get_file_mod "mod" "-L" "${latest_path}/bin"
1058- 
1059- # 给latest/bin目录提升权限到750
1060- chmod "750" "${latest_path}/bin"
1061- ret="$?" && [ $ret -ne 0 ] && return $ret
1062- 
1063- # 设置setenv
1064- add_setenv "${latest_path}" "${package}" "NA" "${username}" "${usergroup}" "true" "${docker_root}"
1065- ret="$?" && [ $ret -ne 0 ] && return $ret
1066- 
1067- # 给latest目录添加prereq_check条目
1068- add_prereq_check "${latest_path}" "${package}" "${username}" "${usergroup}" "${docker_root}"
1069- ret="$?" && [ $ret -ne 0 ] && return $ret
1070- 
1071- # 恢复bin目录的权限
1072- chmod "${mod}" "${latest_path}/bin"
1073- ret="$?" && [ $ret -ne 0 ] && return $ret
1074- 
1075- return 0
1076-}
1077- 
1078-# latest目录下公共脚本删除条目
1079-del_latest_common_script() {
1080- local latest_path="$1"
1081- local package="$2"
1082- local username="$3"
1083- local docker_root="$4"
1084- local ret
1085- 
1086- # 保存bin目录的权限
1087- get_file_mod "mod" "-L" "${latest_path}/bin"
1088- 
1089- # 给latest/bin目录提升权限到750
1090- chmod "750" "${latest_path}/bin"
1091- ret="$?" && [ $ret -ne 0 ] && return $ret
1092- 
1093- # unsetenv
1094- del_setenv "${latest_path}" "${package}" "${username}" "${docker_root}"
1095- ret="$?" && [ $ret -ne 0 ] && return $ret
1096- 
1097- # 给latest目录删除prereq_check条目
1098- del_prereq_check "${latest_path}" "${package}" "${docker_root}"
1099- ret="$?" && [ $ret -ne 0 ] && return $ret
1100- 
1101- # 恢复bin目录的权限
1102- chmod "${mod}" "${latest_path}/bin"
1103- ret="$?" && [ $ret -ne 0 ] && return $ret
1104- 
1105- return 0
1106-}
1107- 
1108-# 文件中添加一个空行
1109-add_blank_line_to_file() {
1110- local filepath="$1"
1111- 
1112- echo >> "${filepath}"
1113-}
1114- 
1115-# 总设置软链脚本是否存在
1116-# 返回值0为真,1为假
1117-total_create_softlink_script_exists() {
1118- local install_path="$1"
1119- local version_dir="$2"
1120- 
1121- if [ -f "$install_path/$version_dir/$CREATE_VERSION_SOFTLINK" ]; then
1122- return 0
1123- fi
1124- return 1
1125-}
1126- 
1127-# 创建总设置软链脚本
1128-create_total_create_softlink_script() {
1129- local script_dir="$1"
1130- local install_path="$2"
1131- local version_dir="$3"
1132- local username="$4"
1133- local usergroup="$5"
1134- local script_filepath="$install_path/$version_dir/$CREATE_VERSION_SOFTLINK"
1135- local ret
1136- 
1137- rm -f "$script_filepath"
1138- cat "$script_dir/common_installer.inc" > "$script_filepath"
1139- ret="$?" && [ $ret -ne 0 ] && return $ret
1140- 
1141- add_blank_line_to_file "$script_filepath"
1142- ret="$?" && [ $ret -ne 0 ] && return $ret
1143- 
1144- sed -n '/^## module log/,/^## end module/ p' "$script_dir/common_func.inc" >> "$script_filepath"
1145- ret="$?" && [ $ret -ne 0 ] && return $ret
1146- 
1147- add_blank_line_to_file "$script_filepath"
1148- ret="$?" && [ $ret -ne 0 ] && return $ret
1149- 
1150- echo "LATEST_DIR=\"$LATEST_DIR\"" >> "$script_filepath"
1151- ret="$?" && [ $ret -ne 0 ] && return $ret
1152- 
1153- add_blank_line_to_file "$script_filepath"
1154- ret="$?" && [ $ret -ne 0 ] && return $ret
1155- 
1156- echo "notify_latest_manager_create_version_softlink" >> "$script_filepath"
1157- ret="$?" && [ $ret -ne 0 ] && return $ret
1158- 
1159- change_mod "$script_filepath" "550" ""
1160- ret="$?" && [ $ret -ne 0 ] && return $ret
1161- 
1162- change_own "$script_filepath" "${username}:${usergroup}"
1163- ret="$?" && [ $ret -ne 0 ] && return $ret
1164- 
1165- 
1166- return 0
1167-}
1168- 
1169-# 总删除软链脚本是否存在
1170-# 返回值0为真,1为假
1171-total_remove_softlink_script_exists() {
1172- local install_path="$1"
1173- local latest_dir="$2"
1174- 
1175- if [ -f "$install_path/$latest_dir/$REMOVE_LATEST_SOFTLINK" ]; then
1176- return 0
1177- fi
1178- return 1
1179-}
1180- 
1181-# 创建总删除软链脚本
1182-create_total_remove_softlink_script() {
1183- local script_dir="$1"
1184- local install_path="$2"
1185- local latest_dir="$3"
1186- local username="$4"
1187- local usergroup="$5"
1188- local script_filepath="$install_path/$latest_dir/$REMOVE_LATEST_SOFTLINK"
1189- local ret
1190- 
1191- rm -f "$script_filepath"
1192- cat "$script_dir/common_installer.inc" > "$script_filepath"
1193- ret="$?" && [ $ret -ne 0 ] && return $ret
1194- 
1195- add_blank_line_to_file "$script_filepath"
1196- ret="$?" && [ $ret -ne 0 ] && return $ret
1197- 
1198- sed -n '/^## module log/,/^## end module/ p' "$script_dir/common_func.inc" >> "$script_filepath"
1199- ret="$?" && [ $ret -ne 0 ] && return $ret
1200- 
1201- add_blank_line_to_file "$script_filepath"
1202- ret="$?" && [ $ret -ne 0 ] && return $ret
1203- 
1204- echo "LATEST_DIR=\"$LATEST_DIR\"" >> "$script_filepath"
1205- ret="$?" && [ $ret -ne 0 ] && return $ret
1206- 
1207- add_blank_line_to_file "$script_filepath"
1208- ret="$?" && [ $ret -ne 0 ] && return $ret
1209- 
1210- echo "notify_latest_manager_remove_latest_softlink" >> "$script_filepath"
1211- ret="$?" && [ $ret -ne 0 ] && return $ret
1212- 
1213- change_mod "$script_filepath" "550" ""
1214- ret="$?" && [ $ret -ne 0 ] && return $ret
1215- 
1216- change_own "$script_filepath" "${username}:${usergroup}"
1217- ret="$?" && [ $ret -ne 0 ] && return $ret
1218- 
1219- return 0
1220-}
1221- 
1222-# 删除总创建软链脚本
1223-del_total_create_softlink_script() {
1224- local install_path="$1"
1225- local latest_dir="$2"
1226- local version="$3"
1227- local version_dir="$4"
1228- local script_filepath="${install_path}/${version_dir}/${CREATE_VERSION_SOFTLINK}"
1229- local ret version_cfg_path packages
1230- 
1231- get_version_cfg_path "version_cfg_path" "${install_path}/${latest_dir}"
1232- if [ ! -f "${version_cfg_path}" ]; then
1233- remove_file "${script_filepath}" "NA"
1234- ret="$?" && [ $ret -ne 0 ] && return $ret
1235- 
1236- return 0
1237- fi
1238- 
1239- get_installed_packages_by_version_version_dir "packages" "${install_path}/${latest_dir}" "${version}" "${version_dir}"
1240- if [ "${packages}" = "" ]; then
1241- remove_file "${script_filepath}" "NA"
1242- ret="$?" && [ $ret -ne 0 ] && return $ret
1243- fi
1244- 
1245- return 0
1246-}
1247- 
1248-# 删除总删除软链脚本
1249-del_total_remove_softlink_script() {
1250- local install_path="$1"
1251- local latest_dir="$2"
1252- local script_filepath="${install_path}/${latest_dir}/${REMOVE_LATEST_SOFTLINK}"
1253- local ret version_cfg_path
1254- 
1255- get_version_cfg_path "version_cfg_path" "${install_path}/${latest_dir}"
1256- 
1257- if [ ! -f "${version_cfg_path}" ]; then
1258- remove_file "${script_filepath}" "NA"
1259- ret="$?" && [ $ret -ne 0 ] && return $ret
1260- fi
1261- 
1262- return 0
1263-}
1264- 
1265-# platform.ini配置是否存在
1266-# 返回值0为真,1为假
1267-platform_ini_exists() {
1268- local install_path="$1"
1269- local latest_dir="$2"
1270- 
1271- if [ -f "$install_path/$latest_dir/platform.ini" ]; then
1272- return 0
1273- fi
1274- return 1
1275-}
1276- 
1277-# 创建platform.ini配置
1278-create_platform_ini() {
1279- local install_path="$1"
1280- local latest_dir="$2"
1281- local username="$3"
1282- local usergroup="$4"
1283- local config_dirpath="${install_path}/${latest_dir}"
1284- local config_filepath="${config_dirpath}/platform.ini"
1285- local ret mod
1286- 
1287- # 配置文件存在则跳过
1288- if [ -f "${config_filepath}" ]; then
1289- return 0
1290- fi
1291- 
1292- # 保存当前目录的权限
1293- get_file_mod "mod" -L "${config_dirpath}"
1294- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
1295- 
1296- # 恢复权限
1297- cleanup="chmod ${mod} \"${config_dirpath}\""
1298- 
1299- # 添加目录写权限
1300- chmod u+w "${config_dirpath}"
1301- cleanup_if_error "$?" "${cleanup}"
1302- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
1303- 
1304- touch "${config_filepath}"
1305- cleanup_if_error "$?" "${cleanup}"
1306- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
1307- 
1308- change_mod "${config_filepath}" "660" ""
1309- cleanup_if_error "$?" "${cleanup}"
1310- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
1311- 
1312- change_own "${config_filepath}" "${username}:${usergroup}"
1313- cleanup_if_error "$?" "${cleanup}"
1314- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
1315- 
1316- eval "${cleanup}"
1317- 
1318- return 0
1319-}
1320- 
1321-# 删除platform.ini配置
1322-del_platform_ini() {
1323- local install_path="$1"
1324- local latest_dir="$2"
1325- local config_dirpath="${install_path}/${latest_dir}"
1326- local config_filepath="${config_dirpath}/platform.ini"
1327- local ret mod
1328- 
1329- if [ ! -f "${config_filepath}" ]; then
1330- return 0
1331- fi
1332- 
1333- # 保存当前目录的权限
1334- get_file_mod "mod" -L "${config_dirpath}"
1335- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
1336- 
1337- # 恢复权限
1338- cleanup="chmod ${mod} \"${config_dirpath}\""
1339- 
1340- # 添加目录写权限
1341- chmod u+w "${config_dirpath}"
1342- cleanup_if_error "$?" "${cleanup}"
1343- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
1344- 
1345- remove_file "${config_filepath}" "NA"
1346- cleanup_if_error "$?" "${cleanup}"
1347- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
1348- 
1349- eval "${cleanup}"
1350- 
1351- return 0
1352-}
1353- 
1354-# 获取aicpu创建软链脚本路径
1355-get_aicpu_custom_create_softlink_path() {
1356- local _outvar="$1"
1357- local _install_path="$2"
1358- local _version_dir="$3"
1359- local _chip_name="$4"
1360- 
1361- eval "${_outvar}=\"${_install_path}/${_version_dir}/pto_isa/${_chip_name}/aicpu/script/aicpu_custom_create_softlink.sh\""
1362-}
1363- 
1364-# 重建软链时处理aicpu软链
1365-deal_with_aicpu_package() {
1366- local install_path="$1"
1367- local version_dir="$2"
1368- local latest_dir="$3"
1369- local chip_name
1370- 
1371- for chip_name in "Ascend910" "Ascend310P" "Ascend310" "Ascend310RC" "Ascend"; do
1372- get_aicpu_custom_create_softlink_path "aicpu_script" "${install_path}" "${version_dir}" "${chip_name}"
1373- if [ -f "${aicpu_script}" ]; then
1374- # 创建软链时,支持强制覆盖老版本软链。
1375- # 因为存在这样的场景:先卸载新版本的runtime包,再卸载新版本的aicpu_kernels包,
1376- # 卸载新版本的runtime包时触发该流程,此时老版本的aicpu_kernels包软链还在存在于环境上。
1377- "${aicpu_script}" --install-path="${install_path}" --version-dir="${version_dir}" --latest-dir="${latest_dir}"
1378- # 忽略aicpu创建软链报错
1379- fi
1380- done
1381- 
1382- return 0
1383-}
1384- 
1385-# 恢复可兼容的子包软链接
1386-recreate_compatiable_softlink_sibling_package() {
1387- local package="$1"
1388- 
1389- # 判断是否有本包的running版本包
1390- get_running_package_version "version_pair" "$scope_install_path/$scope_latest_dir" "$package"
1391- ret="$?" && [ $ret -ne 0 ] && return $ret
1392- 
1393- if [ "$version_pair" != "" ]; then
1394- return 0
1395- fi
1396- 
1397- if check_current_package_compatiable "$scope_install_path" "$scope_version" "$scope_version_dir" "$package"; then
1398- compat_create_package_softlink_to_latest "$scope_install_path" "$package" "$scope_version" "$scope_version_dir" "$scope_latest_dir" \
1399- "$scope_username" "$scope_usergroup" "$scope_install_for_all" "$scope_docker_root"
1400- ret="$?" && [ $ret -ne 0 ] && return $ret
1401- fi
1402- 
1403- return 0
1404-}
1405- 
1406-# 多版本卸载时检查版本兼容性
1407-recreate_compatiable_softlink_in_multi_version_uninstall() {
1408- local scope_install_path="$1"
1409- local scope_latest_dir="$2"
1410- local package="$3"
1411- local scope_username="$4"
1412- local scope_usergroup="$5"
1413- local scope_docker_root="$6"
1414- local ret installed_versions version_pair version_pair_arr scope_version scope_version_dir
1415- local scope_install_for_all version_cfg_path sibling_package
1416- 
1417- # 切换目录避免使用uninstall.sh脚本,重建子包软链接时,找到不当前路径问题
1418- # sh: 0: getcwd() failed: No such file or directory
1419- cd "${scope_install_path}"
1420- 
1421- # 判断是否有本包的running版本包
1422- get_running_package_version "version_pair" "${scope_install_path}/${scope_latest_dir}" "${package}"
1423- ret="$?" && [ $ret -ne 0 ] && return $ret
1424- 
1425- if [ "${version_pair}" != "" ]; then
1426- return 0
1427- fi
1428- 
1429- # 枚举version.cfg中本包installed版本
1430- get_installed_package_versions "installed_versions" "${scope_install_path}/${scope_latest_dir}" "${package}"
1431- ret="$?" && [ $ret -ne 0 ] && return $ret
1432- 
1433- get_install_for_all "scope_install_for_all"
1434- ret="$?" && [ $ret -ne 0 ] && return $ret
1435- INSTALL_FOR_ALL="$scope_install_for_all"
1436- 
1437- for version_pair in ${installed_versions}; do
1438- unpack_version_pair "version_pair_arr" "${version_pair}"
1439- __index_list "${version_pair_arr}" 0 "scope_version" 1 "scope_version_dir"
1440- 
1441- if check_current_package_compatiable "$scope_install_path" "$scope_version" "$scope_version_dir" "$package"; then
1442- compat_create_package_softlink_to_latest "${scope_install_path}" "${package}" "${scope_version}" "${scope_version_dir}" "${scope_latest_dir}" \
1443- "${scope_username}" "${scope_usergroup}" "${scope_install_for_all}" "${scope_docker_root}"
1444- ret="$?" && [ $ret -ne 0 ] && return $ret
1445- 
1446- # 恢复同版本下可兼容的其他子包软链接
1447- get_version_cfg_path "version_cfg_path" "$scope_latest_dir"
1448- grep -F "installed_version=" "$version_cfg_path" | grep -F "[$scope_version:$scope_version_dir]" | cut -d= -f1 | sed 's/_installed_version$//' | while read sibling_package; do
1449- if [ "$sibling_package" != "$package" ]; then
1450- recreate_compatiable_softlink_sibling_package "$sibling_package"
1451- fi
1452- done
1453- 
1454- if [ "$package" = "runtime" ]; then
1455- deal_with_aicpu_package "${scope_install_path}" "${scope_version_dir}" "${scope_latest_dir}"
1456- ret="$?" && [ $ret -ne 0 ] && return $ret
1457- fi
1458- 
1459- break
1460- fi
1461- done
1462- 
1463- return 0
1464-}
1465- 
1466-# 检查包版本兼容性
1467-check_package_compatiable() {
1468- local install_path="$1"
1469- local version_left="$2"
1470- local version_dir_left="$3"
1471- local package_left="$4"
1472- local version_right="$5"
1473- local version_dir_right="$6"
1474- local package_right="$7"
1475- local script_dir="$8"
1476- local pkg_version_info_path_left pkg_version_info_path_right
1477- local required_left required_right
1478- 
1479- get_package_version_info "pkg_version_info_path_left" "$install_path" "$version_dir_left" "$package_left"
1480- get_package_version_info "pkg_version_info_path_right" "$install_path" "$version_dir_right" "$package_right"
1481- 
1482- if ! _get_required_package_info "required_left" "$pkg_version_info_path_right" "$package_left"; then
1483- return 2
1484- fi
1485- if ! _get_required_package_info "required_right" "$pkg_version_info_path_left" "$package_right"; then
1486- return 2
1487- fi
1488- 
1489- if [ "$required_left" = "" ] && [ "$required_right" = "" ]; then
1490- return 0
1491- fi
1492- 
1493- if _check_version_required "$version_left" "$required_left" "$script_dir" || _check_version_required "$version_right" "$required_right" "$script_dir"; then
1494- return 0
1495- fi
1496- return 1
1497-}
1498- 
1499-# 检查当前包与latest版本兼容性
1500-check_current_package_compatiable() {
1501- local install_path="$1"
1502- local current_version="$2"
1503- local current_version_dir="$3"
1504- local current_package="$4"
1505- local script_dir="$install_path/$LATEST_DIR/var/manager"
1506- local ret running_packages package version_pair version version_dir
1507- 
1508- get_running_packages "running_packages" "$install_path/$LATEST_DIR"
1509- for package in ${running_packages}; do
1510- if [ "$current_package" = "$package" ]; then
1511- continue
1512- fi
1513- 
1514- get_running_package_version "version_pair" "$install_path/$LATEST_DIR" "$package"
1515- ret="$?" && [ $ret -ne 0 ] && return $ret
1516- 
1517- unpack_version_pair "version_pair_arr" "$version_pair"
1518- __index_list "$version_pair_arr" 0 "version" 1 "version_dir"
1519- 
1520- if ! check_package_compatiable "$install_path" "$current_version" "$current_version_dir" "$current_package" \
1521- "$version" "$version_dir" "$package" "$script_dir"; then
1522- return 1
1523- fi
1524- done
1525- 
1526- return 0
1527-}
1528- 
1529-# 多版本安装时检查版本兼容性
1530-check_compatiable_in_multi_version_install() {
1531- local install_path="$1"
1532- local username="$2"
1533- local docker_root="$3"
1534- local current_version="$4"
1535- local current_version_dir="$5"
1536- local current_package="$6"
1537- local script_dir="$install_path/$LATEST_DIR/var/manager"
1538- local ret running_packages package version_pair version version_dir
1539- 
1540- get_running_packages "running_packages" "$install_path/$LATEST_DIR"
1541- for package in ${running_packages}; do
1542- if [ "$current_package" = "$package" ]; then
1543- continue
1544- fi
1545- 
1546- get_running_package_version "version_pair" "$install_path/$LATEST_DIR" "$package"
1547- ret="$?" && [ $ret -ne 0 ] && return $ret
1548- 
1549- unpack_version_pair "version_pair_arr" "$version_pair"
1550- __index_list "$version_pair_arr" 0 "version" 1 "version_dir"
1551- 
1552- if ! check_package_compatiable "$install_path" "$current_version" "$current_version_dir" "$current_package" \
1553- "$version" "$version_dir" "$package" "$script_dir"; then
1554- compat_del_package_softlink_in_latest "$install_path" "$package" "$version" "$version_dir" \
1555- "$LATEST_DIR" "$username" "$docker_root"
1556- ret="$?" && [ $ret -ne 0 ] && return $ret
1557- fi
1558- done
1559- 
1560- return 0
1561-}
1562- 
1563-# 创建版本包目录软链接到latest目录下
1564-create_package_dir_softlink_to_latest() {
1565- local install_path="$1"
1566- local version_dir="$2"
1567- local latest_dir="$3"
1568- local package="$4"
1569- local username="$5"
1570- local usergroup="$6"
1571- local install_for_all="$7"
1572- local ret package_dirpath package_prefix
1573- 
1574- get_package_dirpath "package_dirpath" "${package}"
1575- package_prefix="$(dirname "${package_dirpath}")"
1576- 
1577- if [ ! -d "${install_path}/${version_dir}/${package_dirpath}" ]; then
1578- return 0
1579- fi
1580- 
1581- if [ ! -d "${install_path}/${latest_dir}/${package_prefix}" ]; then
1582- make_dir_with_permission "${install_path}/${latest_dir}/${package_prefix}" "750" "${username}" "${usergroup}" "${install_for_all}"
1583- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
1584- fi
1585- 
1586- create_softlink_icp "-r" "${install_path}/${version_dir}/${package_dirpath}" "${install_path}/${latest_dir}/${package_dirpath}"
1587- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
1588- 
1589- return 0
1590-}
1591- 
1592-# 删除latest目录下版本包目录的软链接
1593-del_package_dir_softlink_in_latest() {
1594- local install_path="$1"
1595- local latest_dir="$2"
1596- local package="$3"
1597- local ret package_dirpath package_prefix
1598- 
1599- get_package_dirpath "package_dirpath" "${package}"
1600- package_prefix="$(dirname "${package_dirpath}")"
1601- 
1602- remove_softlink_icp "${install_path}/${latest_dir}/${package_dirpath}"
1603- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
1604- 
1605- if [ "${package_prefix}" != "." ]; then
1606- remove_dir_if_empty "${install_path}/${latest_dir}/${package_prefix}"
1607- ret="$?" && [ ${ret} -ne 0 ] && return ${ret}
1608- fi
1609- 
1610- return 0
1611-}
1612- 
1613-# 生成包db.info条目列表
1614-packages_db_items() {
1615- local install_path="$1"
1616- local running_packages="$2"
1617- local total_ret=0 ret running_packages package version_pair version_pair_arr version version_dir
1618- local install_type feature_type feature_param filelist_path install_info_path
1619- 
1620- for package in $running_packages; do
1621- if ! get_running_package_version "version_pair" "$install_path/$LATEST_DIR" "$package"; then
1622- total_ret=1
1623- continue
1624- fi
1625- 
1626- unpack_version_pair "version_pair_arr" "$version_pair"
1627- __index_list "$version_pair_arr" 0 "version" 1 "version_dir"
1628- 
1629- get_package_filelist "filelist_path" "$install_path" "$version_dir" "$package"
1630- get_package_install_info "install_info_path" "$install_path" "$version_dir" "$package"
1631- 
1632- check_file_exists "$install_info_path" "$install_info_path doesn't exist in create package softlink to latest!"
1633- ret="$?" && [ $ret -ne 0 ] && total_ret=1 && continue
1634- 
1635- get_package_install_type "install_type" "$install_info_path" "$package"
1636- check_ret_error "$?" "Get install_type from ascend_install.info failed in create package softlink to latest!"
1637- ret="$?" && [ $ret -ne 0 ] && total_ret=1 && continue
1638- 
1639- get_package_feature_type "feature_type" "$install_info_path" "$package" "install"
1640- check_ret_error "$?" "Get feature_type from ascend_install.info failed in create package softlink to latest!"
1641- ret="$?" && [ $ret -ne 0 ] && total_ret=1 && continue
1642- 
1643- get_package_chip_type "chip_type" "$install_info_path" "$package" "install"
1644- check_ret_error "$?" "Get chip_type from ascend_install.info failed in create package softlink to latest!"
1645- ret="$?" && [ $ret -ne 0 ] && total_ret=1 && continue
1646- 
1647- pack_feature_param "feature_param" "${feature_type}" "n" "${chip_type}"
1648- 
1649- all_common_dirs_blocks_in_filelist "$install_type" "$filelist_path" "$feature_param" \
1650- | blocks_to_db_item "$package" "$version_dir"
1651- ret="$?" && [ $ret -ne 0 ] && total_ret=1 && continue
1652- done
1653- 
1654- return $total_ret
1655-}
1656- 
1657-# 生成运行包的ascend_package_db.info
1658-generate_running_packages_db_info() {
1659- local var_path="$1"
1660- local ret install_path running_packages db_info install_for_all
1661- local db_info_path="$var_path/ascend_package_db.info"
1662- 
1663- install_path="$(dirname "$(dirname "$var_path")")"
1664- 
1665- if [ -f "$db_info_path" ]; then
1666- return 0
1667- fi
1668- 
1669- get_running_packages "running_packages" "$install_path/$LATEST_DIR"
1670- ret="$?" && [ $ret -ne 0 ] && return $ret
1671- 
1672- if [ "${running_packages}" = "" ]; then
1673- return 0
1674- fi
1675- 
1676- db_info="$(
1677- packages_db_items "$install_path" "$running_packages" \
1678- | remove_blank_line | sort_1 "|" | fold_2 "|" ""
1679- )"
1680- ret="$?" && [ $ret -ne 0 ] && return $ret
1681- 
1682- get_install_for_all "install_for_all"
1683- ret="$?" && [ $ret -ne 0 ] && return $ret
1684- 
1685- ensure_file "$db_info_path" "440" "$USERNAME" "$USERGROUP" "$install_for_all"
1686- ret="$?" && [ $ret -ne 0 ] && return $ret
1687- 
1688- with_chmod "$db_info_path" "700" write_text "$db_info" "$db_info_path"
1689- ret="$?" && [ $ret -ne 0 ] && return $ret
1690- 
1691- return 0
1692-}
1693- 
1694-# 升级setenv脚本
1695-upgrade_setenv() {
1696- local latest_path="$1"
1697- local shell_type config_path
1698- for shell_type in ${ENV_SHELL_TYPES}; do
1699- get_setenv_filepath "config_path" "$latest_path" "$shell_type"
1700- add_ascend_home_path_env "$config_path" "$shell_type" "$latest_path"
1701- done
1702-}
1703- 
1704-# latest下数据迁移
1705-migrate_latest_data() {
1706- local var_path="$1"
1707- local latest_path="$(dirname "$var_path")"
1708- local ret config_path
1709- 
1710- generate_running_packages_db_info "$var_path"
1711- ret="$?" && [ $ret -ne 0 ] && return $ret
1712- 
1713- get_setenv_filepath "config_path" "$latest_path" "bash"
1714- if ! has_ascend_home_path_env "$config_path"; then
1715- with_chmod "$latest_path/bin" "700" upgrade_setenv "$latest_path"
1716- fi
1717- 
1718- return 0
1719-}
Dscripts/package/latest_manager/scripts/uninstall.sh+0-52
@@ -1,52 +0,0 @@
1-#!/bin/bash
2-# --------------------------------------------------------------------------------
3-# Copyright (c) 2025 Huawei Technologies Co., Ltd.
4-# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
5-# CANN Open Software License Agreement Version 2.0 (the "License").
6-# Please refer to the License for details. You may not use this file except in compliance with the License.
7-# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
8-# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
9-# See LICENSE in the root of the software repository for the full text of the License.
10-# --------------------------------------------------------------------------------
11- 
12-CURPATH=$(dirname $(readlink -f "$0"))
13-VARPATH="$(dirname "$CURPATH")"
14-USERNAME=$(id -un)
15-USERGROUP=$(id -gn)
16-common_func_path="$CURPATH/common_func.inc"
17-manager_func_path="$CURPATH/manager_func.sh"
18- 
19-. "$common_func_path"
20-. "$manager_func_path"
21- 
22-set_comm_log "Latest_manager" "$COMM_LOGFILE"
23- 
24-IS_UPGRADE="n"
25- 
26-while true
27-do
28- case "$1" in
29- --upgrade)
30- IS_UPGRADE="y"
31- shift
32- ;;
33- *)
34- break
35- ;;
36- esac
37-done
38- 
39-if ! sh "$CURPATH/install_common_parser.sh" --package="latest_manager" --uninstall --username="$USERNAME" --usergroup="$USERGROUP" \
40- --simple-uninstall "full" "$VARPATH" "$CURPATH/filelist.csv" "all"; then
41- comm_log "ERROR" "uninstall failed!"
42- exit 1
43-fi
44- 
45-if [ "$IS_UPGRADE" = "n" ]; then
46- remove_manager_refs "$VARPATH"
47-fi
48- 
49-if ! remove_dir_if_empty "$VARPATH"; then
50- comm_log "ERROR" "uninstall failed!"
51- exit 1
52-fi
Dscripts/package/latest_manager/scripts/version.info+0-1
@@ -1 +0,0 @@
1-Version=45
Dscripts/package/package.py+0-797
@@ -1,797 +0,0 @@
1-#!/usr/bin/env python3
2-# -*- coding: UTF-8 -*-
3-# --------------------------------------------------------------------------------
4-# Copyright (c) 2025 Huawei Technologies Co., Ltd.
5-# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
6-# CANN Open Software License Agreement Version 2.0 (the "License").
7-# Please refer to the License for details. You may not use this file except in compliance with the License.
8-# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
9-# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
10-# See LICENSE in the root of the software repository for the full text of the License.
11-# --------------------------------------------------------------------------------
12- 
13-import os
14-import sys
15-import subprocess
16-import argparse
17-import traceback
18-import csv
19-from argparse import Namespace
20-from collections import namedtuple
21-from datetime import datetime, timezone
22-from functools import partial
23-from itertools import chain
24-from typing import Dict, Iterator, List, Set, Tuple, TextIO
25- 
26-from common.py.utils import pkg_utils
27-from common.py.filelist import (
28- FileItem, FileList, check_filelist, create_file_item, generate_filelist,
29- get_transform_nested_path_func,
30-)
31-from common.py.packer import (
32- PackageName, create_makeself_pkg_params_factory, create_run_package_command
33-)
34-from common.py.pkg_parser import (
35- ParseOption, XmlConfig, parse_xml_config, get_cann_version_info, get_target_name
36-)
37-from common.py.utils.pkg_utils import (
38- CONFIG_SCRIPT_PATH, CompressError, ContainAsteriskError, DELIVERY_PATH, FAIL,
39- FilelistError, GenerateFilelistError, PackageNameEmptyError, SUCC, TOP_DIR,
40- UnknownOperateTypeError, path_join
41-)
42-from common.py.utils.funcbase import invoke, pipe
43-from common.py.utils.comm_log import CommLog
44- 
45- 
46-def get_comments(package_name: PackageName) -> str:
47- """获取run包注释。"""
48- comments = '_'.join(
49- [package_name.product_name.upper(), package_name.func_name.upper(), 'RUN_PACKAGE']
50- )
51- return f'"{comments}"'
52- 
53- 
54-def get_compress_cmd(pkg_args: Namespace,
55- xml_config: XmlConfig) -> str:
56- """获取makeself压缩命令"""
57- suffix = xml_config.package_attr.get('suffix')
58- if suffix == "run":
59- package_name = PackageName(xml_config.package_attr, pkg_args, xml_config.version)
60- factory = create_makeself_pkg_params_factory(
61- package_name.getvalue(), get_comments(package_name)
62- )
63- params = factory(xml_config.package_attr)
64- pack_cmd, err_msg = create_run_package_command(params)
65- if err_msg:
66- CommLog.cilog_error(err_msg)
67- CommLog.cilog_error("create_run_command failed!")
68- else:
69- CommLog.cilog_error("the repack type '%s' is not support!", suffix)
70- sys.exit(FAIL)
71- try:
72- makeself_dir = os.path.join(TOP_DIR, "build/makeself.txt")
73- with open(makeself_dir, 'w') as f:
74- f.write(pack_cmd)
75- except Exception as exception:
76- CommLog.cilog_error(f"save makeself.txt failed!{str(exception)}")
77- sys.exit(FAIL)
78- return package_name.getvalue()
79- 
80- 
81-def make_parse_option(args_: argparse.Namespace) -> ParseOption:
82- """创建解析参数。"""
83- 
84- return ParseOption(
85- args_.os_arch, args_.pkg_version,
86- args_.build_type,
87- args_.package_check,
88- args_.ext_name
89- )
90- 
91- 
92-PrivatePackageOption = namedtuple(
93- 'PrivatePackageOption',
94- [
95- 'os_arch', 'package_suffix', 'not_in_name', 'pkg_version', 'ext_name',
96- 'chip_name', 'func_name', 'version_dir', 'disable_multi_version', 'suffix'
97- ]
98-)
99- 
100- 
101-class PackageOption(PrivatePackageOption):
102- """打包配置参数。"""
103- __slots__ = () # 优化内存,避免创建 __dict__
104- 
105- def __new__(cls, *package_option_args, **kwargs):
106- return super().__new__(cls, *package_option_args, **kwargs)
107- 
108- 
109-def do_copy(target_conf=None,
110- delivery_dir='',
111- release_dir='',
112- package_name=None):
113- '''
114- 功能描述:根据拷贝类型来执行文件或目录拷贝
115- 返回值:SUCC/FAIL
116- '''
117- if target_conf is None:
118- target_conf = {}
119- target_name = get_target_name(target_conf)
120- dst_path = os.path.join(release_dir, target_conf.get('dst_path', ''))
121- dst_fullpath = os.path.join(dst_path, target_name)
122- 
123- pkg_softlink = target_conf.get('pkg_softlink')
124- if pkg_softlink:
125- rets = [
126- create_softlink(dst_fullpath, os.path.join(release_dir, link))
127- for link in pkg_softlink
128- ]
129- if not all(rets):
130- return FAIL
131- return SUCC
132- 
133- 
134-def do_chmod(target_conf=None, release_dir=''):
135- """打包时设置权限 。"""
136- if target_conf is None:
137- target_conf = {}
138- target_name = get_target_name(target_conf)
139- dst_path = os.path.join(release_dir, target_conf.get('dst_path', ''))
140- dst_fullpath = os.path.join(dst_path, target_name)
141- pkg_mod = target_conf.get('pkg_mod', '')
142- if pkg_mod:
143- # 将命令拆分为列表,不使用 shell=True
144- cmd_list = ['chmod', '-R', pkg_mod, dst_fullpath]
145- 
146- try:
147- result = subprocess.run(cmd_list, capture_output=True, text=True, check=False)
148- status = result.returncode
149- output = result.stdout + result.stderr
150- 
151- if status != 0: # 这里的 SUCC 通常对应 0
152- CommLog.cilog_error("chmod failed! Command: %s", " ".join(cmd_list))
153- CommLog.cilog_info("%s", output)
154- return FAIL
155- except Exception as e:
156- CommLog.cilog_error("Execute chmod exception: %s", str(e))
157- return FAIL
158- 
159- return SUCC
160- 
161- 
162-def create_softlink(source, target) -> bool:
163- '''
164- 功能描述:创建软连接
165- 参数:source, target
166- 返回值:成功或失败
167- '''
168- source = os.path.abspath(source.strip())
169- target = os.path.abspath(target.strip())
170- 
171- link_target_path = os.path.dirname(target)
172- link_target_name = os.path.basename(target)
173- relative_path = os.path.relpath(source, link_target_path)
174- if os.path.isfile(target):
175- cmd_list = ['rm', '-f', target]
176- try:
177- result = subprocess.run(cmd_list, capture_output=True, text=True, check=False)
178- status = result.returncode
179- output = result.stdout + result.stderr
180- if status != 0:
181- CommLog.cilog_error("Error: rm -f %s failed, %s", target, output)
182- return False
183- except Exception as e:
184- CommLog.cilog_error("Execute rm exception: %s", str(e))
185- return False
186- if os.path.isdir(target):
187- CommLog.cilog_warning("Warning: %s is already a directory, skipping soft link creation.", target)
188- return True
189- if not os.path.exists(link_target_path):
190- os.makedirs(link_target_path)
191- tmp_dir = os.getcwd()
192- os.chdir(link_target_path)
193- os.symlink(relative_path, link_target_name)
194- os.chdir(tmp_dir)
195- return True
196- 
197- 
198-def generate_info_content(target_conf, ext_name) -> List[str]:
199- """生成info内容。"""
200- 
201- def toolchain_llvm_config() -> Iterator[Tuple[str, str]]:
202- if 'llvm' in ext_name:
203- yield 'toolchain', 'llvm'
204- 
205- content_list = [
206- f'{key}={value}'
207- for key, value in chain(
208- target_conf['content'].items(), toolchain_llvm_config()
209- )
210- ]
211- return content_list
212- 
213- 
214-def generate_version_header_content(target_conf) -> Iterator[str]:
215- """生成version_header内容。"""
216- guard_name = target_conf['value'].replace('.', '_').upper()
217- yield f'#ifndef {guard_name}'
218- yield f'#define {guard_name}'
219- yield ''
220- for name, value in target_conf['content'].items():
221- if name.endswith('_VERSION'):
222- version_infos = get_cann_version_info(name, value)
223- for version_name, version_value in version_infos:
224- yield f'#define {version_name} {version_value}'
225- else:
226- yield f'#define {name} {value}'
227- yield ''
228- yield f'#endif /* {guard_name} */'
229- yield ''
230- 
231- 
232-def generate_customized_file(target_conf, ext_name):
233- filepath = os.path.join(TOP_DIR, "build", target_conf.get('value'))
234- 
235- generator = target_conf.get('generator', 'info')
236- if generator == 'version_header':
237- content_list = generate_version_header_content(target_conf)
238- else:
239- content_list = generate_info_content(target_conf, ext_name)
240- 
241- file_content = '\n'.join(content_list)
242- try:
243- with open(filepath, 'w') as file:
244- file.write(file_content)
245- except Exception as ex:
246- CommLog.cilog_error(f"generate customized file {filepath} failed: {ex}!")
247- return FAIL
248- 
249- return SUCC
250- 
251- 
252-def get_module(target_config) -> str:
253- """获取配置模块。"""
254- module = target_config.get('module', 'NA')
255- return module if module else 'NA'
256- 
257- 
258-def get_operation(operation, target_config) -> str:
259- """获取操作类型。"""
260- if operation in ('copy', 'move') and target_config.get('entity') == 'true':
261- return 'copy_entity'
262- return operation
263- 
264- 
265-def get_permission(target_config) -> str:
266- """获取配置权限。"""
267- return target_config.get('install_mod', 'NA')
268- 
269- 
270-def get_owner_group(target_config) -> str:
271- """获取配置属主。"""
272- # install_own的可能值为$username:$usergroup
273- # 防止变量在install_common_parser.sh中,被eval展开,添加\转义$
274- # 由于awk会消耗1个\,所以需要2个转义符
275- return target_config.get('install_own', 'NA').replace('$', '\\\\$')
276- 
277- 
278-def get_install_type(target_config) -> str:
279- """获取安装类型。"""
280- return target_config.get('install_type', 'NA')
281- 
282- 
283-def get_softlink(target_config) -> List[str]:
284- """获取配置软链。"""
285- softlink_str = target_config.get('install_softlink')
286- if not softlink_str:
287- return []
288- return softlink_str.split(';')
289- 
290- 
291-def get_feature(target_config) -> Set[str]:
292- """获取配置特性。"""
293- return target_config['feature']
294- 
295- 
296-def get_chip(target_config) -> Set[str]:
297- """获取配置芯片。"""
298- return target_config['chip']
299- 
300- 
301-def get_configurable(target_config) -> str:
302- """获取配置是否为配置文件。"""
303- return target_config.get('configurable', 'FALSE')
304- 
305- 
306-def get_hash_value(target_config) -> str:
307- """获取配置哈希值。"""
308- return target_config.get('hash', 'NA')
309- 
310- 
311-def get_block(target_config) -> str:
312- """获取配置块信息。"""
313- return target_config.get('name', 'NA')
314- 
315- 
316-def get_pkg_inner_softlink(target_config) -> List[str]:
317- """获取配置包内软链。"""
318- softlink_str = target_config.get('pkg_inner_softlink')
319- if not softlink_str:
320- return []
321- return softlink_str.split(';')
322- 
323- 
324-def validate_path_consistency(target_config, target_name):
325- """验证 dst_path 和 install_path 一致性,不一致则抛出异常。"""
326- dst_path_val = target_config.get('dst_path', '')
327- install_path_val = target_config.get('install_path', '')
328- 
329- if dst_path_val != install_path_val:
330- value_path = target_config.get('value', 'unknown')
331- CommLog.cilog_error(
332- f"Configuration Error: 'dst_path' MUST be equal to 'install_path'.\n"
333- f" - Current dst_path: {dst_path_val}\n"
334- f" - Current install_path: {install_path_val}\n"
335- )
336- raise GenerateFilelistError(
337- f"dst_path ({dst_path_val}) does not match install_path ({install_path_val})"
338- )
339- 
340- 
341-def parse_install_info(infos: List, operate_type, filter_key) -> Iterator[FileItem]:
342- """根据配置解析生成安装信息。"""
343- for target_config in infos:
344- target_name = get_target_name(target_config)
345- if target_config.get("optional") == 'true' and operate_type in ('copy', 'move'):
346- path = os.path.join(TOP_DIR, DELIVERY_PATH, target_config.get('dst_path'))
347- vaule = os.path.join(TOP_DIR, DELIVERY_PATH, target_config.get('dst_path'), target_name)
348- if not os.path.exists(path):
349- continue
350- if not os.path.exists(vaule):
351- continue
352- if operate_type in ('copy', 'move'):
353- relative_path_in_pkg = os.path.join(target_config.get('dst_path'), target_name)
354- relative_install_path = path_join(target_config.get('install_path'), target_name)
355- is_dir = target_config.get('is_dir', False)
356- # 验证dst_path和install_path的一致性
357- validate_path_consistency(target_config, target_name)
358- elif operate_type == 'mkdir':
359- relative_path_in_pkg = 'NA'
360- relative_install_path = target_config.get('value')
361- is_dir = False
362- elif operate_type == 'del':
363- relative_path_in_pkg = 'NA'
364- relative_install_path = path_join(target_config.get('install_path'), target_name)
365- is_dir = False
366- else:
367- raise UnknownOperateTypeError(f"unknown operate type {operate_type}")
368- 
369- if relative_install_path is None:
370- continue
371- 
372- install_type = get_install_type(target_config)
373- if any(key in install_type for key in filter_key):
374- is_in_docker = 'TRUE'
375- else:
376- is_in_docker = 'FALSE'
377- 
378- file_item = create_file_item(
379- get_module(target_config),
380- get_operation(operate_type, target_config),
381- relative_path_in_pkg,
382- relative_install_path,
383- is_in_docker,
384- get_permission(target_config),
385- get_owner_group(target_config),
386- install_type,
387- get_softlink(target_config),
388- get_feature(target_config),
389- 'N',
390- get_configurable(target_config),
391- get_hash_value(target_config),
392- get_block(target_config),
393- get_pkg_inner_softlink(target_config),
394- get_chip(target_config),
395- is_dir,
396- )
397- 
398- yield file_item
399- 
400- 
401-def execute_repack_process(xmlconfig: XmlConfig,
402- delivery_dir: str,
403- pkg_args: Namespace,
404- package_name: PackageName = None,
405- package_option: PackageOption = None):
406- """
407- 功能描述: 执行打包流程(拷贝--->签名--->打包)
408- 返回值: SUCC/FAIL
409- """
410- status = SUCC
411- release_dir = delivery_dir
412- # 生成自定义文件
413- for item in xmlconfig.generate_infos:
414- if generate_customized_file(item, package_option.ext_name):
415- return FAIL
416- 
417- for item in chain(xmlconfig.package_content_list, xmlconfig.move_content_list):
418- #拷贝文件
419- if do_copy(item,
420- delivery_dir,
421- release_dir,
422- package_name):
423- status = FAIL
424- continue
425- if status != SUCC:
426- return FAIL
427- 
428- # 校验包中文件或目录大小
429- if pkg_args.check_size == "True":
430- limit_list, tag = processing_csv_file(
431- release_dir, package_name.func_name, package_name.chip_name, pkg_args.build_type
432- )
433- if not tag:
434- return FAIL
435- if limit_list:
436- abspath = os.path.abspath(release_dir)
437- replace_path = abspath + "/"
438- result = check_add_dir(replace_path, abspath, limit_list)
439- if not result:
440- return FAIL
441- try:
442- package_name = get_compress_cmd(pkg_args, xmlconfig)
443- except CompressError:
444- return FAIL
445- 
446- CommLog.cilog_info("package %s generate filelist.csv and makeself cmd successfully!",
447- package_name)
448- return SUCC
449- 
450- 
451-def check_path_is_conflict(xml_config):
452- """
453- 功能描述: 检查打包时安装路径与软连接路径是否冲突
454- 参数: xml_config
455- 返回值: SUCC/FAIL
456- """
457- install_path_list = set()
458- pkg_softlink_list = set()
459- for item in xml_config.package_content_list:
460- value_list = item.get('value').split('/')
461- target_name = value_list[-1] if value_list[-1] else value_list[-2]
462- if item.get('install_path'):
463- install_path_list.add(
464- os.path.join(item['install_path'], target_name)
465- )
466- if item.get('pkg_inner_softlink'):
467- pkg_softlink = item.get('pkg_inner_softlink')
468- pkg_softlink_list.add(pkg_softlink)
469- if install_path_list & pkg_softlink_list:
470- CommLog.cilog_info('intersection:{}'.format(install_path_list & pkg_softlink_list))
471- CommLog.cilog_info('path conflicting: pkg_inner_softlink dir equals install_path!!')
472- return FAIL
473- return SUCC
474- 
475- 
476-def checksum_value(limit_value, release_dir):
477- """
478- 功能描叙: 校验传入的文件或目录大小是否合格
479- 参数:
480- limit_value: limit.csv中的一行数据如[compiler/bin, 3976, 110%]
481- 返回值: True/False
482- """
483- path = os.path.join(release_dir, limit_value[1])
484- if len(limit_value) >= 7:
485- try:
486- max_value = int(limit_value[4])
487- except ValueError:
488- CommLog.cilog_error("{0} configuration is not standard., Please check limit.csv.".format(path))
489- return True
490- else:
491- CommLog.cilog_error("{0} configuration is less than four, Please check limit.csv.".format(path))
492- return True
493- if not os.path.exists(path):
494- CommLog.cilog_warning("{0} doesn't exist, Please check limit.csv.".format(path))
495- return True
496- size = 0
497- for root, dirs, files in os.walk(path):
498- size += os.path.getsize(root)
499- for f in files:
500- filepath = os.path.join(root, f)
501- if os.path.islink(filepath):
502- continue
503- if not os.path.exists(filepath):
504- continue
505- size += os.path.getsize(os.path.join(root, f))
506- if size == 0:
507- size = os.path.getsize(path)
508- if size > max_value * 1024:
509- CommLog.cilog_error(f"\n{path} size {size} bytes exceeds maximum {max_value * 1024} bytes")
510- return False
511- return True
512- 
513- 
514-def processing_csv_file(release_dir, package_name, chip_name, build_type):
515- """
516- 功能描叙: 处理limit.csv文件数据
517- 返回值: [],True/[],False
518- """
519- ret = True
520- limit_list = []
521- product = os.path.basename(os.path.dirname(release_dir))
522- limit_path = os.path.join(pkg_utils.TOP_SOURCE_DIR, CONFIG_SCRIPT_PATH, "common/limit.csv")
523- if not os.path.exists(limit_path):
524- CommLog.cilog_warning("{0} doesn't exist.".format(limit_path))
525- return limit_list, ret
526- with open(limit_path, "r") as file:
527- reader = csv.reader(file)
528- next(reader)
529- for data in reader:
530- if not data:
531- CommLog.cilog_warning("The limit.csv file contains empty lines.")
532- continue
533- if is_match_line(package_name, chip_name, product, build_type, data):
534- if data[1][-1] == "/":
535- limit_list.append(data[1][:-1])
536- else:
537- limit_list.append(data[1])
538- res = checksum_value(data, release_dir)
539- if not res:
540- ret = False
541- return limit_list, ret
542- 
543- 
544-def is_match_line(package_name, chip_name, product, build_type, data):
545- return package_name == data[0] and chip_name == data[5] and product == data[6] and build_type == data[7].lower()
546- 
547- 
548-def check_add_dir(package_path, dirs, limit_list, ret=True):
549- """
550- 功能描述: 校验新增目录
551- 参数: path, limit_list
552- 返回值: False/True
553- """
554- for limit_path in limit_list:
555- if dirs == os.path.join(os.path.split(dirs)[0], limit_path):
556- return ret
557- for dir_file in os.listdir(dirs):
558- path = os.path.join(dirs, dir_file)
559- relative_path = path.replace(package_path, "")
560- if os.path.isfile(path) and relative_path not in limit_list:
561- CommLog.cilog_error("{0} is not in limit.csv file and is newly added.".format(path))
562- ret = False
563- elif os.path.isdir(path) and relative_path not in limit_list:
564- ret = check_add_dir(package_path, path, limit_list, ret)
565- return ret
566- 
567- 
568-def gen_file_install_list(xml_config: XmlConfig,
569- filter_key) -> Tuple[FileList, FileList]:
570- """生成filelist列表。"""
571- file_install_list = []
572- 
573- dir_filelist = parse_install_info(
574- xml_config.dir_install_list, 'mkdir', filter_key
575- )
576- move_filelist = parse_install_info(
577- xml_config.move_content_list, 'move', filter_key
578- )
579- pkg_filelist = parse_install_info(
580- xml_config.package_content_list, 'copy', filter_key
581- )
582- gen_filelist = parse_install_info(
583- xml_config.generate_infos, 'copy', filter_key
584- )
585- # file_info中配置为文件夹,这里是被展开的文件,则需要单独删除
586- del_filelist = parse_install_info(
587- xml_config.expand_content_list, 'del', filter_key
588- )
589- collect_filelist = list(chain(dir_filelist, move_filelist, pkg_filelist, gen_filelist))
590- collect_filelist = list(xml_config.packer_config.fill_is_common_path(collect_filelist))
591- all_filelist = list(chain(collect_filelist, del_filelist))
592- for file_item in all_filelist:
593- file_install_list.append(file_item)
594- 
595- return file_install_list, []
596- 
597- 
598-def generate_filelist_file_by_xml_config(xml_config: XmlConfig,
599- filter_key: List[str],
600- package_check: bool):
601- """生成文件列表文件。"""
602- check_move = xml_config.package_attr.get('use_move', False)
603- transform_nested_path_func = get_transform_nested_path_func(
604- xml_config.package_attr.get('parallel') or check_move
605- )
606- check_features = xml_config.package_attr.get('check_features', False)
607- 
608- file_install_list, [] = invoke(
609- pipe(
610- gen_file_install_list,
611- partial(map, transform_nested_path_func),
612- tuple,
613- ),
614- xml_config, filter_key
615- )
616- generate_filelist(file_install_list, 'filelist.csv')
617- # 先生成再检查,有利于问题定位
618- check_filelist(file_install_list, check_features, check_move)
619- 
620- 
621-def get_pkg_xml_relative_path(pkg_args: Namespace) -> str:
622- """获取包配置文件相对路径。"""
623- 
624- def parts():
625- yield CONFIG_SCRIPT_PATH
626- yield pkg_args.pkg_name
627- if pkg_args.chip_scenes:
628- yield pkg_args.chip_scenes
629- # 可以通過build_rule指定xml_file,而且优先级高于默认值
630- if pkg_args.xml_file:
631- yield pkg_args.xml_file
632- else:
633- yield f'{pkg_args.pkg_name}.xml'
634- 
635- return os.path.join(*parts())
636- 
637- 
638-def write_config_inc_var(name: str, package_attr: Dict, file: TextIO):
639- """向config.inc文件写入变量。"""
640- if name in package_attr:
641- value = str(package_attr[name]).lower()
642- file.write(f"{name.upper()}={value}\n")
643- 
644- 
645-def generate_config_inc(package_attr: Dict):
646- """生成config.inc文件。"""
647- if 'parallel' not in package_attr and 'parallel_limit' not in package_attr and 'use_move' not in package_attr:
648- return
649- year = datetime.now(timezone.utc).year
650- config_inc = os.path.join(TOP_DIR, "build", 'config.inc')
651- header = [
652- '#!/bin/sh\n',
653- '#----------------------------------------------------------------------------\n',
654- f'# Copyright Huawei Technologies Co., Ltd. 2023-{year}. All rights reserved.\n',
655- '#----------------------------------------------------------------------------\n',
656- '\n',
657- ]
658- if os.path.isfile(config_inc):
659- os.chmod(config_inc, 0o700)
660- with open(config_inc, 'w', encoding='utf-8') as file:
661- file.writelines(header)
662- write_config_inc_var('parallel', package_attr, file)
663- write_config_inc_var('parallel_limit', package_attr, file)
664- write_config_inc_var('use_move', package_attr, file)
665- 
666- os.chmod(config_inc, 0o500)
667-def main(pkg_name='', xml_file='', main_args=None):
668- """
669- 功能描述: 执行打包流程(解析配置--->生成文件列表--->执行拷贝/打包动作)
670- 参数: pkg_name, os_arch, type
671- 返回值: SUCC/FAIL
672- """
673- delivery_dir = os.path.join(TOP_DIR, DELIVERY_PATH)
674- if not os.path.exists(delivery_dir):
675- return FAIL
676- 
677- config_relative_path = get_pkg_xml_relative_path(main_args)
678- pkg_xml_file = os.path.join(pkg_utils.TOP_SOURCE_DIR, config_relative_path)
679- parse_option = make_parse_option(main_args)
680- 
681- try:
682- xml_config = parse_xml_config(
683- pkg_xml_file, delivery_dir, parse_option, main_args
684- )
685- except ContainAsteriskError as ex:
686- CommLog.cilog_error(f"Value contain '*' in {config_relative_path}. value is '{ex.value}'.")
687- return FAIL
688- 
689- if pkg_name in ['driver', 'firmware']:
690- filter_key = ['all', 'docker']
691- elif pkg_name in ['aicpu_kernels_device', 'aicpu_kernels_host']:
692- filter_key = []
693- else:
694- filter_key = ['all', 'run']
695- 
696- # 生成filelist.csv安装列表文件
697- try:
698- generate_filelist_file_by_xml_config(
699- xml_config, filter_key,
700- main_args.package_check or xml_config.package_attr.get('package_check')
701- )
702- except PackageNameEmptyError:
703- CommLog.cilog_error(f'package name is empty in {xml_file}, please check it')
704- return FAIL
705- except GenerateFilelistError as ex:
706- CommLog.cilog_error(f'generate filelist {ex.filename} failed!', )
707- return FAIL
708- except FilelistError as ex:
709- CommLog.cilog_error('check filelist error! %s', str(ex))
710- return FAIL
711- 
712- generate_config_inc(xml_config.package_attr)
713- 
714- package_option = PackageOption(
715- main_args.os_arch, main_args.package_suffix, main_args.not_in_name, main_args.pkg_version, main_args.ext_name,
716- chip_name=main_args.chip_name, func_name=main_args.func_name, version_dir=main_args.version_dir,
717- disable_multi_version=main_args.disable_multi_version, suffix=main_args.suffix)
718- 
719- package_name = PackageName(xml_config.package_attr, main_args, xml_config.version)
720- 
721- # 检查install_path与pkg_inner_softlink路径是否冲突,若冲突则报错
722- if check_path_is_conflict(xml_config) == FAIL:
723- return FAIL
724- 
725- # 生成打包命令
726- return execute_repack_process(xml_config, delivery_dir, main_args,
727- package_name=package_name, package_option=package_option)
728- 
729- 
730-def args_parse():
731- """
732- 功能描述 : 脚本入参解析
733- 参数 : 调用脚本的传参
734- 返回值 : 解析后的参数值
735- """
736- parser = argparse.ArgumentParser(
737- description='This script is for package repack processing.')
738- parser.add_argument('-c', '--chip_scenes', metavar='chip_scenes', required=False, dest='chip_scenes', nargs='?',
739- const='',
740- default='', help='This parameter define chip id for package.')
741- parser.add_argument('-n', '--pkg_name', metavar='pkg_name', required=False,
742- help='This parameter define pkg_name for config_xml.')
743- parser.add_argument('-o', '--os_arch', metavar='os_arch', required=False, dest='os_arch', nargs='?', const='',
744- default=None, help="This parameter define the package's os_arch")
745- parser.add_argument('-t', '--type', metavar='type', required=False, dest='type', nargs='?', const='',
746- default='repack', help="This parameter define this script's function")
747- parser.add_argument('-i', '--not_in_name', metavar='not_in_name', required=False, dest='not_in_name', nargs='?',
748- const='',
749- default='', help="This parameter define the package's name not contain the element")
750- parser.add_argument('-v', '--pkg_version', metavar='pkg_version', required=False, dest='pkg_version', nargs='?',
751- const='',
752- default='', help="This parameter define the version for package.")
753- parser.add_argument('-e', '--ext_name', metavar='ext_name', required=False, dest='ext_name', nargs='?', const='',
754- default='', help="This parameter define the package's ext_name")
755- parser.add_argument('--package_suffix', nargs='?', const='none',
756- default='none', help="This parameter define the package suffix, debug or none")
757- parser.add_argument('--suffix', metavar='suffix', required=False, dest='suffix', nargs='?', const='',
758- default=None, help="This parameter define the package suffix, for example such as tar.gz")
759- parser.add_argument('-b', '--build_type', metavar='build_type', required=False, dest='build_type', nargs='?',
760- const='',
761- default='debug', help="This parameter define release type of package")
762- parser.add_argument('-x', '--xml', metavar='xml_file', required=False, dest='xml_file', nargs='?', const='',
763- default='', help="This parameter define xml file")
764- parser.add_argument('--chip_name', metavar='chip_name', required=False, dest='chip_name', nargs='?', const=None,
765- default=None,
766- help="This parameter define package chip name, has higher priority than chip name in xml")
767- parser.add_argument('--func_name', metavar='func_name', required=False, dest='func_name', nargs='?', const=None,
768- default=None,
769- help="This parameter define package func name, has higher priority than func name in xml")
770- parser.add_argument('--source_root', metavar='source_root', required=False, dest='source_root', nargs='?', const='',
771- help='source root dir.')
772- parser.add_argument('--version_dir', nargs='?', const='', default='', help='Set version dir.')
773- parser.add_argument('--tag', metavar='tag', nargs='?', const='', default='')
774- parser.add_argument('--disable-multi-version', action='store_true', help='Disable multi version.')
775- # 检查打包配置
776- parser.add_argument('--package-check', action='store_true', help='check package config.')
777- parser.add_argument('--check_size', nargs='?', const='', default='', help="Check the size of a file or directory.")
778- parser.add_argument('--pkg-name-style', metavar='pkg_name_style', default='common', help='Package name style.')
779- return parser.parse_args()
780- 
781- 
782-if __name__ == "__main__":
783- CommLog.cilog_info("%s", " ".join(sys.argv))
784- args = args_parse()
785- try:
786- if args.source_root:
787- pkg_utils.TOP_SOURCE_DIR = args.source_root
788- if args.build_type == '':
789- args.build_type = 'debug'
790- else:
791- args.build_type = args.build_type.lower()
792- status = main(args.pkg_name, args.xml_file, main_args=args)
793- except Exception as e:
794- CommLog.cilog_error("exception is occurred (%s)!", e)
795- CommLog.cilog_info("%s", traceback.format_exc())
796- status = FAIL
797- sys.exit(status)
Mscripts/package/pto_isa/pto_isa.xml+1-0
@@ -3,6 +3,7 @@
3 <package_info>3 <package_info>
4 <product_name>cann</product_name>4 <product_name>cann</product_name>
5 <func_name>pto-isa</func_name>5 <func_name>pto-isa</func_name>
6+ <share_info_name>pto_isa</share_info_name>
6 <suffix>run</suffix>7 <suffix>run</suffix>
7 <install_script>share/info/pto_isa/scripts/install.sh</install_script>8 <install_script>share/info/pto_isa/scripts/install.sh</install_script>
8 <help>share/info/pto_isa/scripts/help.info</help>9 <help>share/info/pto_isa/scripts/help.info</help>
Mversion.cmake+9-9
@@ -8,13 +8,13 @@
8# See LICENSE in the root of the software repository for the full text of the License.8# See LICENSE in the root of the software repository for the full text of the License.
9# -----------------------------------------------------------------------------------------------------------9# -----------------------------------------------------------------------------------------------------------
10 10 
11-set_package(pto-isa VERSION "9.1.0")11+set_cann_package(pto-isa VERSION "9.1.0")
12 12 
13-set_run_dependencies(runtime "CUR_MAJOR_MINOR_VER")13+set_cann_run_dependencies(runtime "CUR_MAJOR_MINOR_VER")
14-set_run_dependencies(opbase "CUR_MAJOR_MINOR_VER")14+set_cann_run_dependencies(opbase "CUR_MAJOR_MINOR_VER")
15-set_run_dependencies(asc-devkit "CUR_MAJOR_MINOR_VER")15+set_cann_run_dependencies(asc-devkit "CUR_MAJOR_MINOR_VER")
16-set_run_dependencies(metadef "CUR_MAJOR_MINOR_VER")16+set_cann_run_dependencies(metadef "CUR_MAJOR_MINOR_VER")
17-set_run_dependencies(ge-executor "CUR_MAJOR_MINOR_VER")17+set_cann_run_dependencies(ge-executor "CUR_MAJOR_MINOR_VER")
18-set_run_dependencies(bisheng-compiler "CUR_MAJOR_MINOR_VER")18+set_cann_run_dependencies(bisheng-compiler "CUR_MAJOR_MINOR_VER")
19-set_run_dependencies(asc-tools "CUR_MAJOR_MINOR_VER")19+set_cann_run_dependencies(asc-tools "CUR_MAJOR_MINOR_VER")
20-set_run_dependencies(ge-compiler "CUR_MAJOR_MINOR_VER")20+set_cann_run_dependencies(ge-compiler "CUR_MAJOR_MINOR_VER")