diff --git a/src/oebuild/app/conf/plugins.yaml b/src/oebuild/app/conf/plugins.yaml index 64c03113157b11285ff5cec8c80607cb75852e81..7fbf92d6bd0f38f048307c04c1e6924f2706f8f1 100644 --- a/src/oebuild/app/conf/plugins.yaml +++ b/src/oebuild/app/conf/plugins.yaml @@ -20,3 +20,6 @@ plugins: - name: runqemu class: RunQemu path: plugins/run_qemu/run_qemu.py +- name: fix_env + class: FixEnv + path: plugins/fix_env/fix_env.py diff --git a/src/oebuild/app/plugins/fix_env/fix_env.py b/src/oebuild/app/plugins/fix_env/fix_env.py new file mode 100644 index 0000000000000000000000000000000000000000..98cdb06575d4d067a73893cfb430a74de80c0376 --- /dev/null +++ b/src/oebuild/app/plugins/fix_env/fix_env.py @@ -0,0 +1,168 @@ +''' +Copyright (c) 2023 openEuler Embedded +oebuild is licensed under Mulan PSL v2. +You can use this software according to the terms and conditions of the Mulan PSL v2. +You may obtain a copy of Mulan PSL v2 at: + http://license.coscl.org.cn/MulanPSL2 +THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, +EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, +MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. +See the Mulan PSL v2 for more details. +''' + +import argparse +import datetime +import fcntl +import os +import pty +import re +import struct +import sys +import termios +import threading +import subprocess +import textwrap +import logging +import time + +from oebuild.command import OebuildCommand +from oebuild.util import * +from oebuild.configure import Configure + +logger = logging.getLogger() + + +class FixEnv(OebuildCommand): + + def __init__(self): + self.configure = Configure() + super().__init__( + 'fixenv', + 'preparation of the environment before compilation', + description=textwrap.dedent('''\ + This is an environment preparation method. You can either pass in the folder path after the + execution of the SDK, or the corresponding SDK environment preparation has been carried out + on the SDK path. This environment preparation will not affect your current environment. +''' + )) + + def do_add_parser(self, parser_adder) -> argparse.ArgumentParser: + parser = self._parser( + parser_adder, + usage=''' + %(prog)s [-d directory] [-f file] +''') + + parser.add_argument('-d', '--directory', dest='directory', + help=''' + this param is build directory + ''' + ) + + parser.add_argument('-f', '--file', dest='file', + help=''' + this param is build file + ''' + ) + + return parser + + def do_run(self, args: argparse.Namespace, unknown=None): + # perpare parse help command + if self.pre_parse_help(args, unknown): + return + + args = args.parse_args(unknown) + # Check if the file path exists + if args.directory and os.path.exists(args.directory): + return self.execute_setup_directory(args.directory) + + elif args.file and os.path.exists(args.file): + return self.execute_sdk_file(args.file) + print('Please provide the correct folder path') + + def execute_setup_directory(self, setup_file_path): + """ + Prepare the environment using the parsed SDK folder provided + Args: + setup_file_path: Resolve completed sdk folder path + + Returns: results of execution + + """ + file_list = str(os.listdir(setup_file_path)) + # number of setup_file + setup_num = len(re.findall('environment-setup', file_list)) + # Determine the number of setup_files + if setup_num == 1: + try: + # Determine if the path ends with / + setup_file_path = setup_file_path if setup_file_path.endswith('/') else setup_file_path + '/' + # Convert relative path to absolute path + file_path = setup_file_path + re.search('environment-setup.*?(?=\')', file_list).group() + absolute_file_path = os.path.abspath(file_path) + # Splice Execution Command + shell_command = '. ' + absolute_file_path + + print(shell_command) + print('setup_file matching successful') + # Obtain the current terminal height and length + 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(f'''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(f'''sed -i '$a\{shell_command}' ~/.bashrc''', shell=True) + # Replace Console Prompt + subprocess.check_output('''sed -i 's/\$ />>>>> /g' ~/.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 + subprocess.check_output(f'''sed -i '$a\echo "{prompt_words}"' ~/.bashrc''', shell=True) + pty.spawn("/bin/bash") + # Add delete command + delete_shell_command = shell_command.replace('/', '\/') + subprocess.check_output(f'''sed -i '/{rows_command}/d' ~/.bashrc''', shell=True) + subprocess.check_output(f'''sed -i '/{delete_shell_command}/d' ~/.bashrc''', shell=True) + # delete Console Prompt + subprocess.check_output('''sed -i 's/>>>>> /\$ /g' ~/.bashrc''', shell=True) + # delete prompt words + delete_prompt = f""" + sed -i 's/echo \"{separator}\|{prompt_one}\|{prompt_two}\|###!###\|{separator}\"//g' ~/.bashrc + """ + subprocess.check_output(f'''{delete_prompt}''', shell=True) + except Exception as c_e: + print(c_e) + print('Please provide the correct folder path') + logger.error(c_e) + return False + return True + elif setup_num == 0: + print('not setup_file, please check your directory') + return False + else: + print('Illegal path, only one environment setup file allowed') + return False + + def execute_sdk_file(self, sdk_file): + """ + Execute the SDK file, produce the setup folder, and then execute the corresponding method for the setup file + Args: + sdk_file: SDK file path + + Returns: results of execution + + """ + setup_file_path = 'sdk_info_' + datetime.datetime.now().strftime('%Y%m%d%H%M%S') + try: + execte_sdk = subprocess.check_output(f'sh {sdk_file} -d {setup_file_path} -y', shell=True) + print(execte_sdk.decode()) + self.execute_setup_directory(os.getcwd() + '/' + setup_file_path) + except Exception as c_e: + print(c_e) + logger.error(c_e) + print('Please provide the correct folder path') +