From 0e14f5604d4558e68ee49c6ff6d510cd213aa7b0 Mon Sep 17 00:00:00 2001 From: lixinyu Date: Tue, 5 Mar 2024 15:03:13 +0800 Subject: [PATCH] code: format the code * make the code more compliant with flake8 and pylint inspection standards Signed-off-by: lixinyu --- src/oebuild/app/plugins/bitbake/bitbake.py | 6 +- .../app/plugins/bitbake/in_container.py | 76 ++++++++++++------- src/oebuild/bblayers.py | 4 +- src/oebuild/check_docker_tag.py | 10 ++- src/oebuild/command.py | 8 +- src/oebuild/const.py | 6 +- src/oebuild/docker_proxy.py | 19 +++-- src/oebuild/util.py | 7 +- 8 files changed, 81 insertions(+), 55 deletions(-) diff --git a/src/oebuild/app/plugins/bitbake/bitbake.py b/src/oebuild/app/plugins/bitbake/bitbake.py index 512e485..96fd493 100644 --- a/src/oebuild/app/plugins/bitbake/bitbake.py +++ b/src/oebuild/app/plugins/bitbake/bitbake.py @@ -42,9 +42,9 @@ class Bitbake(OebuildCommand): '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 + 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 ''') ) diff --git a/src/oebuild/app/plugins/bitbake/in_container.py b/src/oebuild/app/plugins/bitbake/in_container.py index 7bf8b31..0f1b7c6 100644 --- a/src/oebuild/app/plugins/bitbake/in_container.py +++ b/src/oebuild/app/plugins/bitbake/in_container.py @@ -151,8 +151,10 @@ class InContainer(BaseBuild): container=container, command="bash .bashrc", user=oebuild_const.CONTAINER_USER, - work_space=f"/home/{oebuild_const.CONTAINER_USER}", - demux=True) + params={ + "work_space": f"/home/{oebuild_const.CONTAINER_USER}", + "demux": True + }) exit_code = 0 for line in res.output: if line[1] is not None: @@ -185,8 +187,10 @@ class InContainer(BaseBuild): container=container, command=f"bash /home/{oebuild_const.CONTAINER_USER}/.bashrc", user=oebuild_const.CONTAINER_USER, - work_space=f"/home/{oebuild_const.CONTAINER_USER}", - stream=False) + params={ + "work_space": f"/home/{oebuild_const.CONTAINER_USER}", + "stream": False + }) if res.exit_code != 0: raise ValueError(res.output.decode()) # raise ValueError("bitbake init faild") @@ -196,8 +200,11 @@ class InContainer(BaseBuild): container=container, user='root', command=f"id {oebuild_const.CONTAINER_USER}", - work_space=f"/home/{oebuild_const.CONTAINER_USER}", - stream=False) + params={ + "work_space": f"/home/{oebuild_const.CONTAINER_USER}", + "stream": False + }) + if res.exit_code != 0: raise ValueError("check docker user id faild") @@ -231,16 +238,20 @@ class InContainer(BaseBuild): container=container, user='root', command=f"usermod -u {uid} {oebuild_const.CONTAINER_USER}", - work_space=f"/home/{oebuild_const.CONTAINER_USER}", - stream=False) + params={ + "work_space": f"/home/{oebuild_const.CONTAINER_USER}", + "stream": False + }) def _change_container_gid(self, container: Container, gid: int): self.client.container_exec_command( container=container, user='root', command=f"groupmod -g {gid} {oebuild_const.CONTAINER_USER}", - work_space=f"/home/{oebuild_const.CONTAINER_USER}", - stream=False) + params={ + "work_space": f"/home/{oebuild_const.CONTAINER_USER}", + "stream": False + }) def _install_sudo(self, container: Container): self._replace_yum_mirror(container=container) @@ -249,9 +260,10 @@ class InContainer(BaseBuild): container=container, user='root', command="which sudo", - work_space=f"/home/{oebuild_const.CONTAINER_USER}", - stream=False - ) + params={ + "work_space": f"/home/{oebuild_const.CONTAINER_USER}", + "stream": False + }) if resp.exit_code != 0: logger.info( "=========================install sudo===============================") @@ -271,20 +283,22 @@ class InContainer(BaseBuild): container=container, user='root', command=r""" - sed -i 's/repo.openeuler.org/mirrors.huaweicloud.com\/openeuler/g' /etc/yum.repos.d/openEuler.repo +sed -i 's/repo.openeuler.org/mirrors.huaweicloud.com\/openeuler/g' /etc/yum.repos.d/openEuler.repo """, - work_space=f"/home/{oebuild_const.CONTAINER_USER}", - stream=False - ) + params={ + "work_space": f"/home/{oebuild_const.CONTAINER_USER}", + "stream": False + }) def _install_software(self, container: Container, software: str): resp = self.client.container_exec_command( container=container, user='root', command=f"yum install {software} -y", - work_space=f"/home/{oebuild_const.CONTAINER_USER}", - stream=True - ) + params={ + "work_space": f"/home/{oebuild_const.CONTAINER_USER}", + "stream": True + }) for line in resp.output: logger.info(line.decode().strip('\n')) @@ -298,12 +312,8 @@ class InContainer(BaseBuild): content = self._get_bashrc_content(container=container) # get host proxy information and set in container - init_proxy_command = "" host_proxy = oebuild_util.get_host_proxy(oebuild_const.PROXY_LIST) - for key, value in host_proxy.items(): - key_git = key.replace('_', '.') - command = f'export {key}={value}; git config --global {key_git} {value};' - init_proxy_command = f'{init_proxy_command} {command}' + init_proxy_command = self._set_container_proxy(host_proxy=host_proxy) # get nativesdk environment path automatic for next step sdk_env_path = oebuild_util.get_nativesdk_environment(container=container) @@ -319,6 +329,14 @@ class InContainer(BaseBuild): self.update_bashrc(container=container, content=new_content) + def _set_container_proxy(self, host_proxy): + init_proxy_command = "" + for key, value in host_proxy.items(): + key_git = key.replace('_', '.') + command = f'export {key}={value}; git config --global {key_git} {value};' + init_proxy_command = f'{init_proxy_command} {command}' + return init_proxy_command + def update_bashrc(self, container: Container, content: str): ''' update user initialization script by replace file, first create @@ -332,7 +350,7 @@ class InContainer(BaseBuild): to_path=f'/home/{oebuild_const.CONTAINER_USER}') container.exec_run( cmd=f''' - mv /home/{oebuild_const.CONTAINER_USER}/{tmp_file} /home/{oebuild_const.CONTAINER_USER}/.bashrc +mv /home/{oebuild_const.CONTAINER_USER}/{tmp_file} /home/{oebuild_const.CONTAINER_USER}/.bashrc ''', user="root" ) @@ -352,8 +370,10 @@ class InContainer(BaseBuild): container=container, command=f"cat /home/{oebuild_const.CONTAINER_USER}/.bashrc", user="root", - work_space=None, - stream=False) + params={ + "work_space": None, + "stream": False + }) if res.exit_code != 0: logger.error(res.output) diff --git a/src/oebuild/bblayers.py b/src/oebuild/bblayers.py index e92770c..cb4ca73 100644 --- a/src/oebuild/bblayers.py +++ b/src/oebuild/bblayers.py @@ -40,7 +40,7 @@ class BBLayers: ''' return self._bblayers_dir - def add_layer(self, pre_dir: str, layers: str or list): + def add_layer(self, pre_dir: str, layers: str | list): ''' Add a layer layer to bblayers.conf, but our layer layer verification is done on the host, @@ -64,7 +64,7 @@ class BBLayers: bb_utils.edit_bblayers_conf(self.bblayers_dir, add=bblayers, remove=None) - def check_layer_exist(self, layers: str or list): + def check_layer_exist(self, layers: str | list): ''' To check if it is legitimate to add a layer, the main thing is to verify the existence of layer.conf diff --git a/src/oebuild/check_docker_tag.py b/src/oebuild/check_docker_tag.py index dc6bda2..1080d16 100644 --- a/src/oebuild/check_docker_tag.py +++ b/src/oebuild/check_docker_tag.py @@ -45,6 +45,9 @@ class CheckDockerTag: self.tag_list.append(key) def get_tags(self,): + ''' + return docker image tag list + ''' return self.tag_list def list_image_tag(self,): @@ -62,12 +65,13 @@ the openeuler embedded docker tag can be selected list: print(log) def get_tag(self,) -> str: - + ''' + return docker instance tag + ''' if self.docker_tag is not None and self.docker_tag != "": if self.docker_tag not in self.tag_list: return None - else: - return str(self.docker_tag) + return str(self.docker_tag) yocto_dir = self.configure.source_yocto_dir() env_path = os.path.join(yocto_dir, ".oebuild/env.yaml") diff --git a/src/oebuild/command.py b/src/oebuild/command.py index 04cf37d..5570329 100644 --- a/src/oebuild/command.py +++ b/src/oebuild/command.py @@ -40,10 +40,10 @@ class OebuildCommand(ABC): def pre_parse_help(self, args: argparse.ArgumentParser, unknown: List[str]): ''' - Whether to parse the help command in advance, designed to adapt to some extended - scenarios that do not require command resolution, generally the function is placed - in the front of the do_run to execute, if it returns true, it means that it is a - help command, then there is no need to continue to execute, otherwise the specific + Whether to parse the help command in advance, designed to adapt to some extended + scenarios that do not require command resolution, generally the function is placed + in the front of the do_run to execute, if it returns true, it means that it is a + help command, then there is no need to continue to execute, otherwise the specific function content is executed ''' pars = args.parse_args(unknown) diff --git a/src/oebuild/const.py b/src/oebuild/const.py index 6262456..afd139e 100644 --- a/src/oebuild/const.py +++ b/src/oebuild/const.py @@ -36,9 +36,9 @@ SSTATE_CACHE = '/usr1/openeuler/sstate-cache' # used for bitbake/in_container.py BASH_BANNER = ''' - Welcome to the openEuler Embedded build environment, - where you can run 'bitbake openeuler-image' to build - standard images + Welcome to the openEuler Embedded build environment, + where you can run 'bitbake openeuler-image' to build + standard images ''' # used for configure.py diff --git a/src/oebuild/docker_proxy.py b/src/oebuild/docker_proxy.py index d0e3a03..458c935 100644 --- a/src/oebuild/docker_proxy.py +++ b/src/oebuild/docker_proxy.py @@ -97,7 +97,7 @@ class DockerProxy: def get_all_container(self): ''' get all container like command 'docker ps -a' - args: + args: None ''' return self._docker.containers.list(all=True) @@ -183,26 +183,25 @@ class DockerProxy: res = tar.extractall(path=dst_path) return res is None - def container_exec_command(self, - container: Container, - command: str or list, + def container_exec_command(self, container: Container, + command: str | list, user: str = '', - work_space=None, - stream=True, - demux=False): + params=None): ''' run command like 'docker run exec', other param will be default and just use a little params returns a data stream ''' + if params is None: + params = {} res = container.exec_run( cmd=command, user=user, - workdir=work_space, + workdir=None if "work_space" not in params else params['work_space'], stderr=True, stdout=True, - stream=stream, - demux=demux + stream=True if "stream" not in params else params['stream'], + demux=False if "demux" not in params else params['dumex'] ) return res diff --git a/src/oebuild/util.py b/src/oebuild/util.py index cf628f9..14945fb 100644 --- a/src/oebuild/util.py +++ b/src/oebuild/util.py @@ -265,9 +265,12 @@ def get_host_proxy(proxy_name): @contextmanager def suppress_print(): + ''' + make standand stdout to null, so the output can be none + ''' + original_stdout = sys.stdout try: - with open('/dev/null', 'w') as f: - original_stdout = sys.stdout + with open('/dev/null', 'w', encoding="utf-8") as f: sys.stdout = f yield finally: -- Gitee