blob: a8e357f7aa1fb019e04809285b53347771a69c23 [file] [log] [blame]
James Feist7248e962020-09-10 11:10:47 -07001#!/usr/bin/python3
2
3# SPDX-License-Identifier: Apache-2.0
4# Copyright 2020 Intel Corp.
5
James Feist7248e962020-09-10 11:10:47 -07006import argparse
Patrick Williamsa3db66b2022-12-04 16:27:08 -06007import subprocess
James Feist7248e962020-09-10 11:10:47 -07008
Patrick Williamsa3db66b2022-12-04 16:27:08 -06009from flask import Flask
10
11REPLACE_CHAR = "~"
James Feist7248e962020-09-10 11:10:47 -070012
13app = Flask(__name__)
14
Patrick Williamsa3db66b2022-12-04 16:27:08 -060015parser = 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)
James Feist7248e962020-09-10 11:10:47 -070020args = parser.parse_args()
21
Patrick Williamsa3db66b2022-12-04 16:27:08 -060022busctl = "sshpass -p {} busctl -H {}@{} ".format(
23 args.password, args.username, args.address
24)
25AVATAR_URL = "https://avatars1.githubusercontent.com/u/13670043?s=200&v=4"
26header = f'<head><link rel="icon" href="{AVATAR_URL}" /></head>'
James Feist7248e962020-09-10 11:10:47 -070027
28
29def getBusNames():
Patrick Williamsa3db66b2022-12-04 16:27:08 -060030 out = subprocess.check_output(busctl + "list --acquired", shell=True)
31 out = out.split(b"\n")
James Feist7248e962020-09-10 11:10:47 -070032 out = out[1:]
33 names = []
34 for line in out:
Patrick Williamsa3db66b2022-12-04 16:27:08 -060035 name = line.split(b" ")[0]
James Feist7248e962020-09-10 11:10:47 -070036 if name:
37 names.append(name.decode())
38 return names
39
40
41def doTree(busname):
Patrick Williamsa3db66b2022-12-04 16:27:08 -060042 out = subprocess.check_output(busctl + "tree " + busname, shell=True)
43 out = out.split(b"\n")
James Feist7248e962020-09-10 11:10:47 -070044 tree = []
45 for line in out:
Patrick Williamsa3db66b2022-12-04 16:27:08 -060046 path = line.split(b"/", 1)[-1].decode()
47 path = "/" + path
James Feist7248e962020-09-10 11:10:47 -070048 tree.append(path)
49 return tree
50
51
52def doIntrospect(busname, path):
53 out = subprocess.check_output(
Patrick Williamsa3db66b2022-12-04 16:27:08 -060054 busctl + "introspect {} {}".format(busname, path), shell=True
55 )
56 return out.decode().split("\n")
James Feist7248e962020-09-10 11:10:47 -070057
58
Patrick Williamsa3db66b2022-12-04 16:27:08 -060059@app.route("/")
James Feist7248e962020-09-10 11:10:47 -070060def root():
61 out = header
Patrick Williamsa3db66b2022-12-04 16:27:08 -060062 out += "<div><H2>Bus Names {}</H2></div>".format(args.address)
James Feist7248e962020-09-10 11:10:47 -070063 for name in getBusNames():
Patrick Williamsa3db66b2022-12-04 16:27:08 -060064 out += "<div> "
James Feist7248e962020-09-10 11:10:47 -070065 out += '<a href="{}"> {} </a>'.format(name, name)
Patrick Williamsa3db66b2022-12-04 16:27:08 -060066 out += "</div>"
James Feist7248e962020-09-10 11:10:47 -070067 return out
68
69
Patrick Williamsa3db66b2022-12-04 16:27:08 -060070@app.route("/favicon.ico")
James Feist7248e962020-09-10 11:10:47 -070071def favicon():
Patrick Williamsa3db66b2022-12-04 16:27:08 -060072 return f'<link rel="icon" type="image/png" href="{AVATAR_URL}" />'
James Feist7248e962020-09-10 11:10:47 -070073
74
Patrick Williamsa3db66b2022-12-04 16:27:08 -060075@app.route("/<name>")
James Feist7248e962020-09-10 11:10:47 -070076def busname(name):
77 out = header
Patrick Williamsa3db66b2022-12-04 16:27:08 -060078 out += "<div><H2>tree {}</H2></div>".format(name)
James Feist7248e962020-09-10 11:10:47 -070079 for path in doTree(name):
Patrick Williamsa3db66b2022-12-04 16:27:08 -060080 out += "<div> "
James Feist7248e962020-09-10 11:10:47 -070081 out += '<a href="{}/{}"> {} </a>'.format(
Patrick Williamsa3db66b2022-12-04 16:27:08 -060082 name, path.replace("/", REPLACE_CHAR), path
83 )
84 out += "</div>"
James Feist7248e962020-09-10 11:10:47 -070085 return out
86
87
Patrick Williamsa3db66b2022-12-04 16:27:08 -060088@app.route("/<name>/<path>")
James Feist7248e962020-09-10 11:10:47 -070089def path(name, path):
Patrick Williamsa3db66b2022-12-04 16:27:08 -060090 path = path.replace(REPLACE_CHAR, "/")
James Feist7248e962020-09-10 11:10:47 -070091 out = header
Patrick Williamsa3db66b2022-12-04 16:27:08 -060092 out += "<div><H2>introspect {} {}</H2></div>".format(name, path)
James Feist7248e962020-09-10 11:10:47 -070093 for line in doIntrospect(name, path):
Patrick Williamsa3db66b2022-12-04 16:27:08 -060094 out += "<div> "
95 out += "<pre> {} </pre>".format(line)
96 out += "</div>"
James Feist7248e962020-09-10 11:10:47 -070097 return out
98
99
Patrick Williamsa3db66b2022-12-04 16:27:08 -0600100app.run(port=args.port, host="0.0.0.0")