1#!/usr/bin/python3
2
3# SPDX-License-Identifier: Apache-2.0
4# Copyright 2020 Intel Corp.
5
6import subprocess
7from flask import Flask
8from flask import send_file
9import argparse
10
11REPLACE_CHAR = '~'
12
13app = Flask(__name__)
14
15parser = argparse.ArgumentParser(description='Remote DBus Viewer')
16parser.add_argument('-u', '--username', default='root')
17parser.add_argument('-p', '--password', default='0penBmc')
18parser.add_argument('-a', '--address', required=True)
19parser.add_argument('-x', '--port', required=True)
20args = parser.parse_args()
21
22busctl = 'sshpass -p {} busctl -H {}@{} '.format(
23    args.password, args.username, args.address)
24header = '<head><link rel="icon" href="https://avatars1.githubusercontent.com/u/13670043?s=200&v=4" /></head>'
25
26
27def getBusNames():
28    out = subprocess.check_output(busctl + 'list --acquired', shell=True)
29    out = out.split(b'\n')
30    out = out[1:]
31    names = []
32    for line in out:
33        name = line.split(b' ')[0]
34        if name:
35            names.append(name.decode())
36    return names
37
38
39def doTree(busname):
40    out = subprocess.check_output(busctl + 'tree ' + busname, shell=True)
41    out = out.split(b'\n')
42    tree = []
43    for line in out:
44        path = line.split(b'/', 1)[-1].decode()
45        path = '/' + path
46        tree.append(path)
47    return tree
48
49
50def doIntrospect(busname, path):
51    out = subprocess.check_output(
52        busctl + 'introspect {} {}'.format(busname, path), shell=True)
53    return out.decode().split('\n')
54
55
56@app.route('/')
57def root():
58    out = header
59    out += '<div><H2>Bus Names {}</H2></div>'.format(args.address)
60    for name in getBusNames():
61        out += '<div> '
62        out += '<a href="{}"> {} </a>'.format(name, name)
63        out += '</div>'
64    return out
65
66
67@app.route('/favicon.ico')
68def favicon():
69    return '<link rel="icon" type="image/png" href="https://avatars1.githubusercontent.com/u/13670043?s=200&v=4" />'
70
71
72@app.route('/<name>')
73def busname(name):
74    out = header
75    out += '<div><H2>tree {}</H2></div>'.format(name)
76    for path in doTree(name):
77        out += '<div> '
78        out += '<a href="{}/{}"> {} </a>'.format(
79            name, path.replace('/', REPLACE_CHAR), path)
80        out += '</div>'
81    return out
82
83
84@app.route('/<name>/<path>')
85def path(name, path):
86    path = path.replace(REPLACE_CHAR, '/')
87    out = header
88    out += '<div><H2>introspect {} {}</H2></div>'.format(name, path)
89    for line in doIntrospect(name, path):
90        out += '<div> '
91        out += '<pre> {} </pre>'.format(line)
92        out += '</div>'
93    return out
94
95
96app.run(port=args.port, host='0.0.0.0')
97