• 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 argparse
17import os
18import subprocess
19import sys
20
21THIS_FILE_PATH = os.path.dirname(os.path.abspath(__file__))
22
23def untar_file(tar_file_path, extract_path, extract_dir_path):
24    try:
25        if os.path.exists(extract_path):
26            rm_cmd = ['rm', '-rf', extract_path]
27            subprocess.run(rm_cmd, check=True)
28
29        tar_cmd = ['tar', '-zxvf', tar_file_path, '-C', extract_dir_path]
30        print("tar_cmd:{}".format(tar_cmd))
31        subprocess.run(tar_cmd, check=True)
32
33    except Exception as e:
34        print("tar error!")
35        return
36
37
38def apply_patch(patch_file, target_dir):
39    try:
40        if not os.path.exists(target_dir):
41            return
42
43        patch_cmd = ['patch', '-p1', "--fuzz=0", "--no-backup-if-mismatch", '-i', patch_file, '-d', target_dir]
44        print("patch_cmd:{}".format(patch_cmd))
45        subprocess.run(patch_cmd, check=True)
46
47    except Exception as e:
48        print("apply_patch error!")
49        return
50
51
52def do_patch(args, target_dir):
53    patch_file = [
54        "Fix error whenparses the value of 5E-324 with libc++.patch",
55        "0001-Parse-large-floats-as-infinity-1349-1353.patch",
56        "0001-Use-default-rather-than-hard-coded-8-for-maximum-agg.patch",
57        "Fix out-of-bounds read.patch"
58    ]
59
60    for patch in patch_file:
61        file_path = os.path.join(args.source_file, patch)
62        apply_patch(file_path, target_dir)
63
64def do_copy(source_dir, target_dir):
65    try:
66        cp_cmd = ["cp", "-rf", source_dir, target_dir]
67        print("cp_cmd:{}".format(cp_cmd))
68        subprocess.run(cp_cmd, check=True)
69    except Exception as e:
70        print("copy error!")
71        return
72def do_remove(target_dir):
73    try:
74        rm_cmd = ["rm", "-rf", target_dir]
75        subprocess.run(rm_cmd, check=True)
76    except Exception as e:
77        print("remove dir:%s error!" % target_dir)
78        return
79
80def main():
81    libpng_path = argparse.ArgumentParser()
82    libpng_path.add_argument('--gen-dir', help='generate path of jsoncpp')
83    libpng_path.add_argument('--source-file', help='jsoncpp source compressed dir')
84    args = libpng_path.parse_args()
85    tar_file_path = os.path.join(args.source_file, "jsoncpp-1.9.5.tar.gz")
86    tmp_dir = os.path.join(THIS_FILE_PATH, "jsoncpp-1.9.5")
87    untar_file(tar_file_path, tmp_dir, THIS_FILE_PATH)
88    do_patch(args, tmp_dir)
89    do_copy(tmp_dir, args.gen_dir)
90    do_remove(tmp_dir)
91    return 0
92
93
94if __name__ == '__main__':
95    sys.exit(main())
96