Jian Zhang | 2302fa4 | 2024-03-21 23:58:13 +0800 | [diff] [blame^] | 1 | #!/bin/python |
| 2 | |
| 3 | import re |
| 4 | import sys |
| 5 | |
| 6 | from anytree import Node, RenderTree |
| 7 | |
| 8 | usage = """ |
| 9 | Usage: using local file or stdin to parse i2c bus info to tree |
| 10 | 1. ssh user@<ip> "i2cdetect -l" > i2c_bus_info.txt |
| 11 | 2. python tree.py i2c_bus_info.txt |
| 12 | |
| 13 | or |
| 14 | |
| 15 | ssh user@<ip> "i2cdetect -l" | python tree.py |
| 16 | """ |
| 17 | |
| 18 | node_dict = {} |
| 19 | root = Node("root") |
| 20 | |
| 21 | |
| 22 | def parse_line(line): |
| 23 | line = line.strip() |
| 24 | regex = re.compile(r"(i2c-\d+)\s+i2c\s+(.*)\s+I2C adapter") |
| 25 | m = regex.match(line) |
| 26 | if m: |
| 27 | bus = m.group(1) |
| 28 | name = m.group(2).strip() |
| 29 | return (bus, name) |
| 30 | else: |
| 31 | return None |
| 32 | |
| 33 | |
| 34 | def draw(line): |
| 35 | i2c = parse_line(line) |
| 36 | if not i2c: |
| 37 | return |
| 38 | |
| 39 | bus, name = i2c |
| 40 | |
| 41 | if "mux" not in name: |
| 42 | node_dict[bus] = Node(bus, parent=root) |
| 43 | else: |
| 44 | mux_regex = re.compile(r"i2c-(\d+)-mux \(chan_id (\d+)\)") |
| 45 | m = mux_regex.match(name) |
| 46 | if m: |
| 47 | i2c = m.group(1) |
| 48 | if "i2c-" + i2c in node_dict: |
| 49 | node_dict[bus] = Node(bus, parent=node_dict["i2c-" + i2c]) |
| 50 | |
| 51 | |
| 52 | def parse_from_file(file): |
| 53 | with open(file, "r") as f: |
| 54 | lines = f.readlines() |
| 55 | for line in lines: |
| 56 | draw(line) |
| 57 | |
| 58 | |
| 59 | def parse_from_stdin(): |
| 60 | lines = sys.stdin.readlines() |
| 61 | for line in lines: |
| 62 | draw(line) |
| 63 | |
| 64 | |
| 65 | def print_tree(): |
| 66 | for pre, fill, node in RenderTree(root): |
| 67 | print("%s%s" % (pre, node.name)) |
| 68 | |
| 69 | |
| 70 | if __name__ == "__main__": |
| 71 | if len(sys.argv) == 2: |
| 72 | if ( |
| 73 | sys.argv[1] == "-h" |
| 74 | or sys.argv[1] == "--help" |
| 75 | or sys.argv[1] == "help" |
| 76 | or sys.argv[1] == "?" |
| 77 | ): |
| 78 | print(usage) |
| 79 | sys.exit(0) |
| 80 | |
| 81 | try: |
| 82 | if len(sys.argv) == 2: |
| 83 | file = sys.argv[1] |
| 84 | parse_from_file(file) |
| 85 | else: |
| 86 | parse_from_stdin() |
| 87 | except Exception as e: |
| 88 | print(e) |
| 89 | print(usage) |
| 90 | sys.exit(1) |
| 91 | |
| 92 | print_tree() |