Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
102 changes: 79 additions & 23 deletions ci_scripts/hooks/pre-doc-compile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,20 @@ else
echo "INFO: Tools directory ${TOOLS_DIR} already exists"
fi

# Define API mapping files URLs
# Define API mapping files URLs (only main URLs, no backups)
API_ALIAS_MAPPING_URL="https://raw.githubusercontent.com/PaddlePaddle/PaConvert/master/paconvert/api_alias_mapping.json"
API_MAPPING_URL="https://raw.githubusercontent.com/PaddlePaddle/PaConvert/master/paconvert/api_mapping.json"
GLOBAL_VAR_URL="https://raw.githubusercontent.com/PaddlePaddle/PaConvert/master/paconvert/global_var.py"
ATTRIBUTE_MAPPING_URL="https://raw.githubusercontent.com/PaddlePaddle/PaConvert/master/paconvert/attribute_mapping.json"

# Define backup URLs
BACKUP_API_ALIAS_MAPPING_URL="https://paddle-paconvert.bj.bcebos.com/api_alias_mapping.json"
BACKUP_API_MAPPING_URL="https://paddle-paconvert.bj.bcebos.com/api_mapping.json"
BACKUP_GLOBAL_VAR_URL="https://paddle-paconvert.bj.bcebos.com/global_var.py"
BACKUP_ATTRIBUTE_MAPPING_URL="https://paddle-paconvert.bj.bcebos.com/attribute_mapping.json"

# Check for proxy settings
PROXY=""
if [ -n "$https_proxy" ]; then
PROXY="$https_proxy"
echo "INFO: find proxy"
echo "INFO: Using proxy"
elif [ -n "$http_proxy" ]; then
PROXY="$http_proxy"
echo "INFO: find proxy"
echo "INFO: Using proxy"
else
echo "INFO: No proxy detected, downloading directly."
fi
Expand All @@ -56,7 +50,7 @@ download_file() {
local url=$1
local dest=$2
local filename=$(basename "$dest")
local max_retries=5
local max_retries=3
local retry_count=0

echo "INFO: Starting download of ${filename} from ${url}"
Expand All @@ -78,39 +72,87 @@ download_file() {
return 1
}

# Download each file with detailed logging
# Check if cached file exists
check_cached_file() {
local cached_file="${APIMAPPING_ROOT}/cached_pytorch_api_mapping_cn.md"
if [ -f "$cached_file" ]; then
echo "INFO: Cached file found at $cached_file"
return 0
else
echo "INFO: No cached file found at $cached_file"
return 1
fi
}

# Copy cached file to target if download fails
copy_cached_file() {
local cached_file="${APIMAPPING_ROOT}/cached_pytorch_api_mapping_cn.md"
local target_file="${APIMAPPING_ROOT}/pytorch_api_mapping_cn.md"

if [ -f "$cached_file" ]; then
echo "INFO: Copying cached file to target: $cached_file -> $target_file"
cp "$cached_file" "$target_file"
echo "INFO: Successfully copied cached file to $target_file"
return 0
else
echo "ERROR: Cached file not found at $cached_file"
return 1
fi
}

# Download API mapping files
echo "INFO: Downloading API alias mapping file"
if ! download_file "${API_ALIAS_MAPPING_URL}" "${TOOLS_DIR}/api_alias_mapping.json"; then
echo "INFO: Trying backup URL for API alias mapping file"
if ! download_file "${BACKUP_API_ALIAS_MAPPING_URL}" "${TOOLS_DIR}/api_alias_mapping.json"; then
echo "ERROR: API alias mapping download failed (both main and backup URLs). Exiting."
echo "INFO: API alias mapping download failed. Checking for cached file..."
if check_cached_file; then
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里的逻辑需要优化一下,如果有1个没下载成功。则复制cache,同时后面也不需要再下载文件,也不需要再跑 api_alias_mapping判断、get_api_difference_info、generate_pytorch_api_mapping这些冗余逻辑。

也就是要么走 自动生成generate_pytorch_api_mapping整体的逻辑、要么复制cache。两者二选一。

if ! copy_cached_file; then
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果copy_cached_file了,那么就不用继续下载其他的文件,也不用跑下面的get_api_difference_info、generate_pytorch_api_mapping这些逻辑。直接就结束了。

echo "ERROR: Failed to copy cached file to target"
exit 1
fi
else
echo "ERROR: API alias mapping download failed and no cached file available"
exit 1
fi
fi

echo "INFO: Downloading API mapping file"
if ! download_file "${API_MAPPING_URL}" "${TOOLS_DIR}/api_mapping.json"; then
echo "INFO: Trying backup URL for API mapping file"
if ! download_file "${BACKUP_API_MAPPING_URL}" "${TOOLS_DIR}/api_mapping.json"; then
echo "ERROR: API mapping download failed (both main and backup URLs). Exiting."
echo "INFO: API mapping download failed. Checking for cached file..."
if check_cached_file; then
if ! copy_cached_file; then
echo "ERROR: Failed to copy cached file to target"
exit 1
fi
else
echo "ERROR: API mapping download failed and no cached file available"
exit 1
fi
fi

echo "INFO: Downloading global variable file"
if ! download_file "${GLOBAL_VAR_URL}" "${TOOLS_DIR}/global_var.py"; then
echo "INFO: Trying backup URL for global variable file"
if ! download_file "${BACKUP_GLOBAL_VAR_URL}" "${TOOLS_DIR}/global_var.py"; then
echo "ERROR: Global variable download failed (both main and backup URLs). Exiting."
echo "INFO: Global variable download failed. Checking for cached file..."
if check_cached_file; then
if ! copy_cached_file; then
echo "ERROR: Failed to copy cached file to target"
exit 1
fi
else
echo "ERROR: Global variable download failed and no cached file available"
exit 1
fi
fi

echo "INFO: Downloading attribute mapping file"
if ! download_file "${ATTRIBUTE_MAPPING_URL}" "${TOOLS_DIR}/attribute_mapping.json"; then
echo "INFO: Trying backup URL for attribute mapping file"
if ! download_file "${BACKUP_ATTRIBUTE_MAPPING_URL}" "${TOOLS_DIR}/attribute_mapping.json"; then
echo "ERROR: Attribute mapping download failed (both main and backup URLs). Exiting."
echo "INFO: Attribute mapping download failed. Checking for cached file..."
if check_cached_file; then
if ! copy_cached_file; then
echo "ERROR: Failed to copy cached file to target"
exit 1
fi
else
echo "ERROR: Attribute mapping download failed and no cached file available"
exit 1
fi
fi
Expand Down Expand Up @@ -142,3 +184,17 @@ if ! python "${APIMAPPING_ROOT}/tools/generate_pytorch_api_mapping.py"; then
echo "ERROR: generate_pytorch_api_mapping.py failed. Please check the script."
exit 1
fi

# Create backup of generated file
BACKUP_FILE="${APIMAPPING_ROOT}/cached_pytorch_api_mapping_cn.md"
GENERATED_FILE="${APIMAPPING_ROOT}/pytorch_api_mapping_cn.md"

if [ -f "$GENERATED_FILE" ]; then
echo "INFO: Generated API mapping file successfully created at $GENERATED_FILE"
echo "INFO: Creating backup file: $BACKUP_FILE"
cp "$GENERATED_FILE" "$BACKUP_FILE"
echo "INFO: Successfully created backup file at $BACKUP_FILE"
else
echo "ERROR: Generated API mapping file not found at $GENERATED_FILE"
exit 1
fi
Loading