| 1 |
# /usr/bin/env python |
| 2 |
"""varlink cli tool |
| 3 |
|
| 4 |
Call with: |
| 5 |
$ python -m varlink.cli --help |
| 6 |
|
| 7 |
""" |
| 8 |
|
| 9 |
import argparse |
| 10 |
import json |
| 11 |
import shlex |
| 12 |
import socket |
| 13 |
import sys |
| 14 |
|
| 15 |
import varlink |
| 16 |
|
| 17 |
|
| 18 |
def varlink_call(args): |
| 19 |
deli = args.METHOD.rfind(".") |
| 20 |
if deli == -1: |
| 21 |
print("No method found", file=sys.stderr) |
| 22 |
sys.exit(1) |
| 23 |
|
| 24 |
method = args.METHOD[deli + 1 :] |
| 25 |
interface = args.METHOD[:deli] |
| 26 |
|
| 27 |
deli = interface.rfind("/") |
| 28 |
if deli != -1: |
| 29 |
address = interface[:deli] |
| 30 |
interface = interface[deli + 1 :] |
| 31 |
else: |
| 32 |
address = None |
| 33 |
|
| 34 |
def new_client(address): |
| 35 |
if address: |
| 36 |
client = varlink.Client.new_with_address(address) |
| 37 |
else: |
| 38 |
if args.activate: |
| 39 |
client = varlink.Client.new_with_activate(shlex.split(args.activate)) |
| 40 |
elif args.bridge: |
| 41 |
client = varlink.Client.new_with_bridge(shlex.split(args.bridge)) |
| 42 |
else: |
| 43 |
client = varlink.Client.new_with_resolved_interface(interface, args.resolver) |
| 44 |
return client |
| 45 |
|
| 46 |
with new_client(address) as client: |
| 47 |
got = False |
| 48 |
try: |
| 49 |
with client.open(interface) as con: |
| 50 |
out = { |
| 51 |
"method": interface + "." + method, |
| 52 |
"more": args.more, |
| 53 |
"parameters": json.loads(args.ARGUMENTS or "{}"), |
| 54 |
} |
| 55 |
con._send_message(json.dumps(out, cls=varlink.VarlinkEncoder).encode("utf-8")) |
| 56 |
more = True |
| 57 |
while more: |
| 58 |
(message, more) = con._next_varlink_message() |
| 59 |
if message: |
| 60 |
print(json.dumps(message, cls=varlink.VarlinkEncoder, indent=2, sort_keys=True)) |
| 61 |
got = True |
| 62 |
except varlink.VarlinkError as e: |
| 63 |
print(e, file=sys.stderr) |
| 64 |
except BrokenPipeError: |
| 65 |
if not got or args.more: |
| 66 |
print("Connection closed") |
| 67 |
sys.exit(1) |
| 68 |
|
| 69 |
|
| 70 |
def varlink_bridge(args): |
| 71 |
message = b"" |
| 72 |
last_interface = None |
| 73 |
con = None |
| 74 |
client = None |
| 75 |
|
| 76 |
if args.connect: |
| 77 |
client = varlink.Client.new_with_address(args.connect) |
| 78 |
con = client.open_connection() |
| 79 |
elif args.bridge: |
| 80 |
client = varlink.Client.new_with_bridge(shlex.split(args.bridge)) |
| 81 |
con = client.open_connection() |
| 82 |
elif args.activate: |
| 83 |
client = varlink.Client.new_with_activate(shlex.split(args.activate)) |
| 84 |
con = client.open_connection() |
| 85 |
else: |
| 86 |
print("No --connect or --bridge or --activate") |
| 87 |
|
| 88 |
if hasattr(sys.stdout, "buffer"): |
| 89 |
stdout = sys.stdout.buffer |
| 90 |
else: |
| 91 |
stdout = sys.stdout |
| 92 |
|
| 93 |
if hasattr(sys.stdin, "buffer"): |
| 94 |
stdin = sys.stdin.buffer |
| 95 |
else: |
| 96 |
stdin = sys.stdin |
| 97 |
|
| 98 |
while not sys.stdin.closed: |
| 99 |
c = stdin.read(1) |
| 100 |
|
| 101 |
if c == b"": |
| 102 |
break |
| 103 |
|
| 104 |
if c != b"\0": |
| 105 |
message += c |
| 106 |
continue |
| 107 |
|
| 108 |
req = json.loads(message.decode("utf-8")) |
| 109 |
|
| 110 |
if not args.connect and not args.activate and not args.bridge: |
| 111 |
if req["method"] == "org.varlink.service.GetInfo": |
| 112 |
req["method"] = "org.varlink.resolver.GetInfo" |
| 113 |
|
| 114 |
interface_name, _, method_name = req.get("method", "").rpartition(".") |
| 115 |
|
| 116 |
if req["method"] == "org.varlink.service.GetInterfaceDescription": |
| 117 |
resolving_interface = req["parameters"]["interface"] |
| 118 |
else: |
| 119 |
resolving_interface = interface_name |
| 120 |
|
| 121 |
if not interface_name or not method_name: |
| 122 |
stdout.write( |
| 123 |
json.dumps(varlink.InterfaceNotFound(interface_name), cls=varlink.VarlinkEncoder).encode( |
| 124 |
"utf-8" |
| 125 |
) |
| 126 |
+ b"\0" |
| 127 |
) |
| 128 |
sys.stdout.flush() |
| 129 |
continue |
| 130 |
|
| 131 |
if last_interface != resolving_interface: |
| 132 |
if con: |
| 133 |
if hasattr(con, "shutdown"): |
| 134 |
con.shutdown(socket.SHUT_RDWR) |
| 135 |
else: |
| 136 |
con.close() |
| 137 |
|
| 138 |
if client: |
| 139 |
client.cleanup() |
| 140 |
|
| 141 |
try: |
| 142 |
client = varlink.Client.new_with_resolved_interface( |
| 143 |
resolving_interface, resolver_address=args.resolver |
| 144 |
) |
| 145 |
except varlink.VarlinkError as e: |
| 146 |
stdout.write(json.dumps(e, cls=varlink.VarlinkEncoder).encode("utf-8") + b"\0") |
| 147 |
sys.stdout.flush() |
| 148 |
continue |
| 149 |
|
| 150 |
con = client.open_connection() |
| 151 |
last_interface = resolving_interface |
| 152 |
|
| 153 |
if hasattr(con, "send"): |
| 154 |
con.send(message + b"\0") |
| 155 |
else: |
| 156 |
con.write(message + b"\0") |
| 157 |
|
| 158 |
if req.get("oneway", False): |
| 159 |
continue |
| 160 |
|
| 161 |
message = b"" |
| 162 |
|
| 163 |
ret_message = b"" |
| 164 |
while True: |
| 165 |
c = con.recv(1) |
| 166 |
|
| 167 |
if c == b"": |
| 168 |
break |
| 169 |
|
| 170 |
if c != b"\0": |
| 171 |
ret_message += c |
| 172 |
continue |
| 173 |
|
| 174 |
stdout.write(ret_message + b"\0") |
| 175 |
|
| 176 |
sys.stdout.flush() |
| 177 |
|
| 178 |
ret = json.loads(ret_message.decode("utf-8")) |
| 179 |
ret_message = b"" |
| 180 |
|
| 181 |
if not ret.get("continues", False): |
| 182 |
break |
| 183 |
|
| 184 |
if ret.get("upgraded", False): |
| 185 |
raise NotImplementedError("Bridging upgraded connection not yet supported") |
| 186 |
|
| 187 |
|
| 188 |
def varlink_help(args): |
| 189 |
deli = args.INTERFACE.rfind("/") |
| 190 |
if deli != -1: |
| 191 |
address = args.INTERFACE[:deli] |
| 192 |
interface_name = args.INTERFACE[deli + 1 :] |
| 193 |
client = varlink.Client.new_with_address(address) |
| 194 |
else: |
| 195 |
interface_name = args.INTERFACE |
| 196 |
if args.activate: |
| 197 |
client = varlink.Client.new_with_activate(shlex.split(args.activate)) |
| 198 |
elif args.bridge: |
| 199 |
client = varlink.Client.new_with_bridge(shlex.split(args.bridge)) |
| 200 |
else: |
| 201 |
client = varlink.Client.new_with_resolved_interface(interface_name, args.resolver) |
| 202 |
|
| 203 |
interface = client.get_interface(interface_name) |
| 204 |
del client |
| 205 |
print(interface.description) |
| 206 |
|
| 207 |
|
| 208 |
def varlink_info(args): |
| 209 |
if args.ADDRESS: |
| 210 |
client = varlink.Client.new_with_address(args.ADDRESS) |
| 211 |
elif args.bridge: |
| 212 |
client = varlink.Client.new_with_bridge(shlex.split(args.bridge)) |
| 213 |
elif args.activate: |
| 214 |
client = varlink.Client.new_with_activate(shlex.split(args.activate)) |
| 215 |
else: |
| 216 |
print("No ADDRESS or --bridge or --activate") |
| 217 |
|
| 218 |
client.get_interfaces() |
| 219 |
info = client.info |
| 220 |
del client |
| 221 |
print("Vendor:", info["vendor"]) |
| 222 |
print("Product:", info["product"]) |
| 223 |
print("Version:", info["version"]) |
| 224 |
print("URL:", info["url"]) |
| 225 |
print("Interfaces:") |
| 226 |
for i in info["interfaces"]: |
| 227 |
print(" ", i) |
| 228 |
|
| 229 |
|
| 230 |
if __name__ == "__main__": |
| 231 |
parser = argparse.ArgumentParser() |
| 232 |
|
| 233 |
subparsers = parser.add_subparsers(title="commands") |
| 234 |
parser.add_argument("-r", "--resolver", default=None, help="address of the resolver") |
| 235 |
parser.add_argument( |
| 236 |
"-A", |
| 237 |
"--activate", |
| 238 |
default=None, |
| 239 |
help="Service to socket-activate and connect to. " |
| 240 |
"The temporary UNIX socket address is exported as $VARLINK_ADDRESS.", |
| 241 |
) |
| 242 |
parser.add_argument("-b", "--bridge", default=None, help="Command to execute and connect to") |
| 243 |
|
| 244 |
parser_info = subparsers.add_parser("info", help="Print information about a service") |
| 245 |
parser_info.add_argument("ADDRESS", nargs="?") |
| 246 |
parser_info.set_defaults(func=varlink_info) |
| 247 |
|
| 248 |
parser_help = subparsers.add_parser("help", help="Print interface description or service information") |
| 249 |
parser_help.add_argument("INTERFACE") |
| 250 |
parser_help.set_defaults(func=varlink_help) |
| 251 |
|
| 252 |
parser_bridge = subparsers.add_parser( |
| 253 |
"bridge", help="Bridge varlink messages to services on this machine" |
| 254 |
) |
| 255 |
parser_bridge.add_argument( |
| 256 |
"-c", |
| 257 |
"--connect", |
| 258 |
default=None, |
| 259 |
help="Optional varlink address to connect to, without using the resolver.", |
| 260 |
) |
| 261 |
parser_bridge.set_defaults(func=varlink_bridge) |
| 262 |
|
| 263 |
parser_call = subparsers.add_parser("call", help="Call a method") |
| 264 |
parser_call.add_argument( |
| 265 |
"-m", "--more", action="store_true", help="wait for multiple method returns if supported" |
| 266 |
) |
| 267 |
parser_call.add_argument("METHOD") |
| 268 |
parser_call.add_argument("ARGUMENTS", nargs="?", default="") |
| 269 |
parser_call.set_defaults(func=varlink_call) |
| 270 |
|
| 271 |
args = parser.parse_args() |
| 272 |
if not hasattr(args, "func"): |
| 273 |
parser.print_usage(sys.stderr) |
| 274 |
sys.exit(1) |
| 275 |
|
| 276 |
args.func(args) |