From b95036a78a94eab74f0e107bc7060c2cc3c5a8aa Mon Sep 17 00:00:00 2001 From: dingsheng Date: Wed, 9 Oct 2024 12:48:14 +0000 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=88=86=E7=B1=BB=E7=AE=97?= =?UTF-8?q?=E6=B3=95=E5=85=A5=E5=8F=A3=E5=87=BD=E6=95=B0=EF=BC=8Cutil?= =?UTF-8?q?=E6=A8=A1=E5=9D=97=EF=BC=8C=E8=B0=83=E6=95=B4=E5=B7=A5=E5=85=B7?= =?UTF-8?q?=E7=B1=BB=E4=BD=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/__init__.py | 0 src/main/__init__.py | 0 src/main/algclassify.py | 13 ++++- src/utils/__init__.py | 0 src/utils/depparse.py | 67 +------------------------- src/utils/util.py | 102 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 114 insertions(+), 68 deletions(-) create mode 100644 src/__init__.py create mode 100644 src/main/__init__.py create mode 100644 src/utils/__init__.py create mode 100755 src/utils/util.py diff --git a/src/__init__.py b/src/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/main/__init__.py b/src/main/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/main/algclassify.py b/src/main/algclassify.py index 4ad76d8..83acd3f 100755 --- a/src/main/algclassify.py +++ b/src/main/algclassify.py @@ -23,11 +23,20 @@ class AlgClassify(object): """ @classmethod - def run(cls): + def run(cls,data_obj,json_f): """ 分类算法入口函数 + Args: + data_obj : DataParse类对象 + json_f (str): 分类数据json文件 + + Returns: + res: 分类字典 """ - pass + category_d1 = cls._get_pkg2category_by_jsonf(json_f) + category_d2 = cls._get_pkg2category_by_rpmgroup(data_obj) + res = cls._merge_pkg2category_dict(data_obj,category_d1,category_d2) + return res @staticmethod def _load_data(data_f): diff --git a/src/utils/__init__.py b/src/utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/utils/depparse.py b/src/utils/depparse.py index d6e4b53..32bda9d 100755 --- a/src/utils/depparse.py +++ b/src/utils/depparse.py @@ -12,12 +12,10 @@ # See the Mulan PSL v2 for more details. # ********************************************************************************** """ -import os import copy from collections import defaultdict -from fnmatch import fnmatch -import isoparser import hawkey +from util import ISOUtils class DepParse(object): """ @@ -122,66 +120,3 @@ class ISODepParse(DepParse): deps_by_dict[tmp_k] = tmp_v return deps_by_dict - -class ISOUtils(object): - """ - ISO处理工具类 - """ - - @staticmethod - def parse_iso_repodata(iso_path,repodata_dir='/opt/kyclassifier/iso_parse/repodata'): - """ - 解析ISO中repodata目录 - Args: - iso_path (string): iso文件 - repodata_dir (string, optional): repodata目录. Defaults to '/opt/kyclassifier/iso_parse/repodata'. - """ - os.makedirs(repodata_dir,exist_ok=True) - iso = isoparser.parse(iso_path) - for repo in iso.record(b'repodata').children: - file_name = repo.name.decode('utf-8') - content = repo.get_stream().read() - with open(os.path.join(repodata_dir,file_name),'wb') as f: - f.write(content) - - @staticmethod - def get_repo_from_dir(repodata_dir='/opt/kyclassifier/iso_parse/repodata'): - """ - 解析repodata目录中数据文件 - Args: - repodata_dir (str, optional): repodata目录. Defaults to '/opt/kyclassifier/iso_parse/repodata'. - Returns: - tuple: ISO数据文件路径 - """ - repomd_fn = [] - primary_fn = [] - filelists_fn = [] - path = repodata_dir - for res in os.walk(path): - files = res[-1] - for f in files: - if fnmatch(f,'repomd.xml'): - repomd_fn.append(f) - elif fnmatch(f,'*primary.xml.*'): - primary_fn.append(f) - elif fnmatch(f,'*filelists.xml.*'): - filelists_fn.append(f) - else: - continue - if any(len(x)!=1 for x in [repomd_fn,primary_fn,filelists_fn]): - raise FileExistsError - else: - return os.path.join(path,repomd_fn[0]),os.path.join(path,primary_fn[0]),os.path.join(path,filelists_fn[0]) - - @classmethod - def parase_iso_repofile(cls,iso_path,target_dir='/opt/kyclassifier/iso_parse/repodata'): - """ - 解析ISO数据文件 - Args: - iso_path (str): ISO文件 - target_dir (string, optional): 数据文件保存目录. Defaults to '/opt/kyclassifier/iso_parse/repodata'. - Returns: - tuple: ISO数据文件路径 - """ - cls.parse_iso_repodata(iso_path,target_dir) - return cls.get_repo_from_dir() \ No newline at end of file diff --git a/src/utils/util.py b/src/utils/util.py new file mode 100755 index 0000000..929d89b --- /dev/null +++ b/src/utils/util.py @@ -0,0 +1,102 @@ +#-*- coding:utf-8 -*- +""" +# ********************************************************************************** +# Copyright (c) KylinSoft Co., Ltd. 2024.All rights reserved. +# [kyclassifier] licensed under the 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 os +import datetime +from fnmatch import fnmatch +import isoparser + +def get_formatted_time(): + """ + 获取当前时间格式化输出 + Returns: + formatted_time (str) : 格式化输出当前时间 + """ + current_datetime = datetime.datetime.now() + formatted_time = current_datetime.strftime('%Y-%m-%d-%H-%M-%S') + return formatted_time + +def trans_set2list(key2set_dict): + """ + 数据类型转换, set2list + Args: + key2set_dict (dict): 输入字典 + + Returns: + res: 输出转换类型后字典 + """ + return { k:list(v) for k,v in key2set_dict.items()} + +class ISOUtils(object): + """ + ISO处理工具类 + """ + + @staticmethod + def parse_iso_repodata(iso_path,repodata_dir='/opt/kyclassifier/iso_parse/repodata'): + """ + 解析ISO中repodata目录 + Args: + iso_path (string): iso文件 + repodata_dir (string, optional): repodata目录. Defaults to '/opt/kyclassifier/iso_parse/repodata'. + """ + os.makedirs(repodata_dir,exist_ok=True) + iso = isoparser.parse(iso_path) + for repo in iso.record(b'repodata').children: + file_name = repo.name.decode('utf-8') + content = repo.get_stream().read() + with open(os.path.join(repodata_dir,file_name),'wb') as f: + f.write(content) + + @staticmethod + def get_repo_from_dir(repodata_dir='/opt/kyclassifier/iso_parse/repodata'): + """ + 解析repodata目录中数据文件 + Args: + repodata_dir (str, optional): repodata目录. Defaults to '/opt/kyclassifier/iso_parse/repodata'. + Returns: + tuple: ISO数据文件路径 + """ + repomd_fn = [] + primary_fn = [] + filelists_fn = [] + path = repodata_dir + for res in os.walk(path): + files = res[-1] + for f in files: + if fnmatch(f,'repomd.xml'): + repomd_fn.append(f) + elif fnmatch(f,'*primary.xml.*'): + primary_fn.append(f) + elif fnmatch(f,'*filelists.xml.*'): + filelists_fn.append(f) + else: + continue + if any(len(x)!=1 for x in [repomd_fn,primary_fn,filelists_fn]): + raise FileExistsError + else: + return os.path.join(path,repomd_fn[0]),os.path.join(path,primary_fn[0]),os.path.join(path,filelists_fn[0]) + + @classmethod + def parase_iso_repofile(cls,iso_path,target_dir='/opt/kyclassifier/iso_parse/repodata'): + """ + 解析ISO数据文件 + Args: + iso_path (str): ISO文件 + target_dir (string, optional): 数据文件保存目录. Defaults to '/opt/kyclassifier/iso_parse/repodata'. + Returns: + tuple: ISO数据文件路径 + """ + cls.parse_iso_repodata(iso_path,target_dir) + return cls.get_repo_from_dir() \ No newline at end of file -- Gitee