From b512075f5bf2104c63d048160e91edaf06ad3fe6 Mon Sep 17 00:00:00 2001 From: panwentao Date: Mon, 15 Jan 2024 15:37:15 +0800 Subject: [PATCH] auto_completion: Add oebuild code autocompletion feature. * Add oebuild code autocompletion feature. Signed-off-by: panwentao --- src/oebuild/app/conf/oebuild.sh | 23 +++++++++ src/oebuild/app/main.py | 4 +- src/oebuild/auto_completion.py | 85 +++++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+), 1 deletion(-) create mode 100755 src/oebuild/app/conf/oebuild.sh create mode 100644 src/oebuild/auto_completion.py diff --git a/src/oebuild/app/conf/oebuild.sh b/src/oebuild/app/conf/oebuild.sh new file mode 100755 index 0000000..57744bb --- /dev/null +++ b/src/oebuild/app/conf/oebuild.sh @@ -0,0 +1,23 @@ +complete -F _myobj_complete_func oebuild +_myobj_complete_func() { + COMPREPLY=() + command_name="${COMP_WORDS[COMP_CWORD]}" + secondary_command_name="${COMP_WORDS[COMP_CWORD-1]}" + completion_txt="init update generate bitbake manifest clear runqemu menv deploy-target undeploy-target mplugin" + case "${secondary_command_name}" in oebuild) + COMPREPLY=($(compgen -W "${completion_txt}" -- ${command_name})) + esac + case "${secondary_command_name}" in + menv) + secondary_command="create list activate remove" + COMPREPLY=($(compgen -W "${secondary_command}" ${command_name})) + return 0 + ;; + mplugin) + secondary_command="install list enable disable remove" + COMPREPLY=($(compgen -W "${secondary_command}" ${command_name})) + return 0 + ;; + esac + return 0 +} diff --git a/src/oebuild/app/main.py b/src/oebuild/app/main.py index aed6e09..88674fc 100644 --- a/src/oebuild/app/main.py +++ b/src/oebuild/app/main.py @@ -18,6 +18,7 @@ import getpass import colorama import oebuild.util as oebuild_util +from oebuild.auto_completion import AutoCompletion from oebuild.version import __version__ from oebuild.spec import get_spec,_ExtCommand from oebuild.command import OebuildCommand @@ -42,7 +43,7 @@ class OebuildApp: self.append_plugins_dir = pathlib.Path(self.oebuild_plugins_path, 'append_plugins.yaml') self.command_ext = self.get_command_ext(oebuild_util.read_yaml(plugins_dir)['plugins']) if os.path.exists(self.append_plugins_dir) \ - and oebuild_util.read_yaml(self.append_plugins_dir): + and oebuild_util.read_yaml(self.append_plugins_dir): self.command_ext = self.get_command_ext( oebuild_util.read_yaml(self.append_plugins_dir)['plugins'], self.command_ext) @@ -194,6 +195,7 @@ def main(argv=None): return colorama.init() + AutoCompletion().run() app = OebuildApp() app.run(argv or sys.argv[1:]) diff --git a/src/oebuild/auto_completion.py b/src/oebuild/auto_completion.py new file mode 100644 index 0000000..c910409 --- /dev/null +++ b/src/oebuild/auto_completion.py @@ -0,0 +1,85 @@ +''' +Copyright (c) 2023 openEuler Embedded +oebuild is licensed under Mulan PSL v2. +You can use this software according to the terms and conditions of the Mulan PSL v2. +You may obtain a copy of Mulan PSL v2 at: + http://license.coscl.org.cn/MulanPSL2 +THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, +EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, +MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. +See the Mulan PSL v2 for more details. +''' +import hashlib +import re +import sys +import textwrap +import logging +from os.path import abspath, expanduser, expandvars, join + +logger = logging.getLogger() + + +class AutoCompletion(): + """ + code hint + """ + + def run(self): + """ + Subclasses must implement; called to run the command. + Returns: + + """ + oebuild_rc = textwrap.dedent(""" + ###!###>>>>>>>>>>>oebuild_complete>>>>>>>>>>>>>>> + if pip list | grep oebuild &> /dev/null ; then + export oebuild_sh=$(pip show oebuild | grep Location | awk -F" " '{print $2}')/oebuild/app/conf/oebuild.sh + if [ -f $oebuild_sh ] ; then + . $oebuild_sh + fi + fi + ###!###<<<<<<<<<<>>>>>>>>>>oebuild_complete>>>>>>>>>>>>>>>)[\W\w]+(?<=###!###<<<<<<<<<<<' + 'oebuild_complete<<<<<<<<<<<<<<<)') + + re_info = re.search(pattern_bashrc, rc_content) + + if re_info is None: + rc_content += f"\n{oebuild_rc}\n" + with open(bashrc_path, 'w', encoding='utf-8') as fh: + fh.write(rc_content) + else: + bashrc_data = textwrap.dedent(re_info.group()).lstrip() + bashrc_ma5 = self.md5_string(bashrc_data) + rc_content_md5 = self.md5_string(oebuild_rc) + if bashrc_ma5 != rc_content_md5: + rc_content = re.sub(pattern_bashrc, f"\n{oebuild_rc}\n", rc_content) + with open(bashrc_path, 'w', encoding='utf-8') as fh: + fh.write(rc_content) + + def md5_string(self, in_str): + """ + md5 str + Args: + in_str: + + Returns: + + """ + md5 = hashlib.md5() + md5.update(in_str.encode("utf8")) + result = md5.hexdigest() + return result -- Gitee