Skip to content

Commit e3cafca

Browse files
authored
Merge pull request #54 from SummerGGift/20180910
【完善】:下载软件包和子模块时对镜像地址进行有效性检查
2 parents 9397304 + d4fb61a commit e3cafca

File tree

1 file changed

+44
-33
lines changed

1 file changed

+44
-33
lines changed

cmds/cmd_package.py

Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def modify_submod_file_to_mirror(submod_path):
132132
get_package_url, get_ver_sha = get_url_from_mirror_server(
133133
query_submodule_name, 'latest')
134134

135-
if get_package_url != None:
135+
if get_package_url != None and determine_url_valid(get_package_url):
136136
replace_list.append(
137137
(submod_git_url, replace_url, submodule_name))
138138

@@ -202,8 +202,32 @@ def get_url_from_mirror_server(pkgs_name_in_json, pkgs_ver):
202202

203203
except Exception, e:
204204
print('e.message:%s\t' % e.message)
205-
print(
206-
"The server could not be contacted. Please check your network connection.")
205+
print("The server could not be contacted. Please check your network connection.")
206+
207+
208+
def determine_url_valid(url_from_srv):
209+
210+
headers = {'Connection': 'keep-alive',
211+
'Accept-Encoding': 'gzip, deflate',
212+
'Accept': '*/*',
213+
'User-Agent': 'curl/7.54.0'}
214+
215+
try:
216+
for i in range(0, 3):
217+
r = requests.get(url_from_srv, stream=True, headers=headers)
218+
if r.status_code == requests.codes.not_found:
219+
if i == 2:
220+
print("Warning : %s is invalid." % package_url)
221+
return False
222+
time.sleep(1)
223+
else:
224+
break
225+
226+
return True
227+
228+
except Exception, e:
229+
# print('e.message:%s\t' % e.message)
230+
print('Network connection error or the url : %s is invalid.\n' % url_from_srv)
207231

208232

209233
def install_pkg(env_root, bsp_root, pkg):
@@ -242,41 +266,34 @@ def install_pkg(env_root, bsp_root, pkg):
242266

243267
get_package_url = None
244268
get_ver_sha = None
269+
upstream_change_flag = False
245270

246271
if os.path.isfile(env_config_file) and find_macro_in_config(env_config_file, 'SYS_PKGS_DOWNLOAD_ACCELERATE'):
247272
get_package_url, get_ver_sha = get_url_from_mirror_server(pkgs_name_in_json, pkg['ver'])
248273

249-
if get_package_url != None:
250-
package_url = get_package_url
251-
252-
if get_ver_sha != None:
253-
ver_sha = get_ver_sha
274+
# determine whether the package package url is valid
275+
if get_package_url != None and determine_url_valid(get_package_url):
276+
package_url = get_package_url
254277

255-
beforepath = os.getcwd()
278+
if get_ver_sha != None:
279+
ver_sha = get_ver_sha
256280

257-
# print(package_url)
281+
upstream_change_flag = True
258282

259283
if package_url[-4:] == '.git':
284+
260285
repo_path = os.path.join(bsp_pkgs_path, pkgs_name_in_json)
261286
repo_path = repo_path + '-' + pkg['ver']
262-
cmd = 'git clone ' + package_url + ' ' + repo_path
263-
os.system(cmd)
264-
os.chdir(repo_path)
265287

266-
# #print("Checkout SHA : %s"%ver_sha)
267-
# cmd = 'git reset --hard ' + ver_sha
268-
# os.system(cmd)
269-
270-
# print("cwd : %s"%os.getcwd())
271-
# print("repo_path : %s"%repo_path)
288+
cmd = 'git clone ' + package_url + ' ' + repo_path
289+
execute_command(cmd, cwd=bsp_pkgs_path)
272290

273-
if os.getcwd() != repo_path:
274-
print("Error : Can't find dir : repo_path.\n %s download fail.",
275-
pkgs_name_in_json)
276-
return
291+
cmd = 'git checkout -q ' + ver_sha
292+
execute_command(cmd, cwd=repo_path)
277293

278-
cmd = 'git checkout ' + ver_sha
279-
os.system(cmd)
294+
if upstream_change_flag:
295+
cmd = 'git remote set-url origin ' + url_from_json
296+
execute_command(cmd, cwd=repo_path)
280297

281298
# If there is a .gitmodules file in the package, prepare to update the
282299
# submodule.
@@ -288,8 +305,7 @@ def install_pkg(env_root, bsp_root, pkg):
288305
replace_list = modify_submod_file_to_mirror(submod_path) # Modify .gitmodules file
289306

290307
cmd = 'git submodule update --init --recursive'
291-
if not os.system(cmd):
292-
print("Submodule update successful")
308+
execute_command(cmd, cwd=repo_path)
293309

294310
if os.path.isfile(env_config_file) and find_macro_in_config(env_config_file, 'SYS_PKGS_DOWNLOAD_ACCELERATE'):
295311
if len(replace_list):
@@ -299,15 +315,10 @@ def install_pkg(env_root, bsp_root, pkg):
299315
cmd = 'git remote set-url origin ' + item[0]
300316
execute_command(cmd, cwd=submod_dir_path)
301317

302-
cmd = 'git remote set-url origin ' + url_from_json
303-
os.system(cmd)
304-
305318
if os.path.isfile(env_config_file) and find_macro_in_config(env_config_file, 'SYS_PKGS_DOWNLOAD_ACCELERATE'):
306319
if os.path.isfile(submod_path):
307320
cmd = 'git checkout .gitmodules'
308-
os.system(cmd)
309-
310-
os.chdir(beforepath)
321+
execute_command(cmd, cwd=repo_path)
311322

312323
else:
313324
# Download a package of compressed package type.

0 commit comments

Comments
 (0)