• 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
55    for patch in patch_file:
56        file_path = os.path.join(args.source_file, patch)
57        apply_patch(file_path, target_dir)
58
59def do_copy(source_dir, target_dir):
60    try:
61        cp_cmd = ["cp", "-rf", source_dir, target_dir]
62        print("cp_cmd:{}".format(cp_cmd))
63        subprocess.run(cp_cmd, check=True)
64    except Exception as e:
65        print("copy error!")
66        return
67def do_remove(target_dir):
68    try:
69        rm_cmd = ["rm", "-rf", target_dir]
70        subprocess.run(rm_cmd, check=True)
71    except Exception as e:
72        print("remove dir:%s error!" % target_dir)
73        return
74
75def main():
76    libpng_path = argparse.ArgumentParser()
77    libpng_path.add_argument('--gen-dir', help='generate path of jsoncpp')
78    libpng_path.add_argument('--source-file', help='jsoncpp source compressed dir')
79    args = libpng_path.parse_args()
80    tar_file_path = os.path.join(args.source_file, "jsoncpp-1.9.6.tar.gz")
81    tmp_dir = os.path.join(THIS_FILE_PATH, "jsoncpp-1.9.6")
82    untar_file(tar_file_path, tmp_dir, THIS_FILE_PATH)
83    do_patch(args, tmp_dir)
84    do_copy(tmp_dir, args.gen_dir)
85    do_remove(tmp_dir)
86    return 0
87
88
89if __name__ == '__main__':
90    sys.exit(main())
91