Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
75 changes: 66 additions & 9 deletions cmds/cmd_package/cmd_package_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,40 @@
import pkgsdb
from package import PackageOperation, Bridge_SConscript
from vars import Import, Export
from .cmd_package_utils import get_url_from_mirror_server, execute_command, git_pull_repo, user_input, find_bool_macro_in_config
from .cmd_package_utils import (
get_url_from_mirror_server,
execute_command,
git_pull_repo,
user_input,
find_bool_macro_in_config,
)


def _get_git_restore_url(package_obj, ver):
"""Return a suitable git URL to restore remote.origin.url.

Prefer the per-version URL if it ends with '.git'. Otherwise, fall back to
the 'repository' field (appending '.git' when necessary). Return None when
no suitable git URL can be determined.
Comment on lines +51 to +55
Copy link

Copilot AI Oct 11, 2025

Choose a reason for hiding this comment

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

The docstring should specify the parameter types and return type for better clarity. Consider adding Args and Returns sections to document package_obj and ver parameters.

Suggested change
"""Return a suitable git URL to restore remote.origin.url.
Prefer the per-version URL if it ends with '.git'. Otherwise, fall back to
the 'repository' field (appending '.git' when necessary). Return None when
no suitable git URL can be determined.
"""
Return a suitable git URL to restore remote.origin.url.
Prefer the per-version URL if it ends with '.git'. Otherwise, fall back to
the 'repository' field (appending '.git' when necessary). Return None when
no suitable git URL can be determined.
Args:
package_obj (object): The package object that provides the `get_url` method and a `pkg` attribute.
ver (str): The version identifier to query for a specific URL.
Returns:
str or None: The git URL as a string if found, otherwise None.

Copilot uses AI. Check for mistakes.

"""
try:
url = package_obj.get_url(ver)
except Exception:
url = None
Comment on lines +57 to +60
Copy link

Copilot AI Oct 11, 2025

Choose a reason for hiding this comment

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

Catching bare Exception is too broad. Consider catching more specific exceptions that get_url might raise to avoid masking unexpected errors.

Copilot uses AI. Check for mistakes.


if url and isinstance(url, str) and url.endswith('.git'):
return url

repo = None
try:
repo = package_obj.pkg.get('repository') if package_obj and package_obj.pkg else None
except Exception:
Copy link

Copilot AI Oct 11, 2025

Choose a reason for hiding this comment

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

Catching bare Exception is too broad. Consider catching more specific exceptions that might be raised when accessing package_obj.pkg to avoid masking unexpected errors.

Suggested change
except Exception:
except (AttributeError, TypeError):

Copilot uses AI. Check for mistakes.

repo = None

if repo and isinstance(repo, str):
return repo if repo.endswith('.git') else repo + '.git'

return None


def determine_support_chinese(env_root):
Expand Down Expand Up @@ -167,7 +200,12 @@ def need_using_mirror_download():
elif os.path.isfile(config_file) and find_bool_macro_in_config(config_file, 'SYS_DOWNLOAD_SERVER_GITEE'):
is_China_ip = True # Gitee which means China IP
server_decision = "manually decision"
elif not os.path.isfile(config_file):
# env cmds/.config not found: default to GitHub without noisy prompts
is_China_ip = False
server_decision = "default GitHub (no env .config)"
else:
# env .config exists but no explicit server set: keep legacy auto decision
try:
ip = requests.get('https://ifconfig.me/ip').content.decode()
url = 'http://www.ip-api.com/json/' + ip
Expand Down Expand Up @@ -214,7 +252,15 @@ def update_submodule(repo_path, use_esp_mirror):
execute_command(cmd, cwd=repo_path)


def install_git_package(bsp_package_path, package_name, package_info, package_url, ver_sha, upstream_changed, url_origin):
def install_git_package(
bsp_package_path,
package_name,
package_info,
package_url,
ver_sha,
upstream_changed,
url_origin,
):
try:
repo_path = os.path.join(bsp_package_path, package_name)
repo_path = repo_path + '-' + package_info['ver']
Expand All @@ -236,8 +282,9 @@ def install_git_package(bsp_package_path, package_name, package_info, package_ur
print("\nFailed to download software package with git. Please check the network connection.")
return False

# change upstream to origin url
if upstream_changed:
# change upstream back to origin url when applicable
# only restore when a valid git URL is available
if upstream_changed and url_origin and str(url_origin).endswith('.git'):
cmd = 'git remote set-url origin ' + url_origin
execute_command(cmd, cwd=repo_path)

Expand Down Expand Up @@ -340,9 +387,18 @@ def install_package(env_root, pkgs_root, bsp_root, package_info, force_update):
logging.warning("Failed to connect to the mirror server, package will be downloaded from non-mirror server.\n")

logging.info("Package url: %s" % package_url)
# compute a safe origin url to restore (git only)
restore_origin_url = _get_git_restore_url(package, package_info['ver'])

if is_git_url(package_url):
if not install_git_package(
bsp_package_path, pkgs_name_in_json, package_info, package_url, ver_sha, upstream_changed, url_from_json
bsp_package_path,
pkgs_name_in_json,
package_info,
package_url,
ver_sha,
upstream_changed,
restore_origin_url,
):
result = False
else:
Expand Down Expand Up @@ -463,12 +519,13 @@ def update_latest_packages(sys_value):
# If the package has submodules, update the submodules.
update_submodule(repo_path, pkgs_name_in_json == u"ESP-IDF")

# recover origin url to the path which get from packages.json file
if package.get_url(pkg['ver']):
cmd = 'git remote set-url origin ' + package.get_url(pkg['ver'])
# recover origin url to a proper git URL from package info
restore_url = _get_git_restore_url(package, pkg['ver'])
if restore_url:
cmd = 'git remote set-url origin ' + restore_url
git_cmd_exec(cmd, repo_path)
else:
print("Can't find the package : %s's url in file : %s" % (payload_pkgs_name_in_json, pkg_path))
print("Can't restore origin: no git URL found for package in %s" % pkg_path)

print("==============================> %s update done\n" % pkgs_name_in_json)

Expand Down
2 changes: 1 addition & 1 deletion cmds/cmd_package/cmd_package_upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def Information_statistics():
env_kconfig_path = os.path.join(env_root, 'tools', 'scripts', 'cmds')
env_config_file = os.path.join(env_kconfig_path, '.config')

if find_bool_macro_in_config(env_config_file, 'SYS_PKGS_USING_STATISTICS'):
if os.path.isfile(env_config_file) and find_bool_macro_in_config(env_config_file, 'SYS_PKGS_USING_STATISTICS'):
mac_addr = get_mac_address()
response = requests.get(
'https://www.rt-thread.org/studio/statistics/api/envuse?userid='
Expand Down