Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
251 changes: 125 additions & 126 deletions axvisor.sh

Large diffs are not rendered by default.

136 changes: 68 additions & 68 deletions scripts/bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,51 @@
# -*- coding: utf-8 -*-

# Axvisor Bootstrap Script
# 此脚本用于创建 Python 虚拟环境并安装 task.py 所需的依赖
# This script creates a Python virtual environment and installs task.py dependencies

set -e # 遇到错误时退出
set -e # Exit on error

# 颜色输出
# Colored output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 输出函数
# Output helpers
info() { echo -e "${BLUE}ℹ${NC} $1"; }
success() { echo -e "${GREEN}✓${NC} $1"; }
warning() { echo -e "${YELLOW}⚠${NC} $1"; }
error() { echo -e "${RED}✗${NC} $1"; }

# 获取项目根目录
# Get project root directory
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
cd "$PROJECT_ROOT"

# 检查是否已经在虚拟环境中
# If already inside a virtual environment, just update deps
if [[ -n "$VIRTUAL_ENV" ]]; then
info "检测到已在虚拟环境中: $VIRTUAL_ENV"
info "Detected active virtual environment: $VIRTUAL_ENV"

# 检查 requirements.txt 文件是否存在
# Ensure requirements.txt exists
if [[ ! -f "scripts/requirements.txt" ]]; then
error "scripts/requirements.txt 文件未找到"
error "scripts/requirements.txt not found"
exit 1
fi

# 安装/更新依赖
info "更新 Python 依赖..."
# Install / update dependencies
info "Updating Python dependencies..."
pip install -q -r scripts/requirements.txt -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple
success "依赖更新完成"
success "Dependencies updated"
exit 0
fi

# 虚拟环境和标记文件
# Virtual environment and marker file
VENV_DIR="venv"
MARKER_FILE="$VENV_DIR/.bootstrapped"
REQUIREMENTS_FILE="scripts/requirements.txt"

# 计算依赖哈希值
# Compute dependency hash
compute_dep_hash() {
local pyver
pyver=$(python3 --version 2>/dev/null || echo "unknown")
Expand All @@ -60,128 +60,128 @@ compute_dep_hash() {
fi
}

# 检查是否需要重新引导
# Decide if bootstrap is needed
check_bootstrap_needed() {
# 如果虚拟环境不存在,需要引导
# Need bootstrap if venv directory missing
if [[ ! -d "$VENV_DIR" ]]; then
return 0 # 需要引导
return 0 # need bootstrap
fi

# 如果标记文件不存在,需要引导
# Need bootstrap if marker file missing
if [[ ! -f "$MARKER_FILE" ]]; then
return 0 # 需要引导
return 0 # need bootstrap
fi

# 检查哈希值是否匹配
# Check dependency hash
local existing_hash current_hash
existing_hash=$(awk -F":" '/^hash:/ {print $2}' "$MARKER_FILE" 2>/dev/null | tr -d '[:space:]') || existing_hash=""
current_hash=$(compute_dep_hash)

if [[ "$existing_hash" != "$current_hash" ]]; then
info "检测到依赖变更,需要重新引导"
return 0 # 需要引导
info "Dependency changes detected, re-bootstrap required"
return 0 # need bootstrap
fi

# 检查虚拟环境的Python是否可用
# Ensure python executable exists in venv
if [[ ! -x "$VENV_DIR/bin/python3" ]]; then
warning "虚拟环境的 Python 不可用,需要重新引导"
return 0 # 需要引导
warning "Python in virtual env not available, re-bootstrap required"
return 0 # need bootstrap
fi

return 1 # 不需要引导
return 1 # bootstrap not needed
}
# 快速检查并退出
# Fast path: already bootstrapped
if ! check_bootstrap_needed; then
success "引导已完成且依赖未更改,跳过引导"
success "Bootstrap already done and dependencies unchanged, skipping"
exit 0
fi

info "开始设置 Python 虚拟环境..."
info "Starting Python virtual environment setup..."

# 检查系统依赖
# Check system dependencies
check_system_deps() {
info "检查系统依赖..."
info "Checking system dependencies..."

# 检查 Python 3
# Check python3 exists
if ! command -v python3 >/dev/null 2>&1; then
error "python3 未找到,请先安装 Python 3"
error "python3 not found. Please install Python 3"
exit 1
fi

# 检查 Python 版本
# Report Python version
local pyver
pyver=$(python3 --version 2>&1 | awk '{print $2}' | cut -d. -f1,2)
info "检测到 Python 版本: $pyver"
info "Detected Python version: $pyver"

# 检查 venv 模块
# Check venv module
if ! python3 -c "import venv" 2>/dev/null; then
error "python3-venv 模块未找到"
echo "请安装 python3-venv:"
echo " Ubuntu/Debian: sudo apt install python3-venv"
echo " CentOS/RHEL: sudo yum install python3-venv"
echo " Fedora: sudo dnf install python3-venv"
error "python3-venv module not found"
echo "Install python3-venv via your package manager:"
echo " Ubuntu/Debian: sudo apt install python3-venv"
echo " CentOS/RHEL: sudo yum install python3-venv"
echo " Fedora: sudo dnf install python3-venv"
exit 1
fi

# 检查 requirements.txt
# Check requirements.txt exists
if [[ ! -f "$REQUIREMENTS_FILE" ]]; then
error "$REQUIREMENTS_FILE 文件未找到"
error "$REQUIREMENTS_FILE not found"
exit 1
fi

success "系统依赖检查完成"
success "System dependency check passed"
}

# 创建虚拟环境
# Create virtual environment
setup_venv() {
info "设置虚拟环境..."
info "Preparing virtual environment..."

# 如果虚拟环境已存在但损坏,删除它
# Remove broken venv
if [[ -d "$VENV_DIR" ]] && [[ ! -x "$VENV_DIR/bin/python3" ]]; then
warning "检测到损坏的虚拟环境,正在删除..."
warning "Corrupted virtual environment detected, removing..."
rm -rf "$VENV_DIR"
fi

# 创建虚拟环境
# Create venv if missing
if [[ ! -d "$VENV_DIR" ]]; then
info "创建新的虚拟环境..."
info "Creating new virtual environment..."
python3 -m venv "$VENV_DIR"
success "虚拟环境已创建"
success "Virtual environment created"
else
info "使用现有虚拟环境"
info "Using existing virtual environment"
fi
}

# 安装依赖
# Install dependencies
install_deps() {
info "安装 Python 依赖..."
info "Installing Python dependencies..."

# 激活虚拟环境
source "$VENV_DIR/bin/activate"

# 升级 pip(静默)
# Upgrade pip (quiet)
python -m pip install -q --upgrade pip -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple

# 安装依赖
# Install requirements
pip install -q -r "$REQUIREMENTS_FILE" -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple

success "依赖安装完成"
success "Dependencies installed"
}

# 验证安装
# Verify installation
verify_installation() {
info "验证安装..."
info "Verifying installation..."

# 测试 task.py
if source "$VENV_DIR/bin/activate" && python3 ./scripts/task.py --help >/dev/null 2>&1; then
success "task.py 运行正常"
success "task.py runs correctly"
else
error "task.py 运行失败"
error "task.py execution failed"
exit 1
fi
}

# 写入完成标记
# Write completion marker
write_marker() {
local dep_hash
dep_hash=$(compute_dep_hash)
Expand All @@ -192,21 +192,21 @@ timestamp: $(date -u +%Y-%m-%dT%H:%M:%SZ)
python_version: $(python3 --version)
EOF

success "引导完成标记已写入 (hash: ${dep_hash:0:8}...)"
success "Bootstrap marker written (hash: ${dep_hash:0:8}...)"
}

# 主要执行流程
# Main execution flow
main() {
check_system_deps
setup_venv
install_deps
verify_installation
write_marker

success "虚拟环境设置完成!"
info "使用 'source venv/bin/activate' 激活环境"
info "使用 'make help' 查看可用命令"
success "Virtual environment setup complete!"
info "Activate with: source venv/bin/activate"
info "Use 'make help' to see available commands"
}

# 执行主函数
# Execute main function
main "$@"
42 changes: 22 additions & 20 deletions scripts/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,50 +9,52 @@


def main(args) -> int:
"""构建项目"""
print("执行 build 功能...")
"""Build the project"""
print("Running build task...")

# 获取配置文件路径
# Get config file path
config_file_path = getattr(args, "config", ".hvconfig.toml")

# 检查配置文件是否存在
# Check if config file exists
config_exists = os.path.exists(config_file_path)

# 首先设置 arceos 依赖
print("设置 arceos 依赖...")
# Setup arceos dependency first
print("Setting up arceos dependency...")
if not setup_arceos():
print("设置 arceos 失败,无法继续构建")
print("Failed to setup arceos, cannot build")
return 1

# 创建配置对象
# Create config object
config: AxvisorConfig = create_config_from_args(args)

# 构建 make 命令
# Build make command
cmd = config.format_make_command("")

print(f"执行命令: {cmd}")
print(f"Executing: {cmd}")

try:
# 执行 make 命令
# Run make command
result = subprocess.run(
cmd, shell=True, check=True, env=config.get_subprocess_env()
)
print("构建成功!")
print("Build succeeded!")

# 如果配置文件不存在且有有意义的命令行参数,则创建配置文件
# If config file missing and CLI args meaningful, create the config file
if not config_exists:
print(f"检测到 {config_file_path} 不存在,根据命令行参数创建配置文件...")
print(
f"Detected missing {config_file_path}, creating config file from arguments..."
)
if save_config_to_file(config, config_file_path):
print(
f"配置文件创建成功,下次可以直接运行 './task.py build -c {config_file_path}' 而无需指定参数"
f"Config file created. Next time just run './task.py build -c {config_file_path}'"
)
else:
print("配置文件创建失败,下次仍需手动指定参数")
print(
"Failed to create config file; you'll need to pass arguments again next run"
)

return 0
except subprocess.CalledProcessError as e:
print(f"构建失败,退出码: {e.returncode}")
print(f"Build failed, exit code: {e.returncode}")
return e.returncode
except Exception as e:
print(f"构建过程中发生错误: {e}")
print(f"Error during build: {e}")
return 1
22 changes: 11 additions & 11 deletions scripts/clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,32 @@


def main(args) -> int:
"""清理构建产物"""
print("执行 clean 功能...")
"""Clean build artifacts"""
print("Running clean task...")

# 首先设置 arceos 依赖
print("设置 arceos 依赖...")
# Setup arceos dependency first
print("Setting up arceos dependency...")
if not setup_arceos():
print("设置 arceos 失败,无法继续执行 clean")
print("Failed to setup arceos, cannot clean")
return 1

cmd = format_make_command_base()

cmd.append("clean")

# 构建 make 命令
# Build make command string
cmd = " ".join(cmd)

print(f"执行命令: {cmd}")
print(f"Executing: {cmd}")

try:
# 执行 make 命令
# Run make command
subprocess.run(cmd, shell=True, check=True)
print("清理完成!")
print("Clean succeeded!")
return 0
except subprocess.CalledProcessError as e:
print(f"清理失败,退出码: {e.returncode}")
print(f"Clean failed, exit code: {e.returncode}")
return e.returncode
except Exception as e:
print(f"清理过程中发生错误: {e}")
print(f"Error during clean: {e}")
return 1
Loading
Loading