libvirtpy 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (154) hide show
  1. libvirtpy/__init__.py +5 -0
  2. libvirtpy/client/__init__.py +54 -0
  3. libvirtpy/client/_bases.py +326 -0
  4. libvirtpy/client/_runner.py +57 -0
  5. libvirtpy/client/aio.py +5922 -0
  6. libvirtpy/client/client.py +5695 -0
  7. libvirtpy/client/operations/__init__.py +631 -0
  8. libvirtpy/client/operations/_common.py +495 -0
  9. libvirtpy/client/operations/checkpoint.py +196 -0
  10. libvirtpy/client/operations/connection.py +93 -0
  11. libvirtpy/client/operations/connection_extra.py +256 -0
  12. libvirtpy/client/operations/domain.py +306 -0
  13. libvirtpy/client/operations/domain_agent.py +115 -0
  14. libvirtpy/client/operations/domain_block.py +368 -0
  15. libvirtpy/client/operations/domain_info.py +400 -0
  16. libvirtpy/client/operations/domain_lifecycle.py +338 -0
  17. libvirtpy/client/operations/domain_migration.py +164 -0
  18. libvirtpy/client/operations/domain_network.py +113 -0
  19. libvirtpy/client/operations/domain_tuning.py +494 -0
  20. libvirtpy/client/operations/host.py +246 -0
  21. libvirtpy/client/operations/host_interface.py +100 -0
  22. libvirtpy/client/operations/interface_extra.py +45 -0
  23. libvirtpy/client/operations/network.py +135 -0
  24. libvirtpy/client/operations/network_info.py +155 -0
  25. libvirtpy/client/operations/network_port.py +142 -0
  26. libvirtpy/client/operations/node_device.py +76 -0
  27. libvirtpy/client/operations/node_device_info.py +189 -0
  28. libvirtpy/client/operations/nwfilter.py +67 -0
  29. libvirtpy/client/operations/nwfilter_binding.py +74 -0
  30. libvirtpy/client/operations/secret.py +93 -0
  31. libvirtpy/client/operations/secret_extra.py +24 -0
  32. libvirtpy/client/operations/snapshot.py +100 -0
  33. libvirtpy/client/operations/snapshot_tree.py +114 -0
  34. libvirtpy/client/operations/storage.py +267 -0
  35. libvirtpy/client/operations/storage_info.py +188 -0
  36. libvirtpy/client/operations/streams.py +201 -0
  37. libvirtpy/client/references.py +249 -0
  38. libvirtpy/client/subscribing.py +88 -0
  39. libvirtpy/client/testing/__init__.py +24 -0
  40. libvirtpy/client/testing/_common.py +167 -0
  41. libvirtpy/client/testing/_domains.py +294 -0
  42. libvirtpy/client/testing/_objects.py +205 -0
  43. libvirtpy/client/testing/_state.py +246 -0
  44. libvirtpy/client/testing/_storage.py +171 -0
  45. libvirtpy/client/testing/async_transport.py +30 -0
  46. libvirtpy/client/testing/fake.py +81 -0
  47. libvirtpy/client/transport.py +87 -0
  48. libvirtpy/client/types.py +59 -0
  49. libvirtpy/client/wire.py +436 -0
  50. libvirtpy/client/wire_aio.py +348 -0
  51. libvirtpy/errors.py +7 -0
  52. libvirtpy/py.typed +0 -0
  53. libvirtpy/rpc/__init__.py +1 -0
  54. libvirtpy/rpc/aio.py +387 -0
  55. libvirtpy/rpc/cpumap.py +48 -0
  56. libvirtpy/rpc/errors.py +186 -0
  57. libvirtpy/rpc/events.py +259 -0
  58. libvirtpy/rpc/options.py +66 -0
  59. libvirtpy/rpc/procedure.py +27 -0
  60. libvirtpy/rpc/protocol/__init__.py +0 -0
  61. libvirtpy/rpc/protocol/procedures.py +5163 -0
  62. libvirtpy/rpc/protocol/typed_hints.py +210 -0
  63. libvirtpy/rpc/protocol/types.py +9299 -0
  64. libvirtpy/rpc/remote.py +295 -0
  65. libvirtpy/rpc/remote_aio.py +106 -0
  66. libvirtpy/rpc/sasl.py +467 -0
  67. libvirtpy/rpc/session.py +424 -0
  68. libvirtpy/rpc/sockets.py +251 -0
  69. libvirtpy/rpc/stream.py +323 -0
  70. libvirtpy/rpc/subscription.py +451 -0
  71. libvirtpy/rpc/sync.py +460 -0
  72. libvirtpy/rpc/typed_params.py +97 -0
  73. libvirtpy/rpc/uri.py +85 -0
  74. libvirtpy/rpc/xdr.py +552 -0
  75. libvirtpy/xml/__init__.py +49 -0
  76. libvirtpy/xml/_base.py +145 -0
  77. libvirtpy/xml/_shared/__init__.py +1 -0
  78. libvirtpy/xml/_shared/metadata.py +67 -0
  79. libvirtpy/xml/_shared/network.py +108 -0
  80. libvirtpy/xml/_shared/storage.py +131 -0
  81. libvirtpy/xml/_shared/sysinfo.py +83 -0
  82. libvirtpy/xml/_types.py +197 -0
  83. libvirtpy/xml/_xml_element.py +134 -0
  84. libvirtpy/xml/backup/__init__.py +25 -0
  85. libvirtpy/xml/backup/backup.py +89 -0
  86. libvirtpy/xml/backup/checkpoint.py +48 -0
  87. libvirtpy/xml/capabilities/__init__.py +43 -0
  88. libvirtpy/xml/capabilities/domain.py +436 -0
  89. libvirtpy/xml/capabilities/host.py +413 -0
  90. libvirtpy/xml/capabilities/storagepool.py +59 -0
  91. libvirtpy/xml/domain/__init__.py +493 -0
  92. libvirtpy/xml/domain/blkiotune.py +81 -0
  93. libvirtpy/xml/domain/clock.py +79 -0
  94. libvirtpy/xml/domain/cpu.py +265 -0
  95. libvirtpy/xml/domain/devices/__init__.py +495 -0
  96. libvirtpy/xml/domain/devices/address.py +152 -0
  97. libvirtpy/xml/domain/devices/audio.py +204 -0
  98. libvirtpy/xml/domain/devices/chardev.py +197 -0
  99. libvirtpy/xml/domain/devices/common.py +75 -0
  100. libvirtpy/xml/domain/devices/controller.py +262 -0
  101. libvirtpy/xml/domain/devices/devices.py +79 -0
  102. libvirtpy/xml/domain/devices/disk.py +471 -0
  103. libvirtpy/xml/domain/devices/filesystem.py +206 -0
  104. libvirtpy/xml/domain/devices/graphics.py +245 -0
  105. libvirtpy/xml/domain/devices/hostdev.py +221 -0
  106. libvirtpy/xml/domain/devices/input.py +89 -0
  107. libvirtpy/xml/domain/devices/interface.py +459 -0
  108. libvirtpy/xml/domain/devices/iommu.py +54 -0
  109. libvirtpy/xml/domain/devices/memballoon.py +42 -0
  110. libvirtpy/xml/domain/devices/memory_device.py +101 -0
  111. libvirtpy/xml/domain/devices/misc.py +169 -0
  112. libvirtpy/xml/domain/devices/nvram.py +20 -0
  113. libvirtpy/xml/domain/devices/shmem.py +46 -0
  114. libvirtpy/xml/domain/devices/sound.py +42 -0
  115. libvirtpy/xml/domain/devices/tpm.py +103 -0
  116. libvirtpy/xml/domain/devices/usb.py +86 -0
  117. libvirtpy/xml/domain/devices/video.py +101 -0
  118. libvirtpy/xml/domain/domain.py +157 -0
  119. libvirtpy/xml/domain/features.py +312 -0
  120. libvirtpy/xml/domain/iothread.py +41 -0
  121. libvirtpy/xml/domain/launch_security.py +82 -0
  122. libvirtpy/xml/domain/memory.py +149 -0
  123. libvirtpy/xml/domain/misc.py +113 -0
  124. libvirtpy/xml/domain/namespaces.py +96 -0
  125. libvirtpy/xml/domain/numa.py +140 -0
  126. libvirtpy/xml/domain/os.py +267 -0
  127. libvirtpy/xml/domain/qemu.py +102 -0
  128. libvirtpy/xml/domain/seclabel.py +29 -0
  129. libvirtpy/xml/domain/storage_source.py +466 -0
  130. libvirtpy/xml/domain/sysinfo.py +34 -0
  131. libvirtpy/xml/interface/__init__.py +17 -0
  132. libvirtpy/xml/interface/interface.py +153 -0
  133. libvirtpy/xml/network/__init__.py +101 -0
  134. libvirtpy/xml/network/network.py +311 -0
  135. libvirtpy/xml/network/port.py +84 -0
  136. libvirtpy/xml/nodedev/__init__.py +13 -0
  137. libvirtpy/xml/nodedev/nodedev.py +490 -0
  138. libvirtpy/xml/nwfilter/__init__.py +81 -0
  139. libvirtpy/xml/nwfilter/binding.py +51 -0
  140. libvirtpy/xml/nwfilter/filter.py +92 -0
  141. libvirtpy/xml/nwfilter/rules.py +534 -0
  142. libvirtpy/xml/secret/__init__.py +11 -0
  143. libvirtpy/xml/secret/secret.py +36 -0
  144. libvirtpy/xml/snapshot/__init__.py +29 -0
  145. libvirtpy/xml/snapshot/snapshot.py +136 -0
  146. libvirtpy/xml/storage/__init__.py +75 -0
  147. libvirtpy/xml/storage/pool.py +219 -0
  148. libvirtpy/xml/storage/volume.py +102 -0
  149. libvirtpy/xml/sysinfo.py +40 -0
  150. libvirtpy-0.1.0.dist-info/METADATA +109 -0
  151. libvirtpy-0.1.0.dist-info/RECORD +154 -0
  152. libvirtpy-0.1.0.dist-info/WHEEL +4 -0
  153. libvirtpy-0.1.0.dist-info/licenses/LICENSE +501 -0
  154. libvirtpy-0.1.0.dist-info/licenses/NOTICE +23 -0
@@ -0,0 +1,323 @@
1
+ """Data streams: console, screenshot and volume upload/download.
2
+
3
+ A stream is the open end of a data channel that a call set up. ``RemoteStream``
4
+ is a file-like object (``read``, ``write``, ``close``, ``with``); ``AsyncRemoteStream``
5
+ has the same methods as coroutines::
6
+
7
+ with client.open_domain_console("vm") as console:
8
+ console.write(b"\\n")
9
+ print(console.read(100))
10
+
11
+ with client.download_pool_volume("pool", "disk.img") as source:
12
+ data = source.read()
13
+ """
14
+
15
+ import asyncio
16
+ import contextlib
17
+ import io
18
+ import queue
19
+ from types import TracebackType
20
+ from typing import Any, Literal, TypeVar
21
+
22
+ from libvirtpy.rpc.aio import AsyncRpc
23
+ from libvirtpy.rpc.session import StreamEvent
24
+ from libvirtpy.rpc.sync import SyncRpc
25
+
26
+ __all__ = ["AsyncRemoteStream", "Mode", "RemoteStream"]
27
+
28
+ Mode = Literal["read", "write", "both"]
29
+ CHUNK_SIZE = 65536
30
+ CLOSE_TIMEOUT_SECONDS = 10
31
+ AsyncSelf = TypeVar("AsyncSelf", bound="AsyncRemoteStream")
32
+
33
+
34
+ class RemoteStream(io.RawIOBase):
35
+ """A blocking, file-like stream to or from a daemon."""
36
+
37
+ def __init__(self, rpc: SyncRpc, mode: Mode) -> None:
38
+ """Create the stream; the connection sets ``serial`` when the call is sent.
39
+
40
+ :param rpc: the connection the stream is on.
41
+ :param mode: ``read`` (data comes from the daemon), ``write`` (data goes to
42
+ it) or ``both`` (a console).
43
+ """
44
+ super().__init__()
45
+ self.mode = mode
46
+ self.serial = -1
47
+ self.reply: Any = None
48
+ """What the call that opened the stream returned (for a screenshot: the
49
+ MIME type)."""
50
+
51
+ self._rpc = rpc
52
+ self._events: queue.Queue[StreamEvent] = queue.Queue()
53
+ self._pending = b""
54
+ self._eof = False
55
+ self._dead = False
56
+
57
+ def deliver(self, event: StreamEvent) -> None:
58
+ """Receive what the connection read for this stream (reading thread).
59
+
60
+ :param event: the data, the end, or the error.
61
+ """
62
+ self._events.put(event)
63
+
64
+ def readable(self) -> bool:
65
+ """Tell whether data can be read from the stream.
66
+
67
+ :returns: ``True`` for a ``read`` or ``both`` stream.
68
+ """
69
+ return self.mode in ("read", "both")
70
+
71
+ def writable(self) -> bool:
72
+ """Tell whether data can be written to the stream.
73
+
74
+ :returns: ``True`` for a ``write`` or ``both`` stream.
75
+ """
76
+ return self.mode in ("write", "both")
77
+
78
+ def readinto( # type: ignore[override] # narrower than RawIOBase's buffer type
79
+ self, buffer: bytearray | memoryview
80
+ ) -> int:
81
+ """Read bytes into a buffer, waiting for the daemon if there are none.
82
+
83
+ :param buffer: where to put the bytes.
84
+ :returns: how many bytes; 0 when the daemon has sent everything.
85
+ :raises LibvirtError: if the daemon reports a failure.
86
+ :raises UnsupportedOperation: if the stream is write-only.
87
+ """
88
+ if not self.readable():
89
+ raise io.UnsupportedOperation("this stream is write-only")
90
+
91
+ while not self._pending and not self._eof:
92
+ self._take(self._events.get())
93
+ if not self._pending:
94
+ return 0
95
+
96
+ view = memoryview(buffer)
97
+ count = min(len(view), len(self._pending))
98
+ view[:count] = self._pending[:count]
99
+ self._pending = self._pending[count:]
100
+ return count
101
+
102
+ def write( # type: ignore[override] # narrower than RawIOBase's buffer type
103
+ self, data: bytes | bytearray | memoryview
104
+ ) -> int:
105
+ """Send bytes to the daemon.
106
+
107
+ :param data: the bytes.
108
+ :returns: how many bytes were sent: all of them.
109
+ :raises UnsupportedOperation: if the stream is read-only.
110
+ """
111
+ if not self.writable():
112
+ raise io.UnsupportedOperation("this stream is read-only")
113
+
114
+ view = memoryview(data)
115
+ for start in range(0, len(view), CHUNK_SIZE):
116
+ self._rpc.send_stream_data(
117
+ self.serial, bytes(view[start : start + CHUNK_SIZE])
118
+ )
119
+ return len(view)
120
+
121
+ def close(self) -> None:
122
+ """End the stream: finish what we send, or give up on what we read."""
123
+ if self.closed:
124
+ return
125
+ try:
126
+ self._finish()
127
+ finally:
128
+ super().close()
129
+
130
+ def _take(self, event: StreamEvent) -> None:
131
+ """Handle one event while reading.
132
+
133
+ :param event: what the connection delivered.
134
+ :raises Exception: the error the daemon (or the connection) reported.
135
+ """
136
+ if event.kind == "data":
137
+ self._pending += event.data
138
+ return
139
+
140
+ self._eof = True
141
+ if event.kind in ("closed", "error"):
142
+ self._dead = True
143
+ if event.error is not None:
144
+ raise event.error
145
+
146
+ def _finish(self) -> None:
147
+ """Tell the daemon we are done, and wait for it to confirm.
148
+
149
+ A stream we were still reading is given up (aborted) instead.
150
+ """
151
+ if self._dead:
152
+ return
153
+ self._dead = True
154
+
155
+ if self.mode == "read" and not self._eof:
156
+ self._rpc.abort_stream(self.serial)
157
+ return
158
+
159
+ self._rpc.finish_stream(self.serial)
160
+ with contextlib.suppress(queue.Empty, Exception):
161
+ while self._events.get(timeout=CLOSE_TIMEOUT_SECONDS).kind not in (
162
+ "closed",
163
+ "error",
164
+ ):
165
+ pass
166
+
167
+
168
+ class AsyncRemoteStream:
169
+ """A stream to or from a daemon, for ``async`` code."""
170
+
171
+ def __init__(self, rpc: AsyncRpc, mode: Mode) -> None:
172
+ """Create the stream; the connection sets ``serial`` when the call is sent.
173
+
174
+ :param rpc: the connection the stream is on.
175
+ :param mode: ``read``, ``write`` or ``both`` (a console).
176
+ """
177
+ self.mode = mode
178
+ self.serial = -1
179
+ self.reply: Any = None
180
+ """What the call that opened the stream returned."""
181
+
182
+ self._rpc = rpc
183
+ self._events: asyncio.Queue[StreamEvent] = asyncio.Queue()
184
+ self._pending = b""
185
+
186
+ self._eof = False
187
+ self._dead = False
188
+ self._closed = False
189
+
190
+ def deliver(self, event: StreamEvent) -> None:
191
+ """Receive what the connection read for this stream (in the event loop).
192
+
193
+ :param event: the data, the end, or the error.
194
+ """
195
+ self._events.put_nowait(event)
196
+
197
+ async def read(self, size: int = -1) -> bytes:
198
+ """Read bytes, waiting for the daemon if there are none.
199
+
200
+ :param size: the most bytes to return; -1 reads until the end.
201
+ :returns: the bytes; empty when the daemon has sent everything.
202
+ :raises LibvirtError: if the daemon reports a failure.
203
+ """
204
+ self._check_readable()
205
+ while not self._eof and (size < 0 or len(self._pending) < size):
206
+ self._take(await self._events.get())
207
+
208
+ count = len(self._pending) if size < 0 else min(size, len(self._pending))
209
+ data, self._pending = self._pending[:count], self._pending[count:]
210
+ return data
211
+
212
+ async def read_chunk(self) -> bytes:
213
+ """Read the next piece the daemon sent.
214
+
215
+ :returns: the bytes; empty when the daemon has sent everything.
216
+ :raises LibvirtError: if the daemon reports a failure.
217
+ """
218
+ self._check_readable()
219
+ while not self._pending and not self._eof:
220
+ self._take(await self._events.get())
221
+ data, self._pending = self._pending, b""
222
+ return data
223
+
224
+ async def write(self, data: bytes) -> int:
225
+ """Send bytes to the daemon.
226
+
227
+ :param data: the bytes.
228
+ :returns: how many bytes were sent: all of them.
229
+ :raises UnsupportedOperation: if the stream is read-only.
230
+ """
231
+ if self.mode == "read":
232
+ raise io.UnsupportedOperation("this stream is read-only")
233
+
234
+ for start in range(0, len(data), CHUNK_SIZE):
235
+ await self._rpc.send_stream_data(
236
+ self.serial, data[start : start + CHUNK_SIZE]
237
+ )
238
+ return len(data)
239
+
240
+ async def close(self) -> None:
241
+ """End the stream: finish what we send, or give up on what we read."""
242
+ if self._closed:
243
+ return
244
+ self._closed = True
245
+ if self._dead:
246
+ return
247
+ self._dead = True
248
+
249
+ if self.mode == "read" and not self._eof:
250
+ await self._rpc.abort_stream(self.serial)
251
+ return
252
+
253
+ await self._rpc.finish_stream(self.serial)
254
+ with contextlib.suppress(asyncio.TimeoutError, Exception):
255
+ while True:
256
+ event = await asyncio.wait_for(
257
+ self._events.get(), CLOSE_TIMEOUT_SECONDS
258
+ )
259
+ if event.kind in ("closed", "error"):
260
+ break
261
+
262
+ def __aiter__(self: AsyncSelf) -> AsyncSelf:
263
+ """Iterate over the pieces the daemon sends.
264
+
265
+ :returns: the stream.
266
+ """
267
+ return self
268
+
269
+ async def __anext__(self) -> bytes:
270
+ """Read the next piece.
271
+
272
+ :returns: the bytes.
273
+ :raises StopAsyncIteration: when the daemon has sent everything.
274
+ """
275
+ data = await self.read_chunk()
276
+ if not data:
277
+ raise StopAsyncIteration
278
+ return data
279
+
280
+ async def __aenter__(self: AsyncSelf) -> AsyncSelf:
281
+ """Use the stream in an ``async with`` block.
282
+
283
+ :returns: the stream.
284
+ """
285
+ return self
286
+
287
+ async def __aexit__(
288
+ self,
289
+ exception_type: type[BaseException] | None,
290
+ exception: BaseException | None,
291
+ traceback: TracebackType | None,
292
+ ) -> None:
293
+ """Close the stream when the block ends.
294
+
295
+ :param exception_type: the exception class, if the block raised.
296
+ :param exception: the exception, if the block raised.
297
+ :param traceback: its traceback, if the block raised.
298
+ """
299
+ await self.close()
300
+
301
+ def _check_readable(self) -> None:
302
+ """Refuse to read a write-only stream.
303
+
304
+ :raises UnsupportedOperation: if it is write-only.
305
+ """
306
+ if self.mode == "write":
307
+ raise io.UnsupportedOperation("this stream is write-only")
308
+
309
+ def _take(self, event: StreamEvent) -> None:
310
+ """Handle one event while reading.
311
+
312
+ :param event: what the connection delivered.
313
+ :raises Exception: the error the daemon (or the connection) reported.
314
+ """
315
+ if event.kind == "data":
316
+ self._pending += event.data
317
+ return
318
+
319
+ self._eof = True
320
+ if event.kind in ("closed", "error"):
321
+ self._dead = True
322
+ if event.error is not None:
323
+ raise event.error