Skip to content

Commit 6077d0f

Browse files
committed
自动通过IP地址来确认是否使用镜像源
1 parent 4d575e4 commit 6077d0f

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

cmds/cmd_package/cmd_package_update.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -134,21 +134,26 @@ def is_user_mange_package(bsp_package_path, pkg):
134134
return False
135135

136136

137-
def need_using_mirror_download(config_file):
138-
"""default using mirror url to download packages"""
139-
140-
if not os.path.isfile(config_file):
141-
return True
142-
elif os.path.isfile(config_file) and find_macro_in_config(config_file, 'SYS_PKGS_DOWNLOAD_ACCELERATE'):
143-
return True
137+
def need_using_mirror_download():
138+
try:
139+
ip = requests.get('http://ip.42.pl/raw').content.decode()
140+
url = 'http://www.ip-api.com/json/' + ip
141+
if requests.get(url).json()['country'] == 'China':
142+
print("Use gitee mirror sever")
143+
return True
144+
else:
145+
return False
146+
except:
147+
print('Fail to get the ip location!')
148+
return False
144149

145150

146151
def is_git_url(package_url):
147152
return package_url.endswith('.git')
148153

149154

150155
def install_git_package(bsp_package_path, package_name, package_info, package_url, ver_sha, upstream_changed,
151-
url_origin, env_config_file):
156+
url_origin):
152157
try:
153158
repo_path = os.path.join(bsp_package_path, package_name)
154159
repo_path = repo_path + '-' + package_info['ver']
@@ -174,21 +179,21 @@ def install_git_package(bsp_package_path, package_name, package_info, package_ur
174179
submodule_path = os.path.join(repo_path, '.gitmodules')
175180
if os.path.isfile(submodule_path):
176181
print("Start to update submodule")
177-
if need_using_mirror_download(env_config_file):
182+
if need_using_mirror_download():
178183
replace_list = modify_submod_file_to_mirror(submodule_path) # Modify .gitmodules file
179184

180185
cmd = 'git submodule update --init --recursive'
181186
execute_command(cmd, cwd=repo_path)
182187

183-
if need_using_mirror_download(env_config_file):
188+
if need_using_mirror_download():
184189
if len(replace_list):
185190
for item in replace_list:
186191
submodule_path = os.path.join(repo_path, item[2])
187192
if os.path.isdir(submodule_path):
188193
cmd = 'git remote set-url origin ' + item[0]
189194
execute_command(cmd, cwd=submodule_path)
190195

191-
if need_using_mirror_download(env_config_file):
196+
if need_using_mirror_download():
192197
if os.path.isfile(submodule_path):
193198
cmd = 'git checkout .gitmodules'
194199
execute_command(cmd, cwd=repo_path)
@@ -241,9 +246,6 @@ def install_package(env_root, pkgs_root, bsp_root, package_info, force_update):
241246
else:
242247
logging.info("NOT User managed package {0}, {1} need install. \n".format(bsp_package_path, package_info))
243248

244-
# get the .config file from env
245-
env_config_file = os.path.join(env_root, r'tools\scripts\cmds', '.config')
246-
247249
package = PackageOperation()
248250
pkg_path = package_info['path']
249251
if pkg_path[0] == '/' or pkg_path[0] == '\\':
@@ -267,7 +269,7 @@ def install_package(env_root, pkgs_root, bsp_root, package_info, force_update):
267269

268270
# noinspection PyBroadException
269271
try:
270-
if need_using_mirror_download(env_config_file):
272+
if need_using_mirror_download():
271273
get_package_url, get_ver_sha = get_url_from_mirror_server(pkgs_name_in_json, package_info['ver'])
272274

273275
# Check whether the package package url is valid
@@ -284,7 +286,7 @@ def install_package(env_root, pkgs_root, bsp_root, package_info, force_update):
284286
if is_git_url(package_url):
285287
if not install_git_package(bsp_package_path, pkgs_name_in_json, package_info, package_url, ver_sha,
286288
upstream_changed,
287-
url_from_json, env_config_file):
289+
url_from_json):
288290
result = False
289291
else:
290292
if not install_not_git_package(package, package_info, local_pkgs_path, package_url, bsp_package_path,
@@ -360,8 +362,6 @@ def update_latest_packages(sys_value):
360362
env_root = Import('env_root')
361363
pkgs_root = Import('pkgs_root')
362364

363-
env_config_file = os.path.join(env_root, r'tools\scripts\cmds', '.config')
364-
365365
with open(package_filename, 'r') as f:
366366
read_back_pkgs_json = json.load(f)
367367

@@ -384,7 +384,7 @@ def update_latest_packages(sys_value):
384384
# noinspection PyBroadException
385385
try:
386386
# If mirror acceleration is enabled, get the update address from the mirror server.
387-
if need_using_mirror_download(env_config_file):
387+
if need_using_mirror_download():
388388
payload_pkgs_name_in_json = pkgs_name_in_json.encode("utf-8")
389389

390390
# Change repo's upstream address.

0 commit comments

Comments
 (0)