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 22from pathlib import Path 23 24 25def copy_file(dir): 26 src_name = '/mime.convs.in' 27 dest_name = '/mime.convs' 28 src_file = dir + src_name 29 dest_file = dir + dest_name 30 print(f'copy from %s to %s', src_file, dest_file) 31 shutil.copy2(src_file, dest_file) 32 33def move_cups_files(src_dir, dst_dir): 34 source_dir = Path(src_dir) 35 dest_dir = Path(dst_dir) 36 for src_path in source_dir.rglob("*"): 37 if src_path.is_file(): 38 rel_path = src_path.relative_to(source_dir) 39 dst_path = dest_dir / rel_path 40 dst_path.parent.mkdir(parents=True, exist_ok=True) 41 shutil.copy(src_path, dst_path) 42 43 backend_files = [ 44 "usb_ipp_manager.h", 45 "usb_ipp_manager.cpp", 46 "usb_monitor.h", 47 "usb_monitor.cpp" 48 ] 49 move_files_dir = "move_files" 50 backend_dir = "backend" 51 for file in backend_files: 52 src_file = os.path.join(src_dir, move_files_dir, file) 53 dst_file = os.path.join(dst_dir, backend_dir, file) 54 shutil.copy(src_file, dst_file) 55 56def apply_patch(patch_file, target_dir): 57 try: 58 if not os.path.exists(target_dir): 59 return 60 patch_cmd = ['patch', '-p1', "--fuzz=0", "--no-backup-if-mismatch", '-i', patch_file, '-d', target_dir] 61 subprocess.run(patch_cmd, check=True) 62 except Exception as e: 63 print(f"apply_patch error! patch: {patch_file}") 64 return 65 66 67def do_patch(target_dir): 68 patch_file = [ 69 "ohos-multi-file-print.patch", 70 "ohos-modify-pthread.patch", 71 "ohos_ip_conflict.patch", 72 "ohos-usb-manager.patch", 73 "ohos-usb-print.patch", 74 "ohos-ppdfile-not-generated.patch", 75 "ohos-hilog-print.patch", 76 "ohos-uni-print-driver-path.patch", 77 "ohos-cups-badfd.patch", 78 "ohos-verify-backend.patch", 79 "cups-usb-job-state-monitor.patch", 80 "ohos-filetypes-crash.patch" 81 ] 82 83 for patch in patch_file: 84 apply_patch(patch, target_dir) 85 86 87def main(): 88 cups_path = argparse.ArgumentParser() 89 cups_path.add_argument('--gen-dir', help='generate path of log', required=True) 90 cups_path.add_argument('--source-dir', help='generate path of log', required=True) 91 args = cups_path.parse_args() 92 target_dir = os.path.join(args.gen_dir, "cups-2.4.12") 93 convs_dir = os.path.join(target_dir, "conf") 94 95 move_cups_files(args.source_dir, target_dir) 96 do_patch(target_dir) 97 copy_file(convs_dir) 98 return 0 99 100if __name__ == '__main__': 101 sys.exit(main()) 102