From dbfce61345691218ec5f7fc83576ec3a91a7a3ca Mon Sep 17 00:00:00 2001 From: alichinese Date: Mon, 29 Apr 2024 15:56:44 +0800 Subject: [PATCH] code: optimize code structure * adapted for python3.8 version: list[type] -> list * Removed the unused log color feature Signed-off-by: alichinese --- setup.py | 1 - src/oebuild/app/main.py | 4 +--- src/oebuild/app/plugins/generate/generate.py | 6 +++--- src/oebuild/app/plugins/init/init.py | 14 ++++++++++++-- src/oebuild/command.py | 13 +++---------- src/oebuild/configure.py | 4 ++-- src/oebuild/docker_proxy.py | 2 +- src/oebuild/m_log.py | 13 ------------- src/oebuild/parse_param.py | 6 +++--- src/oebuild/struct.py | 2 +- 10 files changed, 26 insertions(+), 39 deletions(-) diff --git a/setup.py b/setup.py index 4248684..0b7b5c5 100644 --- a/setup.py +++ b/setup.py @@ -52,7 +52,6 @@ setuptools.setup( 'PyYaml', 'docker', 'GitPython', - 'colorama', 'ruamel.yaml', 'dataclasses', 'reprint', diff --git a/src/oebuild/app/main.py b/src/oebuild/app/main.py index 80aae9b..c9b0364 100644 --- a/src/oebuild/app/main.py +++ b/src/oebuild/app/main.py @@ -15,8 +15,6 @@ import pathlib from collections import OrderedDict import getpass -import colorama - import oebuild.util as oebuild_util import oebuild.const as oebuild_const from oebuild.m_log import logger @@ -254,6 +252,7 @@ class QuickBuild(): 'update', 'layer' ] + os.chdir(self.oebuild_env.workdir) self.app.run(argv or sys.argv[1:]) def do_build_list(self,): @@ -342,7 +341,6 @@ def main(argv=None): if not check_user(): return - colorama.init() AutoCompletion().run() if (len(sys.argv) > 1) and 'yaml' in sys.argv[1]: build = QuickBuild(build_yaml_path=sys.argv[1]) diff --git a/src/oebuild/app/plugins/generate/generate.py b/src/oebuild/app/plugins/generate/generate.py index 1aa8b56..3fc7b1c 100644 --- a/src/oebuild/app/plugins/generate/generate.py +++ b/src/oebuild/app/plugins/generate/generate.py @@ -30,7 +30,7 @@ import oebuild.util as oebuild_util from oebuild.configure import Configure from oebuild.parse_template import BaseParseTemplate, ParseTemplate, \ get_docker_param_dict, parse_repos_layers_local_obj -from oebuild.m_log import logger, INFO_COLOR +from oebuild.m_log import logger from oebuild.check_docker_tag import CheckDockerTag import oebuild.const as oebuild_const @@ -503,7 +503,7 @@ Wrong platform, please run `oebuild generate -l` to view support feature""") table.add_row([platform.replace('.yml', '')]) if platform.endswith('.yaml'): table.add_row([platform.replace('.yaml', '')]) - print(table, INFO_COLOR) + print(table) def _list_feature(self, ): yocto_dir = self.configure.source_yocto_dir() @@ -524,7 +524,7 @@ Wrong platform, please run `oebuild generate -l` to view support feature""") table.add_row([feature_name, feat.get('support')]) else: table.add_row([feature_name, "all"]) - print(table, INFO_COLOR) + print(table) def check_support_oebuild(self, yocto_dir): ''' diff --git a/src/oebuild/app/plugins/init/init.py b/src/oebuild/app/plugins/init/init.py index ed3944f..0bd1f1b 100644 --- a/src/oebuild/app/plugins/init/init.py +++ b/src/oebuild/app/plugins/init/init.py @@ -102,6 +102,13 @@ class Init(OebuildCommand): self.print_help_msg() sys.exit(1) + # check if oebuild workspace exist, if exist, given notice + if Configure().is_oebuild_dir(pathlib.Path(args.directory)): + logger.info("the %s has already been initialized", + str(pathlib.Path(args.directory).absolute())) + self._print_notice(args.directory) + return + if not self.init_workspace(args.directory): logger.error("mkdir %s failed", args.directory) sys.exit(-1) @@ -122,14 +129,17 @@ class Init(OebuildCommand): self.configure.update_oebuild_config(oebuild_config) logger.info("init %s successful", args.directory) + self._print_notice(args.directory) + + def _print_notice(self, directory): format_msg = f''' -There is a build configuration example file under {args.directory}/.oebuild/compile.yaml.sample, +There is a build configuration example file under {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())} + cd {pathlib.Path(directory).absolute()} oebuild update ''' print(format_msg) diff --git a/src/oebuild/command.py b/src/oebuild/command.py index 5570329..cb2b0f8 100644 --- a/src/oebuild/command.py +++ b/src/oebuild/command.py @@ -13,13 +13,6 @@ See the Mulan PSL v2 for more details. from abc import ABC, abstractmethod import argparse from typing import List -import colorama - -INF_COLOR = colorama.Fore.LIGHTGREEN_EX - -WRN_COLOR = colorama.Fore.LIGHTYELLOW_EX - -ERR_COLOR = colorama.Fore.LIGHTRED_EX class OebuildCommand(ABC): @@ -31,14 +24,14 @@ class OebuildCommand(ABC): self.description = description self.parser: argparse.ArgumentParser = None - def run(self, args: argparse.ArgumentParser, unknown: List[str]): + def run(self, args: argparse.ArgumentParser, unknown: List): ''' The executing body, each inherited class will register the executor with the executor body for execution ''' self.do_run(args=args, unknown=unknown) - def pre_parse_help(self, args: argparse.ArgumentParser, unknown: List[str]): + def pre_parse_help(self, args: argparse.ArgumentParser, unknown: List): ''' 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 @@ -76,7 +69,7 @@ class OebuildCommand(ABC): ''' @abstractmethod - def do_run(self, args: argparse.Namespace, unknown: List[str]): + def do_run(self, args: argparse.Namespace, unknown: List): ''' Subclasses must implement; called to run the command. :param args: ``argparse.Namespace`` of parsed arguments diff --git a/src/oebuild/configure.py b/src/oebuild/configure.py index 9ee56ef..ab916f1 100644 --- a/src/oebuild/configure.py +++ b/src/oebuild/configure.py @@ -103,12 +103,12 @@ class Configure: return os.path.join(Configure.oebuild_topdir(start), '.oebuild') @staticmethod - def is_oebuild_dir(): + def is_oebuild_dir(start: Optional[PathType] = None): ''' Determine whether OEBuild is initialized ''' try: - Configure.oebuild_topdir() + Configure.oebuild_topdir(start) return True except OebuildNotFound: return False diff --git a/src/oebuild/docker_proxy.py b/src/oebuild/docker_proxy.py index aa7382c..f1a33fe 100644 --- a/src/oebuild/docker_proxy.py +++ b/src/oebuild/docker_proxy.py @@ -211,7 +211,7 @@ class DockerProxy: def create_container(self, image: str, parameters: str, - volumes: List[str], + volumes: List, command: str) -> Container: ''' create a new container diff --git a/src/oebuild/m_log.py b/src/oebuild/m_log.py index 2179c86..dc32330 100644 --- a/src/oebuild/m_log.py +++ b/src/oebuild/m_log.py @@ -11,19 +11,6 @@ See the Mulan PSL v2 for more details. ''' import sys import logging -import colorama - -#: Color used (when applicable) for printing with successful() -INFO_COLOR = colorama.Fore.WHITE - -#: Color used (when applicable) for printing with successful() -SUCCESS_COLOR = colorama.Fore.LIGHTGREEN_EX - -#: Color used (when applicable) for printing with wrn() -WRN_COLOR = colorama.Fore.LIGHTYELLOW_EX - -#: Color used (when applicable) for printing with err() and die() -ERR_COLOR = colorama.Fore.LIGHTRED_EX logger = logging.getLogger() logger.setLevel(logging.INFO) diff --git a/src/oebuild/parse_param.py b/src/oebuild/parse_param.py index 9393ab9..ff88f99 100644 --- a/src/oebuild/parse_param.py +++ b/src/oebuild/parse_param.py @@ -82,11 +82,11 @@ class ParseDockerParam: class DockerParam: image: str parameters: str - volumns: list[str] + volumns: list command: str ''' @staticmethod - def parse_to_obj(docker_param_dict: Dict[str, str | list[str]]) -> DockerParam: + def parse_to_obj(docker_param_dict: Dict) -> DockerParam: ''' parse dict to DockerParam ''' @@ -98,7 +98,7 @@ class ParseDockerParam: ) @staticmethod - def parse_to_dict(docker_param_obj: DockerParam) -> Dict[str, str | list[str]]: + def parse_to_dict(docker_param_obj: DockerParam): ''' parse dict to DockerParam ''' diff --git a/src/oebuild/struct.py b/src/oebuild/struct.py index cbd2c5a..d6b1179 100644 --- a/src/oebuild/struct.py +++ b/src/oebuild/struct.py @@ -47,7 +47,7 @@ class DockerParam: # point out the parameter for create container parameters: str # point out the volumns for create container - volumns: list[str] + volumns: list # point out the command for create container command: str -- Gitee