| 1 |
|
| 2 |
""" |
| 3 |
pyxs._compat |
| 4 |
~~~~~~~~~~~~ |
| 5 |
|
| 6 |
This module implements compatibility interface for scripts, |
| 7 |
using ``xen.lowlevel.xs``. |
| 8 |
|
| 9 |
:copyright: (c) 2011 by Selectel. |
| 10 |
:copyright: (c) 2016 by pyxs authors and contributors, see AUTHORS |
| 11 |
for more details. |
| 12 |
:license: LGPL, see LICENSE for more details. |
| 13 |
""" |
| 14 |
|
| 15 |
from __future__ import absolute_import |
| 16 |
|
| 17 |
__all__ = ["xs", "Error"] |
| 18 |
|
| 19 |
import errno |
| 20 |
|
| 21 |
from .client import Client |
| 22 |
from .exceptions import PyXSError as Error |
| 23 |
|
| 24 |
|
| 25 |
class xs(object): |
| 26 |
"""XenStore client with a backward compatible interface, useful for |
| 27 |
switching from ``xen.lowlevel.xs``. |
| 28 |
""" |
| 29 |
def __init__(self): |
| 30 |
self.client = Client() |
| 31 |
self.client.connect() |
| 32 |
self.monitor = self.client.monitor() |
| 33 |
self.token_aliases = {} |
| 34 |
|
| 35 |
def close(self): |
| 36 |
self.client.close() |
| 37 |
|
| 38 |
def get_permissions(self, tx_id, path): |
| 39 |
self.client.tx_id = int(tx_id or 0) |
| 40 |
self.client.get_perms(path) |
| 41 |
|
| 42 |
def set_permissions(self, tx_id, path, perms): |
| 43 |
self.client.tx_id = int(tx_id or 0) |
| 44 |
self.client.set_perms(path, perms) |
| 45 |
|
| 46 |
def ls(self, tx_id, path): |
| 47 |
self.client.tx_id = int(tx_id or 0) |
| 48 |
try: |
| 49 |
return self.client.list(path) |
| 50 |
except Error as e: |
| 51 |
if e.args[0] == errno.ENOENT: |
| 52 |
return |
| 53 |
|
| 54 |
raise |
| 55 |
|
| 56 |
def mkdir(self, tx_id, path): |
| 57 |
self.client.tx_id = int(tx_id or 0) |
| 58 |
self.client.mkdir(path) |
| 59 |
|
| 60 |
def rm(self, tx_id, path): |
| 61 |
self.client.tx_id = int(tx_id or 0) |
| 62 |
self.client.delete(path) |
| 63 |
|
| 64 |
def read(self, tx_id, path): |
| 65 |
self.client.tx_id = int(tx_id or 0) |
| 66 |
return self.client.read(path) |
| 67 |
|
| 68 |
def write(self, tx_id, path, value): |
| 69 |
self.client.tx_id = int(tx_id or 0) |
| 70 |
return self.client.write(path, value) |
| 71 |
|
| 72 |
def get_domain_path(self, domid): |
| 73 |
return self.client.get_domain_path(domid) |
| 74 |
|
| 75 |
def introduce_domain(self, domid, mfn, eventchn): |
| 76 |
self.client.introduce_domain(domid, mfn, eventchn) |
| 77 |
|
| 78 |
def release_domain(self, domid): |
| 79 |
self.client.release_domain(domid) |
| 80 |
|
| 81 |
def resume_domain(self, domid): |
| 82 |
self.client.resume_domain(domid) |
| 83 |
|
| 84 |
def set_target(self, domid, target): |
| 85 |
self.client.set_target(domid, target) |
| 86 |
|
| 87 |
def transaction_start(self): |
| 88 |
return str(self.client.transaction()).encode() |
| 89 |
|
| 90 |
def transaction_end(self, tx_id, abort=0): |
| 91 |
self.client.tx_id = int(tx_id or 0) |
| 92 |
if abort: |
| 93 |
self.client.rollback() |
| 94 |
else: |
| 95 |
try: |
| 96 |
self.client.commit() |
| 97 |
except Error as e: |
| 98 |
if e.args[0] == errno.EAGAIN: |
| 99 |
return False |
| 100 |
|
| 101 |
raise |
| 102 |
|
| 103 |
return True |
| 104 |
|
| 105 |
def watch(self, path, token): |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
stub = str(id(token)).encode() |
| 112 |
self.token_aliases[stub] = token |
| 113 |
return self.monitor.watch(path, stub) |
| 114 |
|
| 115 |
def unwatch(self, path, token): |
| 116 |
stub = str(id(token)).encode() |
| 117 |
self.monitor.unwatch(path, stub) |
| 118 |
del self.token_aliases[stub] |
| 119 |
|
| 120 |
def read_watch(self): |
| 121 |
event = next(self.monitor.wait()) |
| 122 |
return event._replace(token=self.token_aliases[event.token]) |