146407a4bSopenharmony_ci#!/usr/bin/env python
246407a4bSopenharmony_ci# -*- coding: utf-8 -*-
346407a4bSopenharmony_ci# Copyright (c) 2023 Huawei Device Co., Ltd.
446407a4bSopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
546407a4bSopenharmony_ci# you may not use this file except in compliance with the License.
646407a4bSopenharmony_ci# You may obtain a copy of the License at
746407a4bSopenharmony_ci#
846407a4bSopenharmony_ci#     http://www.apache.org/licenses/LICENSE-2.0
946407a4bSopenharmony_ci#
1046407a4bSopenharmony_ci# Unless required by applicable law or agreed to in writing, software
1146407a4bSopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS,
1246407a4bSopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1346407a4bSopenharmony_ci# See the License for the specific language governing permissions and
1446407a4bSopenharmony_ci# limitations under the License.
1546407a4bSopenharmony_ci
1646407a4bSopenharmony_ciimport tarfile
1746407a4bSopenharmony_ciimport argparse
1846407a4bSopenharmony_ciimport os
1946407a4bSopenharmony_ciimport subprocess
2046407a4bSopenharmony_ciimport sys
2146407a4bSopenharmony_ciimport shutil
2246407a4bSopenharmony_ci
2346407a4bSopenharmony_ci
2446407a4bSopenharmony_cidef untar_file(tar_file_path, extract_path):
2546407a4bSopenharmony_ci    try:
2646407a4bSopenharmony_ci        tar_cmd = ['tar', '-xvf', tar_file_path, '-C', extract_path, '--strip-components=1']
2746407a4bSopenharmony_ci        subprocess.run(tar_cmd, check=True)
2846407a4bSopenharmony_ci    except Exception as e:
2946407a4bSopenharmony_ci        print("tar error!")
3046407a4bSopenharmony_ci        return
3146407a4bSopenharmony_ci
3246407a4bSopenharmony_ci
3346407a4bSopenharmony_cidef move_file(src_path, dst_path):
3446407a4bSopenharmony_ci    files = [
3546407a4bSopenharmony_ci        "backport-CVE-2023-2004.patch",
3646407a4bSopenharmony_ci        "backport-freetype-2.2.1-enable-valid.patch",
3746407a4bSopenharmony_ci        "backport-freetype-2.3.0-enable-spr.patch",
3846407a4bSopenharmony_ci        "backport-freetype-2.6.5-libtool.patch",
3946407a4bSopenharmony_ci        "backport-freetype-2.8-multilib.patch",
4046407a4bSopenharmony_ci        "backport-freetype-2.10.0-internal-outline.patch",
4146407a4bSopenharmony_ci        "backport-freetype-2.10.1-debughook.patch",
4246407a4bSopenharmony_ci        "backport-freetype-2.12.1-enable-funcs.patch",
4346407a4bSopenharmony_ci        "ftconfig.h"
4446407a4bSopenharmony_ci    ]
4546407a4bSopenharmony_ci    for file in files:
4646407a4bSopenharmony_ci        src_file = os.path.join(src_path, file)
4746407a4bSopenharmony_ci        dst_file = os.path.join(dst_path, file)
4846407a4bSopenharmony_ci        shutil.copy(src_file, dst_file)
4946407a4bSopenharmony_ci
5046407a4bSopenharmony_cidef move_include(src_path, dst_path):
5146407a4bSopenharmony_ci    try:
5246407a4bSopenharmony_ci        cp_cmd = ['cp', '-rf', dst_path, src_path]
5346407a4bSopenharmony_ci        subprocess.run(cp_cmd, check=True)
5446407a4bSopenharmony_ci    except Exception as e:
5546407a4bSopenharmony_ci        print("cp -rf error!")
5646407a4bSopenharmony_ci        return
5746407a4bSopenharmony_ci
5846407a4bSopenharmony_ci
5946407a4bSopenharmony_cidef apply_patch(patch_file, target_dir):
6046407a4bSopenharmony_ci    try:
6146407a4bSopenharmony_ci        if not os.path.exists(target_dir):
6246407a4bSopenharmony_ci            return
6346407a4bSopenharmony_ci        patch_cmd = ['patch', '-p1', "--fuzz=0", "--no-backup-if-mismatch", '-i', patch_file, '-d', target_dir]
6446407a4bSopenharmony_ci        subprocess.run(patch_cmd, check=True)
6546407a4bSopenharmony_ci    except Exception as e:
6646407a4bSopenharmony_ci        print("apply_patch error!")
6746407a4bSopenharmony_ci        return
6846407a4bSopenharmony_ci
6946407a4bSopenharmony_ci
7046407a4bSopenharmony_cidef do_patch(target_dir):
7146407a4bSopenharmony_ci    patch_file = [
7246407a4bSopenharmony_ci        "backport-CVE-2023-2004.patch",
7346407a4bSopenharmony_ci        "backport-freetype-2.2.1-enable-valid.patch",
7446407a4bSopenharmony_ci        "backport-freetype-2.3.0-enable-spr.patch",
7546407a4bSopenharmony_ci        "backport-freetype-2.6.5-libtool.patch",
7646407a4bSopenharmony_ci        "backport-freetype-2.8-multilib.patch",
7746407a4bSopenharmony_ci        "backport-freetype-2.10.0-internal-outline.patch",
7846407a4bSopenharmony_ci        "backport-freetype-2.10.1-debughook.patch",
7946407a4bSopenharmony_ci        "backport-freetype-2.12.1-enable-funcs.patch"
8046407a4bSopenharmony_ci    ]
8146407a4bSopenharmony_ci
8246407a4bSopenharmony_ci    for patch in patch_file:
8346407a4bSopenharmony_ci        apply_patch(patch, target_dir)
8446407a4bSopenharmony_ci
8546407a4bSopenharmony_ci
8646407a4bSopenharmony_cidef main():
8746407a4bSopenharmony_ci    freetype_path = argparse.ArgumentParser()
8846407a4bSopenharmony_ci    freetype_path.add_argument('--gen-dir', help='generate path of log', required=True)
8946407a4bSopenharmony_ci    freetype_path.add_argument('--source-dir', help='generate path of log', required=True)
9046407a4bSopenharmony_ci    args = freetype_path.parse_args()
9146407a4bSopenharmony_ci    tar_file_path = os.path.join(args.source_dir, "freetype-2.12.1.tar.xz")
9246407a4bSopenharmony_ci    target_dir = os.path.join(args.gen_dir, "freetype")
9346407a4bSopenharmony_ci    target_include_dir = os.path.join(target_dir, "include")
9446407a4bSopenharmony_ci
9546407a4bSopenharmony_ci    untar_file(tar_file_path, target_dir)
9646407a4bSopenharmony_ci    move_file(args.source_dir, target_dir)
9746407a4bSopenharmony_ci    move_include(args.source_dir, target_include_dir)
9846407a4bSopenharmony_ci    do_patch(target_dir)
9946407a4bSopenharmony_ci    return 0
10046407a4bSopenharmony_ci
10146407a4bSopenharmony_ciif __name__ == '__main__':
10246407a4bSopenharmony_ci    sys.exit(main())
103