• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
32
33def move_file(src_path, dst_path):
34    files = [
35        "backport-avoid-random-test-failure.patch",
36        "CVE-2019-6129.patch",
37        "huawei_libpng_CMakeList.patch",
38        "libpng-fix-arm-neon.patch",
39        "libpng-multilib.patch",
40        "pnglibconf.h"
41    ]
42    for file in files:
43        src_file = os.path.join(src_path, file)
44        dst_file = os.path.join(dst_path, file)
45        shutil.copy(src_file, dst_file)
46
47
48def apply_patch(patch_file, target_dir):
49    try:
50        if not os.path.exists(target_dir):
51            return
52        patch_cmd = ['patch', '-p1', "--fuzz=0", "--no-backup-if-mismatch", '-i', patch_file, '-d', target_dir]
53        subprocess.run(patch_cmd, check=True)
54    except Exception as e:
55        print("apply_patch error!")
56        return
57
58
59def do_patch(target_dir):
60    patch_file = [
61        "backport-avoid-random-test-failure.patch",
62        "CVE-2019-6129.patch",
63        "huawei_libpng_CMakeList.patch",
64        "libpng-fix-arm-neon.patch",
65        "libpng-multilib.patch"
66    ]
67
68    for patch in patch_file:
69        apply_patch(patch, target_dir)
70
71
72def main():
73    libpng_path = argparse.ArgumentParser()
74    libpng_path.add_argument('--gen-dir', help='generate path of log', required=True)
75    libpng_path.add_argument('--source-dir', help='generate path of log', required=True)
76    args = libpng_path.parse_args()
77    tar_file_path = os.path.join(args.source_dir, "libpng-1.6.37.tar.gz")
78    target_dir = os.path.join(args.gen_dir, "libpng-1.6.37")
79
80    untar_file(tar_file_path, args.gen_dir)
81    move_file(args.source_dir, target_dir)
82    do_patch(target_dir)
83    return 0
84
85if __name__ == '__main__':
86    sys.exit(main())
87