2020 echo " INFO: Tools directory ${TOOLS_DIR} already exists"
2121fi
2222
23- # Define API mapping files URLs (only main URLs, no backups)
23+ # Define API mapping files URLs
2424API_ALIAS_MAPPING_URL=" https://raw.githubusercontent.com/PaddlePaddle/PaConvert/master/paconvert/api_alias_mapping.json"
2525API_MAPPING_URL=" https://raw.githubusercontent.com/PaddlePaddle/PaConvert/master/paconvert/api_mapping.json"
2626GLOBAL_VAR_URL=" https://raw.githubusercontent.com/PaddlePaddle/PaConvert/master/paconvert/global_var.py"
4545 CURL_PROXY_ARGS=" "
4646fi
4747
48- # Download API mapping files with retry
48+ # Handle failure: copy cached file and exit
49+ handle_failure () {
50+ local cached_file=" ${APIMAPPING_ROOT} /cached_pytorch_api_mapping_cn.md"
51+ local target_file=" ${APIMAPPING_ROOT} /pytorch_api_mapping_cn.md"
52+
53+ if [ -f " $cached_file " ]; then
54+ echo " INFO: Copying cached file to target: $cached_file -> $target_file "
55+ cp " $cached_file " " $target_file "
56+ echo " INFO: Successfully copied cached file to $target_file "
57+ exit 0
58+ else
59+ echo " ERROR: Cached file not found at $cached_file "
60+ exit 1
61+ fi
62+ }
63+
64+ # Download file with retry and failure handling
4965download_file () {
5066 local url=$1
5167 local dest=$2
@@ -69,91 +85,26 @@ download_file() {
6985 done
7086
7187 echo " ERROR: Failed to download ${filename} after $max_retries attempts"
72- return 1
88+ handle_failure
7389}
7490
75- # Check if cached file exists
76- check_cached_file () {
77- local cached_file=" ${APIMAPPING_ROOT} /cached_pytorch_api_mapping_cn.md"
78- if [ -f " $cached_file " ]; then
79- echo " INFO: Cached file found at $cached_file "
80- return 0
81- else
82- echo " INFO: No cached file found at $cached_file "
83- return 1
84- fi
85- }
86-
87- # Copy cached file to target if download fails
88- copy_cached_file () {
89- local cached_file=" ${APIMAPPING_ROOT} /cached_pytorch_api_mapping_cn.md"
90- local target_file=" ${APIMAPPING_ROOT} /pytorch_api_mapping_cn.md"
91-
92- if [ -f " $cached_file " ]; then
93- echo " INFO: Copying cached file to target: $cached_file -> $target_file "
94- cp " $cached_file " " $target_file "
95- echo " INFO: Successfully copied cached file to $target_file "
96- return 0
97- else
98- echo " ERROR: Cached file not found at $cached_file "
99- return 1
100- fi
101- }
102-
103- # Function to handle download failure
104- handle_download_failure () {
105- local filename=$1
106- echo " INFO: Download or Execute $filename failed. Checking for cached file..."
107- if check_cached_file; then
108- if copy_cached_file; then
109- echo " INFO: Successfully copied cached file. Exiting."
110- exit 0
111- else
112- echo " ERROR: Failed to copy cached file to target"
113- exit 1
114- fi
115- else
116- echo " ERROR: Download failed and no cached file available"
117- exit 1
118- fi
119- }
120-
121- # Download API alias mapping file
122- echo " INFO: Downloading API alias mapping file"
123- if ! download_file " ${API_ALIAS_MAPPING_URL} " " ${TOOLS_DIR} /api_alias_mapping.json" ; then
124- handle_download_failure " API alias mapping"
125- fi
126-
127- # Download API mapping file
128- echo " INFO: Downloading API mapping file"
129- if ! download_file " ${API_MAPPING_URL} " " ${TOOLS_DIR} /api_mapping.json" ; then
130- handle_download_failure " API mapping"
131- fi
132-
133- # Download global variable file
134- echo " INFO: Downloading global variable file"
135- if ! download_file " ${GLOBAL_VAR_URL} " " ${TOOLS_DIR} /global_var.py" ; then
136- handle_download_failure " Global variable"
137- fi
138-
139- # Download attribute mapping file
140- echo " INFO: Downloading attribute mapping file"
141- if ! download_file " ${ATTRIBUTE_MAPPING_URL} " " ${TOOLS_DIR} /attribute_mapping.json" ; then
142- handle_download_failure " Attribute mapping"
143- fi
91+ # Download all API mapping files
92+ download_file " ${API_ALIAS_MAPPING_URL} " " ${TOOLS_DIR} /api_alias_mapping.json"
93+ download_file " ${API_MAPPING_URL} " " ${TOOLS_DIR} /api_mapping.json"
94+ download_file " ${GLOBAL_VAR_URL} " " ${TOOLS_DIR} /global_var.py"
95+ download_file " ${ATTRIBUTE_MAPPING_URL} " " ${TOOLS_DIR} /attribute_mapping.json"
14496
145- # If we get here, all files were downloaded successfully
14697echo " INFO: All API mapping files successfully downloaded"
14798
148- # Run the remaining scripts
99+ # Run the remaining scripts with failure handling
149100echo " INFO: Running get_api_difference_info.py"
150101if ! python " ${APIMAPPING_ROOT} /tools/get_api_difference_info.py" ; then
151- handle_download_failure " get_api_difference_info.py "
102+ handle_failure
152103fi
153104
154105echo " INFO: Running generate_pytorch_api_mapping.py"
155106if ! python " ${APIMAPPING_ROOT} /tools/generate_pytorch_api_mapping.py" ; then
156- handle_download_failure " generate_pytorch_api_mapping.py "
107+ handle_failure
157108fi
158109
159110# Create backup of generated file
@@ -167,5 +118,5 @@ if [ -f "$GENERATED_FILE" ]; then
167118 echo " INFO: Successfully created backup file at $BACKUP_FILE "
168119else
169120 echo " ERROR: Generated API mapping file not found at $GENERATED_FILE "
170- exit 1
121+ handle_failure
171122fi
0 commit comments