Skip to content

Commit ba96f77

Browse files
authored
Merge pull request #58 from RT-Thread/develop
Develop
2 parents a6ceb5b + c678be2 commit ba96f77

File tree

2 files changed

+58
-34
lines changed

2 files changed

+58
-34
lines changed

cmds/cmd_package.py

Lines changed: 56 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,23 @@
2929
import pkgsdb
3030
import shutil
3131
import platform
32-
import requests
3332
import subprocess
3433
import time
3534
import logging
3635
import sys
3736

37+
try:
38+
import requests
39+
except ImportError:
40+
print("****************************************\n"
41+
"* Import requests module error.\n"
42+
"* Please install requests module first.\n"
43+
"* pip install step:\n"
44+
"* $ pip install requests\n"
45+
"* command install step:\n"
46+
"* $ sudo apt-get install python-requests\n"
47+
"****************************************\n")
48+
3849
from package import Package, Bridge_SConscript, Kconfig_file, Package_json_file, Sconscript_file
3950
from vars import Import, Export
4051
from string import Template
@@ -132,7 +143,7 @@ def modify_submod_file_to_mirror(submod_path):
132143
get_package_url, get_ver_sha = get_url_from_mirror_server(
133144
query_submodule_name, 'latest')
134145

135-
if get_package_url != None:
146+
if get_package_url != None and determine_url_valid(get_package_url):
136147
replace_list.append(
137148
(submod_git_url, replace_url, submodule_name))
138149

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

203214
except Exception, e:
204215
print('e.message:%s\t' % e.message)
205-
print(
206-
"The server could not be contacted. Please check your network connection.")
216+
print("The server could not be contacted. Please check your network connection.")
217+
218+
219+
def determine_url_valid(url_from_srv):
220+
221+
headers = {'Connection': 'keep-alive',
222+
'Accept-Encoding': 'gzip, deflate',
223+
'Accept': '*/*',
224+
'User-Agent': 'curl/7.54.0'}
225+
226+
try:
227+
for i in range(0, 3):
228+
r = requests.get(url_from_srv, stream=True, headers=headers)
229+
if r.status_code == requests.codes.not_found:
230+
if i == 2:
231+
print("Warning : %s is invalid." % url_from_srv)
232+
return False
233+
time.sleep(1)
234+
else:
235+
break
236+
237+
return True
238+
239+
except Exception, e:
240+
# print('e.message:%s\t' % e.message)
241+
print('Network connection error or the url : %s is invalid.\n' % url_from_srv)
207242

208243

209244
def install_pkg(env_root, bsp_root, pkg):
@@ -242,41 +277,34 @@ def install_pkg(env_root, bsp_root, pkg):
242277

243278
get_package_url = None
244279
get_ver_sha = None
280+
upstream_change_flag = False
245281

246282
if os.path.isfile(env_config_file) and find_macro_in_config(env_config_file, 'SYS_PKGS_DOWNLOAD_ACCELERATE'):
247283
get_package_url, get_ver_sha = get_url_from_mirror_server(pkgs_name_in_json, pkg['ver'])
248284

249-
if get_package_url != None:
250-
package_url = get_package_url
285+
# determine whether the package package url is valid
286+
if get_package_url != None and determine_url_valid(get_package_url):
287+
package_url = get_package_url
251288

252-
if get_ver_sha != None:
253-
ver_sha = get_ver_sha
289+
if get_ver_sha != None:
290+
ver_sha = get_ver_sha
254291

255-
beforepath = os.getcwd()
256-
257-
# print(package_url)
292+
upstream_change_flag = True
258293

259294
if package_url[-4:] == '.git':
295+
260296
repo_path = os.path.join(bsp_pkgs_path, pkgs_name_in_json)
261297
repo_path = repo_path + '-' + pkg['ver']
262-
cmd = 'git clone ' + package_url + ' ' + repo_path
263-
os.system(cmd)
264-
os.chdir(repo_path)
265-
266-
# #print("Checkout SHA : %s"%ver_sha)
267-
# cmd = 'git reset --hard ' + ver_sha
268-
# os.system(cmd)
269298

270-
# print("cwd : %s"%os.getcwd())
271-
# print("repo_path : %s"%repo_path)
299+
cmd = 'git clone ' + package_url + ' ' + repo_path
300+
execute_command(cmd, cwd=bsp_pkgs_path)
272301

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
302+
cmd = 'git checkout -q ' + ver_sha
303+
execute_command(cmd, cwd=repo_path)
277304

278-
cmd = 'git checkout ' + ver_sha
279-
os.system(cmd)
305+
if upstream_change_flag:
306+
cmd = 'git remote set-url origin ' + url_from_json
307+
execute_command(cmd, cwd=repo_path)
280308

281309
# If there is a .gitmodules file in the package, prepare to update the
282310
# submodule.
@@ -288,8 +316,7 @@ def install_pkg(env_root, bsp_root, pkg):
288316
replace_list = modify_submod_file_to_mirror(submod_path) # Modify .gitmodules file
289317

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

294321
if os.path.isfile(env_config_file) and find_macro_in_config(env_config_file, 'SYS_PKGS_DOWNLOAD_ACCELERATE'):
295322
if len(replace_list):
@@ -299,15 +326,10 @@ def install_pkg(env_root, bsp_root, pkg):
299326
cmd = 'git remote set-url origin ' + item[0]
300327
execute_command(cmd, cwd=submod_dir_path)
301328

302-
cmd = 'git remote set-url origin ' + url_from_json
303-
os.system(cmd)
304-
305329
if os.path.isfile(env_config_file) and find_macro_in_config(env_config_file, 'SYS_PKGS_DOWNLOAD_ACCELERATE'):
306330
if os.path.isfile(submod_path):
307331
cmd = 'git checkout .gitmodules'
308-
os.system(cmd)
309-
310-
os.chdir(beforepath)
332+
execute_command(cmd, cwd=repo_path)
311333

312334
else:
313335
# Download a package of compressed package type.

env.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ def main():
5555
if env_root == None:
5656
if platform.system() != 'Windows':
5757
env_root = os.path.join(os.getenv('HOME'), '.env')
58+
else:
59+
env_root = os.path.join(os.getenv('USERPROFILE'), '.env')
5860

5961
sys.path = sys.path + [os.path.join(script_root)]
6062

0 commit comments

Comments
 (0)