From 6ce389caa3a1aa60f35c00e0cdb7739f7d517f74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=80=E7=9B=B4=E5=90=91=E5=89=8D=E7=9A=84=E5=B0=8F?= =?UTF-8?q?=E7=99=BD=E7=BE=8A?= Date: Fri, 21 Jul 2023 05:51:17 +0000 Subject: [PATCH] =?UTF-8?q?update=20tools/check=5Fpackage=5Fbinary=5Farchi?= =?UTF-8?q?ve.py.=20=E5=AF=B9=E4=BB=A3=E7=A0=81=E8=BF=9B=E8=A1=8C=E4=BC=98?= =?UTF-8?q?=E5=8C=96=EF=BC=9A=201.=E5=B0=86=E5=91=BD=E4=BB=A4=E8=A1=8C?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E8=A7=A3=E6=9E=90=E9=83=A8=E5=88=86=E5=B0=81?= =?UTF-8?q?=E8=A3=85=E4=B8=BA=E4=B8=80=E4=B8=AA=E5=87=BD=E6=95=B0=EF=BC=8C?= =?UTF-8?q?=E5=B9=B6=E6=94=B9=E8=BF=9B=E4=BA=86=E5=8F=82=E6=95=B0=E7=9A=84?= =?UTF-8?q?=E5=91=BD=E5=90=8D=E5=92=8C=E5=B8=AE=E5=8A=A9=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E3=80=82=202.=E5=9C=A8=E5=87=BD=E6=95=B0=E5=86=85=E9=83=A8?= =?UTF-8?q?=EF=BC=8C=E5=AF=B9=E4=B8=80=E4=BA=9B=E5=8F=98=E9=87=8F=E5=92=8C?= =?UTF-8?q?=E8=B7=AF=E5=BE=84=E8=BF=9B=E8=A1=8C=E5=B1=80=E9=83=A8=E5=8C=96?= =?UTF-8?q?=EF=BC=8C=E9=81=BF=E5=85=8D=E5=85=A8=E5=B1=80=E5=8F=98=E9=87=8F?= =?UTF-8?q?=E7=9A=84=E4=BD=BF=E7=94=A8=E3=80=82=203.=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E4=BA=86=E9=83=A8=E5=88=86=E5=8F=98=E9=87=8F=E5=92=8C=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E7=9A=84=E5=91=BD=E5=90=8D=EF=BC=8C=E9=81=B5=E5=BE=AA?= =?UTF-8?q?=E4=BA=86=20Python=20=E7=9A=84=E5=91=BD=E5=90=8D=E8=A7=84?= =?UTF-8?q?=E8=8C=83=E3=80=82=204.=E5=B0=86=E5=85=8B=E9=9A=86=20Gitee=20?= =?UTF-8?q?=E4=BB=93=E5=BA=93=E5=92=8C=E8=AF=BB=E5=8F=96=20YAML=20?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E7=9A=84=E9=80=BB=E8=BE=91=E6=8F=90=E5=8F=96?= =?UTF-8?q?=E4=B8=BA=E7=8B=AC=E7=AB=8B=E7=9A=84=E5=87=BD=E6=95=B0=EF=BC=8C?= =?UTF-8?q?=E5=B9=B6=E4=BC=A0=E5=85=A5=E5=BF=85=E8=A6=81=E7=9A=84=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E3=80=82=205.=E5=B0=86=E8=8E=B7=E5=8F=96=20OBS=20?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E5=99=A8=E4=B8=8A=E4=BE=9D=E8=B5=96=E4=BB=93?= =?UTF-8?q?=E5=BA=93=E7=9A=84=E4=BA=8C=E8=BF=9B=E5=88=B6=E5=88=97=E8=A1=A8?= =?UTF-8?q?=E7=9A=84=E9=80=BB=E8=BE=91=E6=8F=90=E5=8F=96=E4=B8=BA=E7=8B=AC?= =?UTF-8?q?=E7=AB=8B=E7=9A=84=E5=87=BD=E6=95=B0=EF=BC=8C=E5=B9=B6=E4=BC=A0?= =?UTF-8?q?=E5=85=A5=E5=BF=85=E8=A6=81=E7=9A=84=E5=8F=82=E6=95=B0=E3=80=82?= =?UTF-8?q?=206.=E5=B0=86=E6=A3=80=E6=9F=A5=E4=BA=8C=E8=BF=9B=E5=88=B6?= =?UTF-8?q?=E5=8C=85=E5=BD=92=E6=A1=A3=E5=AE=8C=E6=95=B4=E6=80=A7=E5=92=8C?= =?UTF-8?q?=E5=8F=91=E9=80=81=E9=82=AE=E4=BB=B6=E9=80=9A=E7=9F=A5=E7=9A=84?= =?UTF-8?q?=E9=80=BB=E8=BE=91=E6=8F=90=E5=8F=96=E4=B8=BA=E7=8B=AC=E7=AB=8B?= =?UTF-8?q?=E7=9A=84=E5=87=BD=E6=95=B0=EF=BC=8C=E5=B9=B6=E4=BC=A0=E5=85=A5?= =?UTF-8?q?=E5=BF=85=E8=A6=81=E7=9A=84=E5=8F=82=E6=95=B0=E3=80=82=207.?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E5=88=97=E8=A1=A8=E6=8E=A8=E5=AF=BC=E5=BC=8F?= =?UTF-8?q?=E7=AE=80=E5=8C=96=E4=BA=86=E6=A3=80=E6=9F=A5=E4=BA=8C=E8=BF=9B?= =?UTF-8?q?=E5=88=B6=E5=8C=85=E5=BD=92=E6=A1=A3=E5=AE=8C=E6=95=B4=E6=80=A7?= =?UTF-8?q?=E7=9A=84=E9=80=BB=E8=BE=91=E3=80=82=208.=E9=87=8D=E6=9E=84?= =?UTF-8?q?=E4=BA=86=E4=BB=A3=E7=A0=81=E7=BB=93=E6=9E=84=EF=BC=8C=E4=BD=BF?= =?UTF-8?q?=E5=85=B6=E6=9B=B4=E5=8A=A0=E6=A8=A1=E5=9D=97=E5=8C=96=E5=92=8C?= =?UTF-8?q?=E5=8F=AF=E8=AF=BB=E6=80=A7=E6=9B=B4=E9=AB=98=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 一直向前的小白羊 --- tools/check_package_binary_archive.py | 127 +++++++++++++------------- 1 file changed, 62 insertions(+), 65 deletions(-) diff --git a/tools/check_package_binary_archive.py b/tools/check_package_binary_archive.py index 7d6f2f4..429b9e2 100644 --- a/tools/check_package_binary_archive.py +++ b/tools/check_package_binary_archive.py @@ -27,37 +27,32 @@ import smtplib from email.mime.text import MIMEText from email.header import Header -par = argparse.ArgumentParser() -par.add_argument("-p", "--project", default=None, help="obs project name", required=True) -par.add_argument("-dp", "--dep_project", default=None, - help="obs binary dependent project, example:openEuler:selfbuild:BaseOS", required=True) -par.add_argument("-dr", "--dep_repo", default=None, - help="dependent repo name, example:mainline_standard_aarch64", required=True) -par.add_argument("-a", "--arch", default=None, - help="architecture name, example:aarch64", required=True) -par.add_argument("-ip", "--obs_ip", default=None, help="obs server ipaddress", required=True) -par.add_argument("-ou", "--obs_user", default=None, help="obs server user", required=True) -par.add_argument("-opwd", "--obs_password", default=None, help="obs server password", required=True) -par.add_argument("-eu", "--email_user", help="email user name", required=True) -par.add_argument("-epwd", "--email_passwd", help="email user password", required=True) -par.add_argument("-tu", "--to_addr", help="recive email user", required=True) -args = par.parse_args() - -branch = args.project.replace(":", "-") -gitee_repo_path = os.path.join(os.getcwd(), "obs_pkg_rpms") -yaml_file = os.path.join(gitee_repo_path, "repo_files", "%s_%s.yaml" % (branch, args.arch)) -full_path = os.path.join("/srv/obs/build", args.dep_project, args.dep_repo, args.arch, ":full") - +def parse_arguments(): + """ + 解析命令行参数 + """ + par = argparse.ArgumentParser() + par.add_argument("-p", "--project", help="obs project name", required=True) + par.add_argument("-dp", "--dep_project", help="obs binary dependent project, example:openEuler:selfbuild:BaseOS", required=True) + par.add_argument("-dr", "--dep_repo", help="dependent repo name, example:mainline_standard_aarch64", required=True) + par.add_argument("-a", "--arch", help="architecture name, example:aarch64", required=True) + par.add_argument("-ip", "--obs_ip", help="obs server ipaddress", required=True) + par.add_argument("-ou", "--obs_user", help="obs server user", required=True) + par.add_argument("-opwd", "--obs_password", help="obs server password", required=True) + par.add_argument("-eu", "--email_user", help="email user name", required=True) + par.add_argument("-epwd", "--email_passwd", help="email user password", required=True) + par.add_argument("-tu", "--to_addr", help="receive email user", required=True) + return par.parse_args() -def git_clone(): +def git_clone(gitee_repo_path): """ - git clone gitee repo + 克隆 Gitee 仓库 """ if os.path.exists(gitee_repo_path): shutil.rmtree(gitee_repo_path) git_url = "https://gitee.com/openeuler_latest_rpms/obs_pkg_rpms.git" cmd = "git clone --depth 1 %s" % git_url - for i in range(5): + for _ in range(5): if os.system(cmd) != 0: print("[ERROR]: Git clone failed, try again...") else: @@ -66,9 +61,9 @@ def git_clone(): if not os.path.exists(gitee_repo_path): sys.exit(1) -def read_yaml(): +def read_yaml(yaml_file): """ - read yaml file + 读取 YAML 文件 """ rpmlist = [] if os.path.exists(yaml_file): @@ -76,17 +71,16 @@ def read_yaml(): file_msg = yaml.load(f, Loader=yaml.SafeLoader) for rpm in file_msg.values(): rpmlist.extend(rpm) - shutil.rmtree(gitee_repo_path) return rpmlist -def get_server_rpmlist(): +def get_server_rpmlist(obs_ip, obs_user, obs_password, full_path): """ - get obs server dependent repos binary list + 获取 OBS 服务器上依赖仓库的二进制列表 """ pmk = paramiko.SSHClient() key = paramiko.AutoAddPolicy() pmk.set_missing_host_key_policy(key) - pmk.connect(args.obs_ip, "22", args.obs_user, args.obs_password, timeout=5) + pmk.connect(obs_ip, "22", obs_user, obs_password, timeout=5) cmd = "cd %s && ls *.rpm | grep -v 'src.rpm'" % full_path stdin, stdout, stderr = pmk.exec_command(cmd) error_msg = stderr.readlines() @@ -95,58 +89,61 @@ def get_server_rpmlist(): sys.exit(1) server_rpmlist = [] for line in stdout.readlines(): - server_rpmlist.append(line.replace("\n", "")) + server_rpmlist.append(line.strip()) return server_rpmlist -def rpm_exist(rpmlist, server_rpmlist): +def check_binary_archive(project, dep_project, dep_repo, arch, obs_ip, obs_user, obs_password, email_user, email_passwd, to_addr): """ - check rpms archive + 检查二进制包归档完整性并发送邮件通知 """ - notfind = [] - for rpm in rpmlist: - if rpm and rpm.endswith(".rpm") and rpm not in server_rpmlist: - notfind.append(rpm) + branch = project.replace(":", "-") + gitee_repo_path = os.path.join(os.getcwd(), "obs_pkg_rpms") + yaml_file = os.path.join(gitee_repo_path, "repo_files", "%s_%s.yaml" % (branch, arch)) + full_path = os.path.join("/srv/obs/build", dep_project, dep_repo, arch, ":full") + + if arch not in dep_repo: + print("[ERROR]: %s and %s are not match." % (dep_repo, arch)) + sys.exit(1) + + git_clone(gitee_repo_path) + rpmlist = read_yaml(yaml_file) + server_rpmlist = get_server_rpmlist(obs_ip, obs_user, obs_password, full_path) + + notfind = [rpm for rpm in rpmlist if rpm.endswith(".rpm") and rpm not in server_rpmlist] if notfind: - msg = "

%s %s some binary not archived into %s as follows:

" %( - args.project, args.arch, full_path) + msg = "

%s %s some binaries are missing in %s:

" % (project, arch, full_path) print(msg) for line in notfind: - msg = msg + "
%s" % line + msg += "
%s" % line print(line) - return msg + send_email(msg, email_user, email_passwd, to_addr) + sys.exit(1) else: - print("[INFO]: %s %s all binary archived into %s are no missing." - % (args.project, args.arch, full_path)) - sys.exit(0) + print("[INFO]: %s %s all binaries in %s are complete." % (project, arch, full_path)) -def send_email(message): +def send_email(message, email_user, email_passwd, to_addr): """ - send email + 发送邮件通知 """ msg = MIMEText(message, 'html') - msg['Subject'] = Header("[Check %s Project Binary Archive]" % args.project, "utf-8") - msg['From'] = Header(args.email_user) - msg['To'] = Header(args.to_addr) + msg['Subject'] = Header("[Check Project Binary Archive]", "utf-8") + msg['From'] = Header(email_user) + msg['To'] = Header(to_addr) smtp_server = "smtp.163.com" try: server = smtplib.SMTP_SSL(smtp_server) - server.login(args.email_user, args.email_passwd) - server.sendmail(args.email_user, args.to_addr.split(','), msg.as_string()) + server.login(email_user, email_passwd) + server.sendmail(email_user, to_addr.split(','), msg.as_string()) server.quit() - print("[INFO]: send email succeed!") + print("[INFO]: Email sent successfully!") except smtplib.SMTPException as e: - raise SystemExit("send email failed, reason:%s" % e) + raise SystemExit("Failed to send email, reason: %s" % e) -def check_binary_archive(): - if args.arch not in args.dep_repo: - print("[ERROR]: %s and %s are not match." % (args.dep_repo, args.arch)) - sys.exit(1) - git_clone() - rpmlist = read_yaml() - server_rpmlist = get_server_rpmlist() - msg = rpm_exist(rpmlist, server_rpmlist) - if msg: - send_email(msg) - sys.exit(1) +def main(): + args = parse_arguments() + check_binary_archive(args.project, args.dep_project, args.dep_repo, args.arch, args.obs_ip, + args.obs_user, args.obs_password, args.email_user, args.email_passwd, args.to_addr) + +if __name__ == "__main__": + main() -check_binary_archive() -- Gitee