-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.py
executable file
·91 lines (83 loc) · 3.44 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/python3
import sys
import re
import logging
from typing import Callable, List
from interfaces import UIState, ConnectionIDSink, CommandSink
from core import matcher, ConnectionManager
from core.util import check_gdb, set_color_output, set_verbose, color
from core.wl import protocol
from frontends.tui import Controller, TerminalUI, parse_args, Arguments, Mode
from backends.libwayland_debug_output import parse, run_program
from backends import gdb_plugin
from core.output import stream, Output
logging.basicConfig()
set_verbose(False)
if sys.version_info[0] < 3 or sys.version_info[1] < 8:
logging.error('Needs at least Python 3.8!')
def piped_input_main(output: Output, connection_id_sink: ConnectionIDSink) -> None:
logging.info('Getting input piped from stdin')
parse.into_sink(sys.stdin, output, connection_id_sink)
logging.info('Done with input')
def file_input_main(
file_path: str,
output: Output,
connection_id_sink: ConnectionIDSink,
command_sink: CommandSink,
ui_state: UIState,
input_func: Callable[[str], str]
) -> None:
ui = TerminalUI(command_sink, ui_state, input_func)
logging.info('Opening ' + file_path)
try:
input_file = open(file_path)
parse.into_sink(input_file, output, connection_id_sink)
input_file.close()
except FileNotFoundError:
output.error(file_path + ' not found')
ui.run_until_stopped()
logging.info('Done with file')
def main(args: Arguments, output: Output, input_func: Callable[[str], str]) -> None:
# If we want to run inside GDB, the rest of main does not get called in this instance of the script
# Instead GDB is run, an instance of wayland-debug is run inside it and main() is run in that
if args.mode == Mode.GDB_RUNNER:
try:
gdb_plugin.run_gdb(args, False)
except RuntimeError as e:
logging.error(e)
else:
protocol.load_all(output)
connection_list = ConnectionManager()
ui_controller = Controller(output, connection_list, args.filter_matcher, args.stop_matcher)
if args.mode == Mode.GDB_PLUGIN:
try:
gdb_plugin.plugin.Plugin(output, connection_list, ui_controller, ui_controller)
except:
import traceback
traceback.print_exc()
elif args.mode == Mode.LOAD_FROM_FILE:
file_input_main(args.load_path, output, connection_list, ui_controller, ui_controller, input_func)
elif args.mode == Mode.PIPE:
if args.stop_matcher != matcher.never:
output.warn('Ignoring stop matcher when stdin is used for messages')
piped_input_main(output, connection_list)
elif args.mode == Mode.RUN:
returncode = run_program(output, args, connection_list, ui_controller, ui_controller, input_func)
exit(returncode)
else:
assert False, 'invalid mode ' + repr(args.mode)
if __name__ == '__main__':
if check_gdb():
out_stream, err_stream = gdb_plugin.plugin.output_streams()
else:
out_stream = stream.Std(sys.stdout)
err_stream = stream.Std(sys.stderr)
try:
args = parse_args(sys.argv)
set_color_output(args.show_color)
set_verbose(args.show_verbose)
output = Output(args.show_verbose, args.show_unprocessed_output, out_stream, err_stream)
main(args, output, input)
except RuntimeError as e:
logging.error(e)
exit(1)