annet 0.16.34__py3-none-any.whl → 1.0.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.

Potentially problematic release.


This version of annet might be problematic. Click here for more details.

annet/deploy.py CHANGED
@@ -1,37 +1,50 @@
1
1
  # pylint: disable=unused-argument
2
-
3
-
4
2
  import abc
5
3
  import itertools
6
- import re
7
4
  from collections import namedtuple
8
- from contextlib import contextmanager
9
5
  from typing import Dict, List, Optional, Any, OrderedDict, Tuple, Type
10
6
 
11
7
  from contextlog import get_logger
12
8
 
13
- from annet import text_term_format
14
9
  from annet.annlib.command import Command, Question, CommandList
15
10
  from annet.annlib.netdev.views.hardware import HardwareView
16
11
  from annet.annlib.rbparser.deploying import MakeMessageMatcher, Answer
17
12
  from annet.cli_args import DeployOptions
18
13
  from annet.connectors import Connector, get_connector_from_config
19
- from annet.output import TextArgs
20
14
  from annet.rulebook import get_rulebook, deploying
21
15
  from annet.storage import Device
22
16
 
23
17
 
24
- NCURSES_SIZE_T = 2 ** 15 - 1
18
+ _DeployResultBase = namedtuple("_DeployResultBase", ("hostnames", "results", "durations", "original_states"))
25
19
 
26
20
 
27
- _DeployResultBase = namedtuple("_DeployResultBase", ("hostnames", "results", "durations", "original_states"))
21
+ class ProgressBar(abc.ABC):
22
+ @abc.abstractmethod
23
+ def set_content(self, tile_name: str, content: str):
24
+ ...
25
+
26
+ @abc.abstractmethod
27
+ def set_progress(self,
28
+ tile_name: str,
29
+ iteration: int,
30
+ total: int,
31
+ prefix: str = "",
32
+ suffix: str = "",
33
+ fill: str = "",
34
+ error: bool = False,
35
+ ):
36
+ ...
37
+
38
+ @abc.abstractmethod
39
+ def set_exception(self, tile_name: str, cmd_exc: str, last_cmd: str, progress_max: int):
40
+ ...
28
41
 
29
42
 
30
43
  class DeployResult(_DeployResultBase): # noqa: E302
31
- def add_results(self, results: Dict[str, Optional[Exception]]) -> None:
32
- for hostname, result in results.items():
44
+ def add_results(self, results: dict[str, tuple[list[str], list[Exception]]]) -> None:
45
+ for hostname, (excs, result) in results.items():
33
46
  self.hostnames.append(hostname)
34
- self.results[hostname] = result
47
+ self.results[hostname] = excs
35
48
  self.durations[hostname] = 0.0
36
49
  self.original_states[hostname] = None
37
50
 
@@ -64,14 +77,20 @@ driver_connector = _DriverConnector()
64
77
 
65
78
  class Fetcher(abc.ABC):
66
79
  @abc.abstractmethod
67
- def fetch_packages(self, devices: List[Device],
68
- processes: int = 1, max_slots: int = 0) -> Tuple[Dict[Device, str], Dict[Device, Any]]:
80
+ async def fetch_packages(self,
81
+ devices: list[Device],
82
+ processes: int = 1,
83
+ max_slots: int = 0,
84
+ ) -> tuple[dict[Device, str], dict[Device, Any]]:
69
85
  pass
70
86
 
71
87
  @abc.abstractmethod
72
- def fetch(self, devices: List[Device],
73
- files_to_download: Dict[str, List[str]] = None,
74
- processes: int = 1, max_slots: int = 0):
88
+ async def fetch(self,
89
+ devices: list[Device],
90
+ files_to_download: dict[str, list[str]] | None = None,
91
+ processes: int = 1,
92
+ max_slots: int = 0,
93
+ ):
75
94
  pass
76
95
 
77
96
 
@@ -83,7 +102,7 @@ def get_fetcher() -> Fetcher:
83
102
 
84
103
  class DeployDriver(abc.ABC):
85
104
  @abc.abstractmethod
86
- async def bulk_deploy(self, deploy_cmds: dict, args: DeployOptions) -> DeployResult:
105
+ async def bulk_deploy(self, deploy_cmds: dict, args: DeployOptions, progress_bar: ProgressBar | None = None) -> DeployResult:
87
106
  pass
88
107
 
89
108
  @abc.abstractmethod
@@ -106,7 +125,7 @@ def get_deployer() -> DeployDriver:
106
125
 
107
126
 
108
127
  # ===
109
- def scrub_config(text, breed):
128
+ def scrub_config(text: str, breed: str) -> str:
110
129
  return text
111
130
 
112
131
 
@@ -114,331 +133,6 @@ def show_bulk_report(hostnames, results, durations, log_dir):
114
133
  pass
115
134
 
116
135
 
117
- class AskConfirm:
118
- CUT_WARN_MSG = "WARNING: the text was cut because of curses limits."
119
-
120
- def __init__(self, text: str, text_type="diff", alternative_text: str = "",
121
- alternative_text_type: str = "diff", allow_force_yes: bool = False):
122
- self.text = [text, text_type]
123
- self.alternative_text = [alternative_text, alternative_text_type]
124
- self.color_to_curses: Dict[Optional[str], int] = {}
125
- self.lines: Dict[int, List[TextArgs]] = {}
126
- self.rows = None
127
- self.cols = None
128
- self.top = 0
129
- self.left = 0
130
- self.pad = None
131
- self.screen = None
132
- self.found_pos = {}
133
- self.curses_lines = None
134
- self.debug_prompt = TextArgs("")
135
- self.page_position = TextArgs("")
136
- s_force = "/f" if allow_force_yes else ""
137
- self.prompt = [
138
- TextArgs("Execute these commands? [Y%s/q] (/ - search, a - patch/cmds)" % s_force, "blue", offset=0),
139
- self.page_position,
140
- self.debug_prompt]
141
-
142
- def _parse_text(self):
143
- txt = self.text[0]
144
- txt_split = txt.splitlines()
145
- # curses pad, который тут используется, имеет ограничение на количество линий
146
- if (len(txt_split) + 1) >= NCURSES_SIZE_T: # +1 для того чтобы курсор можно было переместить на пустую строку
147
- del txt_split[NCURSES_SIZE_T - 3:]
148
- txt_split.insert(0, self.CUT_WARN_MSG)
149
- txt_split.append(self.CUT_WARN_MSG)
150
- txt = "\n".join(txt_split)
151
- self.rows = len(txt_split)
152
- self.cols = max(len(line) for line in txt_split)
153
- res = text_term_format.curses_format(txt, self.text[1])
154
- self.lines = res
155
-
156
- def _update_search_pos(self, expr):
157
- self.found_pos = {}
158
- if not expr:
159
- return
160
- try:
161
- expr = re.compile(expr)
162
- except Exception:
163
- return None
164
- lines = self.text[0].splitlines()
165
- for (line_no, line) in enumerate(lines):
166
- for match in re.finditer(expr, line):
167
- if line_no not in self.found_pos:
168
- self.found_pos[line_no] = []
169
- self.found_pos[line_no].append(TextArgs(match.group(0), "highlight", match.start()))
170
-
171
- def _init_colors(self):
172
- self.color_to_curses = init_colors()
173
-
174
- def _init_pad(self):
175
- import curses
176
-
177
- with self._store_xy():
178
- self.pad = curses.newpad(self.rows + 1, self.cols)
179
- self.pad.keypad(True) # accept arrow keys
180
- self._render_to_pad(self.lines)
181
-
182
- def _render_to_pad(self, lines: dict):
183
- """
184
- Рендерим данный на pad
185
- :param lines: словарь проиндексированный по номерам линий
186
- :return:
187
- """
188
- with self._store_xy():
189
- for line_no, line_data in sorted(lines.items()):
190
- line_pos_calc = 0
191
- for line_part in line_data:
192
- if line_part.offset is not None:
193
- line_pos = line_part.offset
194
- else:
195
- line_pos = line_pos_calc
196
- if line_part.color:
197
- self.pad.addstr(line_no, line_pos, line_part.text, self.color_to_curses[line_part.color])
198
- else:
199
- self.pad.addstr(line_no, line_pos, line_part.text)
200
- line_pos_calc += len(line_part.text)
201
-
202
- def _add_prompt(self):
203
- for prompt_part in self.prompt:
204
- if not prompt_part:
205
- continue
206
- if prompt_part.offset is None:
207
- offset = 0
208
- else:
209
- offset = prompt_part.offset
210
- self.screen.addstr(self.curses_lines - 1, offset, prompt_part.text, self.color_to_curses[prompt_part.color])
211
-
212
- def _clear_prompt(self):
213
- with self._store_xy():
214
- self.screen.move(self.curses_lines - 1, 0)
215
- self.screen.clrtoeol()
216
-
217
- def show(self):
218
- self._add_prompt()
219
- self.screen.refresh()
220
- size = self.screen.getmaxyx()
221
- self.pad.refresh(self.top, self.left, 0, 0, size[0] - 2, size[1] - 2)
222
-
223
- @contextmanager
224
- def _store_xy(self):
225
- if self.pad is not None:
226
- current_y, current_x = self.pad.getyx()
227
- yield current_y, current_x
228
- max_y, max_x = self.pad.getmaxyx()
229
- current_y = min(max_y - 1, current_y)
230
- current_x = min(max_x - 1, current_x)
231
-
232
- self.pad.move(current_y, current_x)
233
- else:
234
- yield
235
-
236
- def search_next(self, prev=False):
237
- to = None
238
- current_y, current_x = self.pad.getyx()
239
- if prev:
240
- for line_index in sorted(self.found_pos, reverse=True):
241
- for text_args in self.found_pos[line_index]:
242
- if line_index > current_y:
243
- continue
244
-
245
- if line_index < current_y or line_index == current_y and text_args.offset < current_x:
246
- to = line_index, text_args.offset
247
- break
248
- if to:
249
- break
250
- else:
251
- for line_index in sorted([i for i in self.found_pos if i >= current_y]):
252
- for text_args in self.found_pos[line_index]:
253
- if line_index > current_y or line_index == current_y and text_args.offset > current_x:
254
- to = line_index, text_args.offset
255
- break
256
- if to:
257
- break
258
- if to:
259
- return to[0] - current_y, to[1] - current_x
260
- else:
261
- return 0, 0
262
-
263
- def _search_prompt(self):
264
- import curses
265
-
266
- search_prompt = [TextArgs("Search: ", "green_bold", offset=0)]
267
- current_prompt = self.prompt
268
- self.prompt = search_prompt
269
- with self._store_xy():
270
- self._clear_prompt()
271
- self.show()
272
- curses.echo()
273
- expr = self.screen.getstr().decode()
274
- curses.noecho()
275
- self._update_search_pos(expr)
276
- self._parse_text()
277
- self._init_pad()
278
- # срендерем поверх pad слой с подстветкой
279
- self._render_to_pad(self.found_pos)
280
- y_offset, x_offset = self.search_next()
281
- self.prompt = current_prompt
282
- return y_offset, x_offset
283
-
284
- def _do_commands(self):
285
- import curses
286
-
287
- while True:
288
- self._clear_prompt()
289
- try:
290
- ch = self.pad.getch()
291
- except KeyboardInterrupt:
292
- return "n"
293
- max_y, max_x = self.screen.getmaxyx()
294
- _, pad_max_x = self.pad.getmaxyx()
295
- max_y -= 2 # prompt
296
- y_offset = 0
297
- x_offset = 0
298
- margin = 0
299
- y_delta = 0
300
- x_delta = 0
301
-
302
- y, x = self.pad.getyx()
303
- if ch == ord("q"):
304
- return "exit"
305
- elif ch in [ord("y"), ord("Y")]:
306
- return "y"
307
- elif ch in [ord("f"), ord("F")]:
308
- return "force-yes"
309
- elif ch == ord("a"):
310
- if self.alternative_text:
311
- self.text, self.alternative_text = self.alternative_text, self.text
312
- self.screen.clear()
313
- self._parse_text()
314
- self._init_pad()
315
- elif ch == ord("d"):
316
- if self.debug_prompt.text == "":
317
- self.debug_prompt.text = "init"
318
- else:
319
- self.debug_prompt.text = ""
320
- elif ch == ord("n"):
321
- y_offset, x_offset = self.search_next()
322
- margin = 10
323
- elif ch == ord("N"):
324
- y_offset, x_offset = self.search_next(prev=True)
325
- margin = 10
326
- elif ch == ord("/"):
327
- y_offset, x_offset = self._search_prompt()
328
- margin = 10
329
- elif ch == curses.KEY_UP:
330
- y_offset = -1
331
- elif ch == curses.KEY_PPAGE:
332
- y_offset = -10
333
- elif ch == curses.KEY_HOME:
334
- y_offset = -len(self.lines)
335
- elif ch == curses.KEY_DOWN:
336
- y_offset = 1
337
- elif ch == curses.KEY_NPAGE:
338
- y_offset = 10
339
- elif ch == curses.KEY_END:
340
- y_offset = len(self.lines)
341
- elif ch == curses.KEY_LEFT:
342
- x_offset = -1
343
- elif ch == curses.KEY_RIGHT:
344
- x_offset = 1
345
-
346
- if y_offset or x_offset:
347
- y = max(0, y + y_offset)
348
- y = min(self.rows, y)
349
- x = max(0, x + x_offset)
350
- x = min(self.cols, x)
351
-
352
- y_delta = y - (self.top + max_y - margin)
353
- if y_delta > 0:
354
- self.top += y_delta
355
- elif (y - margin) < self.top:
356
- self.top = y
357
-
358
- self.top = min(self.top, len(self.lines) - max_y)
359
-
360
- x_delta = x - (self.left + max_x)
361
- if x_delta > 0:
362
- self.left += x_delta
363
- elif x < self.left:
364
- self.left = x
365
-
366
- x = min(x, pad_max_x - 1)
367
- self.pad.move(y, x)
368
-
369
- if self.debug_prompt.text != "":
370
- debug_line = "y=%s x=%s, x_delta=%s y_delta=%s top=%s, max_y=%s max_x=%s lines=%s" % \
371
- (y, x, x_delta, y_delta, self.top, max_y, max_x, len(self.lines))
372
- self.debug_prompt.text = debug_line
373
- self.debug_prompt.color = "green_bold"
374
- self.debug_prompt.offset = max_x - len(debug_line) - 1
375
-
376
- if self.debug_prompt.text == "":
377
- self.page_position.color = "highlight"
378
- self.page_position.text = "line %s/%s" % (y, len(self.lines))
379
- self.page_position.offset = max_x - len(self.page_position.text) - 1
380
-
381
- self.show()
382
-
383
- def loop(self):
384
- import curses
385
-
386
- res = None
387
- old_cursor = None
388
- try:
389
- self.screen = curses.initscr()
390
- self.screen.leaveok(True)
391
- self.curses_lines = curses.LINES # pylint: disable=maybe-no-member
392
- curses.start_color()
393
- curses.noecho() # no echo key input
394
- curses.cbreak() # input with no-enter keyed
395
- try:
396
- old_cursor = curses.curs_set(2)
397
- except Exception:
398
- pass
399
- self._init_colors()
400
- self._parse_text()
401
- self._init_pad()
402
- self.pad.move(0, 0)
403
- self.show()
404
- res = self._do_commands()
405
- except Exception as err:
406
- get_logger().exception("%s", err)
407
- finally:
408
- if old_cursor is not None:
409
- curses.curs_set(old_cursor)
410
- curses.nocbreak()
411
- curses.echo()
412
- curses.endwin()
413
- return res
414
-
415
-
416
- def init_colors():
417
- import curses
418
-
419
- curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_BLACK)
420
- curses.init_pair(2, curses.COLOR_CYAN, curses.COLOR_BLACK)
421
- curses.init_pair(3, curses.COLOR_RED, curses.COLOR_BLACK)
422
- curses.init_pair(4, curses.COLOR_MAGENTA, curses.COLOR_BLACK)
423
- curses.init_pair(5, curses.COLOR_YELLOW, curses.COLOR_BLACK)
424
- curses.init_pair(6, curses.COLOR_BLUE, curses.COLOR_WHITE)
425
- curses.init_pair(7, curses.COLOR_RED, curses.COLOR_WHITE)
426
- curses.init_pair(8, curses.COLOR_BLACK, curses.COLOR_WHITE)
427
- curses.init_pair(9, curses.COLOR_CYAN, curses.COLOR_BLUE)
428
- return {
429
- "green": curses.color_pair(1),
430
- "green_bold": curses.color_pair(1) | curses.A_BOLD,
431
- "cyan": curses.color_pair(2),
432
- "red": curses.color_pair(3),
433
- "magenta": curses.color_pair(4),
434
- "yellow": curses.color_pair(5),
435
- "blue": curses.color_pair(6),
436
- "highlight": curses.color_pair(7),
437
- None: curses.color_pair(8),
438
- "cyan_blue": curses.color_pair(9),
439
- }
440
-
441
-
442
136
  class RulebookQuestionHandler:
443
137
  def __init__(self, dialogs):
444
138
  self._dialogs = dialogs