From 27540aae7cf07ca76e949d2431fa58395024e086 Mon Sep 17 00:00:00 2001 From: yaokai13 Date: Fri, 19 Mar 2021 15:36:49 +0800 Subject: [PATCH] add check _service file --- core/check_meta_service.py | 168 +++++++++++++++++++++++++++++++++++++ core/runner.py | 8 +- openeuler_obs.py | 6 ++ 3 files changed, 180 insertions(+), 2 deletions(-) create mode 100644 core/check_meta_service.py diff --git a/core/check_meta_service.py b/core/check_meta_service.py new file mode 100644 index 0000000..0138aa0 --- /dev/null +++ b/core/check_meta_service.py @@ -0,0 +1,168 @@ +#!/bin/env python3 +# -*- encoding=utf8 -*- +#****************************************************************************** +# Copyright (c) Huawei Technologies Co., Ltd. 2021-2021. All rights reserved. +# 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. +# Author: yaokai +# Create: 2021-03-19 +# ****************************************************************************** +""" +check the format and URL of the service file +""" +import os +import sys +from common.log_obs import log +from xml.etree import ElementTree as ET + +class CheckMetaPull(object): + """ + Check the format of the service file and the correctness of its URL + """ + def __init__(self, **kwargs): + """ + kawrgs:dict,init dict by 'a': 'A' style + prid: the pullrequest id + """ + self.kwargs = kwargs + self.prid = self.kwargs['pr_id'] + + def _clean(self): + """ + Cleanup the current directory of obs_meta + """ + cmd = "if [ -d obs_meta ];then rm -rf obs_meta && echo 'Finish clean the obs_meta';fi" + rm_result = os.popen(cmd).readlines() + log.info(rm_result) + + def _get_latest_obs_meta(self): + """ + Get the latest obs_meta + """ + log.info("Get the latest obs_meta") + clone_cmd = "git clone --depth=1 https://gitee.com/src-openeuler/obs_meta" + for x in range(5): + log.info("Try to clone %s" % x) + clone_result = "" + pull_result = "" + clone_result = os.popen(clone_cmd).readlines() + pull_result = os.popen("if [ -d obs_meta ];then git -C obs_meta pull; \ + else echo 'Clone error';fi").readlines() + if "Already" in pull_result[0]: + log.info(pull_result) + log.info("Success to clone obs_meta") + break + else: + os.popen("if [ -d obs_meta ];then rm -rf obs_meta") + + def _get_new_pkg(self): + """ + Gets a list of newly added files in obs_meta + """ + fetch_cmd = "git fetch origin pull/%s/head:thispr" % self.prid + branch_cmd = "git branch -a | grep 'thispr'" + checkout_cmd = "git checkout thispr" + changed_file_cmd = "git log --name-status -1" + for x in range(5): + os.chdir("obs_meta") + fetch_result = os.popen(fetch_cmd).read() + log.info(fetch_result) + branch_result = os.popen(branch_cmd).readlines() + if "thispr" in branch_result[0]: + log.info(branch_result) + break + else: + os.chdir("../") + self._clean() + self._get_latest_obs_meta() + show_result = os.popen(checkout_cmd).readlines() + log.info(show_result) + changed_file_result = os.popen(changed_file_cmd).readlines() + log.info(changed_file_result) + new_pkg_path = [] + for pkg in changed_file_result: + if "A\t" in pkg: + all_msg = pkg.replace("\n", "").split('/') + log.info(all_msg) + pkg_path = all_msg[0].replace("A\t", "") + '/' + all_msg[1] + '/' + all_msg[-2] + new_pkg_path.append(pkg_path) + log.info("All_change_pkg:%s" % new_pkg_path) + if not new_pkg_path: + log.info("There have no Package add") + sys.exit() + else: + return new_pkg_path + + def _check_pkg_services(self, pkg_path_list): + """ + Function modules: Check the format of the service file and the correctness of its URL + """ + error_flag = "" + for pkg_path in pkg_path_list: + try: + ET.parse(pkg_path + "/" + "_service") + log.info("**************FORMAT CORRECT***************") + log.info("The %s has a nice _service" % pkg_path) + log.info("**************FORMAT CORRECT***************") + except Exception as e: + log.error("**************FORMAT ERROR*****************") + log.error("MAY be %s has a bad _service format" % pkg_path) + log.error("%s/_service format bad Because:%s" % (pkg_path, e)) + log.error("**************FORMAT ERROR*****************") + error_flag = "yes" + service_path = pkg_path + '/' + '_service' + #log.info("Service_path:%s" % service_path) + url_result = "" + with open(service_path, "r") as f: + log.info('\n' + f.read()) + f.seek(0, 0) + for url in f.readlines(): + if "name=\"url\"" in url.replace("\n", ""): + all_url = url.replace("\n", "").split('/') + #log.info(all_url) + spkg_name = all_url[-2].replace("<", "") + spkg_url = all_url[-3] + pkg_name = pkg_path.split('/')[-1] + pkg_url = pkg_path.split('/')[0] + log.info("Service_pkgname:%s" % spkg_name) + log.info("Service_pkgurl:%s" % spkg_url) + log.info("Pkgname:%s" % pkg_name) + log.info("Pkg_url:%s" % pkg_url) + if spkg_url == "openEuler" and pkg_url == "master": + if spkg_name == pkg_name: + log.info("Yes:The %s in _service url is correct" % pkg_name) + else: + log.error("**************_Service URL ERROR*****************") + log.error("FAILED The %s in _service pkgname is not same" % pkg_name) + error_flag = "yes" + elif spkg_url == pkg_url: + if spkg_name == pkg_name: + log.info("Yes:The %s in _service url is correct" % pkg_name) + else: + log.error("**************_Service URL ERROR*****************") + log.error("FAILED The %s in _service pkgname is not same" % pkg_name) + error_flag = "yes" + else: + log.error("**************_Service URL ERROR*****************") + log.error("FAILED You need to check your %s _service again" % pkg_name) + error_flag = "yes" + break + os.chdir("../") + self._clean() + if error_flag == "yes": + raise SystemExit("*******PLEASE CHECK AGAIN*******") + + def do_all(self): + """ + Assemble all inspection processes and provide external interfaces + """ + self._clean() + self._get_latest_obs_meta() + changelist = self._get_new_pkg() + self._check_pkg_services(changelist) diff --git a/core/runner.py b/core/runner.py index c753abb..5a75b66 100644 --- a/core/runner.py +++ b/core/runner.py @@ -23,6 +23,7 @@ from core.save import SaveInfo from core.project_manager import OBSPrjManager from core.gitee_to_obs import SYNCCode from core.gitee_to_obs import CheckCode +from core.check_meta_service import CheckMetaPull from core.package_manager import OBSPkgManager from core.update_obs_repos import RPMManager import os @@ -113,11 +114,14 @@ class Runner(object): self._save_latest_info() elif self.kwargs["repository"] == "obs_meta": self._obs_meta_action() - elif self.kwargs["repository"] not in self.ignore_list: + elif self.kwargs["repository"] and self.kwargs["repository"] not in self.ignore_list: if not self.update_enabled_flag[self.kwargs["branch"]]: log.debug("can not update branch:%s, package: %s" % (self.kwargs["branch"], self.kwargs["repository"])) self._save_unsync_info() else: self._update_package() - + elif self.kwargs["check_pkg_service"] == "true": + print("check_pkg_service") + check = CheckMetaPull(**self.kwargs) + check.do_all() diff --git a/openeuler_obs.py b/openeuler_obs.py index 69b0298..bcb7e6c 100644 --- a/openeuler_obs.py +++ b/openeuler_obs.py @@ -81,6 +81,10 @@ par.add_argument("-latest", "--latest_info", default=False, par.add_argument("-cc", "--check_codes", default=False, help="check codes same or not between gitee and obs,should be with -b and -p", required=False) +par.add_argument("-cps", "--check_pkg_service", default=False, + help="check if there are any problems with the content of the _service file in the rpm package", required=False) +par.add_argument("-prid", "--pr_id", default=False, + help="use the pr_id to get this pullrequest", required=False) args = par.parse_args() #apply kw = { @@ -110,6 +114,8 @@ kw = { "latest_info": args.latest_info, "check_codes": args.check_codes, + "check_pkg_service": args.check_pkg_service, + "pr_id": args.pr_id, } run = Runner(**kw) -- Gitee