|
| 1 | +from labgrid.protocol import CommandProtocol |
| 2 | + |
| 3 | +def get_systemd_status(command): |
| 4 | + assert isinstance(command, CommandProtocol), "command must be a CommandProtocol" |
| 5 | + # TODO: Use busctl --json if systemd>239 |
| 6 | + array_notation = "a(ssssssouso)" |
| 7 | + out = command.run_check( |
| 8 | + "busctl call --no-pager org.freedesktop.systemd1 \ |
| 9 | + /org/freedesktop/systemd1 org.freedesktop.systemd1.Manager ListUnits" |
| 10 | + ) |
| 11 | + |
| 12 | + out = out[0] |
| 13 | + if array_notation not in out: |
| 14 | + raise ValueError("Systemd ListUnits output changed") |
| 15 | + out = out[len(array_notation):] |
| 16 | + array_length = int(out[:out.index('"')].strip(" ")) |
| 17 | + out = out[out.index('"')+1:-1] |
| 18 | + out = out.split('\" \"') |
| 19 | + data = iter(out) |
| 20 | + services = {} |
| 21 | + for _ in range(array_length): |
| 22 | + name = next(data) |
| 23 | + services[name] = {} |
| 24 | + services[name]["description"] = next(data) |
| 25 | + services[name]["load"] = next(data) |
| 26 | + services[name]["active"] = next(data) |
| 27 | + services[name]["sub"] = next(data) |
| 28 | + services[name]["follow"] = next(data) |
| 29 | + path_and_id = next(data) |
| 30 | + pos = path_and_id.index('"') |
| 31 | + services[name]["path"] = path_and_id[:pos] |
| 32 | + services[name]["id"] = int(path_and_id[pos+1:-1].strip(" ")) |
| 33 | + services[name]["type"] = path_and_id[path_and_id.rfind('"'):] |
| 34 | + services[name]["objpath"] = next(data) |
| 35 | + |
| 36 | + return services |
| 37 | + |
| 38 | +def get_commands(command, directories=None): |
| 39 | + """Returns the commands of a running linux system |
| 40 | + Args: |
| 41 | + command (CommandProtocol): An instance of a Driver implementing the CommandProtocol |
| 42 | + directories (list): An optional list of directories to include |
| 43 | + Returns: |
| 44 | + list: list of commands available under linux |
| 45 | + """ |
| 46 | + assert isinstance(command, CommandProtocol), "command must be a CommandProtocol" |
| 47 | + out = command.run_check("ls /usr/bin") |
| 48 | + out.extend(command.run_check("ls /usr/sbin")) |
| 49 | + if directories: |
| 50 | + assert isinstance(directories, list), "directories must be a list" |
| 51 | + for directory in directories: |
| 52 | + out.extend(command.run_check("ls {}".format(directory))) |
| 53 | + commands = [] |
| 54 | + for line in out: |
| 55 | + for cmd in line.split(" "): |
| 56 | + if cmd: |
| 57 | + commands.append(cmd) |
| 58 | + |
| 59 | + return commands |
0 commit comments