diff --git a/src/oebuild/app/plugins/bitbake/bitbake.py b/src/oebuild/app/plugins/bitbake/bitbake.py index 96fd493e812ad5459d142e0f2848d5c80681256b..04fee5eadad97e5f95aa365d68db0dcd03f59bdb 100644 --- a/src/oebuild/app/plugins/bitbake/bitbake.py +++ b/src/oebuild/app/plugins/bitbake/bitbake.py @@ -34,31 +34,31 @@ class Bitbake(OebuildCommand): command directly, for example: `oebuild bitbake busybox` ''' - 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(''' + 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', 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 @@ -79,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'): @@ -92,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: @@ -111,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/clear/clear.py b/src/oebuild/app/plugins/clear/clear.py index 62a5a115a4fdb78c169627d2b2476000d89618e7..0474bb256c3543ed8ee747bb5bba0c2312e1bc53 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 5548ec727e01e811c3a0ef5dcdd3bc87c3618c85..cfd80f1ddfbf9b6b9244c2115fed6fac9a642fcf 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() @@ -11,20 +10,18 @@ logger = logging.getLogger() class Demo(OebuildCommand): + help_msg = 'this is your help mesasge' + description = textwrap.dedent('''\ + this is your description message + ''') + def __init__(self): self.configure = Configure() - super().__init__( - name='{}', - help='this is your help mesasge', - description=textwrap.dedent('''\ - this is your description message -''' - )) + 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 91d0f4d188015b034886d2c68cd9352b5f2f071f..03227b3fc08e6f24ff2269db672736b240b18710 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 @@ -23,25 +22,27 @@ logger = logging.getLogger() class DeployTarget(OebuildCommand): ''' - we use package in a + 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( - 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 """) @@ -85,23 +91,23 @@ options: class UnDeployTarget(OebuildCommand): ''' - we use package in a + 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( - 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 @@ -117,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/generate/generate.py b/src/oebuild/app/plugins/generate/generate.py index 4e13ed72bebbb3b7b15b97cd820fe812db6fd555..b1ff72856dc1fa1393865d4c959eb25c52193d5f 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 f0eefa6f3cccd76272ea7e2f5ddab41c3024d18e..6cf5ef0156b343b5a2805a2aea4c64688790f965 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,25 +43,36 @@ 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( 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 @@ -87,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) @@ -103,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())} @@ -183,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 4a242e60969ac5fc80f7c618462f4c1e186a5879..a0c3febd4be090fae1aa64660e67f7c850dc1761 100644 --- a/src/oebuild/app/plugins/m_env/m_env.py +++ b/src/oebuild/app/plugins/m_env/m_env.py @@ -37,111 +37,136 @@ 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_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( - 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] 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) - print('Please enter the correct command: oebuild menv activate -n 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) - print('Please enter the correct command: oebuild menv remove -n 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): """ @@ -168,15 +193,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", @@ -185,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) @@ -218,11 +249,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 +274,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): @@ -267,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 @@ -290,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 @@ -308,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): """ @@ -331,16 +377,17 @@ 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) 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 5c4a24526f86e3b270f5e1baf64c4246f161b429..dd9963fc5a24868ccc61ff76450a6364ad4b06d4 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 @@ -32,24 +33,28 @@ 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'] - 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']) - 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: """ @@ -63,40 +68,43 @@ 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 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', 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,101 +124,132 @@ 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] 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()): - 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 ') - 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 + file = str( + pathlib.Path(args.dir_path, + args.major.split('/')[-1])) + self._install_for_dir_check(args=args, file=file) 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': + { + '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 create_or_update_plugin_yaml(self, plugin_name, class_name, python_file_name, plugin_dict): + 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): """ Args: @@ -226,26 +265,28 @@ 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 - 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_obj, plugin_list): """ Modify or insert environmental data Args: @@ -264,15 +305,20 @@ 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 - old_plugin_path = os.path.abspath(os.path.dirname(plugin_data['path'])) - plugin_data['path'] = python_file_name - plugin_data['status'] = plugin_status + 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'] = 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}) + plugin_list.append({ + '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 def query_method(self, file_path): @@ -288,14 +334,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,86 +362,109 @@ 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, 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_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) + old_plugin_path = self.create_or_update_plugin_yaml( + 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) + 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'], + ['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 ", 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) - return + f'rm -rf ~/.local/{old_plugin_path.split("/")[-1]}', + shell=True) + 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}') + subprocess.check_output( + f'rm -rf ~/.local/{old_plugin_path.split("/")[-1]}', + shell=True) + + 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}" + ) diff --git a/src/oebuild/app/plugins/manifest/manifest.py b/src/oebuild/app/plugins/manifest/manifest.py index 85788536632920388be795c434289bbc6d4e1541..d62e94bb5776b757cb8e8a14b744cb60b1ec9120 100644 --- a/src/oebuild/app/plugins/manifest/manifest.py +++ b/src/oebuild/app/plugins/manifest/manifest.py @@ -33,23 +33,21 @@ 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( - parser_adder, - usage=''' + parser = self._parser(parser_adder, + usage=''' %(prog)s [create / download] [-f MANIFEST_DIR] @@ -60,8 +58,7 @@ class Manifest(OebuildCommand): dest='manifest_dir', help=''' specify a manifest path to perform the create or restore operation - ''' - ) + ''') return parser @@ -111,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: @@ -156,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 5b07801fbde3a8034c254bf1ff379c6881e81b16..c4b56fe9a765846cd1cf4b42c438e559d4457f36 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,35 +43,30 @@ 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: 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 @@ -85,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) @@ -104,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} @@ -113,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") @@ -131,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 @@ -145,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): ''' @@ -160,7 +161,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( @@ -188,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): @@ -202,21 +202,20 @@ 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) 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() @@ -227,14 +226,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 96d83389dc0a5dc336fd138f168b22cf3a186812..cf11c2688e3ac3ba235e7741a53e49ef237a289a 100644 --- a/src/oebuild/app/plugins/update/update.py +++ b/src/oebuild/app/plugins/update/update.py @@ -36,46 +36,47 @@ class Update(OebuildCommand): related to the build, such as container images, build base repositories, etc ''' + 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 + parse compile.yaml to get the updated information + ''') + def __init__(self): self.configure = Configure() - super().__init__( - 'update', - 'Update the basic environment required for the build', - 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 - parse compile.yaml to get the updated information - ''') - ) + 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 @@ -127,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() @@ -140,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 @@ -172,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) @@ -182,27 +190,33 @@ 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() 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 5b19ba7c1aa03acfd266beac9de8dcff303b2f8d..cc7d882280bd6b992f594a8fdc38091976e7318d 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 @@ -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: @@ -111,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): diff --git a/src/oebuild/util.py b/src/oebuild/util.py index 14945fb9cd6483a8c03bceaa464a1c172f882a2a..6081532ba58ad95094eb561371c0d9135e896958 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):