From 924878783a64863a8f7c81ae74503e2c64c52cd6 Mon Sep 17 00:00:00 2001 From: xuansen fang Date: Fri, 8 Mar 2024 18:03:10 +0800 Subject: [PATCH 1/4] Optimzed: impore the class init process * Transforming the properties of base class into the properties of subclasses * avoiding instantiate and function callback, improving performance Signed-off-by: fangxuansen --- src/oebuild/app/plugins/bitbake/bitbake.py | 19 ++++++------ src/oebuild/app/plugins/clear/clear.py | 17 +++++----- src/oebuild/app/plugins/demo/demo.py | 15 +++++---- .../app/plugins/deploy/deploy_target.py | 31 +++++++++---------- src/oebuild/app/plugins/generate/generate.py | 23 +++++++------- src/oebuild/app/plugins/init/init.py | 21 ++++++------- src/oebuild/app/plugins/m_env/m_env.py | 16 +++++----- src/oebuild/app/plugins/m_plugin/m_plugin.py | 16 +++++----- src/oebuild/app/plugins/manifest/manifest.py | 17 +++++----- src/oebuild/app/plugins/run_qemu/run_qemu.py | 13 ++++---- src/oebuild/app/plugins/update/update.py | 15 +++++---- src/oebuild/spec.py | 9 +----- src/oebuild/util.py | 2 +- 13 files changed, 99 insertions(+), 115 deletions(-) diff --git a/src/oebuild/app/plugins/bitbake/bitbake.py b/src/oebuild/app/plugins/bitbake/bitbake.py index 96fd493..0c416ad 100644 --- a/src/oebuild/app/plugins/bitbake/bitbake.py +++ b/src/oebuild/app/plugins/bitbake/bitbake.py @@ -34,20 +34,19 @@ class Bitbake(OebuildCommand): command directly, for example: `oebuild bitbake busybox` ''' + help_msg = 'execute bitbake command' + description = textwrap.dedent(''' + The bitbake command performs the build operation, and for the build environment, + there are two types, one is to build in docker and the other is to build in the + host. There are also two construction methods, one is to build directly, and the + other is to call up the build environment to be operated freely by the user + ''') + def __init__(self): self.compile_conf_dir = os.path.join(os.getcwd(), 'compile.yaml') self.configure = Configure() - super().__init__( - 'bitbake', - 'execute bitbake command', - textwrap.dedent(''' - The bitbake command performs the build operation, and for the build environment, - there are two types, one is to build in docker and the other is to build in the - host. There are also two construction methods, one is to build directly, and the - other is to call up the build environment to be operated freely by the user - ''') - ) + super().__init__('bitbake', self.help_msg, self.description) def do_add_parser(self, parser_adder) -> argparse.ArgumentParser: parser = self._parser( diff --git a/src/oebuild/app/plugins/clear/clear.py b/src/oebuild/app/plugins/clear/clear.py index 62a5a11..0474bb2 100644 --- a/src/oebuild/app/plugins/clear/clear.py +++ b/src/oebuild/app/plugins/clear/clear.py @@ -29,18 +29,17 @@ class Clear(OebuildCommand): for some clear task ''' - def __init__(self): - self.configure = Configure() - self.client = None - super().__init__( - 'clear', - 'clear someone which oebuild generate', - textwrap.dedent('''\ + help_msg = 'clear someone which oebuild generate' + description = textwrap.dedent('''\ During the construction process using oebuild, a lot of temporary products will be generated, such as containers,so this command can remove unimportant products, such as containers -''' - )) + ''') + + def __init__(self): + self.configure = Configure() + self.client = None + super().__init__('clear', self.help_msg, self.description) def do_add_parser(self, parser_adder) -> argparse.ArgumentParser: parser = self._parser( diff --git a/src/oebuild/app/plugins/demo/demo.py b/src/oebuild/app/plugins/demo/demo.py index 5548ec7..c78b15c 100644 --- a/src/oebuild/app/plugins/demo/demo.py +++ b/src/oebuild/app/plugins/demo/demo.py @@ -11,16 +11,15 @@ logger = logging.getLogger() class Demo(OebuildCommand): - def __init__(self): - self.configure = Configure() - super().__init__( - name='{}', - help='this is your help mesasge', - description=textwrap.dedent('''\ + help_msg = 'this is your help mesasge' + description = textwrap.dedent('''\ this is your description message -''' - )) + ''') + def __init__(self): + self.configure = Configure() + super().__init__('{}', self.help_msg, self.description) + def do_add_parser(self, parser_adder) -> argparse.ArgumentParser: parser = self._parser( parser_adder, diff --git a/src/oebuild/app/plugins/deploy/deploy_target.py b/src/oebuild/app/plugins/deploy/deploy_target.py index 91d0f4d..c09656d 100644 --- a/src/oebuild/app/plugins/deploy/deploy_target.py +++ b/src/oebuild/app/plugins/deploy/deploy_target.py @@ -26,16 +26,16 @@ class DeployTarget(OebuildCommand): we use package in a ''' + help_msg = 'deploy software on line' + description = textwrap.dedent('''\ + Deploys a recipe's build output (i.e. the output of the do_install task) to a live target machine over ssh. + By default, any existing files will be preserved instead of being overwritten and will be restored if you + run devtool undeploy-target. Note: this only deploys the recipe itself and not any runtime dependencies, + so it is assumed that those have been installed on the target beforehand. + ''') + def __init__(self) -> None: - super().__init__( - '{}', - 'deploy software on line', - textwrap.dedent('''\ -Deploys a recipe's build output (i.e. the output of the do_install task) to a live target machine over ssh. By default, any existing files will be preserved instead of being -overwritten and will be restored if you run devtool undeploy-target. Note: this only deploys the recipe itself and not any runtime dependencies, so it is assumed that those have -been installed on the target beforehand. -''' - )) + super().__init__('{}', self.help_msg, self.description) def do_add_parser(self, parser_adder) -> argparse.ArgumentParser: parser = self._parser( @@ -88,14 +88,13 @@ class UnDeployTarget(OebuildCommand): we use package in a ''' + help_msg = 'undeploy software on line' + description = textwrap.dedent('''\ + Un-deploys recipe output files previously deployed to a live target machine by devtool deploy-target. + ''') + def __init__(self) -> None: - super().__init__( - '{}', - 'undeploy software on line', - textwrap.dedent('''\ -Un-deploys recipe output files previously deployed to a live target machine by devtool deploy-target. -''' - )) + super().__init__('{}', self.help_msg, self.description) def do_add_parser(self, parser_adder) -> argparse.ArgumentParser: parser = self._parser( diff --git a/src/oebuild/app/plugins/generate/generate.py b/src/oebuild/app/plugins/generate/generate.py index 4e13ed7..b1ff728 100644 --- a/src/oebuild/app/plugins/generate/generate.py +++ b/src/oebuild/app/plugins/generate/generate.py @@ -38,6 +38,16 @@ class Generate(OebuildCommand): compile.yaml is generated according to different command parameters by generate ''' + help_msg = 'help to mkdir build directory and generate compile.yaml' + description = textwrap.dedent('''\ + The generate command is the core command in the entire build process, which + is mainly used to customize the build configuration parameters and generate + a compile.yaml by customizing each parameter. In addition, for a large number + of configuration parameter input is not very convenient, generate provides a + way to specify compile.yaml, users can directly specify the file after + customizing the build configuration file + ''') + def __init__(self): self.configure = Configure() self.nativesdk_dir = None @@ -45,18 +55,7 @@ class Generate(OebuildCommand): self.sstate_cache = None self.tmp_dir = None self.oebuild_kconfig_path = os.path.expanduser('~') + '/.local/oebuild_kconfig/' - super().__init__( - 'generate', - 'help to mkdir build directory and generate compile.yaml', - textwrap.dedent('''\ - The generate command is the core command in the entire build process, which - is mainly used to customize the build configuration parameters and generate - a compile.yaml by customizing each parameter. In addition, for a large number - of configuration parameter input is not very convenient, generate provides a - way to specify compile.yaml, users can directly specify the file after - customizing the build configuration file -''' - )) + super().__init__('generate', self.help_msg, self.description) def do_add_parser(self, parser_adder): parser = self._parser( diff --git a/src/oebuild/app/plugins/init/init.py b/src/oebuild/app/plugins/init/init.py index f0eefa6..0d4a0e6 100644 --- a/src/oebuild/app/plugins/init/init.py +++ b/src/oebuild/app/plugins/init/init.py @@ -30,15 +30,8 @@ class Init(OebuildCommand): to be followed by the directory name to be initialized ''' - def __init__(self): - self.configure = Configure() - self.oebuild_dir = None - self.src_dir = None - - super().__init__( - 'init', - 'Initialize an OEBUILD working directory', - textwrap.dedent('''\ + help_msg = 'Initialize an OEBUILD working directory' + description = textwrap.dedent('''\ Initialize the OEbuild working directory, and after executing this command, a new directory will be created as the OEBUILD working directory based on the current path. After initialization, the working directory will create an .oebuild @@ -50,8 +43,14 @@ class Init(OebuildCommand): certain changes according to their own needs。 This file is to meet the user's global consideration of the build configuration of OEbuild, and can be easily called by third parties -''' - )) + ''') + + def __init__(self): + self.configure = Configure() + self.oebuild_dir = None + self.src_dir = None + + super().__init__('init', self.help_msg, self.description) def do_add_parser(self, parser_adder): self._parser( diff --git a/src/oebuild/app/plugins/m_env/m_env.py b/src/oebuild/app/plugins/m_env/m_env.py index 4a242e6..600cfd8 100644 --- a/src/oebuild/app/plugins/m_env/m_env.py +++ b/src/oebuild/app/plugins/m_env/m_env.py @@ -37,20 +37,20 @@ class Menv(OebuildCommand): runned in qt system, the sdk with ros image can be used to develop apps runned in ros system ''' + help_msg = 'Development Environment Management' + description = textwrap.dedent(''' + This is an environment management function that allows you to configure the environment through + SDK files or unzipped setup files, and you can view, delete, and activate the corresponding + environment. These operations will not have any impact on your current machine + ''') + def __init__(self): self.configure = Configure() self.oebuild_env_path = os.path.expanduser('~') + '/.local/oebuild_env/' self.oebuild_env_yaml_path = pathlib.Path( os.path.join(self.oebuild_env_path, 'oebuild_env.yaml')) self.oebuild_env_command = ['list', 'create', 'activate', 'remove'] - super().__init__( - 'menv', - 'Development Environment Management', - description=textwrap.dedent(''' - This is an environment management function that allows you to configure the environment through - SDK files or unzipped setup files, and you can view, delete, and activate the corresponding - environment. These operations will not have any impact on your current machine - ''')) + super().__init__('menv', self.help_msg, self.description) def do_add_parser(self, parser_adder) -> argparse.ArgumentParser: parser = self._parser( diff --git a/src/oebuild/app/plugins/m_plugin/m_plugin.py b/src/oebuild/app/plugins/m_plugin/m_plugin.py index 5c4a245..a142f0f 100644 --- a/src/oebuild/app/plugins/m_plugin/m_plugin.py +++ b/src/oebuild/app/plugins/m_plugin/m_plugin.py @@ -32,6 +32,13 @@ class MPlugin(OebuildCommand): developped, you can use it through oebuild. """ + help_msg = ' Manage Personal Custom plugin' + description = textwrap.dedent(''' + This is a plugin management function that supports users to customize plugin and add them to the oebuild + for use. plugin only affect locally installed oebuilds, and supports viewing personal existing plugin and + uninstalling plugin. + ''') + def __init__(self): self.configure = Configure() self.oebuild_plugin_commands = ['install', 'list', 'enable', 'disable', 'remove'] @@ -42,14 +49,7 @@ class MPlugin(OebuildCommand): plugin_dir = pathlib.Path(oebuild_util.get_plugins_yaml_path()) self.command_ext = OebuildApp().get_command_ext( oebuild_util.read_yaml(plugin_dir)['plugins']) - super().__init__( - 'mplugin', - ' Manage Personal Custom plugin', - description=textwrap.dedent(''' - This is a plugin management function that supports users to customize plugin and add them to the oebuild - for use. plugin only affect locally installed oebuilds, and supports viewing personal existing plugin and - uninstalling plugin.''' - )) + super().__init__('mplugin', self.help_msg, self.description) def do_add_parser(self, parser_adder) -> argparse.ArgumentParser: """ diff --git a/src/oebuild/app/plugins/manifest/manifest.py b/src/oebuild/app/plugins/manifest/manifest.py index 8578853..a797e57 100644 --- a/src/oebuild/app/plugins/manifest/manifest.py +++ b/src/oebuild/app/plugins/manifest/manifest.py @@ -33,18 +33,17 @@ class Manifest(OebuildCommand): relevant source repositories based on the manifest file ''' + help_msg = 'generate manifest from oebuild workspace' + description = textwrap.dedent('''\ + manifest provides the manifest function of generating dependent + source repositories in the build working directory, and can restore + relevant source repositories based on the manifest file + ''') + def __init__(self): self.configure = Configure() self.manifest_command = ['download', 'create'] - super().__init__( - 'manifest', - 'generate manifest from oebuild workspace', - textwrap.dedent('''\ - manifest provides the manifest function of generating dependent - source repositories in the build working directory, and can restore - relevant source repositories based on the manifest file -''' - )) + super().__init__('manifest', self.help_msg, self.description) def do_add_parser(self, parser_adder) -> argparse.ArgumentParser: parser = self._parser( diff --git a/src/oebuild/app/plugins/run_qemu/run_qemu.py b/src/oebuild/app/plugins/run_qemu/run_qemu.py index 5b07801..bf72286 100644 --- a/src/oebuild/app/plugins/run_qemu/run_qemu.py +++ b/src/oebuild/app/plugins/run_qemu/run_qemu.py @@ -31,6 +31,11 @@ class RunQemu(OebuildCommand): The command for run in qemu platform. ''' + help_msg = 'run in qemu platform' + description = textwrap.dedent(''' + The command for run in qemu platform. + ''') + def __init__(self): self.configure = Configure() self.client = None @@ -38,13 +43,7 @@ class RunQemu(OebuildCommand): self.work_dir = os.getcwd() self.old_bashrc = None - super().__init__( - 'run_qemu', - 'run in qemu platform', - textwrap.dedent(''' - The command for run in qemu platform. - ''') - ) + super().__init__('run_qemu', self.help_msg, self.description) def __del__(self): if self.client is not None: diff --git a/src/oebuild/app/plugins/update/update.py b/src/oebuild/app/plugins/update/update.py index 96d8338..633bcaa 100644 --- a/src/oebuild/app/plugins/update/update.py +++ b/src/oebuild/app/plugins/update/update.py @@ -36,13 +36,8 @@ class Update(OebuildCommand): related to the build, such as container images, build base repositories, etc ''' - def __init__(self): - self.configure = Configure() - - super().__init__( - 'update', - 'Update the basic environment required for the build', - textwrap.dedent(''' + help_msg = 'Update the basic environment required for the build' + description = textwrap.dedent(''' The update command will involve three areas, namely the build container, yocto-meta-openeuler and the corresponding layers, if there are no parameters after the update, it will be updated in order yocto-meta-openeuler, build @@ -60,7 +55,11 @@ class Update(OebuildCommand): the information that needs to be updated, and if it is in the build directory, it will parse compile.yaml to get the updated information ''') - ) + + def __init__(self): + self.configure = Configure() + + super().__init__('update', self.help_msg, self.description) def do_add_parser(self, parser_adder): parser = self._parser( diff --git a/src/oebuild/spec.py b/src/oebuild/spec.py index 5b19ba7..6b129cc 100644 --- a/src/oebuild/spec.py +++ b/src/oebuild/spec.py @@ -62,18 +62,11 @@ class _CmdFactory: # Get the attribute which provides the OebuildCommand subclass. try: - cls = getattr(mod, self.attr) + return getattr(mod, self.attr) except AttributeError as a_e: raise ExtensionCommandError( hint=f'no attribute {self.attr} in {self.py_file}') from a_e - # Create the command instance and return it. - try: - return cls() - except Exception as e_p: - raise ExtensionCommandError( - hint='command constructor threw an exception') from e_p - @dataclass class OebuildExtCommandSpec: diff --git a/src/oebuild/util.py b/src/oebuild/util.py index 14945fb..6081532 100644 --- a/src/oebuild/util.py +++ b/src/oebuild/util.py @@ -199,7 +199,7 @@ def get_instance(factory): ''' Instantiate a class ''' - return factory() + return factory()() def restore_bashrc_content(old_content): -- Gitee From 06d4acae31181028af262e57a774dc30bd0ff1b3 Mon Sep 17 00:00:00 2001 From: xuansen fang Date: Thu, 14 Mar 2024 15:36:09 +0800 Subject: [PATCH 2/4] Modify: format error *modify flake8 and pylint format error Signed-off-by: fangxuansen --- src/oebuild/app/plugins/bitbake/bitbake.py | 26 +- src/oebuild/app/plugins/demo/demo.py | 8 +- .../app/plugins/deploy/deploy_target.py | 51 ++-- src/oebuild/app/plugins/init/init.py | 43 ++-- src/oebuild/app/plugins/m_env/m_env.py | 119 ++++++---- src/oebuild/app/plugins/m_plugin/m_plugin.py | 223 ++++++++++-------- src/oebuild/app/plugins/manifest/manifest.py | 40 ++-- src/oebuild/app/plugins/run_qemu/run_qemu.py | 59 ++--- src/oebuild/app/plugins/update/update.py | 89 ++++--- src/oebuild/spec.py | 23 +- 10 files changed, 400 insertions(+), 281 deletions(-) diff --git a/src/oebuild/app/plugins/bitbake/bitbake.py b/src/oebuild/app/plugins/bitbake/bitbake.py index 0c416ad..04fee5e 100644 --- a/src/oebuild/app/plugins/bitbake/bitbake.py +++ b/src/oebuild/app/plugins/bitbake/bitbake.py @@ -36,9 +36,9 @@ class Bitbake(OebuildCommand): help_msg = 'execute bitbake command' description = textwrap.dedent(''' - The bitbake command performs the build operation, and for the build environment, - there are two types, one is to build in docker and the other is to build in the - host. There are also two construction methods, one is to build directly, and the + The bitbake command performs the build operation, and for the build environment, + there are two types, one is to build in docker and the other is to build in the + host. There are also two construction methods, one is to build directly, and the other is to call up the build environment to be operated freely by the user ''') @@ -49,15 +49,16 @@ class Bitbake(OebuildCommand): super().__init__('bitbake', self.help_msg, self.description) def do_add_parser(self, parser_adder) -> argparse.ArgumentParser: - parser = self._parser( - parser_adder, - usage=''' + parser = self._parser(parser_adder, + usage=''' %(prog)s [command] ''') parser_adder.add_argument( - 'command', nargs='?', default=None, + 'command', + nargs='?', + default=None, help='''The name of the directory that will be initialized''') return parser @@ -78,7 +79,8 @@ class Bitbake(OebuildCommand): command = self._get_command(unknow=unknown) if not self.check_support_bitbake(): - logger.error("Please do it in compile workspace which contain compile.yaml") + logger.error( + "Please do it in compile workspace which contain compile.yaml") return if not os.path.exists('.env'): @@ -91,9 +93,11 @@ class Bitbake(OebuildCommand): return # if has manifest.yaml, init layer repo with it - yocto_dir = os.path.join(self.configure.source_dir(), "yocto-meta-openeuler") + yocto_dir = os.path.join(self.configure.source_dir(), + "yocto-meta-openeuler") manifest_path = os.path.join(yocto_dir, ".oebuild/manifest.yaml") - parse_compile.check_with_version(self.configure.source_dir(), manifest_path=manifest_path) + parse_compile.check_with_version(self.configure.source_dir(), + manifest_path=manifest_path) parse_env = ParseEnv(env_dir='.env') if parse_compile.build_in == oebuild_const.BUILD_IN_HOST: @@ -110,7 +114,7 @@ class Bitbake(OebuildCommand): parse_compile=parse_compile, command=command) - def check_support_bitbake(self,): + def check_support_bitbake(self, ): ''' The execution of the bitbake instruction mainly relies on compile.yaml, which is initialized by parsing the file diff --git a/src/oebuild/app/plugins/demo/demo.py b/src/oebuild/app/plugins/demo/demo.py index c78b15c..cfd80f1 100644 --- a/src/oebuild/app/plugins/demo/demo.py +++ b/src/oebuild/app/plugins/demo/demo.py @@ -3,7 +3,6 @@ import textwrap import logging from oebuild.command import OebuildCommand -from oebuild.util import * from oebuild.configure import Configure logger = logging.getLogger() @@ -19,11 +18,10 @@ class Demo(OebuildCommand): def __init__(self): self.configure = Configure() super().__init__('{}', self.help_msg, self.description) - + def do_add_parser(self, parser_adder) -> argparse.ArgumentParser: - parser = self._parser( - parser_adder, - usage=''' + parser = self._parser(parser_adder, + usage=''' %(prog)s [-m URL] [--mr REVISION] [--mf FILE] [directory] %(prog)s -l [--mf FILE] directory diff --git a/src/oebuild/app/plugins/deploy/deploy_target.py b/src/oebuild/app/plugins/deploy/deploy_target.py index c09656d..0dffa21 100644 --- a/src/oebuild/app/plugins/deploy/deploy_target.py +++ b/src/oebuild/app/plugins/deploy/deploy_target.py @@ -14,7 +14,6 @@ import argparse import textwrap import logging - from oebuild.command import OebuildCommand from oebuild.app.plugins.deploy.com_target import ComTarget @@ -28,20 +27,22 @@ class DeployTarget(OebuildCommand): help_msg = 'deploy software on line' description = textwrap.dedent('''\ - Deploys a recipe's build output (i.e. the output of the do_install task) to a live target machine over ssh. - By default, any existing files will be preserved instead of being overwritten and will be restored if you - run devtool undeploy-target. Note: this only deploys the recipe itself and not any runtime dependencies, - so it is assumed that those have been installed on the target beforehand. + Deploys a recipe's build output (i.e. the output of the do_install task) + to a live target machine over ssh. By default, any existing files will be + preserved instead of being overwritten and will be restored if you run + devtool undeploy-target. Note: this only deploys the recipe itself and + not any runtime dependencies, so it is assumed that those have been + installed on the target beforehand. ''') def __init__(self) -> None: super().__init__('{}', self.help_msg, self.description) def do_add_parser(self, parser_adder) -> argparse.ArgumentParser: - parser = self._parser( - parser_adder, - usage=''' -oebuild deploy-target [-h] [-c] [-s] [-n] [-p] [--no-check-space] [-e SSH_EXEC] [-P PORT] [-I KEY] [-S | --no-strip] recipename target + parser = self._parser(parser_adder, + usage=''' +oebuild deploy-target [-h] [-c] [-s] [-n] [-p] [--no-check-space] [-e SSH_EXEC] +[-P PORT] [-I KEY] [-S | --no-strip] recipename target ''') return parser @@ -54,12 +55,15 @@ oebuild deploy-target [-h] [-c] [-s] [-n] [-p] [--no-check-space] [-e SSH_EXEC] com_target = ComTarget() com_target.exec(str_args=str_args, fun="deploy-target") - def print_help_msg(self,): + def print_help_msg(self, ): print(""" -usage: oebuild deploy-target [-h] [-c] [-s] [-n] [-p] [--no-check-space] [-e SSH_EXEC] [-P PORT] [-I KEY] [-S | --no-strip] recipename target +usage: oebuild deploy-target [-h] [-c] [-s] [-n] [-p] [--no-check-space] [-e SSH_EXEC] + [-P PORT] [-I KEY] [-S | --no-strip] recipename target -Deploys a recipe's build output (i.e. the output of the do_install task) to a live target machine over ssh. By default, any existing files will be preserved instead of being -overwritten and will be restored if you run devtool undeploy-target. Note: this only deploys the recipe itself and not any runtime dependencies, so it is assumed that those have +Deploys a recipe's build output (i.e. the output of the do_install task) to a live target + machine over ssh. By default, any existing files will be preserved instead of being +overwritten and will be restored if you run devtool undeploy-target. Note: this only deploys + the recipe itself and not any runtime dependencies, so it is assumed that those have been installed on the target beforehand. arguments: @@ -77,7 +81,9 @@ options: Executable to use in place of ssh -P PORT, --port PORT Specify port to use for connection to the target -I KEY, --key KEY Specify ssh private key for connection to the target - -S, --strip Strip executables prior to deploying (default: False). The default value of this option can be controlled by setting the strip option in the [Deploy] + -S, --strip Strip executables prior to deploying (default: False). + The default value of this option can be controlled by + setting the strip option in the [Deploy] section to True or False. --no-strip Do not strip executables prior to deploy """) @@ -90,17 +96,18 @@ class UnDeployTarget(OebuildCommand): help_msg = 'undeploy software on line' description = textwrap.dedent('''\ - Un-deploys recipe output files previously deployed to a live target machine by devtool deploy-target. + Un-deploys recipe output files previously deployed to a live target machine + by devtool deploy-target. ''') def __init__(self) -> None: super().__init__('{}', self.help_msg, self.description) def do_add_parser(self, parser_adder) -> argparse.ArgumentParser: - parser = self._parser( - parser_adder, - usage=''' -oebuild undeploy-target [-h] [-c] [-s] [-a] [-n] [-e SSH_EXEC] [-P PORT] [-I KEY] [recipename] target + parser = self._parser(parser_adder, + usage=''' +oebuild undeploy-target [-h] [-c] [-s] [-a] [-n] [-e SSH_EXEC] +[-P PORT] [-I KEY] [recipename] target ''') return parser @@ -116,9 +123,11 @@ oebuild undeploy-target [-h] [-c] [-s] [-a] [-n] [-e SSH_EXEC] [-P PORT] [-I KEY def print_help_msg(self): print(""" -usage: oebuild undeploy-target [-h] [-c] [-s] [-a] [-n] [-e SSH_EXEC] [-P PORT] [-I KEY] [recipename] target +usage: oebuild undeploy-target [-h] [-c] [-s] [-a] [-n] [-e SSH_EXEC] + [-P PORT] [-I KEY] [recipename] target -Un-deploys recipe output files previously deployed to a live target machine by devtool deploy-target. +Un-deploys recipe output files previously deployed to a live target machine + by devtool deploy-target. arguments: recipename Recipe to undeploy (if not using -a/--all) diff --git a/src/oebuild/app/plugins/init/init.py b/src/oebuild/app/plugins/init/init.py index 0d4a0e6..6cf5ef0 100644 --- a/src/oebuild/app/plugins/init/init.py +++ b/src/oebuild/app/plugins/init/init.py @@ -44,7 +44,7 @@ class Init(OebuildCommand): consideration of the build configuration of OEbuild, and can be easily called by third parties ''') - + def __init__(self): self.configure = Configure() self.oebuild_dir = None @@ -55,19 +55,24 @@ class Init(OebuildCommand): def do_add_parser(self, parser_adder): self._parser( parser_adder, - usage=''' - - %(prog)s [directory] [-u yocto_remote_url] [-b branch] -''') + usage='''%(prog)s [directory] [-u yocto_remote_url] [-b branch]''') - parser_adder.add_argument('-u', '--yocto_remote_url', dest='yocto_remote_url', - help='''Specifies the remote of yocto-meta-openeuler''') + parser_adder.add_argument( + '-u', + '--yocto_remote_url', + dest='yocto_remote_url', + help='''Specifies the remote of yocto-meta-openeuler''') - parser_adder.add_argument('-b', '--branch', dest='branch', - help='''Specifies the branch of yocto-meta-openeuler''') + parser_adder.add_argument( + '-b', + '--branch', + dest='branch', + help='''Specifies the branch of yocto-meta-openeuler''') parser_adder.add_argument( - 'directory', nargs='?', default=None, + 'directory', + nargs='?', + default=None, help='''The name of the directory that will be initialized''') return parser_adder @@ -86,6 +91,7 @@ class Init(OebuildCommand): if self.configure.is_oebuild_dir(): log = f'The "{os.path.dirname(self.configure.oebuild_dir())}" \ has already been initialized, please change other directory' + logger.error(log) sys.exit(-1) @@ -102,20 +108,24 @@ class Init(OebuildCommand): os.chdir(args.directory) oebuild_config: Config = self.configure.parse_oebuild_config() - yocto_config: ConfigBasicRepo = oebuild_config.basic_repo[oebuild_const.YOCTO_META_OPENEULER] + yocto_config: ConfigBasicRepo = \ + oebuild_config.basic_repo[oebuild_const.YOCTO_META_OPENEULER] + if args.yocto_remote_url is not None: yocto_config.remote_url = args.yocto_remote_url if args.branch is not None: yocto_config.branch = args.branch - oebuild_config.basic_repo[oebuild_const.YOCTO_META_OPENEULER] = yocto_config + oebuild_config.basic_repo[ + oebuild_const.YOCTO_META_OPENEULER] = yocto_config self.configure.update_oebuild_config(oebuild_config) logger.info("init %s successful", args.directory) format_msg = f''' -There is a build configuration example file under {args.directory}/.oebuild/compile.yaml.sample, -if you want to block complex generate instructions, you can directly copy a configuration file, -and then modify it according to your own needs, and then execute `oebuild generate -c `. +There is a build configuration example file under {args.directory}/.oebuild/compile.yaml.sample, +if you want to block complex generate instructions, you can directly copy a configuration file, +and then modify it according to your own needs, and then execute + `oebuild generate -c `. please execute the follow commands next cd {os.path.abspath(os.getcwd())} @@ -182,6 +192,7 @@ please execute the follow commands next ''' try: compil = oebuild_util.get_compile_yaml_dir() - shutil.copyfile(compil, os.path.join(updir, oebuild_const.COMPILE_YAML)) + shutil.copyfile(compil, + os.path.join(updir, oebuild_const.COMPILE_YAML)) except FileNotFoundError: logger.error("mkdir compile.yaml.sample failed") diff --git a/src/oebuild/app/plugins/m_env/m_env.py b/src/oebuild/app/plugins/m_env/m_env.py index 600cfd8..70c16ab 100644 --- a/src/oebuild/app/plugins/m_env/m_env.py +++ b/src/oebuild/app/plugins/m_env/m_env.py @@ -39,55 +39,60 @@ class Menv(OebuildCommand): help_msg = 'Development Environment Management' description = textwrap.dedent(''' - This is an environment management function that allows you to configure the environment through - SDK files or unzipped setup files, and you can view, delete, and activate the corresponding - environment. These operations will not have any impact on your current machine + This is an environment management function that allows you to configure + the environment through SDK files or unzipped setup files, and you can + view, delete, and activate the corresponding environment. + These operations will not have any impact on your current machine ''') def __init__(self): self.configure = Configure() - self.oebuild_env_path = os.path.expanduser('~') + '/.local/oebuild_env/' + self.oebuild_env_path = os.path.expanduser( + '~') + '/.local/oebuild_env/' self.oebuild_env_yaml_path = pathlib.Path( os.path.join(self.oebuild_env_path, 'oebuild_env.yaml')) self.oebuild_env_command = ['list', 'create', 'activate', 'remove'] super().__init__('menv', self.help_msg, self.description) def do_add_parser(self, parser_adder) -> argparse.ArgumentParser: - parser = self._parser( - parser_adder, - usage=''' + parser = self._parser(parser_adder, + usage=''' %(prog)s [create list remove activate][command] create: [-d -f] Create an environment -n env_name - list: View existing environment - remove: -n Delete specified environment - activate: -n Activate specified environment + list: View existing environment + remove: -n Delete specified environment + activate: -n Activate specified environment ''') - parser.add_argument('-d', '--directory', dest='directory', + parser.add_argument('-d', + '--directory', + dest='directory', help=''' this param is build directory - ''' - ) + ''') - parser.add_argument('-f', '--file', dest='file', + parser.add_argument('-f', + '--file', + dest='file', help=''' this param is build file - ''' - ) + ''') - parser.add_argument('-n', '--env_name', dest='env_name', + parser.add_argument('-n', + '--env_name', + dest='env_name', help=''' this param is env_name - ''' - ) + ''') # Secondary command return parser def do_run(self, args: argparse.Namespace, unknown=None): # perpare parse help command - if unknown[0] not in self.oebuild_env_command or (len(set(unknown[1:]).intersection( - {'-d', '-f', '-n'})) == 0 and unknown[0] != 'list'): + if unknown[0] not in self.oebuild_env_command or ( + len(set(unknown[1:]).intersection({'-d', '-f', '-n'})) == 0 + and unknown[0] != 'list'): unknown = ['-h'] else: command = unknown[0] @@ -100,26 +105,32 @@ class Menv(OebuildCommand): if command == 'create': if not args.env_name: print(''' - Please enter the correct command: oebuild menv create [-d -f] Create an environment -n env_name + Please enter the correct command: + oebuild menv create [-d -f] Create an environment -n env_name ''') return # Check if the file path exists if args.directory and os.path.isdir(args.directory): setup_file_path = os.path.abspath(args.directory) - sdk_name = args.env_name if args.env_name else args.directory.split('/')[-1] + sdk_name = args.env_name if args.env_name else args.directory.split( + '/')[-1] self.create_or_update_env_yaml(sdk_name, args.directory) - print(f' Created Environment successfully \n {sdk_name.ljust(30)}{setup_file_path}') + print( + f' Created Environment successfully \n {sdk_name.ljust(30)}{setup_file_path}' + ) return # Creating an environment if args.file and os.path.exists(args.file): sdk_name = args.env_name if args.env_name else ( - args.file.split('/')[-1].replace('.sh', '') if args.file else None - ) + args.file.split('/')[-1].replace('.sh', '') + if args.file else None) setup_file_path = self.oebuild_env_path + sdk_name self.create_or_update_env_yaml(sdk_name, setup_file_path) self.execute_sdk_file(args.file, setup_file_path) - print(f' Created Environment successfully \n {sdk_name.ljust(30)}{setup_file_path}') + print( + f' Created Environment successfully \n {sdk_name.ljust(30)}{setup_file_path}' + ) return print('The path is invalid, please check the path ') @@ -127,7 +138,9 @@ class Menv(OebuildCommand): # Activate Environment if args.env_name: return self.activate_environment(args.env_name) - print('Please enter the correct command: oebuild menv activate -n env_name') + print( + 'Please enter the correct command: oebuild menv activate -n env_name' + ) elif command == 'list': env_dict = oebuild_util.read_yaml(self.oebuild_env_yaml_path) @@ -141,7 +154,9 @@ class Menv(OebuildCommand): elif command == 'remove': if args.env_name: return self.delete_environment(args.env_name) - print('Please enter the correct command: oebuild menv remove -n env_name') + print( + 'Please enter the correct command: oebuild menv remove -n env_name' + ) def execute_setup_directory(self, setup_file_path, env_name): """ @@ -168,15 +183,19 @@ class Menv(OebuildCommand): print(shell_command) print('setup_file matching successful') - subprocess.check_output('cp ~/.bashrc ~/.bashrc_back', shell=True) + subprocess.check_output('cp ~/.bashrc ~/.bashrc_back', + shell=True) # Obtain the current terminal height and length - terminal_info = fcntl.ioctl(sys.stdout.fileno(), termios.TIOCGWINSZ, "1234") + terminal_info = fcntl.ioctl(sys.stdout.fileno(), + termios.TIOCGWINSZ, "1234") rows_and_cloumns = struct.unpack('HH', terminal_info) rows_command = f'stty rows {rows_and_cloumns[0]} columns {rows_and_cloumns[1]}' - subprocess.check_output(rf"sed -i '$a\{rows_command}' ~/.bashrc", shell=True) + subprocess.check_output( + rf"sed -i '$a\{rows_command}' ~/.bashrc", shell=True) # Add the command to execute the setup file in the .bashrc file in the # working directory - subprocess.check_output(rf"sed -i '$a\{shell_command}' ~/.bashrc", shell=True) + subprocess.check_output( + rf"sed -i '$a\{shell_command}' ~/.bashrc", shell=True) # Replace Console Prompt subprocess.check_output( rf"sed -i 's/\$ /({env_name})>>>>> /g' ~/.bashrc", @@ -218,11 +237,15 @@ class Menv(OebuildCommand): """ try: if os.path.isdir(setup_file_path): - print(f'The setup file folder already exists.path is {setup_file_path}') + print( + f'The setup file folder already exists.path is {setup_file_path}' + ) else: print('Extracting sdk...............') - subprocess.check_output(f'sh {sdk_file} -d {setup_file_path} -y', shell=True) - subprocess.check_output(f'chmod -R 755 {setup_file_path}', shell=True) + subprocess.check_output( + f'sh {sdk_file} -d {setup_file_path} -y', shell=True) + subprocess.check_output(f'chmod -R 755 {setup_file_path}', + shell=True) except SubprocessError as s_e: print('Please provide the valid folder path') logger.error(str(s_e)) @@ -239,17 +262,25 @@ class Menv(OebuildCommand): """ if not os.path.exists(self.oebuild_env_yaml_path.absolute()): - if not os.path.exists(os.path.dirname(self.oebuild_env_yaml_path.absolute())): - os.makedirs(os.path.dirname(self.oebuild_env_yaml_path.absolute())) + if not os.path.exists( + os.path.dirname(self.oebuild_env_yaml_path.absolute())): + os.makedirs( + os.path.dirname(self.oebuild_env_yaml_path.absolute())) os.mknod(self.oebuild_env_yaml_path) env_dict = oebuild_util.read_yaml(self.oebuild_env_yaml_path) if env_dict and 'env_config' in env_dict: - env_list = self.input_or_update_dict(env_name, setup_file_path, env_dict['env_config']) + env_list = self.input_or_update_dict(env_name, setup_file_path, + env_dict['env_config']) env_dict['env_config'] = env_list oebuild_util.write_yaml(self.oebuild_env_yaml_path, env_dict) return - env_dict = {'env_config': [{'env_name': env_name, 'env_value': setup_file_path}]} + env_dict = { + 'env_config': [{ + 'env_name': env_name, + 'env_value': setup_file_path + }] + } oebuild_util.write_yaml(self.oebuild_env_yaml_path, env_dict) def input_or_update_dict(self, env_name, env_value, env_list): @@ -271,7 +302,7 @@ class Menv(OebuildCommand): if env_data['env_name'] == env_name: while True: user_input = input(""" - Do you want to overwrite the path of the original environment configuration(Y/N) + Do you want to overwrite the path of the original environment configuration(Y/N) """) if user_input.lower() == 'y': env_data['env_value'] = env_value @@ -331,14 +362,16 @@ class Menv(OebuildCommand): env_list.append(env_data) elif '/.local/oebuild_env/' in env_data['env_value']: try: - subprocess.check_output(f'rm -rf {env_data["env_value"]}', shell=True) + subprocess.check_output( + f'rm -rf {env_data["env_value"]}', shell=True) except SubprocessError as s_e: print('Fail deleted') logger.error(str(s_e)) sys.exit(-1) if len(env_list) == len(env_dict['env_config']): - logger.error('The environment does not exist, please check the input') + logger.error( + 'The environment does not exist, please check the input') sys.exit(-1) env_dict['env_config'] = env_list oebuild_util.write_yaml(self.oebuild_env_yaml_path, env_dict) diff --git a/src/oebuild/app/plugins/m_plugin/m_plugin.py b/src/oebuild/app/plugins/m_plugin/m_plugin.py index a142f0f..2f6730f 100644 --- a/src/oebuild/app/plugins/m_plugin/m_plugin.py +++ b/src/oebuild/app/plugins/m_plugin/m_plugin.py @@ -34,18 +34,22 @@ class MPlugin(OebuildCommand): help_msg = ' Manage Personal Custom plugin' description = textwrap.dedent(''' - This is a plugin management function that supports users to customize plugin and add them to the oebuild - for use. plugin only affect locally installed oebuilds, and supports viewing personal existing plugin and - uninstalling plugin. + This is a plugin management function that supports users to customize plugin and + add them to the oebuild for use. plugin only affect locally installed oebuilds, + and supports viewing personal existing plugin and uninstalling plugin. ''') def __init__(self): self.configure = Configure() - self.oebuild_plugin_commands = ['install', 'list', 'enable', 'disable', 'remove'] - self.oebuild_plugin_path = os.path.expanduser('~') + '/.local/oebuild_plugins/' - self.oebuild_plugin_yaml_path = pathlib.Path( - self.oebuild_plugin_path, 'append_plugins.yaml') - self.oebuild_plugin_repository = pathlib.Path(self.oebuild_plugin_path, 'appends') + self.oebuild_plugin_commands = [ + 'install', 'list', 'enable', 'disable', 'remove' + ] + self.oebuild_plugin_path = os.path.expanduser( + '~') + '/.local/oebuild_plugins/' + self.oebuild_plugin_yaml_path = pathlib.Path(self.oebuild_plugin_path, + 'append_plugins.yaml') + self.oebuild_plugin_repository = pathlib.Path(self.oebuild_plugin_path, + 'appends') plugin_dir = pathlib.Path(oebuild_util.get_plugins_yaml_path()) self.command_ext = OebuildApp().get_command_ext( oebuild_util.read_yaml(plugin_dir)['plugins']) @@ -63,9 +67,8 @@ class MPlugin(OebuildCommand): Returns: """ - parser = self._parser( - parser_adder, - usage=''' + parser = self._parser(parser_adder, + usage=''' %(prog)s [install list remove enable/disable][command] install: -f file_path -n plugin_name @@ -74,29 +77,33 @@ class MPlugin(OebuildCommand): enable/disable: enable/disable -n plugin_name remove: -n plugin_name ''') - parser.add_argument('-f', '--file', dest='file', + parser.add_argument('-f', + '--file', + dest='file', help=''' this param is python file - ''' - ) + ''') - parser.add_argument('-n', '--plugin_name', dest='plugin_name', + parser.add_argument('-n', + '--plugin_name', + dest='plugin_name', help=''' this param is plugin name - ''' - ) + ''') - parser.add_argument('-d', '--dir_path', dest='dir_path', + parser.add_argument('-d', + '--dir_path', + dest='dir_path', help=''' this param is dir path - ''' - ) + ''') - parser.add_argument('-m', '--major', dest='major', + parser.add_argument('-m', + '--major', + dest='major', help=''' this param is major class - ''' - ) + ''') return parser @@ -116,8 +123,9 @@ class MPlugin(OebuildCommand): command = '' if not unknown: unknown = ['-h'] - elif unknown[0] not in self.oebuild_plugin_commands or (len(set(unknown[1:]).intersection( - {'-f', '-n', '-d'})) == 0 and unknown[0] != 'list'): + elif unknown[0] not in self.oebuild_plugin_commands or ( + len(set(unknown[1:]).intersection({'-f', '-n', '-d'})) == 0 + and unknown[0] != 'list'): unknown = ['-h'] else: command = unknown[0] @@ -128,15 +136,18 @@ class MPlugin(OebuildCommand): args = args.parse_args(unknown) if not os.path.exists(self.oebuild_plugin_yaml_path.absolute()): - if not os.path.exists(os.path.dirname(self.oebuild_plugin_yaml_path.absolute())): - os.makedirs(os.path.dirname(self.oebuild_plugin_yaml_path.absolute())) + if not os.path.exists( + os.path.dirname(self.oebuild_plugin_yaml_path.absolute())): + os.makedirs( + os.path.dirname(self.oebuild_plugin_yaml_path.absolute())) os.mknod(self.oebuild_plugin_yaml_path) plugin_dict = oebuild_util.read_yaml(self.oebuild_plugin_yaml_path) plugin_dict_old = copy.deepcopy(plugin_dict) if command == 'install': if not args.plugin_name: - logger.error('Please enter the correct command: Missing -n parameter ') + logger.error( + 'Please enter the correct command: Missing -n parameter ') return if args.plugin_name == 'mplugin': @@ -144,53 +155,53 @@ class MPlugin(OebuildCommand): return if plugin_dict is not None: - append_command_ext = OebuildApp().get_command_ext(plugin_dict['plugins']) + append_command_ext = OebuildApp().get_command_ext( + plugin_dict['plugins']) else: append_command_ext = {} if args.plugin_name in self.command_ext.keys() \ or args.plugin_name in append_command_ext.keys(): while True: - user_input = input( - f'Do you want to overwrite the existing plugin ({args.plugin_name}) in oebuild(Y/N)') + user_input = input(f'Do you want to overwrite \ + the existing plugin ({args.plugin_name}) in oebuild(Y/N)' + ) if user_input.lower() == 'y': break if user_input.lower() == 'n': return if args.file and os.path.exists(args.file): - self.install_plugin(args.file, - args.plugin_name, - plugin_dict, - plugin_dict_old, - command, - None) + self.install_plugin(args.file, args.plugin_name, plugin_dict, + plugin_dict_old, command, None) elif args.dir_path and os.path.exists(args.dir_path): if not args.major: logger.error(" Please specify the major file ") return - file = str(pathlib.Path(args.dir_path, args.major.split('/')[-1])) + file = str( + pathlib.Path(args.dir_path, + args.major.split('/')[-1])) if not os.path.exists(file): - logger.error("the %s not exist, please check the plugin file path", file) + logger.error( + "the %s not exist, please check the plugin file path", + file) return - self.install_plugin( - file, - args.plugin_name, - plugin_dict, - plugin_dict_old, - command, - args.dir_path) + self.install_plugin(file, args.plugin_name, plugin_dict, + plugin_dict_old, command, args.dir_path) else: - logger.error("the %s not exist, please check the plugin file path", args.file) + logger.error( + "the %s not exist, please check the plugin file path", + args.file) return if command == 'list': if plugin_dict and 'plugins' in plugin_dict: print("""# oebuild plugin:\n#""") print(f'{"name".ljust(20)}{"status".ljust(20)}{"path"}') for plugin_data in plugin_dict['plugins']: - print(f'{plugin_data["name"].ljust(20)}{str(plugin_data["status"]).ljust(20)}' - f'{plugin_data["path"]}') + print( + f'{plugin_data["name"].ljust(20)}{str(plugin_data["status"]).ljust(20)}' + f'{plugin_data["path"]}') else: logger.error('No plugin has been created yet') return @@ -200,7 +211,8 @@ class MPlugin(OebuildCommand): for plugin_data in plugin_dict['plugins']: if plugin_data['name'] == args.plugin_name: plugin_data['status'] = command - oebuild_util.write_yaml(self.oebuild_plugin_yaml_path, plugin_dict) + oebuild_util.write_yaml(self.oebuild_plugin_yaml_path, + plugin_dict) print('change success') return logger.error('the plugin %s does not exist', args.plugin_name) @@ -210,7 +222,8 @@ class MPlugin(OebuildCommand): if command == 'remove': self.remove_plugin(args.plugin_name) - def create_or_update_plugin_yaml(self, plugin_name, class_name, python_file_name, plugin_dict): + def create_or_update_plugin_yaml(self, plugin_name, class_name, + python_file_name, plugin_dict): """ Args: @@ -226,26 +239,25 @@ class MPlugin(OebuildCommand): if plugin_dict and 'plugins' in plugin_dict: plugin_list, old_plugin_path = self.input_or_update_dict( - plugin_name, - class_name, - python_file_name, - 'enable', + plugin_name, class_name, python_file_name, 'enable', plugin_dict['plugins']) plugin_dict['plugins'] = plugin_list oebuild_util.write_yaml(self.oebuild_plugin_yaml_path, plugin_dict) return old_plugin_path - plugin_dict = {'plugins': [{'name': plugin_name, 'class': class_name, - 'path': python_file_name, 'status': 'enable'}]} + plugin_dict = { + 'plugins': [{ + 'name': plugin_name, + 'class': class_name, + 'path': python_file_name, + 'status': 'enable' + }] + } oebuild_util.write_yaml(self.oebuild_plugin_yaml_path, plugin_dict) return old_plugin_path - def input_or_update_dict(self, - plugin_name, - class_name, - python_file_name, - plugin_status, - plugin_list): + def input_or_update_dict(self, plugin_name, class_name, python_file_name, + plugin_status, plugin_list): """ Modify or insert environmental data Args: @@ -266,13 +278,18 @@ class MPlugin(OebuildCommand): return plugin_list, old_plugin_path if plugin_data['name'] == plugin_name: plugin_data['class'] = class_name - old_plugin_path = os.path.abspath(os.path.dirname(plugin_data['path'])) + old_plugin_path = os.path.abspath( + os.path.dirname(plugin_data['path'])) plugin_data['path'] = python_file_name plugin_data['status'] = plugin_status insert_flag = False if insert_flag: - plugin_list.append({'name': plugin_name, 'class': class_name, - 'path': python_file_name, 'status': plugin_status}) + plugin_list.append({ + 'name': plugin_name, + 'class': class_name, + 'path': python_file_name, + 'status': plugin_status + }) return plugin_list, old_plugin_path def query_method(self, file_path): @@ -288,14 +305,18 @@ class MPlugin(OebuildCommand): def_name = "" class_name = "" for file_line in file: - if file_line.startswith('def') or file_line.startswith(' def'): + if file_line.startswith('def') or file_line.startswith( + ' def'): if re.search(r'(?<=def)\s+\w+', file_line): - def_name += re.search(r'(?<=def)\s+\w+', file_line).group() + def_name += re.search(r'(?<=def)\s+\w+', + file_line).group() def_name += "," - if file_line.startswith('class') or file_line.startswith(' class'): + if file_line.startswith('class') or file_line.startswith( + ' class'): if re.search(r'((?<=class)\s+\w+\(OebuildCommand\))', file_line) and \ not class_name: - class_name = re.search(r'(?<=class)\s+\w+', file_line).group().strip() + class_name = re.search(r'(?<=class)\s+\w+', + file_line).group().strip() return def_name, class_name def remove_plugin(self, plugin_name): @@ -312,16 +333,20 @@ class MPlugin(OebuildCommand): if plugin_data['name'] == plugin_name: plugin_dict['plugins'].remove(plugin_data) delete_path = os.path.abspath( - pathlib.Path(os.path.dirname(plugin_data['path']), '..')) - subprocess.check_output(f'rm -rf {delete_path}', shell=True) - oebuild_util.write_yaml(self.oebuild_plugin_yaml_path, plugin_dict) + pathlib.Path(os.path.dirname(plugin_data['path']), + '..')) + subprocess.check_output(f'rm -rf {delete_path}', + shell=True) + oebuild_util.write_yaml(self.oebuild_plugin_yaml_path, + plugin_dict) print('delete success') return logger.error('the plugin %s does not exist', plugin_name) else: logger.error('No plugin has been created yet') - def install_plugin(self, file, plugin_name, plugin_dict, plugin_dict_old, command, dir_path): + def install_plugin(self, file, plugin_name, plugin_dict, plugin_dict_old, + command, dir_path): """ install plugin Args: @@ -347,50 +372,60 @@ class MPlugin(OebuildCommand): file_name = pathlib.Path(file_split_info[-2], file_split_info[-1]) else: file_name = pathlib.Path('plugin_info', file_split_info[-1]) - file_path = pathlib.Path(self.oebuild_plugin_repository, plugin_name, file_name) + file_path = pathlib.Path(self.oebuild_plugin_repository, plugin_name, + file_name) - old_plugin_path = self.create_or_update_plugin_yaml(plugin_name, class_name, - str(file_path), plugin_dict) + old_plugin_path = self.create_or_update_plugin_yaml( + plugin_name, class_name, str(file_path), plugin_dict) if old_plugin_path != '': subprocess.check_output( - f'mv {old_plugin_path} ~/.local/{old_plugin_path.split("/")[-1]}', shell=True) + f'mv {old_plugin_path} ~/.local/{old_plugin_path.split("/")[-1]}', + shell=True) - file_dir_path = pathlib.Path(self.oebuild_plugin_repository, plugin_name).absolute() + file_dir_path = pathlib.Path(self.oebuild_plugin_repository, + plugin_name).absolute() if not os.path.exists(pathlib.Path(file_dir_path, 'plugin_info')): os.makedirs(pathlib.Path(file_dir_path, 'plugin_info')) if dir_path is None: subprocess.check_output(f'cp {file} {file_path}', shell=True) else: - subprocess.check_output(f'cp -r {dir_path} {file_dir_path}', shell=True) + subprocess.check_output(f'cp -r {dir_path} {file_dir_path}', + shell=True) - command_info = subprocess.run( - ['oebuild', f'{plugin_name}', '-h'], - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - text=True, - check=False) + command_info = subprocess.run(['oebuild', f'{plugin_name}', '-h'], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + text=True, + check=False) if command_info.returncode != 0: - logger.error("\nError Message!!!!!!!!!!!!! \n\n %s ", command_info.stderr) - logger.error('Installation failed. There is an issue with your code. ' - 'Please check and fix it before reinstalling.') - - oebuild_util.write_yaml(self.oebuild_plugin_yaml_path, plugin_dict_old) + logger.error("\nError Message!!!!!!!!!!!!! \n\n %s ", + command_info.stderr) + logger.error( + 'Installation failed. There is an issue with your code. ' + 'Please check and fix it before reinstalling.') + + oebuild_util.write_yaml(self.oebuild_plugin_yaml_path, + plugin_dict_old) subprocess.check_output(f'rm -rf {file_dir_path}', shell=True) if old_plugin_path != '': os.makedirs(file_dir_path) subprocess.check_output( - f'cp -r ~/.local/{old_plugin_path.split("/")[-1]} {file_dir_path}', shell=True) + f'cp -r ~/.local/{old_plugin_path.split("/")[-1]} {file_dir_path}', + shell=True) subprocess.check_output( - f'rm -rf ~/.local/{old_plugin_path.split("/")[-1]}', shell=True) + f'rm -rf ~/.local/{old_plugin_path.split("/")[-1]}', + shell=True) return if old_plugin_path != '': - subprocess.check_output(f'rm -rf ~/.local/{old_plugin_path.split("/")[-1]}', shell=True) + subprocess.check_output( + f'rm -rf ~/.local/{old_plugin_path.split("/")[-1]}', + shell=True) print(f'{command.title()} plugin successfully \n' f'{"name".ljust(20)}{"status".ljust(20)}{"path"} \n' diff --git a/src/oebuild/app/plugins/manifest/manifest.py b/src/oebuild/app/plugins/manifest/manifest.py index a797e57..d62e94b 100644 --- a/src/oebuild/app/plugins/manifest/manifest.py +++ b/src/oebuild/app/plugins/manifest/manifest.py @@ -39,16 +39,15 @@ class Manifest(OebuildCommand): source repositories in the build working directory, and can restore relevant source repositories based on the manifest file ''') - + def __init__(self): self.configure = Configure() self.manifest_command = ['download', 'create'] super().__init__('manifest', self.help_msg, self.description) def do_add_parser(self, parser_adder) -> argparse.ArgumentParser: - parser = self._parser( - parser_adder, - usage=''' + parser = self._parser(parser_adder, + usage=''' %(prog)s [create / download] [-f MANIFEST_DIR] @@ -59,8 +58,7 @@ class Manifest(OebuildCommand): dest='manifest_dir', help=''' specify a manifest path to perform the create or restore operation - ''' - ) + ''') return parser @@ -110,19 +108,23 @@ class Manifest(OebuildCommand): } print("\r", end="") progress = int((index + 1) / len(src_list) * 100) - print(f"Expose progress: {progress}%: ", "▋" * (progress // 2), end="") + print(f"Expose progress: {progress}%: ", + "▋" * (progress // 2), + end="") sys.stdout.flush() print() manifest_list = dict(sorted(manifest_list.items(), key=lambda s: s[0])) - oebuild_util.write_yaml( - yaml_dir=pathlib.Path(manifest_dir), - data={'manifest_list': manifest_list}) + oebuild_util.write_yaml(yaml_dir=pathlib.Path(manifest_dir), + data={'manifest_list': manifest_list}) self._add_manifest_banner(manifest_dir=os.path.abspath(manifest_dir)) - print(f"expose successful, the directory is {os.path.abspath(manifest_dir)}") + print( + f"expose successful, the directory is {os.path.abspath(manifest_dir)}" + ) def _add_manifest_banner(self, manifest_dir): - oebuild_conf_dir = os.path.join(oebuild_util.get_base_oebuild(), 'app/conf') + oebuild_conf_dir = os.path.join(oebuild_util.get_base_oebuild(), + 'app/conf') manifest_banner_dir = os.path.join(oebuild_conf_dir, 'manifest_banner') with open(manifest_banner_dir, 'r', encoding='utf-8') as r_f: @@ -155,10 +157,16 @@ class Manifest(OebuildCommand): all package download successful!!!""") def _download_repo(self, src_dir, key, value): - logger.info("====================download %s=====================", key) - repo_git = OGit(os.path.join(src_dir, key), remote_url=value['remote_url'], branch=None) + logger.info("====================download %s=====================", + key) + repo_git = OGit(os.path.join(src_dir, key), + remote_url=value['remote_url'], + branch=None) if repo_git.check_out_version(version=value['version']): - logger.info("====================download %s successful=====================", key) + logger.info( + "====================download %s successful=====================", + key) return True - logger.warning("====================download %s failed=====================", key) + logger.warning( + "====================download %s failed=====================", key) return False diff --git a/src/oebuild/app/plugins/run_qemu/run_qemu.py b/src/oebuild/app/plugins/run_qemu/run_qemu.py index bf72286..4a6d628 100644 --- a/src/oebuild/app/plugins/run_qemu/run_qemu.py +++ b/src/oebuild/app/plugins/run_qemu/run_qemu.py @@ -49,23 +49,24 @@ class RunQemu(OebuildCommand): if self.client is not None: try: container = self.client.get_container(self.container_id) - self.client.delete_container(container=container, is_force=True) + self.client.delete_container(container=container, + is_force=True) except DockerException: print(f""" -the container {self.container_id} failed to be destroyed, please run +the container {self.container_id} failed to be destroyed, please run `docker rm {self.container_id}` """) def do_add_parser(self, parser_adder): - parser = self._parser( - parser_adder, - usage=''' + parser = self._parser(parser_adder, usage=''' %(prog)s [command] ''') parser_adder.add_argument( - 'command', nargs='?', default=None, + 'command', + nargs='?', + default=None, help='''The name of the directory that will be initialized''') return parser @@ -84,16 +85,17 @@ the container {self.container_id} failed to be destroyed, please run for index, param in enumerate(unknown): if param.startswith("qemuparams"): - unknown[index] = "qemuparams=\""+param.split("=")[1]+"\"" + unknown[index] = "qemuparams=\"" + param.split("=")[1] + "\"" if param.startswith("bootparams"): - unknown[index] = "bootparams=\""+param.split("=")[1]+"\"" + unknown[index] = "bootparams=\"" + param.split("=")[1] + "\"" self.exec_qemu(' '.join(unknown)) def exec_qemu(self, params): ''' exec qemu ''' - container: Container = self.client.get_container(self.container_id) # type: ignore + container: Container = self.client.get_container( + self.container_id) # type: ignore self.bak_bash(container=container) self.init_bash(container=container) content = self._get_bashrc_content(container=container) @@ -103,8 +105,7 @@ the container {self.container_id} failed to be destroyed, please run ) qemu_helper_dir = os.path.join( oebuild_const.CONTAINER_BUILD, - "/tmp/work/x86_64-linux/qemu-helper-native" - ) + "/tmp/work/x86_64-linux/qemu-helper-native") staging_bindir_native = f""" if [ ! -d {qemu_helper_usr} ];then mkdir -p {qemu_helper_usr} @@ -112,12 +113,13 @@ if [ ! -d {qemu_helper_usr} ];then ln -s /opt/buildtools/nativesdk/sysroots/x86_64-pokysdk-linux/usr/bin {qemu_helper_usr} fi """ + content = oebuild_util.add_bashrc(content=content, + line=staging_bindir_native) content = oebuild_util.add_bashrc( - content=content, line=staging_bindir_native) - content = oebuild_util.add_bashrc( - content=content, line=f"mv -f /root/{self.old_bashrc} /root/.bashrc") - content = oebuild_util.add_bashrc( - content=content, line=f"""runqemu {params} && exit""") + content=content, + line=f"mv -f /root/{self.old_bashrc} /root/.bashrc") + content = oebuild_util.add_bashrc(content=content, + line=f"""runqemu {params} && exit""") self.update_bashrc(container=container, content=content) os.system(f"docker exec -it -u root {container.short_id} bash") @@ -130,9 +132,10 @@ fi ''' old_content = self._get_bashrc_content(container=container) self.update_bashrc(container=container, - content=oebuild_util.restore_bashrc_content(old_content=old_content)) + content=oebuild_util.restore_bashrc_content( + old_content=old_content)) - def _check_qemu_ifup(self,): + def _check_qemu_ifup(self, ): if not os.path.exists("/etc/qemu-ifup"): print("""please create a virtual network interface as follows: 1, open /etc/qemu-ifup in vim or vi @@ -159,7 +162,8 @@ now, you can continue run `oebuild runqemu` in compile directory volumns.append("/dev/net/tun:/dev/net/tun") volumns.append("/etc/qemu-ifup:/etc/qemu-ifup") volumns.append(self.work_dir + ':' + oebuild_const.CONTAINER_BUILD) - volumns.append(self.configure.source_dir() + ':' + oebuild_const.CONTAINER_SRC) + volumns.append(self.configure.source_dir() + ':' + + oebuild_const.CONTAINER_SRC) parameters = oebuild_const.DEFAULT_CONTAINER_PARAMS + " --privileged" container: Container = self.client.create_container( @@ -201,10 +205,12 @@ now, you can continue run `oebuild runqemu` in compile directory # read container default user .bashrc content content = self._get_bashrc_content(container=container) # get nativesdk environment path automatic for next step - sdk_env_path = oebuild_util.get_nativesdk_environment(container=container) + sdk_env_path = oebuild_util.get_nativesdk_environment( + container=container) init_sdk_command = f'. {oebuild_const.NATIVESDK_DIR}/{sdk_env_path}' init_oe_command = f'. {oebuild_const.CONTAINER_SRC}/yocto-poky/oe-init-build-env \ {oebuild_const.CONTAINER_BUILD}' + init_command = [init_sdk_command, init_oe_command] new_content = oebuild_util.init_bashrc_content(content, init_command) self.update_bashrc(container=container, content=new_content) @@ -226,14 +232,11 @@ now, you can continue run `oebuild runqemu` in compile directory remove it ''' tmp_file = self._set_tmpfile_content(content) - self.client.copy_to_container( - container=container, - source_path=tmp_file, - to_path='/root') - container.exec_run( - cmd=f"mv /root/{tmp_file} /root/.bashrc", - user="root" - ) + self.client.copy_to_container(container=container, + source_path=tmp_file, + to_path='/root') + container.exec_run(cmd=f"mv /root/{tmp_file} /root/.bashrc", + user="root") os.remove(tmp_file) def _set_tmpfile_content(self, content: str): diff --git a/src/oebuild/app/plugins/update/update.py b/src/oebuild/app/plugins/update/update.py index 633bcaa..4d1210e 100644 --- a/src/oebuild/app/plugins/update/update.py +++ b/src/oebuild/app/plugins/update/update.py @@ -38,21 +38,21 @@ class Update(OebuildCommand): help_msg = 'Update the basic environment required for the build' description = textwrap.dedent(''' - The update command will involve three areas, namely the build container, - yocto-meta-openeuler and the corresponding layers, if there are no parameters - after the update, it will be updated in order yocto-meta-openeuler, build - container and layers, the update of these three places is related, the update - of the build container will be affected by three factors, first, execute the - tag parameter, The container image related to the tag is updated, the second - identifies the build container image bound to the main build repository, in - yocto-meta-openeuler/.oebuild/env.yaml, the third identifies the branch - information of the main build repository, and identifies the type of build image - through the branch information of the main build repository. The layer update must - rely on yocto-meta-openeuler, if the main build repository does not exist will first - download the main build repository (the relevant git information is in .oebuild/config), - the layers update execution logic is different in different directories, if not in - the build directory will be parsed yocto-meta-openeuler/.oebuild/ common.yaml to get - the information that needs to be updated, and if it is in the build directory, it will + The update command will involve three areas, namely the build container, + yocto-meta-openeuler and the corresponding layers, if there are no parameters + after the update, it will be updated in order yocto-meta-openeuler, build + container and layers, the update of these three places is related, the update + of the build container will be affected by three factors, first, execute the + tag parameter, The container image related to the tag is updated, the second + identifies the build container image bound to the main build repository, in + yocto-meta-openeuler/.oebuild/env.yaml, the third identifies the branch + information of the main build repository, and identifies the type of build image + through the branch information of the main build repository. The layer update must + rely on yocto-meta-openeuler, if the main build repository does not exist will first + download the main build repository (the relevant git information is in .oebuild/config), + the layers update execution logic is different in different directories, if not in + the build directory will be parsed yocto-meta-openeuler/.oebuild/ common.yaml to get + the information that needs to be updated, and if it is in the build directory, it will parse compile.yaml to get the updated information ''') @@ -62,19 +62,21 @@ class Update(OebuildCommand): super().__init__('update', self.help_msg, self.description) def do_add_parser(self, parser_adder): - parser = self._parser( - parser_adder, - usage=''' + parser = self._parser(parser_adder, + usage=''' %(prog)s [yocto docker layer] [-tag] ''') - parser.add_argument('-tag', '--tag', dest='docker_tag', + parser.add_argument('-tag', + '--tag', + dest='docker_tag', help=''' with platform will list support archs, with feature will list support features - ''' - ) + ''') parser.add_argument( - 'item', nargs='?', default=None, + 'item', + nargs='?', + default=None, help='''The name of the directory that will be initialized''') return parser @@ -126,12 +128,13 @@ class Update(OebuildCommand): if update_layer: self.get_layer_repo() - def get_layer_repo(self,): + def get_layer_repo(self, ): ''' download or update layers that will be needed ''' # check the main layer if exists - yocto_dir = os.path.join(self.configure.source_dir(), "yocto-meta-openeuler") + yocto_dir = os.path.join(self.configure.source_dir(), + "yocto-meta-openeuler") if not os.path.exists(yocto_dir): # update main layer self.get_basic_repo() @@ -139,28 +142,32 @@ class Update(OebuildCommand): # or /compile.yaml where in build directory repos = None if os.path.exists(os.path.join(os.getcwd(), "compile.yaml")): - parse_compile = ParseCompile(compile_conf_dir=os.path.join(os.getcwd(), "compile.yaml")) + parse_compile = ParseCompile( + compile_conf_dir=os.path.join(os.getcwd(), "compile.yaml")) repos = parse_compile.repos else: - common_path = pathlib.Path(os.path.join(yocto_dir, ".oebuild/common.yaml")) + common_path = pathlib.Path( + os.path.join(yocto_dir, ".oebuild/common.yaml")) repos = oebuild_util.read_yaml(yaml_dir=common_path)['repos'] if repos is None: return for _, item in repos.items(): if isinstance(item, OebuildRepo): - local_dir = os.path.join(self.configure.source_dir(), item.path) + local_dir = os.path.join(self.configure.source_dir(), + item.path) key_repo = OGit(repo_dir=local_dir, remote_url=item.url, branch=item.refspec) else: - local_dir = os.path.join(self.configure.source_dir(), item['path']) + local_dir = os.path.join(self.configure.source_dir(), + item['path']) key_repo = OGit(repo_dir=local_dir, remote_url=item['url'], branch=item['refspec']) key_repo.clone_or_pull_repo() - def get_basic_repo(self,): + def get_basic_repo(self, ): ''' note: get_basic_repo is to download or update basic repo in config which set in keys basic_repo, the rule is that when the @@ -171,9 +178,11 @@ class Update(OebuildCommand): embedded/src/yocto-meta-openeuler not exists, so just clone from config setting. ''' oebuild_config = self.configure.parse_oebuild_config() - yocto_config: ConfigBasicRepo = oebuild_config.basic_repo[oebuild_const.YOCTO_META_OPENEULER] + yocto_config: ConfigBasicRepo = \ + oebuild_config.basic_repo[oebuild_const.YOCTO_META_OPENEULER] - local_dir = os.path.join(self.configure.source_dir(), yocto_config.path) + local_dir = os.path.join(self.configure.source_dir(), + yocto_config.path) yocto_repo = OGit(repo_dir=local_dir, remote_url=yocto_config.remote_url, branch=yocto_config.branch) @@ -189,19 +198,25 @@ class Update(OebuildCommand): ''' oebuild_config = self.configure.parse_oebuild_config() docker_config = oebuild_config.docker - check_docker_tag = CheckDockerTag(docker_tag=docker_tag, configure=self.configure) + check_docker_tag = CheckDockerTag(docker_tag=docker_tag, + configure=self.configure) if docker_tag is not None: - if check_docker_tag.get_tag() is None or check_docker_tag.get_tag() == "": + if check_docker_tag.get_tag() is None or check_docker_tag.get_tag( + ) == "": check_docker_tag.list_image_tag() return - docker_image = docker_config.repo_url + ":" + check_docker_tag.get_tag() + docker_image = docker_config.repo_url + ":" + check_docker_tag.get_tag( + ) else: - docker_image = YoctoEnv().get_docker_image(self.configure.source_yocto_dir()) + docker_image = YoctoEnv().get_docker_image( + self.configure.source_yocto_dir()) if docker_image is None or docker_image == "": - if check_docker_tag.get_tag() is None or check_docker_tag.get_tag() == "": + if check_docker_tag.get_tag( + ) is None or check_docker_tag.get_tag() == "": check_docker_tag.list_image_tag() return - docker_image = docker_config.repo_url + ":" + check_docker_tag.get_tag() + docker_image = docker_config.repo_url + ":" + check_docker_tag.get_tag( + ) client = DockerProxy() logger.info("Pull %s ...", docker_image) diff --git a/src/oebuild/spec.py b/src/oebuild/spec.py index 6b129cc..cc7d882 100644 --- a/src/oebuild/spec.py +++ b/src/oebuild/spec.py @@ -36,7 +36,7 @@ class ExtensionCommandError(CommandError): def __init__(self, **kwargs): self.hint = kwargs.pop('hint', None) - super(ExtensionCommandError, self).__init__(**kwargs) + super().__init__(**kwargs) @dataclass @@ -104,15 +104,18 @@ def get_spec(pre_dir, command_ext: _ExtCommand): xxx ''' - py_file = os.path.join(os.path.dirname(__file__), pre_dir, command_ext.path) if ( - '/.local/oebuild_plugins/' not in command_ext.path) else command_ext.path - factory = _CmdFactory(py_file=py_file, name=command_ext.name, attr=command_ext.class_name) - - return OebuildExtCommandSpec( - name=command_ext.name, - description=factory().description, - help=factory().help_msg, - factory=factory) + py_file = os.path.join( + os.path.dirname(__file__), pre_dir, + command_ext.path) if ('/.local/oebuild_plugins/' + not in command_ext.path) else command_ext.path + factory = _CmdFactory(py_file=py_file, + name=command_ext.name, + attr=command_ext.class_name) + + return OebuildExtCommandSpec(name=command_ext.name, + description=factory().description, + help=factory().help_msg, + factory=factory) def _commands_module_from_file(file, mod_name): -- Gitee From 5bf11473d64848464c29c492eb5e9eaba8c18203 Mon Sep 17 00:00:00 2001 From: xuansen fang Date: Thu, 14 Mar 2024 15:54:40 +0800 Subject: [PATCH 3/4] Modify: format error *modify flake8 and pylint format error Signed-off-by: fangxuansen --- src/oebuild/app/plugins/deploy/deploy_target.py | 4 ++-- src/oebuild/app/plugins/run_qemu/run_qemu.py | 12 +++--------- src/oebuild/app/plugins/update/update.py | 8 ++++---- 3 files changed, 9 insertions(+), 15 deletions(-) diff --git a/src/oebuild/app/plugins/deploy/deploy_target.py b/src/oebuild/app/plugins/deploy/deploy_target.py index 0dffa21..03227b3 100644 --- a/src/oebuild/app/plugins/deploy/deploy_target.py +++ b/src/oebuild/app/plugins/deploy/deploy_target.py @@ -22,7 +22,7 @@ logger = logging.getLogger() class DeployTarget(OebuildCommand): ''' - we use package in a + we use package in a ''' help_msg = 'deploy software on line' @@ -91,7 +91,7 @@ options: class UnDeployTarget(OebuildCommand): ''' - we use package in a + we use package in a ''' help_msg = 'undeploy software on line' diff --git a/src/oebuild/app/plugins/run_qemu/run_qemu.py b/src/oebuild/app/plugins/run_qemu/run_qemu.py index 4a6d628..c4b56fe 100644 --- a/src/oebuild/app/plugins/run_qemu/run_qemu.py +++ b/src/oebuild/app/plugins/run_qemu/run_qemu.py @@ -147,7 +147,6 @@ fi now, you can continue run `oebuild runqemu` in compile directory """) sys.exit(0) - return def deal_env_container(self, docker_image): ''' @@ -191,9 +190,7 @@ now, you can continue run `oebuild runqemu` in compile directory self.client.container_exec_command( container=container, command=f"cp /root/.bashrc /root/{old_bash}", - user="root", - work_space=None, - stream=False) + user="root") self.old_bashrc = old_bash def init_bash(self, container: Container): @@ -217,11 +214,8 @@ now, you can continue run `oebuild runqemu` in compile directory def _get_bashrc_content(self, container: Container): content = self.client.container_exec_command( - container=container, - command="cat /root/.bashrc", - user="root", - work_space=None, - stream=False).output + container=container, command="cat /root/.bashrc", + user="root").output return content.decode() diff --git a/src/oebuild/app/plugins/update/update.py b/src/oebuild/app/plugins/update/update.py index 4d1210e..cf11c26 100644 --- a/src/oebuild/app/plugins/update/update.py +++ b/src/oebuild/app/plugins/update/update.py @@ -190,10 +190,10 @@ class Update(OebuildCommand): def docker_image_update(self, docker_tag=None): ''' - The container update logic is to update the corresponding tag - container image if tag is specified, otherwise it is determined - according to the yocto-meta-openeuler version branch in config, - and if the version branch does not correspond to it, it will enter + The container update logic is to update the corresponding tag + container image if tag is specified, otherwise it is determined + according to the yocto-meta-openeuler version branch in config, + and if the version branch does not correspond to it, it will enter interactive mode, which is selected by the user ''' oebuild_config = self.configure.parse_oebuild_config() -- Gitee From d1636413a326c19a6a55f48f64594773540cd896 Mon Sep 17 00:00:00 2001 From: xuansen fang Date: Thu, 21 Mar 2024 14:56:45 +0800 Subject: [PATCH 4/4] Modify : format error *modify flake8 and pylint format error Signed-off-by: fangxuansen --- src/oebuild/app/plugins/m_env/m_env.py | 112 ++++---- src/oebuild/app/plugins/m_plugin/m_plugin.py | 262 +++++++++++-------- 2 files changed, 213 insertions(+), 161 deletions(-) diff --git a/src/oebuild/app/plugins/m_env/m_env.py b/src/oebuild/app/plugins/m_env/m_env.py index 70c16ab..a0c3feb 100644 --- a/src/oebuild/app/plugins/m_env/m_env.py +++ b/src/oebuild/app/plugins/m_env/m_env.py @@ -39,10 +39,10 @@ class Menv(OebuildCommand): help_msg = 'Development Environment Management' description = textwrap.dedent(''' - This is an environment management function that allows you to configure - the environment through SDK files or unzipped setup files, and you can - view, delete, and activate the corresponding environment. - These operations will not have any impact on your current machine + This is an environment management function that allows you to configure the + environment through SDK files or unzipped setup files, and you can view, delete, + and activate the corresponding environment. These operations will not have + any impact on your current machine ''') def __init__(self): @@ -99,64 +99,74 @@ class Menv(OebuildCommand): unknown = unknown[1:] if self.pre_parse_help(args, unknown): - return + sys.exit(1) args = args.parse_args(unknown) if command == 'create': if not args.env_name: print(''' - Please enter the correct command: - oebuild menv create [-d -f] Create an environment -n env_name +Please enter the correct command: oebuild menv create [-d -f] Create an environment -n env_name ''') - return - # Check if the file path exists - if args.directory and os.path.isdir(args.directory): - setup_file_path = os.path.abspath(args.directory) - sdk_name = args.env_name if args.env_name else args.directory.split( - '/')[-1] - self.create_or_update_env_yaml(sdk_name, args.directory) - print( - f' Created Environment successfully \n {sdk_name.ljust(30)}{setup_file_path}' - ) - return - - # Creating an environment - if args.file and os.path.exists(args.file): - sdk_name = args.env_name if args.env_name else ( - args.file.split('/')[-1].replace('.sh', '') - if args.file else None) - setup_file_path = self.oebuild_env_path + sdk_name - self.create_or_update_env_yaml(sdk_name, setup_file_path) - self.execute_sdk_file(args.file, setup_file_path) - print( - f' Created Environment successfully \n {sdk_name.ljust(30)}{setup_file_path}' - ) - return - - print('The path is invalid, please check the path ') + sys.exit(-1) + self.create_environment(args=args) elif command == 'activate': # Activate Environment if args.env_name: - return self.activate_environment(args.env_name) + self.activate_environment(args.env_name) + sys.exit(0) print( 'Please enter the correct command: oebuild menv activate -n env_name' ) + sys.exit(-1) elif command == 'list': env_dict = oebuild_util.read_yaml(self.oebuild_env_yaml_path) if env_dict and 'env_config' in env_dict: - self.list_environment(None, env_dict) + self.list_environment(env_dict) + sys.exit(0) else: print('No environment has been created yet') - return + sys.exit(-1) # delete environment elif command == 'remove': if args.env_name: - return self.delete_environment(args.env_name) + self.delete_environment(args.env_name) + sys.exit(0) print( 'Please enter the correct command: oebuild menv remove -n env_name' ) + sys.exit(-1) + + def create_environment(self, args): + ''' + create environment file in ~/.local/oebuild_env/ and do something in next step + ''' + # Check if the file path exists + if args.directory and os.path.isdir(args.directory): + setup_file_path = os.path.abspath(args.directory) + sdk_name = args.env_name if args.env_name else args.directory.split( + '/')[-1] + self.create_or_update_env_yaml(sdk_name, args.directory) + print( + f' Created Environment successfully \n {sdk_name.ljust(30)}{setup_file_path}' + ) + sys.exit(0) + + # Creating an environment + if args.file and os.path.exists(args.file): + sdk_name = args.env_name if args.env_name else (args.file.split( + '/')[-1].replace('.sh', '') if args.file else None) + setup_file_path = self.oebuild_env_path + sdk_name + self.create_or_update_env_yaml(sdk_name, setup_file_path) + self.execute_sdk_file(args.file, setup_file_path) + print( + f' Created Environment successfully \n {sdk_name.ljust(30)}{setup_file_path}' + ) + sys.exit(0) + + print('The path is invalid, please check the path ') + sys.exit(-1) def execute_setup_directory(self, setup_file_path, env_name): """ @@ -204,11 +214,13 @@ class Menv(OebuildCommand): r"sed -i '$a\mv ~/.bashrc_back ~/.bashrc -f' ~/.bashrc", shell=True) # Add prompt words - separator = "====================================================" - prompt_one = "Your environment is ready" - prompt_two = "Please proceed with the subsequent operations here" wrap = '\\n###!###\\n' - prompt_words = separator + wrap + prompt_one + wrap + prompt_two + wrap + separator + prompt_words = f''' +===================================================={wrap} +Your environment is ready{wrap} +Please proceed with the subsequent operations here{wrap} +===================================================={wrap} +''' subprocess.check_output( rf'''sed -i '$a\echo "{prompt_words}"' ~/.bashrc''', shell=True) @@ -298,11 +310,11 @@ class Menv(OebuildCommand): for env_data in env_list: if 'env_name' not in env_data: print('env_name not exits') - return + sys.exit(-1) if env_data['env_name'] == env_name: while True: user_input = input(""" - Do you want to overwrite the path of the original environment configuration(Y/N) +Do you want to overwrite the path of the original environment configuration(Y/N) """) if user_input.lower() == 'y': env_data['env_value'] = env_value @@ -321,14 +333,13 @@ class Menv(OebuildCommand): ''' env_dict = oebuild_util.read_yaml(self.oebuild_env_yaml_path) if env_dict and 'env_config' in env_dict: - setup_file_path = self.list_environment(env_name, env_dict) + setup_file_path = self._get_environment(env_name, env_dict) if setup_file_path: self.execute_setup_directory(setup_file_path, env_name) - return print('The environment does not exist') - return + sys.exit(-1) - def list_environment(self, env_name, env_dict): + def list_environment(self, env_dict): """ View the environment, if env_ If name exists, it is necessary to find the corresponding environment's setup file @@ -339,10 +350,14 @@ class Menv(OebuildCommand): """ print("""# oebuild environment:\n#""") + for env_data in env_dict['env_config']: + print(env_data['env_name'].ljust(30) + env_data['env_value']) + + def _get_environment(self, env_name, env_dict): for env_data in env_dict['env_config']: if env_name and env_data['env_name'] == env_name: return env_data['env_value'] - print(env_data['env_name'].ljust(30) + env_data['env_value']) + return None def delete_environment(self, env_name): """ @@ -376,4 +391,3 @@ class Menv(OebuildCommand): env_dict['env_config'] = env_list oebuild_util.write_yaml(self.oebuild_env_yaml_path, env_dict) print('Successfully deleted') - return diff --git a/src/oebuild/app/plugins/m_plugin/m_plugin.py b/src/oebuild/app/plugins/m_plugin/m_plugin.py index 2f6730f..dd9963f 100644 --- a/src/oebuild/app/plugins/m_plugin/m_plugin.py +++ b/src/oebuild/app/plugins/m_plugin/m_plugin.py @@ -17,6 +17,7 @@ import pathlib import re import subprocess import textwrap +import sys from oebuild.command import OebuildCommand import oebuild.util as oebuild_util @@ -75,7 +76,7 @@ class MPlugin(OebuildCommand): install: -d plugin_dir_path -m major_file -n plugin_name list: enable/disable: enable/disable -n plugin_name - remove: -n plugin_name + remove: -n plugin_name ''') parser.add_argument('-f', '--file', @@ -132,7 +133,7 @@ class MPlugin(OebuildCommand): unknown = unknown[1:] if self.pre_parse_help(args, unknown): - return + sys.exit(0) args = args.parse_args(unknown) if not os.path.exists(self.oebuild_plugin_yaml_path.absolute()): @@ -145,83 +146,108 @@ class MPlugin(OebuildCommand): plugin_dict_old = copy.deepcopy(plugin_dict) if command == 'install': - if not args.plugin_name: - logger.error( - 'Please enter the correct command: Missing -n parameter ') - return - - if args.plugin_name == 'mplugin': - logger.error(' This command does not allow overwrite ') - return - - if plugin_dict is not None: - append_command_ext = OebuildApp().get_command_ext( - plugin_dict['plugins']) - else: - append_command_ext = {} - - if args.plugin_name in self.command_ext.keys() \ - or args.plugin_name in append_command_ext.keys(): - while True: - user_input = input(f'Do you want to overwrite \ - the existing plugin ({args.plugin_name}) in oebuild(Y/N)' - ) - if user_input.lower() == 'y': - break - if user_input.lower() == 'n': - return + self._install_param_check(args=args, plugin_dict=plugin_dict) if args.file and os.path.exists(args.file): - self.install_plugin(args.file, args.plugin_name, plugin_dict, - plugin_dict_old, command, None) - + self.install_plugin( + { + 'file': args.file, + 'plugin_name': args.plugin_name, + 'command': command + }, plugin_dict, plugin_dict_old) + sys.exit(0) elif args.dir_path and os.path.exists(args.dir_path): - if not args.major: - logger.error(" Please specify the major file ") - return file = str( pathlib.Path(args.dir_path, args.major.split('/')[-1])) - if not os.path.exists(file): - logger.error( - "the %s not exist, please check the plugin file path", - file) - return - self.install_plugin(file, args.plugin_name, plugin_dict, - plugin_dict_old, command, args.dir_path) - - else: - logger.error( - "the %s not exist, please check the plugin file path", - args.file) - return - if command == 'list': - if plugin_dict and 'plugins' in plugin_dict: - print("""# oebuild plugin:\n#""") - print(f'{"name".ljust(20)}{"status".ljust(20)}{"path"}') - for plugin_data in plugin_dict['plugins']: - print( - f'{plugin_data["name"].ljust(20)}{str(plugin_data["status"]).ljust(20)}' - f'{plugin_data["path"]}') - else: - logger.error('No plugin has been created yet') - return - - if command in ['enable', 'disable']: - if plugin_dict and 'plugins' in plugin_dict: - for plugin_data in plugin_dict['plugins']: - if plugin_data['name'] == args.plugin_name: - plugin_data['status'] = command - oebuild_util.write_yaml(self.oebuild_plugin_yaml_path, - plugin_dict) - print('change success') - return - logger.error('the plugin %s does not exist', args.plugin_name) - else: - logger.error('No plugin has been created yet') - - if command == 'remove': + self._install_for_dir_check(args=args, file=file) + self.install_plugin( + { + 'file': file, + 'plugin_name': args.plugin_name, + 'command': command, + 'dir_path': args.dir_path + }, plugin_dict, plugin_dict_old) + sys.exit(0) + logger.error("the %s not exist, please check the plugin file path", + args.file) + sys.exit(-1) + elif command == 'list': + self.list_plugin(plugin_dict=plugin_dict) + elif command in ['enable', 'disable']: + self.enable_disable_plugin(command=command, + plugin_dict=plugin_dict, + args=args) + elif command == 'remove': self.remove_plugin(args.plugin_name) + def list_plugin(self, plugin_dict): + ''' + list plugin infomation with format like 'plugin_name status plugin_path' + ''' + if plugin_dict and 'plugins' in plugin_dict: + print("""# oebuild plugin:\n#""") + print(f'{"name".ljust(20)}{"status".ljust(20)}{"path"}') + for plugin_data in plugin_dict['plugins']: + print(f'{str(plugin_data["name"]).ljust(20)}' + f'{str(plugin_data["status"]).ljust(20)}' + f'{str(plugin_data["path"])}') + else: + logger.error('No plugin has been created yet') + sys.exit(-1) + + def enable_disable_plugin(self, command, plugin_dict, args): + ''' + enable plugin or disable plugin + ''' + if plugin_dict and 'plugins' in plugin_dict: + for plugin_data in plugin_dict['plugins']: + if plugin_data['name'] == args.plugin_name: + plugin_data['status'] = command + oebuild_util.write_yaml(self.oebuild_plugin_yaml_path, + plugin_dict) + print('change success') + logger.error('the plugin %s does not exist', args.plugin_name) + sys.exit(-1) + else: + logger.error('No plugin has been created yet') + sys.exit(-1) + + def _install_for_dir_check(self, args, file): + if not args.major: + logger.error(" Please specify the major file ") + sys.exit(-1) + if not os.path.exists(file): + logger.error("the %s not exist, please check the plugin file path", + file) + sys.exit(-1) + + def _install_param_check(self, args, plugin_dict): + if not args.plugin_name: + logger.error( + 'Please enter the correct command: Missing -n parameter ') + sys.exit(-1) + + if args.plugin_name == 'mplugin': + logger.error(' This command does not allow overwrite ') + sys.exit(-1) + + if plugin_dict is not None: + append_command_ext = OebuildApp().get_command_ext( + plugin_dict['plugins']) + else: + append_command_ext = {} + + if args.plugin_name in self.command_ext.keys() \ + or args.plugin_name in append_command_ext.keys(): + while True: + user_input = input( + 'Do you want to overwrite the existing ' + f'plugin ({args.plugin_name}) in oebuild(Y/N)') + if user_input.lower() == 'y': + break + if user_input.lower() == 'n': + sys.exit(0) + def create_or_update_plugin_yaml(self, plugin_name, class_name, python_file_name, plugin_dict): """ @@ -239,8 +265,12 @@ class MPlugin(OebuildCommand): if plugin_dict and 'plugins' in plugin_dict: plugin_list, old_plugin_path = self.input_or_update_dict( - plugin_name, class_name, python_file_name, 'enable', - plugin_dict['plugins']) + { + 'plugin_name': plugin_name, + 'class_name': class_name, + 'python_file_name': python_file_name, + 'plugin_status': "enable" + }, plugin_dict['plugins']) plugin_dict['plugins'] = plugin_list oebuild_util.write_yaml(self.oebuild_plugin_yaml_path, plugin_dict) return old_plugin_path @@ -256,8 +286,7 @@ class MPlugin(OebuildCommand): oebuild_util.write_yaml(self.oebuild_plugin_yaml_path, plugin_dict) return old_plugin_path - def input_or_update_dict(self, plugin_name, class_name, python_file_name, - plugin_status, plugin_list): + def input_or_update_dict(self, plugin_obj, plugin_list): """ Modify or insert environmental data Args: @@ -276,19 +305,19 @@ class MPlugin(OebuildCommand): if 'name' not in plugin_data: logger.error('plugin_name not exits') return plugin_list, old_plugin_path - if plugin_data['name'] == plugin_name: - plugin_data['class'] = class_name + if plugin_data['name'] == plugin_obj['plugin_name']: + plugin_data['class'] = plugin_obj['class_name'] old_plugin_path = os.path.abspath( os.path.dirname(plugin_data['path'])) - plugin_data['path'] = python_file_name - plugin_data['status'] = plugin_status + plugin_data['path'] = plugin_obj['python_file_name'] + plugin_data['status'] = plugin_obj['plugin_status'] insert_flag = False if insert_flag: plugin_list.append({ - 'name': plugin_name, - 'class': class_name, - 'path': python_file_name, - 'status': plugin_status + 'name': plugin_obj['plugin_name'], + 'class': plugin_obj['class_name'], + 'path': plugin_obj['python_file_name'], + 'status': plugin_obj['plugin_status'] }) return plugin_list, old_plugin_path @@ -345,60 +374,67 @@ class MPlugin(OebuildCommand): else: logger.error('No plugin has been created yet') - def install_plugin(self, file, plugin_name, plugin_dict, plugin_dict_old, - command, dir_path): + def install_plugin(self, install_plugin_object, plugin_dict, + plugin_dict_old): """ - install plugin + install plugin the install_plugin_object container follow item + file: xxx + plugin_name: xxx + command: xxx + dir_path: xxx Args: - file: - plugin_name: + install_plugin_object: plugin_dict: plugin_dict_old: - command: - dir_path: Returns: """ - def_name, class_name = self.query_method(file) + def_name, class_name = self.query_method(install_plugin_object['file']) if not ('do_run' in def_name and 'do_add_parser' in def_name): logger.error('do_run or do_add_parser method does not exist') - return + sys.exit(-1) if not class_name: logger.error('class not extends OebuildCommand') - return - file_split_info = file.split('/') + sys.exit(-1) + file_split_info = install_plugin_object['file'].split('/') if len(file_split_info) > 1: file_name = pathlib.Path(file_split_info[-2], file_split_info[-1]) else: file_name = pathlib.Path('plugin_info', file_split_info[-1]) - file_path = pathlib.Path(self.oebuild_plugin_repository, plugin_name, + file_path = pathlib.Path(self.oebuild_plugin_repository, + install_plugin_object['plugin_name'], file_name) old_plugin_path = self.create_or_update_plugin_yaml( - plugin_name, class_name, str(file_path), plugin_dict) + install_plugin_object['plugin_name'], class_name, str(file_path), + plugin_dict) if old_plugin_path != '': subprocess.check_output( f'mv {old_plugin_path} ~/.local/{old_plugin_path.split("/")[-1]}', shell=True) - file_dir_path = pathlib.Path(self.oebuild_plugin_repository, - plugin_name).absolute() + file_dir_path = pathlib.Path( + self.oebuild_plugin_repository, + install_plugin_object['plugin_name']).absolute() if not os.path.exists(pathlib.Path(file_dir_path, 'plugin_info')): os.makedirs(pathlib.Path(file_dir_path, 'plugin_info')) - if dir_path is None: - subprocess.check_output(f'cp {file} {file_path}', shell=True) + if 'dir_path' not in install_plugin_object: + subprocess.check_output( + f"cp {install_plugin_object['file']} {file_path}", shell=True) else: - subprocess.check_output(f'cp -r {dir_path} {file_dir_path}', - shell=True) + subprocess.check_output( + f"cp -r {install_plugin_object['dir_path']} {file_dir_path}", + shell=True) - command_info = subprocess.run(['oebuild', f'{plugin_name}', '-h'], - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - text=True, - check=False) + command_info = subprocess.run( + ['oebuild', f"{install_plugin_object['plugin_name']}", '-h'], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + text=True, + check=False) if command_info.returncode != 0: logger.error("\nError Message!!!!!!!!!!!!! \n\n %s ", @@ -420,13 +456,15 @@ class MPlugin(OebuildCommand): subprocess.check_output( f'rm -rf ~/.local/{old_plugin_path.split("/")[-1]}', shell=True) - return + sys.exit(-1) if old_plugin_path != '': subprocess.check_output( f'rm -rf ~/.local/{old_plugin_path.split("/")[-1]}', shell=True) - print(f'{command.title()} plugin successfully \n' - f'{"name".ljust(20)}{"status".ljust(20)}{"path"} \n' - f'{plugin_name.ljust(20)}{"enable".ljust(20)}{file_path}') + print( + f"{install_plugin_object['command'].title()} plugin successfully \n" + f'{"name".ljust(20)}{"status".ljust(20)}{"path"} \n' + f"{install_plugin_object['plugin_name'].ljust(20)}{'enable'.ljust(20)}{file_path}" + ) -- Gitee