1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3# Copyright (c) 2023 Huawei Device Co., Ltd. 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16import tarfile 17import argparse 18import os 19import subprocess 20import sys 21import shutil 22 23 24def untar_file(tar_file_path, extract_path): 25 try: 26 tar_cmd = ['tar', '-zxf', tar_file_path, '-C', extract_path] 27 subprocess.run(tar_cmd, check=True) 28 except Exception as e: 29 print("tar error!") 30 return 31 32def copy_file(dir): 33 src_name = '/mime.convs.in' 34 dest_name = '/mime.convs' 35 src_file = dir + src_name 36 dest_file = dir + dest_name 37 print(f'copy from %s to %s', src_file, dest_file) 38 shutil.copy2(src_file, dest_file) 39 40def move_file(src_path, dst_path): 41 files = [ 42 "ohos_ip_conflict.patch", 43 "backport-CVE-2022-26691.patch", 44 "backport-CVE-2023-32324.patch", 45 "backport-CVE-2023-34241.patch", 46 "ohos-multi-file-print.patch", 47 "ohos-modify-pthread.patch", 48 "ohos-add-openssl.patch", 49 "backport-CVE-2023-4504.patch", 50 "backport-CVE-2024-35235.patch", 51 "ohos-usb-manager.patch", 52 "ohos-usb-print.patch", 53 "ohos-ppdfile-not-generated.patch", 54 "ohos-hilog-print.patch", 55 "ohos-uni-print-driver-path.patch", 56 "ohos-cups-badfd.patch", 57 "ohos-verify-backend.patch", 58 "config.h" 59 ] 60 for file in files: 61 src_file = os.path.join(src_path, file) 62 dst_file = os.path.join(dst_path, file) 63 shutil.copy(src_file, dst_file) 64 65 66def apply_patch(patch_file, target_dir): 67 try: 68 if not os.path.exists(target_dir): 69 return 70 patch_cmd = ['patch', '-p1', "--fuzz=0", "--no-backup-if-mismatch", '-i', patch_file, '-d', target_dir] 71 subprocess.run(patch_cmd, check=True) 72 except Exception as e: 73 print(f"apply_patch error! patch: {patch_file}") 74 return 75 76 77def do_patch(target_dir): 78 patch_file = [ 79 "backport-CVE-2022-26691.patch", 80 "backport-CVE-2023-32324.patch", 81 "backport-CVE-2023-34241.patch", 82 "ohos-multi-file-print.patch", 83 "ohos-modify-pthread.patch", 84 "ohos-add-openssl.patch", 85 "backport-CVE-2023-4504.patch", 86 "backport-CVE-2024-35235.patch", 87 "ohos_ip_conflict.patch", 88 "ohos-usb-manager.patch", 89 "ohos-usb-print.patch", 90 "ohos-ppdfile-not-generated.patch", 91 "ohos-hilog-print.patch", 92 "ohos-uni-print-driver-path.patch", 93 "ohos-cups-badfd.patch", 94 "ohos-verify-backend.patch" 95 ] 96 97 for patch in patch_file: 98 apply_patch(patch, target_dir) 99 100 101def main(): 102 cups_path = argparse.ArgumentParser() 103 cups_path.add_argument('--gen-dir', help='generate path of log', required=True) 104 cups_path.add_argument('--source-dir', help='generate path of log', required=True) 105 args = cups_path.parse_args() 106 tar_file_path = os.path.join(args.source_dir, "cups-2.4.0-source.tar.gz") 107 target_dir = os.path.join(args.gen_dir, "cups-2.4.0") 108 convs_dir = os.path.join(target_dir, "conf") 109 110 untar_file(tar_file_path, args.gen_dir) 111 move_file(args.source_dir, target_dir) 112 do_patch(target_dir) 113 copy_file(convs_dir) 114 return 0 115 116if __name__ == '__main__': 117 sys.exit(main()) 118