1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3
4#
5# Copyright (c) 2024 Huawei Device Co., Ltd.
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10#     http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18
19
20import argparse
21import os
22import subprocess
23import sys
24
25from src.content_parser import ContentParser
26from src.graph.graph_converter import generate_all_graphs
27from src.pre_process import handle_file_preprocess
28from src.utils.log_wrapper import log_info, log_error, enable_debug
29
30
31def usage():
32    print('python main.py -i input.txt')
33    print('\n Usage: main.py <cmd> <input>')
34    print('         <cmd>: TODO')
35    print('         <input>: input dump file')
36    return
37
38
39def parse_args():
40    parser = argparse.ArgumentParser(description='')
41    parser.add_argument('-d', action='store_true', default=False, help='enable debug info')
42    parser.add_argument('-r', action='store_true', default=False, help='dump event tree with device')
43    parser.add_argument('-i',
44                        type=str, default='./resources/dumpfile/input.txt', help='input the dump source file')
45    parser.add_argument('-m', action='store_true', default=False, help='add details info')
46    argument = parser.parse_args()
47    argument.input_file = argument.i
48    argument.detailed = argument.m
49    argument.dump_from_device = argument.r
50    argument.debug = argument.d
51    return argument
52
53
54def dump_from_device():
55    bat_file_path = r'src\bats\dump_event.bat'
56    try:
57        subprocess.call([bat_file_path])
58        print('capture event tree done.')
59    except FileNotFoundError:
60        print(f'file not found: {bat_file_path}')
61    except Exception as e:
62        print(f'exception: {e}')
63
64
65# python main.py -i input.txt
66if __name__ == '__main__':
67    # parse the args
68    args = parse_args()
69    # config log model
70    if args.debug:
71        enable_debug(True)
72    # dump trace from device if needed
73    if args.dump_from_device:
74        if os.name == 'nt':
75            dump_from_device()
76        else:
77            log_error('only support dump from device on windows')
78            sys.exit(1)
79    # pre process
80    handle_file_preprocess(args.input_file, 'dump_temp.txt')
81    # read the dump file and parse
82    dump_result = ContentParser('dump_temp.txt').do_parse()
83    if dump_result.is_succeed():
84        log_info('parse done')
85        dump_result.dump()
86    else:
87        log_error('parse failed')
88    generate_all_graphs(dump_result, args.detailed)
89