• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
24from pathlib import Path
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
29from src.utils.log_wrapper import log_message
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('-m', action='store_true', default=False, help='add details info')
44    argument = parser.parse_args()
45    argument.detailed = argument.m
46    argument.dump_from_device = argument.r
47    argument.debug = argument.d
48    return argument
49
50def delete_file(file_path):
51    try:
52        os.remove(file_path)
53        log_message("delete file successful")
54    except OSError as e:
55        log_error("delete file error")
56
57def dump_from_device():
58    delete_file("dump_temp.txt")
59    delete_file("log.txt")
60    bat_file_path = r'src\bats\dump_event.bat'
61    try:
62        subprocess.call([bat_file_path])
63        print('capture event tree done.')
64    except FileNotFoundError:
65        print(f'file not found: {bat_file_path}')
66    except Exception as e:
67        print(f'exception: {e}')
68
69
70def isFileExist(file):
71    file_path = Path(file)
72    if file_path.is_file():
73        return False
74    else:
75        return True
76
77# python main.py -i input.txt
78if __name__ == '__main__':
79    # parse the args
80    args = parse_args()
81    # config log model
82    if args.debug:
83        enable_debug(True)
84    # dump trace from device if needed
85    if os.name == 'nt':
86        dump_from_device()
87    else:
88        log_error('only support dump from device on windows')
89        sys.exit(1)
90    file="./resources/dumpfile/input.txt"
91    dump_file="dump_temp.txt"
92    if isFileExist(dump_file):
93        handle_file_preprocess(file, 'dump_temp.txt')
94    try:
95        # read the dump file and parse
96        dump_result = ContentParser('dump_temp.txt').do_parse()
97        if dump_result.is_succeed():
98            log_info('parse done')
99            dump_result.dump()
100        else:
101            log_error('parse failed')
102        generate_all_graphs(dump_result, args.detailed)
103    except Exception as e:
104        log_error("read dump_tmp error")