Stefan Schuermans commited on 2026-03-04 20:34:56
Showing 2 changed files, with 32 additions and 9 deletions.
| ... | ... |
@@ -5,6 +5,9 @@ |
| 5 | 5 |
Process tree command line interface of UProcTrace: "upt-tool pstree". |
| 6 | 6 |
""" |
| 7 | 7 |
import argparse |
| 8 |
+import csv |
|
| 9 |
+import json |
|
| 10 |
+import sys |
|
| 8 | 11 |
import tabulate |
| 9 | 12 |
import uproctrace.formatting |
| 10 | 13 |
import uproctrace.processes |
| ... | ... |
@@ -29,11 +32,8 @@ def build( |
| 29 | 32 |
proc = procs[0] |
| 30 | 33 |
del procs[0] |
| 31 | 34 |
|
| 32 |
- # tree / indentation |
|
| 33 |
- if args.table: |
|
| 34 |
- indent = "--" * (len(to_be_output) - 1) + ">" |
|
| 35 |
- else: |
|
| 36 |
- indent = " " * (len(to_be_output) - 1) |
|
| 35 |
+ # tree level / indentation level |
|
| 36 |
+ indent = str(len(to_be_output) - 1) |
|
| 37 | 37 |
row = [indent] |
| 38 | 38 |
# PIDs |
| 39 | 39 |
if args.pids: |
| ... | ... |
@@ -68,8 +68,7 @@ def output(args: argparse.Namespace, rows: list[list[str]]) -> None: |
| 68 | 68 |
""" |
| 69 | 69 |
Output rows of pstree command. |
| 70 | 70 |
""" |
| 71 |
- if args.table: |
|
| 72 |
- headers = ["tree"] |
|
| 71 |
+ headers = ["tree level"] |
|
| 73 | 72 |
if args.pids: |
| 74 | 73 |
headers += ["proc_id", "pid", "ppid"] |
| 75 | 74 |
headers.append("cmdline")
|
| ... | ... |
@@ -83,9 +82,29 @@ def output(args: argparse.Namespace, rows: list[list[str]]) -> None: |
| 83 | 82 |
"filesys ops", |
| 84 | 83 |
"ctx switches", |
| 85 | 84 |
] |
| 85 |
+ |
|
| 86 |
+ if args.format == "table": |
|
| 87 |
+ headers[0] = "tree" |
|
| 88 |
+ for row in rows: |
|
| 89 |
+ row[0] = int(row[0]) * "--" + ">" |
|
| 86 | 90 |
print(tabulate.tabulate(rows, headers)) |
| 87 |
- else: |
|
| 91 |
+ return |
|
| 92 |
+ |
|
| 93 |
+ if args.format == "csv": |
|
| 94 |
+ wr = csv.writer(sys.stdout, delimiter=";", quoting=csv.QUOTE_ALL) |
|
| 95 |
+ wr.writerow(headers) |
|
| 96 |
+ for row in rows: |
|
| 97 |
+ wr.writerow(row) |
|
| 98 |
+ return |
|
| 99 |
+ |
|
| 100 |
+ if args.format == "json": |
|
| 101 |
+ data = {"headers": headers, "rows": rows}
|
|
| 102 |
+ print(json.dumps(data, indent=2)) |
|
| 103 |
+ return |
|
| 104 |
+ |
|
| 105 |
+ # default: plain |
|
| 88 | 106 |
for row in rows: |
| 107 |
+ row[0] = int(row[0]) * " " |
|
| 89 | 108 |
print(" ".join(row))
|
| 90 | 109 |
|
| 91 | 110 |
|
| ... | ... |
@@ -146,7 +146,11 @@ def parse_args(): |
| 146 | 146 |
help="show proc_id, pid, parent pid (in front of cmdline)", |
| 147 | 147 |
) |
| 148 | 148 |
pstree_parser.add_argument( |
| 149 |
- "--table", "-t", action="store_true", help="output in form of a table" |
|
| 149 |
+ "--format", |
|
| 150 |
+ "-f", |
|
| 151 |
+ choices=["plain", "table", "csv", "json"], |
|
| 152 |
+ default="plain", |
|
| 153 |
+ help="output format", |
|
| 150 | 154 |
) |
| 151 | 155 |
pstree_parser.set_defaults(func=pstree) |
| 152 | 156 |
|
| 153 | 157 |