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 sys
17import argparse
18import subprocess
19import os
20
21
22def args_parse(argv):
23    parser = argparse.ArgumentParser(description='mkchip_ckm.py')
24
25    parser.add_argument("--config-file-path", help="The source file for sload.")
26    parser.add_argument("--src-dir", help="The source file for sload.")
27    parser.add_argument("--device-name", help="The device for mkfs.")
28    parser.add_argument("--mkextimage-tools-path", help="The device for mkfs.")
29    parser.add_argument("--build-image-tools-path", nargs='*', help="The device for mkfs.")
30    parser.add_argument("--root-config-list-json", help="The device for mkfs.")
31    args = parser.parse_known_args(argv)[0]
32    return args
33
34
35def run_cmd(cmd: str):
36    res = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE,
37                           stderr=subprocess.PIPE)
38    sout, serr = res.communicate()
39
40    return res.pid, res.returncode, sout, serr
41
42
43def make_vendor_package(args):
44    src_dir = args.src_dir
45    device_name = args.device_name
46    mkextimage_tools_path = args.mkextimage_tools_path
47    config_file_path = args.config_file_path
48    mk_configs_list = []
49    with open(args.config_file_path, 'r') as f:
50        for line in f:
51            line = line.strip()
52            if not line or line.startswith('#'):
53                continue
54            mk_configs_list.append(line)
55    mk_configs = " ".join(mk_configs_list)
56    mk_configs = " ".join([src_dir, device_name, mk_configs])
57    res = run_cmd(" ".join([mkextimage_tools_path, mk_configs]))
58    if res[1]:
59        print("pid " + str(res[0]) + " ret " + str(res[1]) + "\n" +
60                res[2].decode() + res[3].decode())
61        sys.exit(2)
62
63
64def build(args):
65    args = args_parse(args)
66    if args.build_image_tools_path:
67        env_path = ':'.join(args.build_image_tools_path)
68        os.environ['PATH'] = '{}:{}'.format(env_path, os.environ.get('PATH'))
69    make_vendor_package(args)
70
71if __name__ == '__main__':
72    build(sys.argv[1:])
73