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', '-xvf', tar_file_path, '-C', extract_path, '--strip-components=1'] 27 subprocess.run(tar_cmd, check=True) 28 except Exception as e: 29 print("tar error!") 30 return 31 32 33def move_file(src_path, dst_path): 34 files = [ 35 "backport-CVE-2023-2004.patch", 36 "backport-freetype-2.2.1-enable-valid.patch", 37 "backport-freetype-2.3.0-enable-spr.patch", 38 "backport-freetype-2.6.5-libtool.patch", 39 "backport-freetype-2.8-multilib.patch", 40 "backport-freetype-2.10.0-internal-outline.patch", 41 "backport-freetype-2.10.1-debughook.patch", 42 "backport-freetype-2.12.1-enable-funcs.patch", 43 "backport-Minimal-stop-gap-fix-for-CVE-2025-27363.patch", 44 "ftconfig.h" 45 ] 46 for file in files: 47 src_file = os.path.join(src_path, file) 48 dst_file = os.path.join(dst_path, file) 49 shutil.copy(src_file, dst_file) 50 51def move_include(src_path, dst_path): 52 try: 53 cp_cmd = ['cp', '-rf', dst_path, src_path] 54 subprocess.run(cp_cmd, check=True) 55 except Exception as e: 56 print("cp -rf error!") 57 return 58 59 60def apply_patch(patch_file, target_dir): 61 try: 62 if not os.path.exists(target_dir): 63 return 64 patch_cmd = ['patch', '-p1', "--fuzz=0", "--no-backup-if-mismatch", '-i', patch_file, '-d', target_dir] 65 subprocess.run(patch_cmd, check=True) 66 except Exception as e: 67 print("apply_patch error!") 68 return 69 70 71def do_patch(target_dir): 72 patch_file = [ 73 "backport-CVE-2023-2004.patch", 74 "backport-freetype-2.2.1-enable-valid.patch", 75 "backport-freetype-2.3.0-enable-spr.patch", 76 "backport-freetype-2.6.5-libtool.patch", 77 "backport-freetype-2.8-multilib.patch", 78 "backport-freetype-2.10.0-internal-outline.patch", 79 "backport-freetype-2.10.1-debughook.patch", 80 "backport-freetype-2.12.1-enable-funcs.patch", 81 "backport-Minimal-stop-gap-fix-for-CVE-2025-27363.patch" 82 ] 83 84 for patch in patch_file: 85 apply_patch(patch, target_dir) 86 87 88def main(): 89 freetype_path = argparse.ArgumentParser() 90 freetype_path.add_argument('--gen-dir', help='generate path of log', required=True) 91 freetype_path.add_argument('--source-dir', help='generate path of log', required=True) 92 args = freetype_path.parse_args() 93 tar_file_path = os.path.join(args.source_dir, "freetype-2.12.1.tar.xz") 94 target_dir = os.path.join(args.gen_dir, "freetype") 95 target_include_dir = os.path.join(target_dir, "include") 96 97 untar_file(tar_file_path, target_dir) 98 move_file(args.source_dir, target_dir) 99 move_include(args.source_dir, target_include_dir) 100 do_patch(target_dir) 101 return 0 102 103if __name__ == '__main__': 104 sys.exit(main()) 105