f8187ce05e1ccd01e4c4acb43c0ce13b750129b8
Stefan Schuermans upt-dump -> upt-tool, add p...

Stefan Schuermans authored 4 years ago

1) """
2) Command line interface of UProcTrace: "upt-tool".
3) """
4) 
5) import argparse
Stefan Schuermans implement pstree

Stefan Schuermans authored 4 years ago

6) import shlex
Stefan Schuermans upt-dump -> upt-tool, add p...

Stefan Schuermans authored 4 years ago

7) import sys
Stefan Schuermans implement pstree

Stefan Schuermans authored 4 years ago

8) 
Stefan Schuermans upt-dump -> upt-tool, add p...

Stefan Schuermans authored 4 years ago

9) 
10) def dump(args):
11)     """
12)     Dump all events in trace file to standard output.
13)     """
Stefan Schuermans upt-gui -> upt-tool gui

Stefan Schuermans authored 4 years ago

14)     import uproctrace.dump
Stefan Schuermans upt-dump -> upt-tool, add p...

Stefan Schuermans authored 4 years ago

15)     with open(args.trace, 'rb') as proto_file:
16)         while uproctrace.dump.dump_event(proto_file, sys.stdout):
17)             pass
18) 
19) 
Stefan Schuermans upt-gui -> upt-tool gui

Stefan Schuermans authored 4 years ago

20) def gui(args):
21)     """
22)     Run the graphical user interface.
23)     """
24)     import uproctrace.gui
25)     uproctrace.gui.run(args.trace)
26) 
27) 
Stefan Schuermans implement pstree

Stefan Schuermans authored 4 years ago

28) def pstree(args):
Stefan Schuermans upt-dump -> upt-tool, add p...

Stefan Schuermans authored 4 years ago

29)     """
Stefan Schuermans implement pstree

Stefan Schuermans authored 4 years ago

30)     Print process tree.
Stefan Schuermans upt-dump -> upt-tool, add p...

Stefan Schuermans authored 4 years ago

31)     """
Stefan Schuermans upt-gui -> upt-tool gui

Stefan Schuermans authored 4 years ago

32)     import uproctrace.processes
Stefan Schuermans upt-dump -> upt-tool, add p...

Stefan Schuermans authored 4 years ago

33)     with open(args.trace, 'rb') as proto_file:
Stefan Schuermans implement pstree

Stefan Schuermans authored 4 years ago

34)         processes = uproctrace.processes.Processes(proto_file)
35)     # tree output (iterative)
36)     to_be_output = [processes.toplevel]
37)     while to_be_output:
38)         procs = to_be_output[-1]
39)         if not procs:
40)             del to_be_output[-1]
41)             continue
42)         indent = '  ' * (len(to_be_output) - 1)
43)         proc = procs[0]
44)         del procs[0]
45)         cmdline = proc.cmdline
46)         if cmdline is None:
47)             cmdline_str = '???'
48)         else:
49)             cmdline_str = ' '.join([shlex.quote(s) for s in cmdline])
50)         print(indent + cmdline_str)
51)         to_be_output.append(proc.children)
Stefan Schuermans upt-dump -> upt-tool, add p...

Stefan Schuermans authored 4 years ago

52) 
53) 
54) def parse_args():
55)     """
56)     Parse command line arguments.
57)     """
58)     # set up main parser
Stefan Schuermans implement pstree

Stefan Schuermans authored 4 years ago

59)     parser = argparse.ArgumentParser(description='UProcTrace tool.')
Stefan Schuermans upt-dump -> upt-tool, add p...

Stefan Schuermans authored 4 years ago

60)     parser.add_argument('trace', help='trace file')
61)     subparsers = parser.add_subparsers()
62)     # dump
Stefan Schuermans implement pstree

Stefan Schuermans authored 4 years ago

63)     dump_parser = subparsers.add_parser('dump', help='Dump events to stdout.')
Stefan Schuermans upt-dump -> upt-tool, add p...

Stefan Schuermans authored 4 years ago

64)     dump_parser.set_defaults(func=dump)
Stefan Schuermans upt-gui -> upt-tool gui

Stefan Schuermans authored 4 years ago

65)     # gui
66)     gui_parser = subparsers.add_parser('gui',
67)                                        help='Run graphical user interface.')
68)     gui_parser.set_defaults(func=gui)
Stefan Schuermans implement pstree

Stefan Schuermans authored 4 years ago

69)     # pstree
70)     pstree_parser = subparsers.add_parser('pstree', help='Print process tree.')
71)     pstree_parser.set_defaults(func=pstree)