• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3#
4# Copyright (c) 2020 Huawei Device Co., Ltd.
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18import os
19import sys
20import subprocess
21import argparse
22import shlex
23from tempfile import NamedTemporaryFile
24from shutil import copyfile
25from datetime import datetime
26
27
28def cmd_exec(command, temp_file, error_log_path):
29    start_time = datetime.now().replace(microsecond=0)
30    cmd = shlex.split(command)
31
32    proc = subprocess.Popen(cmd,
33                            stdout=temp_file,
34                            stderr=temp_file,
35                            encoding='utf-8')
36
37    proc.wait()
38    ret_code = proc.returncode
39    if ret_code != 0:
40        temp_file.close()
41        copyfile(temp_file.name, error_log_path)
42        os.remove(temp_file.name)
43        return ret_code
44
45    end_time = datetime.now().replace(microsecond=0)
46    temp_file.write(f'cmd:{command}\ncost time:{end_time-start_time}\n')
47    return ret_code
48
49
50def main():
51    parser = argparse.ArgumentParser()
52    parser.add_argument('--path', help='Build path.')
53    parser.add_argument('--prebuilts', help='Build prebuilts.')
54    parser.add_argument('--command', help='Build command.')
55    parser.add_argument('--enable', help='enable python.', nargs='*')
56    parser.add_argument('--target_dir', nargs=1)
57    parser.add_argument('--out_dir', nargs=1)
58    args = parser.parse_args()
59
60    if args.enable:
61        if args.enable[0] == 'false':
62            return 0
63
64    if args.path:
65        curr_dir = os.getcwd()
66        os.chdir(args.path)
67        temp_file = NamedTemporaryFile(mode='wt', delete=False)
68        if args.prebuilts:
69            status = cmd_exec(args.prebuilts, temp_file, args.out_dir[0])
70            if status != 0:
71                return status
72        if args.command:
73            if '&&' in args.command:
74                command = args.command.split('&&')
75                for data in command:
76                    status = cmd_exec(data, temp_file, args.out_dir[0])
77                    if status != 0:
78                        return status
79            else:
80                status = cmd_exec(args.command, temp_file, args.out_dir[0])
81                if status != 0:
82                    return status
83        temp_file.close()
84        copyfile(temp_file.name, args.target_dir[0])
85        os.remove(temp_file.name)
86
87        os.chdir(curr_dir)
88    return 0
89
90
91if __name__ == '__main__':
92    sys.exit(main())
93