已合并
fix: init_env自动同步版本并安装依赖(#148) #419
fix: init_env自动同步版本并安装依赖(#148) #419
已合并
sinat_31531339创建于 7月10日
3 个文件变更+205-50
Mdocs/en/quick_install.md+3-11文件内容审核中,请稍后刷新重试
@@ -120,17 +120,9 @@ docker run --name oam-tools -it swr.cn-south-1.myhuaweicloud.com/ascendhub/cann:
120curl -fsSL https://raw.gitcode.com/cann/oam-tools/raw/master/init_env.sh | bash120curl -fsSL https://raw.gitcode.com/cann/oam-tools/raw/master/init_env.sh | bash
121```121```
122 122 
123-环境部署完成后,请继续以下步骤:123+`init_env.sh` 会自动安装仓库根目录 `requirements.txt` 中声明的 Python 依赖。环境部署完成后,请继续以下步骤:
124 124 
125-1. **安装 Python 依赖**125+1. **验证环境**
126- 
127- ```bash
128- pip3 install -r requirements.txt
129- ```
130- 
131- > 说明:init_env.sh 已安装 pytest、coverage 等核心依赖,此命令可确保所有依赖完整。
132- 
133-2. **验证环境**
134 126 
135 请参考[环境验证](#环境验证)章节,确认环境和驱动正常。127 请参考[环境验证](#环境验证)章节,确认环境和驱动正常。
136 128 
@@ -336,4 +328,4 @@ mkdir -p ${third_party_path}
336source /usr/local/Ascend/cann/set_env.sh328source /usr/local/Ascend/cann/set_env.sh
337# 指定路径安装329# 指定路径安装
338# source ${install_path}/cann/set_env.sh330# source ${install_path}/cann/set_env.sh
339-```331+```
Minit_env.sh+199-28
@@ -22,10 +22,17 @@ GREEN='\033[0;32m'
22YELLOW='\033[1;33m'22YELLOW='\033[1;33m'
23NC='\033[0m'23NC='\033[0m'
24 24 
25-CANN_VERSION="8.5.0"25+CANN_VERSION="${CANN_VERSION:-}"
26CHIP_TYPE="910b"26CHIP_TYPE="910b"
27CANN_BASE_URL="https://ascend.devcloud.huaweicloud.com/artifactory/cann-run/software"27CANN_BASE_URL="https://ascend.devcloud.huaweicloud.com/artifactory/cann-run/software"
28-INSTALL_PATH="${INSTALL_PATH:-/usr/local/Ascend}"28+OAM_TOOLS_RAW_BASE_URL="${OAM_TOOLS_RAW_BASE_URL:-https://raw.gitcode.com/cann/oam-tools/raw/master}"
29+if [ -z "${INSTALL_PATH:-}" ]; then
30+ if [ "$(id -u)" -eq 0 ]; then
31+ INSTALL_PATH="/usr/local/Ascend"
32+ else
33+ INSTALL_PATH="${HOME}/Ascend"
34+ fi
35+fi
29SKIP_OPS="false"36SKIP_OPS="false"
30 37 
31log_info() { echo -e "${GREEN}[INFO]${NC} $1"; }38log_info() { echo -e "${GREEN}[INFO]${NC} $1"; }
@@ -34,6 +41,66 @@ log_error() { echo -e "${RED}[ERROR]${NC} $1"; }
34 41 
35check_command() { command -v "$1" &>/dev/null; }42check_command() { command -v "$1" &>/dev/null; }
36 43 
44+is_root_user() { [ "$(id -u)" -eq 0 ]; }
45+ 
46+get_missing_required_system_deps() {
47+ local missing_deps=""
48+ 
49+ check_command curl || missing_deps="${missing_deps} curl"
50+ check_command wget || missing_deps="${missing_deps} wget"
51+ check_command git || missing_deps="${missing_deps} git"
52+ check_command cmake || missing_deps="${missing_deps} cmake"
53+ check_command make || missing_deps="${missing_deps} make"
54+ check_command g++ || missing_deps="${missing_deps} g++"
55+ 
56+ printf '%s' "${missing_deps# }"
57+}
58+ 
59+get_cann_version_from_cmake() {
60+ local version_file="$1"
61+ local version=""
62+ local version_content=""
63+ 
64+ if [ -f "$version_file" ]; then
65+ version_content=$(cat "$version_file")
66+ else
67+ if ! check_command curl; then
68+ log_error "version.cmake not found and curl is unavailable: $version_file" >&2
69+ return 1
70+ fi
71+ log_warn "version.cmake not found locally, fetching from ${OAM_TOOLS_RAW_BASE_URL}/version.cmake" >&2
72+ version_content=$(curl -fsSL "${OAM_TOOLS_RAW_BASE_URL}/version.cmake") || {
73+ log_error "Failed to fetch version.cmake" >&2
74+ return 1
75+ }
76+ fi
77+ 
78+ version=$(printf '%s\n' "$version_content" | sed -nE 's/^[[:space:]]*set_cann_package[[:space:]]*\([^)]*VERSION[[:space:]]+"([^"]+)".*/\1/p' | head -n 1)
79+ if [ -z "$version" ]; then
80+ log_error "Failed to parse CANN version from version.cmake" >&2
81+ return 1
82+ fi
83+ 
84+ echo "$version"
85+}
zhangjie
zhangjiezhangjie7月10日

级别:提示 问题:get_ops_package_chip_type 的 case 分支未覆盖 950 等新芯片类型,但 show_help 注释中提到了 950 影响:用户传入 --chip-type 950 时,ops 包名会直接使用 '950',可能与实际包名不匹配导致下载失败 修复建议:确认 950 芯片的 ops 包命名规则并补充对应 case 分支,或在文档中明确列出所有支持的芯片类型

likedislike
86+ 
87+get_ops_package_chip_type() {
88+ case "$1" in
89+ 910_93|910c|910C|910_c|910_C|A3|a3)
90+ echo "A3"
91+ ;;
92+ 910B|910b)
93+ echo "910b"
94+ ;;
95+ 950)
96+ echo "950"
97+ ;;
98+ *)
99+ echo "$1"
100+ ;;
101+ esac
102+}
103+ 
37get_arch() {104get_arch() {
38 local arch=$(uname -m)105 local arch=$(uname -m)
39 case $arch in106 case $arch in
@@ -92,8 +159,9 @@ install_cann() {
92 mkdir -p "$download_dir"159 mkdir -p "$download_dir"
93 cd "$download_dir"160 cd "$download_dir"
94 161 
162+ local ops_package_chip_type=$(get_ops_package_chip_type "$CHIP_TYPE")
95 local toolkit_pkg="Ascend-cann-toolkit_${CANN_VERSION}_linux-${arch}.run"163 local toolkit_pkg="Ascend-cann-toolkit_${CANN_VERSION}_linux-${arch}.run"
96- local ops_pkg="Ascend-cann-${CHIP_TYPE}-ops_${CANN_VERSION}_linux-${arch}.run"164+ local ops_pkg="Ascend-cann-${ops_package_chip_type}-ops_${CANN_VERSION}_linux-${arch}.run"
97 local toolkit_url="${CANN_BASE_URL}/${CANN_VERSION}/${arch}/${toolkit_pkg}"165 local toolkit_url="${CANN_BASE_URL}/${CANN_VERSION}/${arch}/${toolkit_pkg}"
98 local ops_url="${CANN_BASE_URL}/${CANN_VERSION}/${arch}/${ops_pkg}"166 local ops_url="${CANN_BASE_URL}/${CANN_VERSION}/${arch}/${ops_pkg}"
99 167 
@@ -108,7 +176,7 @@ install_cann() {
108 176 
109 if [ "$SKIP_OPS" = "false" ]; then177 if [ "$SKIP_OPS" = "false" ]; then
110 if [ ! -f "$ops_pkg" ]; then178 if [ ! -f "$ops_pkg" ]; then
111- log_info "Downloading CANN ops (${CHIP_TYPE}, ${arch})..."179+ log_info "Downloading CANN ops (${ops_package_chip_type}, ${arch})..."
112 log_info "URL: $ops_url"180 log_info "URL: $ops_url"
113 wget -q --show-progress --no-check-certificate -O "$ops_pkg" "$ops_url" || {181 wget -q --show-progress --no-check-certificate -O "$ops_pkg" "$ops_url" || {
114 log_error "Failed to download ops"182 log_error "Failed to download ops"
@@ -179,8 +247,27 @@ setup_cann_env() {
179install_system_deps() {247install_system_deps() {
180 log_info "Checking system dependencies..."248 log_info "Checking system dependencies..."
181 249 
250+ local missing_cmds
251+ missing_cmds=$(get_missing_required_system_deps)
252+ 
253+ if ! is_root_user; then
254+ if [ -n "$missing_cmds" ]; then
255+ log_warn "Skipping system dependency installation (non-root user)"
256+ log_error "Missing required system commands: ${missing_cmds}"
257+ log_error "Install them manually or rerun this script as root/sudo"
258+ return 1
259+ else
260+ log_info "Required system dependencies are available"
261+ fi
zhangjie
zhangjiezhangjie7月10日

级别:一般 问题:非 root 用户路径下 install_system_deps 检测到 cmake/g++/make 等关键依赖缺失时仅 log_warn 并 return 0,函数名暗示安装但实际未安装且返回成功 影响:调用方认为系统依赖安装成功,后续编译阶段才报错,排错路径变长 修复建议:考虑对 cmake/g++/make 等编译必需工具缺失时 return 1 或设置全局标志,让调用方可以决定是否继续;至少在日志中明确标注 'skipping installation (non-root)'

likedislike
sinat_31531339
sinat_31531339
7月11日 评论:
262+ 
263+ check_command cmake && log_info "cmake: $(cmake --version 2>&1 | head -1)" || log_warn "cmake: not installed"
264+ check_command g++ && log_info "g++: $(g++ --version 2>&1 | head -1)" || log_warn "g++: not installed"
265+ check_command ccache && log_info "ccache: available" || log_warn "ccache: not installed"
266+ return 0
267+ fi
268+ 
182 if check_command apt-get; then269 if check_command apt-get; then
183- apt-get update -qq 2>/dev/null270+ apt-get update -qq 2>/dev/null || log_warn "apt-get update failed"
184 local pkgs=""271 local pkgs=""
185 check_command curl || pkgs="$pkgs curl"272 check_command curl || pkgs="$pkgs curl"
186 check_command wget || pkgs="$pkgs wget"273 check_command wget || pkgs="$pkgs wget"
@@ -189,7 +276,7 @@ install_system_deps() {
189 check_command make || pkgs="$pkgs make"276 check_command make || pkgs="$pkgs make"
190 check_command g++ || pkgs="$pkgs g++"277 check_command g++ || pkgs="$pkgs g++"
191 check_command ccache || pkgs="$pkgs ccache"278 check_command ccache || pkgs="$pkgs ccache"
192- 279+ 
193 if [ -n "$pkgs" ]; then280 if [ -n "$pkgs" ]; then
194 log_info "Installing:$pkgs"281 log_info "Installing:$pkgs"
195 apt-get install -y -qq $pkgs 2>/dev/null || log_warn "Some packages may have failed"282 apt-get install -y -qq $pkgs 2>/dev/null || log_warn "Some packages may have failed"
@@ -202,41 +289,103 @@ install_system_deps() {
202 check_command cmake || pkgs="$pkgs cmake"289 check_command cmake || pkgs="$pkgs cmake"
203 check_command make || pkgs="$pkgs make"290 check_command make || pkgs="$pkgs make"
204 check_command g++ || pkgs="$pkgs gcc-c++"291 check_command g++ || pkgs="$pkgs gcc-c++"
205- 292+ 
206 if [ -n "$pkgs" ]; then293 if [ -n "$pkgs" ]; then
207 log_info "Installing:$pkgs"294 log_info "Installing:$pkgs"
208 yum install -y -q $pkgs 2>/dev/null || log_warn "Some packages may have failed"295 yum install -y -q $pkgs 2>/dev/null || log_warn "Some packages may have failed"
209 fi296 fi
297+ else
298+ log_warn "No supported package manager found, skipping system dependency installation"
210 fi299 fi
211 300 
212- log_info "cmake: $(cmake --version 2>&1 | head -1)"301+ missing_cmds=$(get_missing_required_system_deps)
213- log_info "g++: $(g++ --version 2>&1 | head -1)"302+ if [ -n "$missing_cmds" ]; then
303+ log_error "Missing required system commands after installation attempt: ${missing_cmds}"
304+ return 1
305+ fi
306+ 
307+ check_command cmake && log_info "cmake: $(cmake --version 2>&1 | head -1)" || log_warn "cmake: not installed"
308+ check_command g++ && log_info "g++: $(g++ --version 2>&1 | head -1)" || log_warn "g++: not installed"
214 check_command ccache && log_info "ccache: available" || log_warn "ccache: not installed"309 check_command ccache && log_info "ccache: available" || log_warn "ccache: not installed"
215}310}
216 311 
312+run_pip_command() {
313+ local python="$1"
314+ shift
315+ 
316+ local pip_log
317+ pip_log=$(mktemp "${TMPDIR:-/tmp}/init_env_pip.XXXXXX") || {
318+ log_error "Failed to create temporary pip log file"
319+ return 1
320+ }
321+ 
322+ if "$python" -m pip "$@" >"$pip_log" 2>&1; then
323+ rm -f "$pip_log"
324+ return 0
325+ fi
326+ 
327+ log_warn "pip command failed: $python -m pip $*"
328+ local pip_error_tail
329+ pip_error_tail=$(tail -n 5 "$pip_log" 2>/dev/null || true)
330+ if [ -n "$pip_error_tail" ]; then
331+ while IFS= read -r line; do
332+ log_warn "pip: $line"
333+ done <<< "$pip_error_tail"
334+ fi
335+ rm -f "$pip_log"
336+ return 1
337+}
338+ 
217install_python_deps() {339install_python_deps() {
218 log_info "Checking Python dependencies..."340 log_info "Checking Python dependencies..."
219 341 
220 local python="python3"342 local python="python3"
221 check_command python3 || python="python"343 check_command python3 || python="python"
222- 344+ 
345+ if ! check_command "$python"; then
346+ log_error "Python is not installed"
347+ return 1
348+ fi
349+ 
223 log_info "Python: $($python --version 2>&1)"350 log_info "Python: $($python --version 2>&1)"
224 351 
225- local required=("pytest>=9.0.1" "coverage>=7.10.0" "pytest-cov>=7.0.0" "pytest-mock>=3.14.0")352+ local requirements_file="${1:-requirements.txt}"
226- local to_install=()353+ local remote_requirements_url="${OAM_TOOLS_RAW_BASE_URL}/requirements.txt"
354+ local requirements_content=""
227 355 
228- for pkg in "${required[@]}"; do356+ if [ -f "$requirements_file" ]; then
229- local name=$(echo "$pkg" | sed 's/>=.*//')357+ log_info "Installing Python dependencies from: $requirements_file"
230- $python -c "import $name" 2>/dev/null || to_install+=("$pkg")358+ run_pip_command "$python" install --upgrade pip -q || log_warn "Failed to upgrade pip, continuing"
231- done359+ run_pip_command "$python" install -r "$requirements_file" -q || {
232- 360+ log_error "Failed to install Python dependencies from: $requirements_file"
233- if [ ${#to_install[@]} -ne 0 ]; then361+ return 1
234- log_info "Installing Python packages: ${to_install[*]}"362+ }
235- $python -m pip install --upgrade pip -q 2>/dev/null || true363+ elif check_command curl; then
236- $python -m pip install "${to_install[@]}" -q 2>/dev/null || \364+ log_warn "requirements.txt not found locally, installing from: $remote_requirements_url"
237- log_warn "Some Python packages may have failed to install"365+ requirements_content=$(curl -fsSL "$remote_requirements_url") || {
366+ log_error "Failed to fetch requirements.txt"
367+ return 1
368+ }
zhangjie
zhangjiezhangjie7月10日

级别:一般 问题:远程获取 requirements.txt 失败时 return 0 静默跳过 Python 依赖安装 影响:用户在无本地 requirements.txt 且网络不通时,Python 依赖完全缺失但脚本报告成功,后续 pytest 等工具不可用且原因不明显 修复建议:远程获取失败时应 log_error 并 return 1,或至少在脚本末尾汇总报告中标注 'Python dependencies: NOT installed'

likedislike
369+ local tmp_requirements
370+ tmp_requirements=$(mktemp "${TMPDIR:-/tmp}/oam_requirements.XXXXXX") || {
371+ log_error "Failed to create temporary requirements file"
372+ return 1
373+ }
374+ printf '%s\n' "$requirements_content" > "$tmp_requirements" || {
375+ rm -f "$tmp_requirements"
376+ log_error "Failed to write temporary requirements file"
377+ return 1
378+ }
379+ run_pip_command "$python" install --upgrade pip -q || log_warn "Failed to upgrade pip, continuing"
380+ run_pip_command "$python" install -r "$tmp_requirements" -q || {
381+ rm -f "$tmp_requirements"
382+ log_error "Failed to install Python dependencies from: $remote_requirements_url"
383+ return 1
384+ }
385+ rm -f "$tmp_requirements"
238 else386 else
zhangjie
zhangjiezhangjie7月10日

级别:一般 问题:printf '%s\n' "$requirements_content" | $python -m pip install -r /dev/stdin 在部分环境(如某些容器或旧版 pip)中 /dev/stdin 不可用或不被 pip 支持 影响:远程 requirements 安装路径在某些环境下静默失败(stderr 被 2>/dev/null 抑制),用户无法察觉 修复建议:改用临时文件:将 requirements_content 写入 mktemp 生成的临时文件后 pip install -r 该文件,完成后清理;同时移除 2>/dev/null 或改为 2>&1 | tail -1 以保留关键错误信息

likedislike
239- log_info "All Python dependencies satisfied"387+ log_error "requirements.txt not found and curl is unavailable, cannot install Python dependencies"
388+ return 1
240 fi389 fi
241 390 
242 check_command pytest && log_info "pytest: $(pytest --version 2>&1 | head -1)"391 check_command pytest && log_info "pytest: $(pytest --version 2>&1 | head -1)"
@@ -244,13 +393,22 @@ install_python_deps() {
244}393}
245 394 
246show_help() {395show_help() {
396+ local work_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
397+ local version_file="${work_dir}/version.cmake"
398+ local default_cann_version="${CANN_VERSION:-}"
399+ 
400+ if [ -z "$default_cann_version" ] && [ -f "$version_file" ]; then
zhangjie
zhangjiezhangjie7月10日

级别:提示 问题:show_help 中 default_cann_version 回退显示为字面量 'version.cmake',含义不明确 影响:用户看到 '--cann-version VERSION (default: version.cmake)' 时困惑,不知道实际默认版本号 修复建议:回退时显示 'auto-detect from version.cmake' 或 'unknown' 而非 'version.cmake'

likedislike
401+ default_cann_version=$(get_cann_version_from_cmake "$version_file" 2>/dev/null || true)
402+ fi
403+ default_cann_version="${default_cann_version:-auto-detect from version.cmake}"
404+ 
247 echo "Usage: $0 [OPTIONS]"405 echo "Usage: $0 [OPTIONS]"
248 echo ""406 echo ""
249 echo "oam-tools development environment setup script"407 echo "oam-tools development environment setup script"
250 echo ""408 echo ""
251 echo "Options:"409 echo "Options:"
252- echo " --cann-version VERSION CANN version (default: ${CANN_VERSION})"410+ echo " --cann-version VERSION CANN version (default: ${default_cann_version})"
253- echo " --chip-type TYPE Chip type: 910b, 910_93, etc. (default: ${CHIP_TYPE})"411+ echo " --chip-type TYPE Chip type examples: 910b, 910_93/A3, 950 (default: ${CHIP_TYPE})"
254 echo " --install-path PATH Installation path (default: ${INSTALL_PATH})"412 echo " --install-path PATH Installation path (default: ${INSTALL_PATH})"
255 echo " --skip-cann Skip CANN installation"413 echo " --skip-cann Skip CANN installation"
256 echo " --skip-ops, --toolkit-only Skip ops package download (compile-only scenario)"414 echo " --skip-ops, --toolkit-only Skip ops package download (compile-only scenario)"
@@ -260,12 +418,14 @@ show_help() {
260 echo " $0 # Install with defaults"418 echo " $0 # Install with defaults"
261 echo " $0 --skip-cann # Skip CANN, install only deps"419 echo " $0 --skip-cann # Skip CANN, install only deps"
262 echo " $0 --skip-ops # Install toolkit only, skip ops"420 echo " $0 --skip-ops # Install toolkit only, skip ops"
263- echo " $0 --chip-type 910_93 # Use 910_93 ops package"421+ echo " $0 --chip-type 910_93 # Use A3 ops package"
264 echo ""422 echo ""
265}423}
266 424 
267main() {425main() {
268 local skip_cann=false426 local skip_cann=false
427+ local work_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
428+ local version_file="${work_dir}/version.cmake"
269 429 
270 while [[ $# -gt 0 ]]; do430 while [[ $# -gt 0 ]]; do
271 case $1 in431 case $1 in
@@ -300,6 +460,18 @@ main() {
300 esac460 esac
301 done461 done
302 462 
463+ if [ -z "$CANN_VERSION" ]; then
zhangjie
zhangjiezhangjie7月10日

级别:严重 问题:get_cann_version_from_cmake 失败后无校验,CANN_VERSION 可能为空字符串,后续 install_cann 拼接出错误的下载 URL(如 .../software//linux-x86_64/Ascend-cann-toolkit__linux-...) 影响:若 version.cmake 不存在且远程获取失败,或正则未匹配到版本号,脚本会以空版本继续执行,导致下载失败且错误信息不直观;若 set -e 开启则脚本直接中断无明确提示 修复建议:在 main 中调用 get_cann_version_from_cmake 后增加校验:if [ -z "$CANN_VERSION" ]; then log_error "CANN version could not be determined, please specify --cann-version"; exit 1; fi

likedislike
464+ if ! CANN_VERSION=$(get_cann_version_from_cmake "$version_file"); then
465+ log_error "CANN version could not be determined, please specify --cann-version"
466+ exit 1
467+ fi
468+ fi
469+ 
470+ if [ -z "$CANN_VERSION" ]; then
471+ log_error "CANN version could not be determined, please specify --cann-version"
472+ exit 1
473+ fi
474+ 
303 echo ""475 echo ""
304 echo "=========================================="476 echo "=========================================="
305 echo " oam-tools Development Environment Setup"477 echo " oam-tools Development Environment Setup"
@@ -312,7 +484,6 @@ main() {
312 echo " Skip Ops: ${SKIP_OPS}"484 echo " Skip Ops: ${SKIP_OPS}"
313 echo ""485 echo ""
314 486 
315- local work_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
316 cd "$work_dir"487 cd "$work_dir"
317 log_info "Working directory: $work_dir"488 log_info "Working directory: $work_dir"
318 489 
@@ -325,7 +496,7 @@ main() {
325 fi496 fi
326 497 
327 setup_cann_env498 setup_cann_env
328- install_python_deps499+ install_python_deps "${work_dir}/requirements.txt"
329 500 
330 echo ""501 echo ""
331 log_info "=========================================="502 log_info "=========================================="