| 1 |
|
| 2 |
""" |
| 3 |
ls |
| 4 |
~~ |
| 5 |
|
| 6 |
``xenstore-ls`` implementation with :mod:`pyxs`. |
| 7 |
|
| 8 |
:copyright: (c) 2011 by Selectel, see AUTHORS for more details. |
| 9 |
""" |
| 10 |
|
| 11 |
from __future__ import print_function |
| 12 |
|
| 13 |
import posixpath |
| 14 |
import sys |
| 15 |
|
| 16 |
from pyxs.client import Client |
| 17 |
|
| 18 |
|
| 19 |
def main(client, top): |
| 20 |
|
| 21 |
depth = top.count(b"/") + (top != b"/") |
| 22 |
|
| 23 |
for path, value, children in client.walk(top): |
| 24 |
if path == top: |
| 25 |
continue |
| 26 |
|
| 27 |
node = posixpath.basename(path) |
| 28 |
indent = " " * (path.count(b"/") - depth) |
| 29 |
print("{0}{1} = \"{2}\"".format(indent, node, value)) |
| 30 |
|
| 31 |
|
| 32 |
if __name__ == "__main__": |
| 33 |
try: |
| 34 |
[path] = sys.argv[1:] or ["/"] |
| 35 |
except ValueError: |
| 36 |
sys.exit("usage: %prog PATH") |
| 37 |
|
| 38 |
with Client() as client: |
| 39 |
main(client, posixpath.normpath(path).encode()) |