From 0544687ae88d9d412dfa9349d5165148f7eda69d Mon Sep 17 00:00:00 2001 From: Meco Man <920369182@qq.com> Date: Fri, 16 Sep 2022 22:41:35 -0400 Subject: [PATCH 1/3] =?UTF-8?q?=E5=A2=9E=E5=8A=A0find=5FMDK=5FEXEC=5FPATH?= =?UTF-8?q?=E3=80=81find=5FIAR=5FEXEC=5FPATH=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmds/cmd_menuconfig.py | 12 ++--- cmds/cmd_package/cmd_package_update.py | 2 +- cmds/cmd_package/cmd_package_upgrade.py | 5 +- cmds/cmd_package/cmd_package_utils.py | 63 ++++++++++++++++++++++--- statistics.py | 2 +- 5 files changed, 67 insertions(+), 17 deletions(-) diff --git a/cmds/cmd_menuconfig.py b/cmds/cmd_menuconfig.py index cbe38add..c433464d 100644 --- a/cmds/cmd_menuconfig.py +++ b/cmds/cmd_menuconfig.py @@ -29,7 +29,7 @@ import platform import re from vars import Import -from .cmd_package.cmd_package_utils import find_macro_in_config +from .cmd_package.cmd_package_utils import find_bool_macro_in_config def is_pkg_special_config(config_str): @@ -222,18 +222,18 @@ def cmd(args): if not os.path.isfile(fn): return - if find_macro_in_config(fn, 'SYS_AUTO_UPDATE_PKGS'): + if find_bool_macro_in_config(fn, 'SYS_AUTO_UPDATE_PKGS'): os.system('pkgs --update') print("==============================>The packages have been updated completely.") - if find_macro_in_config(fn, 'SYS_CREATE_MDK_IAR_PROJECT'): - if find_macro_in_config(fn, 'SYS_CREATE_MDK4'): + if find_bool_macro_in_config(fn, 'SYS_CREATE_MDK_IAR_PROJECT'): + if find_bool_macro_in_config(fn, 'SYS_CREATE_MDK4'): os.system('scons --target=mdk4 -s') print("Create mdk4 project done") - elif find_macro_in_config(fn, 'SYS_CREATE_MDK5'): + elif find_bool_macro_in_config(fn, 'SYS_CREATE_MDK5'): os.system('scons --target=mdk5 -s') print("Create mdk5 project done") - elif find_macro_in_config(fn, 'SYS_CREATE_IAR'): + elif find_bool_macro_in_config(fn, 'SYS_CREATE_IAR'): os.system('scons --target=iar -s') print("Create iar project done") diff --git a/cmds/cmd_package/cmd_package_update.py b/cmds/cmd_package/cmd_package_update.py index 01d63c6f..13284755 100644 --- a/cmds/cmd_package/cmd_package_update.py +++ b/cmds/cmd_package/cmd_package_update.py @@ -39,7 +39,7 @@ 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_macro_in_config + find_bool_macro_in_config def determine_support_chinese(env_root): diff --git a/cmds/cmd_package/cmd_package_upgrade.py b/cmds/cmd_package/cmd_package_upgrade.py index 98fd289a..2e42d09f 100644 --- a/cmds/cmd_package/cmd_package_upgrade.py +++ b/cmds/cmd_package/cmd_package_upgrade.py @@ -26,7 +26,7 @@ import os import uuid from vars import Import -from .cmd_package_utils import git_pull_repo, get_url_from_mirror_server, find_macro_in_config +from .cmd_package_utils import git_pull_repo, get_url_from_mirror_server, find_bool_macro_in_config from .cmd_package_update import need_using_mirror_download try: @@ -130,12 +130,11 @@ def get_mac_address(): def Information_statistics(): env_root = Import('env_root') - # get the .config file from env env_kconfig_path = os.path.join(env_root, 'tools\scripts\cmds') env_config_file = os.path.join(env_kconfig_path, '.config') - if find_macro_in_config(env_config_file, 'SYS_PKGS_USING_STATISTICS'): + if 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='+str(mac_addr)+'&username='+str(mac_addr)+'&envversion=1.0&studioversion=2.0&ip=127.0.0.1') if response.status_code != 200: diff --git a/cmds/cmd_package/cmd_package_utils.py b/cmds/cmd_package/cmd_package_utils.py index f269c4b8..52bac290 100644 --- a/cmds/cmd_package/cmd_package_utils.py +++ b/cmds/cmd_package/cmd_package_utils.py @@ -32,7 +32,7 @@ import shutil import requests import logging - +from vars import Import def execute_command(cmd_string, cwd=None, shell=True): """Execute the system command at the specified address.""" @@ -139,13 +139,17 @@ def user_input(msg=None): return value -def find_macro_in_config(filename, macro_name): +# Find the string after '=' +# e.g CONFIG_SYS_AUTO_UPDATE_PKGS=y +# this function will return True and 'y' +# True means this macro has been set and y is the string after '=' +def find_string_in_config(filename, macro_name): try: config = open(filename, "r") except Exception as e: print('Error message:%s' % e) print('open .config failed') - return False + return (False, None) empty_line = 1 @@ -179,12 +183,59 @@ def find_macro_in_config(filename, macro_name): if setting[0].startswith('CONFIG_'): setting[0] = setting[0][7:] - if setting[0] == macro_name and setting[1] == 'y': + if setting[0] == macro_name: config.close() - return True + return (True, setting[1]) config.close() - return False + return (False, None) + + +# check if the bool macro is set or not +# e.g CONFIG_SYS_AUTO_UPDATE_PKGS=y +# will return True because this macro has been set +# If this macro cannot find or the .config cannot find or the macro is not set (n), +# the function will return False +def find_bool_macro_in_config(filename, macro_name): + rst, str = find_string_in_config(filename, macro_name) + if rst == True and str == 'y': + return True + else: + return False + + +# find a string macro is defined or not +# e.g. CONFIG_SYS_CREATE_IAR_EXEC_PATH="C:/Program Files (x86)/IAR Systems/Embedded Workbench 8.3" +# will return "C:/Program Files (x86)/IAR Systems/Embedded Workbench 8.3" +# If this macro cannot find or .config cannot find +# the function will return None +def find_string_macro_in_config(filename, macro_name): + rst, str = find_string_in_config(filename, macro_name) + if rst == True: + str = str.strip('"') + return str + else: + return None + + +# return the execution path string or None for failure +def find_IAR_EXEC_PATH(): + env_root = Import('env_root') + # get the .config file from env + env_kconfig_path = os.path.join(env_root, 'tools\scripts\cmds') + env_config_file = os.path.join(env_kconfig_path, '.config') + + return find_string_macro_in_config(env_config_file, 'SYS_CREATE_IAR_EXEC_PATH') + + +# return the execution path string or None for failure +def find_MDK_EXEC_PATH(): + env_root = Import('env_root') + # get the .config file from env + env_kconfig_path = os.path.join(env_root, 'tools\scripts\cmds') + env_config_file = os.path.join(env_kconfig_path, '.config') + + return find_string_macro_in_config(env_config_file, 'SYS_CREATE_MDK_EXEC_PATH') def remove_folder(folder_path): diff --git a/statistics.py b/statistics.py index 0b0ece40..b274855a 100644 --- a/statistics.py +++ b/statistics.py @@ -52,7 +52,7 @@ def Information_statistics(): return except Exception as e: exit(0) - elif os.path.isfile(env_config_file) and cmd_package.find_macro_in_config(env_config_file, 'SYS_PKGS_NOT_USING_STATISTICS'): + elif os.path.isfile(env_config_file) and cmd_package.find_bool_macro_in_config(env_config_file, 'SYS_PKGS_NOT_USING_STATISTICS'): return True From 839c1c886b123da239d198a69afd37edfd843d35 Mon Sep 17 00:00:00 2001 From: Meco Man <920369182@qq.com> Date: Fri, 16 Sep 2022 23:01:38 -0400 Subject: [PATCH 2/3] correct comment --- cmds/cmd_package/cmd_package_utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmds/cmd_package/cmd_package_utils.py b/cmds/cmd_package/cmd_package_utils.py index 52bac290..a26e052c 100644 --- a/cmds/cmd_package/cmd_package_utils.py +++ b/cmds/cmd_package/cmd_package_utils.py @@ -218,7 +218,7 @@ def find_string_macro_in_config(filename, macro_name): return None -# return the execution path string or None for failure +# return IAR execution path string or None for failure def find_IAR_EXEC_PATH(): env_root = Import('env_root') # get the .config file from env @@ -228,7 +228,7 @@ def find_IAR_EXEC_PATH(): return find_string_macro_in_config(env_config_file, 'SYS_CREATE_IAR_EXEC_PATH') -# return the execution path string or None for failure +# return Keil-MDK execution path string or None for failure def find_MDK_EXEC_PATH(): env_root = Import('env_root') # get the .config file from env From 5e04771ad388d2c41707fbacaf0ea28e627b6ad8 Mon Sep 17 00:00:00 2001 From: Meco Man <920369182@qq.com> Date: Sat, 17 Sep 2022 13:53:13 -0400 Subject: [PATCH 3/3] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=88=9D=E5=A7=8B?= =?UTF-8?q?=E5=8C=96=E6=A3=80=E6=9F=A5IAR\MDK=E8=B7=AF=E5=BE=84=E6=98=AF?= =?UTF-8?q?=E5=90=A6=E8=AE=BE=E7=BD=AE=20=E5=A6=82=E6=9E=9C=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=EF=BC=8C=E5=B0=86=E8=B7=AF=E5=BE=84=E8=B5=8B=E7=BB=99?= =?UTF-8?q?RTT=5FEXEC=5FPATH=E7=8E=AF=E5=A2=83=E5=8F=98=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmds/cmd_package/__init__.py | 11 ++++++++++- cmds/cmd_package/cmd_package_utils.py | 5 ++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/cmds/cmd_package/__init__.py b/cmds/cmd_package/__init__.py index ea7d7967..22bb2a17 100644 --- a/cmds/cmd_package/__init__.py +++ b/cmds/cmd_package/__init__.py @@ -26,11 +26,21 @@ # 2020-04-08 SummerGift Optimize program structure # +import os from .cmd_package_printenv import package_print_env, package_print_help from .cmd_package_list import list_packages from .cmd_package_wizard import package_wizard from .cmd_package_update import package_update from .cmd_package_upgrade import package_upgrade, package_upgrade_modules +from .cmd_package_utils import find_IAR_EXEC_PATH, find_MDK_EXEC_PATH + +iar_exec_path = find_IAR_EXEC_PATH() +if iar_exec_path: + os.environ['RTT_EXEC_PATH'] = iar_exec_path + +mdk_exec_path = find_MDK_EXEC_PATH() +if mdk_exec_path: + os.environ['RTT_EXEC_PATH'] = mdk_exec_path def run_env_cmd(args): """Run packages command.""" @@ -117,4 +127,3 @@ def add_parser(sub): dest='package_print_env') parser.set_defaults(func=run_env_cmd) - diff --git a/cmds/cmd_package/cmd_package_utils.py b/cmds/cmd_package/cmd_package_utils.py index a26e052c..71534003 100644 --- a/cmds/cmd_package/cmd_package_utils.py +++ b/cmds/cmd_package/cmd_package_utils.py @@ -32,7 +32,6 @@ import shutil import requests import logging -from vars import Import def execute_command(cmd_string, cwd=None, shell=True): """Execute the system command at the specified address.""" @@ -220,7 +219,7 @@ def find_string_macro_in_config(filename, macro_name): # return IAR execution path string or None for failure def find_IAR_EXEC_PATH(): - env_root = Import('env_root') + env_root = os.getenv("ENV_ROOT") # get the .config file from env env_kconfig_path = os.path.join(env_root, 'tools\scripts\cmds') env_config_file = os.path.join(env_kconfig_path, '.config') @@ -230,7 +229,7 @@ def find_IAR_EXEC_PATH(): # return Keil-MDK execution path string or None for failure def find_MDK_EXEC_PATH(): - env_root = Import('env_root') + env_root = os.getenv("ENV_ROOT") # get the .config file from env env_kconfig_path = os.path.join(env_root, 'tools\scripts\cmds') env_config_file = os.path.join(env_kconfig_path, '.config')