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 21 22def untar_file(tar_file_path, extract_path, args): 23 try: 24 if os.path.exists(extract_path): 25 rm_cmd = ['rm', '-rf', extract_path] 26 subprocess.run(rm_cmd, check=True) 27 28 tar_cmd = ['tar', '-zxvf', tar_file_path, '-C', args.gen_dir] 29 subprocess.run(tar_cmd, check=True) 30 31 except Exception as e: 32 print("tar error!") 33 return 34 35 36def apply_patch(patch_file, target_dir): 37 try: 38 if not os.path.exists(target_dir): 39 return 40 41 patch_cmd = ['patch', '-p1', "--fuzz=0", "--no-backup-if-mismatch", '-i', patch_file, '-d', target_dir] 42 subprocess.run(patch_cmd, check=True) 43 44 except Exception as e: 45 print("apply_patch error!") 46 return 47 48 49def do_patch(args, target_dir): 50 patch_file = [ 51 "Fix error whenparses the value of 5E-324 with libc++.patch", 52 "0001-Parse-large-floats-as-infinity-1349-1353.patch", 53 "0001-Use-default-rather-than-hard-coded-8-for-maximum-agg.patch", 54 "Fix out-of-bounds read.patch" 55 ] 56 57 for patch in patch_file: 58 file_path = os.path.join(args.source_file, patch) 59 apply_patch(file_path, target_dir) 60 61 62def main(): 63 libpng_path = argparse.ArgumentParser() 64 libpng_path.add_argument('--gen-dir', help='generate path of jsoncpp') 65 libpng_path.add_argument('--source-file', help='jsoncpp source compressed dir') 66 args = libpng_path.parse_args() 67 tar_file_path = os.path.join(args.source_file, "jsoncpp-1.9.5.tar.gz") 68 target_dir = os.path.join(args.gen_dir, "jsoncpp-1.9.5") 69 untar_file(tar_file_path, target_dir, args) 70 do_patch(args, target_dir) 71 return 0 72 73 74if __name__ == '__main__': 75 sys.exit(main()) 76