From 40299f3512e6cc6ca98da4a87549803260cae91f Mon Sep 17 00:00:00 2001 From: lixinyu Date: Fri, 22 Mar 2024 10:13:43 +0800 Subject: [PATCH] code: optimized program status code * The program status codes have been standardized, and the following is an explanation of the status codes: 0 -> The program is running normally. 1 -> Command/Parameter error. -1 -> The program encountered an exception during runtime. Signed-off-by: lixinyu --- src/oebuild/app/plugins/bitbake/bitbake.py | 13 +++-- .../app/plugins/bitbake/in_container.py | 2 +- src/oebuild/app/plugins/bitbake/in_host.py | 55 ++++++++++--------- src/oebuild/app/plugins/clear/clear.py | 5 +- .../app/plugins/deploy/deploy_target.py | 5 +- src/oebuild/app/plugins/generate/generate.py | 20 +++---- src/oebuild/app/plugins/init/init.py | 6 +- src/oebuild/app/plugins/m_env/m_env.py | 6 +- src/oebuild/app/plugins/m_plugin/m_plugin.py | 2 +- src/oebuild/app/plugins/manifest/manifest.py | 2 +- src/oebuild/app/plugins/run_qemu/run_qemu.py | 2 +- src/oebuild/app/plugins/update/update.py | 6 +- 12 files changed, 66 insertions(+), 58 deletions(-) diff --git a/src/oebuild/app/plugins/bitbake/bitbake.py b/src/oebuild/app/plugins/bitbake/bitbake.py index 04fee5e..17b3678 100644 --- a/src/oebuild/app/plugins/bitbake/bitbake.py +++ b/src/oebuild/app/plugins/bitbake/bitbake.py @@ -13,6 +13,7 @@ See the Mulan PSL v2 for more details. import os import argparse import textwrap +import sys from docker.errors import DockerException @@ -74,14 +75,14 @@ class Bitbake(OebuildCommand): ''' if '-h' in unknown or '--help' in unknown: self.print_help_msg() - return + sys.exit(0) command = self._get_command(unknow=unknown) if not self.check_support_bitbake(): logger.error( "Please do it in compile workspace which contain compile.yaml") - return + sys.exit(-1) if not os.path.exists('.env'): os.mknod('.env') @@ -90,7 +91,7 @@ class Bitbake(OebuildCommand): parse_compile = ParseCompile(self.compile_conf_dir) except CheckCompileError as c_e: logger.error(str(c_e)) - return + sys.exit(-1) # if has manifest.yaml, init layer repo with it yocto_dir = os.path.join(self.configure.source_dir(), @@ -103,12 +104,14 @@ class Bitbake(OebuildCommand): if parse_compile.build_in == oebuild_const.BUILD_IN_HOST: in_host = InHost(self.configure) in_host.exec(parse_compile=parse_compile, command=command) - return + sys.exit(0) + try: oebuild_util.check_docker() except DockerException as d_e: logger.error(str(d_e)) - return + sys.exit(-1) + in_container = InContainer(self.configure) in_container.exec(parse_env=parse_env, parse_compile=parse_compile, diff --git a/src/oebuild/app/plugins/bitbake/in_container.py b/src/oebuild/app/plugins/bitbake/in_container.py index 0f1b7c6..f3eef0d 100644 --- a/src/oebuild/app/plugins/bitbake/in_container.py +++ b/src/oebuild/app/plugins/bitbake/in_container.py @@ -55,7 +55,7 @@ class InContainer(BaseBuild): if not docker_proxy.is_image_exists(docker_param.image): logger.error('''The docker image does not exists, please run fellow command: `oebuild update docker`''') - return + sys.exit(-1) self.deal_env_container(env=parse_env, docker_param=docker_param) self.exec_compile(parse_compile=parse_compile, command=command) diff --git a/src/oebuild/app/plugins/bitbake/in_host.py b/src/oebuild/app/plugins/bitbake/in_host.py index 68a696d..8ea2e06 100644 --- a/src/oebuild/app/plugins/bitbake/in_host.py +++ b/src/oebuild/app/plugins/bitbake/in_host.py @@ -58,39 +58,18 @@ class InHost(BaseBuild): except NativesdkNotExist as n_e: logger.error(str(n_e)) logger.error("Please set valid nativesdk directory") - return + sys.exit(1) except NativesdkNotValid as n_e: logger.error(str(n_e)) logger.error(''' -The nativesdk path must be valid, it is recommended -that you download the nativesdk script and then perform +The nativesdk path must be valid, it is recommended +that you download the nativesdk script and then perform initialization operations''') - return + sys.exit(1) if command is not None and command != "": self._append_build_sh(str_list=[command], build_dir=os.getcwd()) - with subprocess.Popen('bash build.sh', - shell=True, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - encoding="utf-8", - text=True) as s_p: - if s_p.returncode is not None and s_p.returncode != 0: - err_msg = '' - if s_p.stderr is not None: - for line in s_p.stderr: - err_msg.join(line) - raise ValueError(err_msg) - res = None - while res is None: - res = s_p.poll() - if s_p.stdout is not None: - for line in s_p.stdout: - logger.info(line.strip('\n')) - if s_p.stderr is not None: - for line in s_p.stderr: - logger.error(line.strip('\n')) - sys.exit(res) + self._bash_build() else: # run in Interactive mode banner_list = [] @@ -109,6 +88,30 @@ initialization operations''') self.update_bashrc(new_content) pty.spawn("bash") + def _bash_build(self,): + with subprocess.Popen('bash build.sh', + shell=True, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + encoding="utf-8", + text=True) as s_p: + if s_p.returncode is not None and s_p.returncode != 0: + err_msg = '' + if s_p.stderr is not None: + for line in s_p.stderr: + err_msg.join(line) + raise ValueError(err_msg) + res = None + while res is None: + res = s_p.poll() + if s_p.stdout is not None: + for line in s_p.stdout: + logger.info(line.strip('\n')) + if s_p.stderr is not None: + for line in s_p.stderr: + logger.error(line.strip('\n')) + sys.exit(res) + def _mk_build_sh(self, nativesdk_dir, build_dir): # get nativesdk environment path automatic for next step sdk_env_path = oebuild_util.get_nativesdk_environment(nativesdk_dir) diff --git a/src/oebuild/app/plugins/clear/clear.py b/src/oebuild/app/plugins/clear/clear.py index ab834f5..99640fd 100644 --- a/src/oebuild/app/plugins/clear/clear.py +++ b/src/oebuild/app/plugins/clear/clear.py @@ -14,6 +14,7 @@ import argparse import textwrap import os import pathlib +import sys from docker.errors import DockerException @@ -58,7 +59,7 @@ class Clear(OebuildCommand): def do_run(self, args: argparse.Namespace, unknown=None): # perpare parse help command if self.pre_parse_help(args, unknown): - return + sys.exit(1) args = args.parse_args(unknown) @@ -67,7 +68,7 @@ class Clear(OebuildCommand): self.client = DockerProxy() except DockerException: logger.error("Please install docker first!!!") - return + sys.exit(-1) self.clear_docker() def clear_docker(self, ): diff --git a/src/oebuild/app/plugins/deploy/deploy_target.py b/src/oebuild/app/plugins/deploy/deploy_target.py index 03227b3..4c051da 100644 --- a/src/oebuild/app/plugins/deploy/deploy_target.py +++ b/src/oebuild/app/plugins/deploy/deploy_target.py @@ -13,6 +13,7 @@ See the Mulan PSL v2 for more details. import argparse import textwrap import logging +import sys from oebuild.command import OebuildCommand from oebuild.app.plugins.deploy.com_target import ComTarget @@ -50,7 +51,7 @@ oebuild deploy-target [-h] [-c] [-s] [-n] [-p] [--no-check-space] [-e SSH_EXEC] def do_run(self, args: argparse.Namespace, unknown=None): if '-h' in unknown or '--help' in unknown: self.print_help_msg() - return + sys.exit(1) str_args = ' '.join(unknown) com_target = ComTarget() com_target.exec(str_args=str_args, fun="deploy-target") @@ -115,7 +116,7 @@ oebuild undeploy-target [-h] [-c] [-s] [-a] [-n] [-e SSH_EXEC] def do_run(self, args: argparse.Namespace, unknown=None): if '-h' in unknown or '--help' in unknown: self.print_help_msg() - return + sys.exit(1) str_args = ' '.join(unknown) com_target = ComTarget() com_target.exec(str_args=str_args, fun="undeploy-target") diff --git a/src/oebuild/app/plugins/generate/generate.py b/src/oebuild/app/plugins/generate/generate.py index dfd1031..d2147dc 100644 --- a/src/oebuild/app/plugins/generate/generate.py +++ b/src/oebuild/app/plugins/generate/generate.py @@ -175,7 +175,7 @@ class Generate(OebuildCommand): def do_run(self, args: argparse.Namespace, unknown=None): # perpare parse help command if self.pre_parse_help(args, unknown): - return + sys.exit(1) if not self.configure.is_oebuild_dir(): logger.error('Your current directory had not finished init') sys.exit(-1) @@ -186,7 +186,7 @@ class Generate(OebuildCommand): 'Currently, yocto-meta-openeuler does not support oebuild, \ please modify .oebuild/config and re-execute `oebuild update`' ) - return + sys.exit(-1) if len(unknown) == 0: config_path = self.create_kconfig(yocto_dir) @@ -204,7 +204,7 @@ class Generate(OebuildCommand): self._check_param_in_host(args=args) except ValueError as v_e: logger.error(str(v_e)) - return + sys.exit(-1) self.nativesdk_dir = args.nativesdk_dir build_in = oebuild_const.BUILD_IN_HOST @@ -219,12 +219,12 @@ class Generate(OebuildCommand): if args.list: self.list_info() - return + sys.exit(0) build_dir = self._init_build_dir(args=args) if build_dir is None: - return + sys.exit(1) parser_template = ParseTemplate(yocto_dir=yocto_dir) @@ -236,10 +236,10 @@ class Generate(OebuildCommand): parser_template=parser_template) except BaseParseTemplate as b_t: logger.error(str(b_t)) - return + sys.exit(-1) except ValueError as v_e: logger.error(str(v_e)) - return + sys.exit(-1) try: self._add_features_template(args=args, @@ -248,10 +248,10 @@ class Generate(OebuildCommand): except BaseParseTemplate as b_t: logger.error(str(b_t)) self._list_feature() - return + sys.exit(-1) except ValueError as v_e: logger.error(str(v_e)) - return + sys.exit(-1) if os.path.exists(os.path.join(build_dir, 'compile.yaml')): os.remove(os.path.join(build_dir, 'compile.yaml')) @@ -275,7 +275,7 @@ class Generate(OebuildCommand): f"{key}, {oebuild_config.docker.repo_url}:{value}") k = input("please entry number:") if k == "q": - return + sys.exit(0) try: index = int(k) docker_tag = image_list[index] diff --git a/src/oebuild/app/plugins/init/init.py b/src/oebuild/app/plugins/init/init.py index 6cf5ef0..29e35db 100644 --- a/src/oebuild/app/plugins/init/init.py +++ b/src/oebuild/app/plugins/init/init.py @@ -84,7 +84,7 @@ class Init(OebuildCommand): # perpare parse help command if self.pre_parse_help(args, unknown): - return + sys.exit(0) args = args.parse_args(unknown) @@ -99,11 +99,11 @@ class Init(OebuildCommand): logger.error("'oebuild init' need param directory") logger.info("\noebuild init help:") self.print_help_msg() - return + sys.exit(1) if not self.init_workspace(args.directory): logger.error("mkdir %s failed", args.directory) - return + sys.exit(-1) os.chdir(args.directory) oebuild_config: Config = self.configure.parse_oebuild_config() diff --git a/src/oebuild/app/plugins/m_env/m_env.py b/src/oebuild/app/plugins/m_env/m_env.py index 6d535d4..15e656d 100644 --- a/src/oebuild/app/plugins/m_env/m_env.py +++ b/src/oebuild/app/plugins/m_env/m_env.py @@ -107,7 +107,7 @@ class Menv(OebuildCommand): print(''' Please enter the correct command: oebuild menv create [-d -f] Create an environment -n env_name ''') - sys.exit(-1) + sys.exit(1) self.create_environment(args=args) elif command == 'activate': # Activate Environment @@ -117,7 +117,7 @@ Please enter the correct command: oebuild menv create [-d -f] Create an environm print( 'Please enter the correct command: oebuild menv activate -n env_name' ) - sys.exit(-1) + sys.exit(1) elif command == 'list': env_dict = oebuild_util.read_yaml(self.oebuild_env_yaml_path) @@ -136,7 +136,7 @@ Please enter the correct command: oebuild menv create [-d -f] Create an environm print( 'Please enter the correct command: oebuild menv remove -n env_name' ) - sys.exit(-1) + sys.exit(1) def create_environment(self, args): ''' diff --git a/src/oebuild/app/plugins/m_plugin/m_plugin.py b/src/oebuild/app/plugins/m_plugin/m_plugin.py index cad99af..83cdc6e 100644 --- a/src/oebuild/app/plugins/m_plugin/m_plugin.py +++ b/src/oebuild/app/plugins/m_plugin/m_plugin.py @@ -170,7 +170,7 @@ class MPlugin(OebuildCommand): sys.exit(0) logger.error("the %s not exist, please check the plugin file path", args.file) - sys.exit(-1) + sys.exit(1) elif command == 'list': self.list_plugin(plugin_dict=plugin_dict) elif command in ['enable', 'disable']: diff --git a/src/oebuild/app/plugins/manifest/manifest.py b/src/oebuild/app/plugins/manifest/manifest.py index d62e94b..cb488a2 100644 --- a/src/oebuild/app/plugins/manifest/manifest.py +++ b/src/oebuild/app/plugins/manifest/manifest.py @@ -75,7 +75,7 @@ class Manifest(OebuildCommand): # perpare parse help command if self.pre_parse_help(args, unknown): - return + sys.exit(0) args = args.parse_args(unknown) manifest_dir = args.manifest_dir if args.manifest_dir else \ diff --git a/src/oebuild/app/plugins/run_qemu/run_qemu.py b/src/oebuild/app/plugins/run_qemu/run_qemu.py index c4b56fe..faed77e 100644 --- a/src/oebuild/app/plugins/run_qemu/run_qemu.py +++ b/src/oebuild/app/plugins/run_qemu/run_qemu.py @@ -76,7 +76,7 @@ the container {self.container_id} failed to be destroyed, please run self.client = DockerProxy() except DockerException: logger.error("Please install docker first!!!") - return + sys.exit(-1) logger.info('Run QEMU......') docker_image = self.get_docker_image() diff --git a/src/oebuild/app/plugins/update/update.py b/src/oebuild/app/plugins/update/update.py index cf11c26..2a7e7c8 100644 --- a/src/oebuild/app/plugins/update/update.py +++ b/src/oebuild/app/plugins/update/update.py @@ -87,7 +87,7 @@ class Update(OebuildCommand): ''' # perpare parse help command if self.pre_parse_help(args, unknown): - return + sys.exit(0) args = args.parse_args(unknown) @@ -111,7 +111,7 @@ class Update(OebuildCommand): update_layer = True else: logger.error('Please run oebuild update [yocto docker layer]') - sys.exit(-1) + sys.exit(1) if update_yocto: self.get_basic_repo() @@ -123,7 +123,7 @@ class Update(OebuildCommand): self.docker_image_update(args.docker_tag) except DockerException as d_e: logger.error(str(d_e)) - return + sys.exit(-1) if update_layer: self.get_layer_repo() -- Gitee