From 8bc34f6e8626e44fbff397269e5bf93faf038b6f Mon Sep 17 00:00:00 2001 From: alichinese Date: Tue, 16 May 2023 11:40:03 +0800 Subject: [PATCH 1/2] oebuild: optimize code * Ways to optimize code rewrites * upgrade version to 0.0.17 Signed-off-by: lixinyu --- src/oebuild/command.py | 8 +++++--- src/oebuild/m_log.py | 6 ------ src/oebuild/parse_compile.py | 30 ++++++++++++++++-------------- src/oebuild/parse_env.py | 10 +++++++--- src/oebuild/version.py | 2 +- 5 files changed, 29 insertions(+), 27 deletions(-) diff --git a/src/oebuild/command.py b/src/oebuild/command.py index a87abd3..ca5593f 100644 --- a/src/oebuild/command.py +++ b/src/oebuild/command.py @@ -12,7 +12,7 @@ See the Mulan PSL v2 for more details. from abc import ABC, abstractmethod import argparse -import importlib +import importlib.util from collections import OrderedDict import os from dataclasses import dataclass @@ -211,8 +211,10 @@ def _commands_module_from_file(file, mod_name): these modules in an (otherwise unpopulated) oebuild.commands.ext package. ''' - spec = importlib.util.spec_from_file_location(mod_name, file) # type: ignore - mod = importlib.util.module_from_spec(spec) # type: ignore + spec = importlib.util.spec_from_file_location(mod_name, file) + if spec is None: + return None + mod = importlib.util.module_from_spec(spec) spec.loader.exec_module(mod) return mod diff --git a/src/oebuild/m_log.py b/src/oebuild/m_log.py index 2b61371..267b36a 100644 --- a/src/oebuild/m_log.py +++ b/src/oebuild/m_log.py @@ -25,25 +25,19 @@ WRN_COLOR = colorama.Fore.LIGHTYELLOW_EX #: Color used (when applicable) for printing with err() and die() ERR_COLOR = colorama.Fore.LIGHTRED_EX -# 创建logger对象 logger = logging.getLogger() logger.setLevel(logging.INFO) -# 创建文件处理器 fh = logging.FileHandler('oebuild.log') fh.setLevel(logging.INFO) -# 创建控制台处理器 ch = logging.StreamHandler() ch.setLevel(logging.INFO) -# 创建格式化器 formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') -# 将格式化器添加到文件处理器和控制台处理器 fh.setFormatter(formatter) ch.setFormatter(formatter) -# 将处理器添加到logger对象 logger.addHandler(fh) logger.addHandler(ch) diff --git a/src/oebuild/parse_compile.py b/src/oebuild/parse_compile.py index 385d952..16d46f6 100644 --- a/src/oebuild/parse_compile.py +++ b/src/oebuild/parse_compile.py @@ -83,7 +83,7 @@ class ParseCompile: repos=None if "repos" not in data else ParseTemplate.parse_oebuild_repo(data['repos']), local_conf=None if "local_conf" not in data else data['local_conf'], layers=None if "layers" not in data else data['layers'] - ) + ) @property def build_in(self): @@ -205,14 +205,15 @@ class ParseCompile: Check whether the compile.yaml content is compliant ''' - if "platform" not in data: - raise CheckCompileError("the key platform is None") + keys = { + "platform": "the key platform is None", + "machine": "the key machine is None", + "toolchain_type": "the key toolchain_type is None" + } - if "machine" not in data: - raise CheckCompileError("the key machine is None") - - if "toolchain_type" not in data: - raise CheckCompileError("the key toolchain_type is None") + for key, value in keys.items(): + if key not in data: + raise CheckCompileError(value) if "toolchain_dir" in data and data['toolchain_dir'] is not None: if not os.path.exists(data['toolchain_dir']): @@ -220,10 +221,11 @@ class ParseCompile: if "repos" in data: for _, repo in data['repos'].items(): - if "url" not in repo: - raise CheckCompileError("the key url is None") - if "path" not in repo: - raise CheckCompileError("the key path is None") - if "refspec" not in repo: - raise CheckCompileError("the key refspec is None") + required_keys = { + "url": "the key url is None", + "path": "the key path is None", + "refspec": "the key refspec is None"} + for key, value in required_keys.items(): + if key not in repo: + raise CheckCompileError(value) \ No newline at end of file diff --git a/src/oebuild/parse_env.py b/src/oebuild/parse_env.py index 4cb482c..2e8e4a7 100644 --- a/src/oebuild/parse_env.py +++ b/src/oebuild/parse_env.py @@ -10,6 +10,7 @@ MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. See the Mulan PSL v2 for more details. ''' +from typing import Optional from dataclasses import dataclass import pathlib @@ -24,7 +25,7 @@ class EnvContainer: branch: str - short_id: str or None + short_id: Optional[str] volumns: list @@ -33,7 +34,7 @@ class Env: ''' the env object ''' - container: EnvContainer or None + container: Optional[EnvContainer] class ParseEnv: ''' @@ -42,7 +43,7 @@ class ParseEnv: ''' def __init__(self, env_dir): self.env_dir = pathlib.Path(env_dir) if isinstance(env_dir, str) else env_dir - self.env = Env + self.env:Env = Env(container=None) self._parse_env() @property @@ -82,6 +83,9 @@ class ParseEnv: if data.volumns is None: raise ValueError("the key volumns is lack") + if self.env is None: + return False + if self.env.container is None: return False diff --git a/src/oebuild/version.py b/src/oebuild/version.py index 3723df2..91d589e 100644 --- a/src/oebuild/version.py +++ b/src/oebuild/version.py @@ -10,4 +10,4 @@ MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. See the Mulan PSL v2 for more details. ''' -__version__ = '0.0.16' +__version__ = '0.0.17' -- Gitee From f1f5b60b69956751c73e0c90e58a9d9ea34e162e Mon Sep 17 00:00:00 2001 From: alichinese Date: Fri, 19 May 2023 10:57:39 +0800 Subject: [PATCH 2/2] log: redirect log output * The original log output is pointed to the err channel by default, which does not conform to the normal log output form, and the redirect log output channel is changed to stdout * upgrade version to 0.0.18 Signed-off-by: lixinyu44@huawei.com --- src/oebuild/m_log.py | 4 ++-- src/oebuild/version.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/oebuild/m_log.py b/src/oebuild/m_log.py index 267b36a..6ac7737 100644 --- a/src/oebuild/m_log.py +++ b/src/oebuild/m_log.py @@ -9,7 +9,7 @@ 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 sys import logging import colorama @@ -31,7 +31,7 @@ logger.setLevel(logging.INFO) fh = logging.FileHandler('oebuild.log') fh.setLevel(logging.INFO) -ch = logging.StreamHandler() +ch = logging.StreamHandler(stream=sys.stdout) ch.setLevel(logging.INFO) formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') diff --git a/src/oebuild/version.py b/src/oebuild/version.py index 91d589e..81392fe 100644 --- a/src/oebuild/version.py +++ b/src/oebuild/version.py @@ -10,4 +10,4 @@ MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. See the Mulan PSL v2 for more details. ''' -__version__ = '0.0.17' +__version__ = '0.0.18' -- Gitee