-
Notifications
You must be signed in to change notification settings - Fork 15
/
examples.py
executable file
·554 lines (432 loc) · 16.1 KB
/
examples.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
#!/bin/env python3
import itertools
import logging
import os
import subprocess
import sys
import tempfile
import textwrap
import time
from craft_cli import CraftError, EmitterMode, emit
USAGE = """
USAGE: explorator.py <test_id> [<extra1>, [...]]")
E.g.:
explorator.py 04
explorator.py 32 brief extrastuff
"""
def example_01():
"""Show a simple message, the expected command result."""
value = 42
emit.message(f"The meaning of life is {value}.")
def example_02():
"""Show some progress, then the result."""
emit.message("We need to know!")
emit.progress("Building computer...")
time.sleep(1.5)
emit.progress("Asking question...")
time.sleep(1.5)
emit.message("The meaning of life is 42.")
def example_03():
"""Show some progress, with one long delay message, then the result."""
emit.message("We need to know!")
emit.progress("Building computer...")
time.sleep(1.4)
emit.progress("Asking question...")
time.sleep(5)
emit.message("The meaning of life is 42.")
def example_04():
"""Show a progress bar in brief mode."""
emit.message("We need to know!")
emit.progress("Deciding to build a computer or upload it...")
time.sleep(1.5)
with emit.progress_bar("Uploading computer: planetary model", 1788) as progress:
for uploaded in [500, 500, 500, 288]:
progress.advance(uploaded)
time.sleep(1.5)
emit.progress("Asking question...")
time.sleep(1.5)
emit.message("The meaning of life is 42.")
def example_05():
"""Show a verbose/debug/trace messages when it makes sense."""
# set _mode directly to avoid the greeting and log messages that appear when using set_mode()
for mode in EmitterMode:
emit._mode = mode
emit.verbose(f"Verbose message when mode={mode}")
for mode in EmitterMode:
emit._mode = mode
emit.debug(f"Debug message when mode={mode}")
for mode in EmitterMode:
emit._mode = mode
emit.trace(f"Trace message when mode={mode}")
def example_06():
"""Very long emit."""
msg = ""
for i in range(30):
msg += "progress ephemeral blah {} ".format(i)
emit.progress(msg)
time.sleep(5)
msg = ""
for i in range(30):
msg += "progress permanent blah {} ".format(i)
emit.progress(msg, permanent=True)
time.sleep(5)
msg = ""
for i in range(30):
msg += "final bleh {} ".format(i)
emit.message(msg)
def example_07():
"""Show information that comes from a subprocess execution as a stream."""
emit.set_mode(EmitterMode.TRACE)
with emit.open_stream("Running ls") as stream:
subprocess.run(["ls", "-l"], stdout=stream, stderr=stream)
emit.message("Great!")
def example_08():
"""Show some progress that are permanent, mixed with ephemeral, then the result."""
emit.message("We need to know!")
emit.progress("Building computer...", permanent=True)
time.sleep(1)
emit.progress("Assembling stuff...")
time.sleep(1)
emit.progress("Asking question...", permanent=True)
time.sleep(1)
emit.message("The meaning of life is 42.")
def example_09():
"""Show a very simple error."""
path = "/dev/null"
raise CraftError(f"The file is broken; path={path!r}")
def example_10():
"""An error from a 3rd API, normal mode."""
# emit.set_mode(EmitterMode.TRACE)
error = {"message": "Invalid channel", "code": "BAD-CHANNEL"}
raise CraftError("Invalid channel (code 'BAD-CHANNEL')", details=repr(error))
def example_11():
"""Unexpected problem, normal mode."""
raise ValueError("pumba")
def example_12():
"""Unexpected problem, verbose."""
emit.set_mode(EmitterMode.TRACE)
raise ValueError("pumba")
def example_13():
"""User cancelled."""
# emit.set_mode(EmitterMode.TRACE)
emit.progress("Will hang...")
time.sleep(120)
def example_14():
"""Support some library logging."""
logger = logging.getLogger()
logger.setLevel(0)
for mode in EmitterMode:
emit.set_mode(mode)
emit.progress(f"Mode set to {mode}", permanent=True)
logger.error(" some logging in ERROR")
logger.info(" some logging in INFO")
logger.debug(" some logging in DEBUG")
logger.log(5, " some logging in custom level 5")
def example_15():
"""Specific combination of long message with final message in TRACE."""
emit.set_mode(EmitterMode.TRACE)
emit.progress("Asking question...")
time.sleep(3)
emit.message("The meaning of life is 42.")
def example_16():
"""Show a progress bar, but advancing with totals."""
emit.message("We need to know!")
emit.progress("Deciding to build a computer or upload it...")
time.sleep(1.5)
with emit.progress_bar("Uploading computer: planetary model", 1788, delta=False) as progress:
for uploaded in [500, 1000, 1500, 1788]:
progress.advance(uploaded)
time.sleep(1.5)
emit.progress("Asking question...")
time.sleep(1.5)
emit.message("The meaning of life is 42.")
def example_17():
"""Raise an error chaining other."""
def f():
raise ValueError("pumba")
emit.set_mode(EmitterMode.VERBOSE)
emit.progress("Start to work", permanent=True)
try:
f()
except ValueError as exc:
raise CraftError("Exploded while working :(") from exc
def example_18():
"""Show information that comes from a subprocess execution as a stream."""
emit.set_mode(EmitterMode.TRACE)
with emit.open_stream("Running a two parts something that will take time") as stream:
cmd = "sleep 5 && echo Part 1 && sleep 5 && echo Part 2"
subprocess.run(cmd, stdout=stream, stderr=stream, shell=True)
emit.message("All done.")
def example_19():
"""Support some deep inside library logging."""
emit.set_mode(EmitterMode.TRACE)
logger = logging.getLogger("foobar.__main__")
logger.setLevel(logging.DEBUG)
logger.debug("Some logging in DEBUG")
def example_20():
"""Show information that comes from a subprocess execution as a stream, Windows version."""
emit.set_mode(EmitterMode.TRACE)
with emit.open_stream("Running a simple Windows command") as stream:
subprocess.run(["python.exe", "-V"], stdout=stream, stderr=subprocess.STDOUT)
emit.message("Great!")
def _run_subprocess_with_emitter(mode):
"""Write a temp app that uses emitter and run it."""
emit.set_mode(mode)
example_test_sub_app = textwrap.dedent(
"""
import sys
import time
from craft_cli import emit, EmitterMode
mode = EmitterMode[sys.argv[1]]
emit.init(mode, "subapp", "An example sub application.")
emit.progress("Sub app: starting")
time.sleep(6)
emit.progress("Sub app: Lot of work")
time.sleep(6)
emit.message("Sub app: Done")
emit.ended_ok()
"""
)
temp_fh, temp_name = tempfile.mkstemp()
with open(temp_fh, "wt", encoding="utf8") as fh:
fh.write(example_test_sub_app)
emit.progress("We're about to test a sub app")
time.sleep(3)
with emit.pause():
subprocess.run([sys.executable, temp_name, mode.name], env={"PYTHONPATH": os.getcwd()})
# note we cannot use `emit` while paused!
os.unlink(temp_name)
emit.message("All done!")
def example_21():
"""Run an app that uses emitter in a subprocess, pausing the external control, brief mode."""
_run_subprocess_with_emitter(EmitterMode.BRIEF)
def example_22():
"""Run an app that uses emitter in a subprocess, pausing the external control, trace mode."""
_run_subprocess_with_emitter(EmitterMode.TRACE)
def example_23():
"""Capture output from an app that uses emitter."""
emit.set_mode(EmitterMode.TRACE)
example_test_sub_app = textwrap.dedent(
"""
import time
from craft_cli import emit, EmitterMode
emit.init(EmitterMode.BRIEF, "subapp", "An example sub application.")
emit.progress("Sub app: starting")
time.sleep(6)
emit.progress("Sub app: Lot of work")
time.sleep(1)
emit.message("Sub app: Done")
emit.ended_ok()
"""
)
temp_fh, temp_name = tempfile.mkstemp()
with open(temp_fh, "wt", encoding="utf8") as fh:
fh.write(example_test_sub_app)
emit.progress("Running subprocess...")
cmd = [sys.executable, temp_name]
proc = subprocess.run(cmd, env={"PYTHONPATH": os.getcwd()}, capture_output=True, text=True)
os.unlink(temp_name)
emit.message("Captured output:")
for line in filter(None, itertools.chain(proc.stderr.split("\n"), proc.stdout.split("\n"))):
emit.message(f":: {line}")
def example_24():
"""Show a progress bar in verbose mode."""
emit.set_mode(EmitterMode.VERBOSE)
emit.progress("We need to know!", permanent=True)
emit.progress("Deciding to build a computer or upload it...")
time.sleep(1.5)
with emit.progress_bar("Uploading computer: planetary model", 1788) as progress:
for uploaded in [500, 500, 500, 288]:
progress.advance(uploaded)
time.sleep(1.5)
emit.progress("Asking question...")
time.sleep(1.5)
emit.message("The meaning of life is 42.")
def example_25():
"""Show a progress bar in debug mode."""
emit.set_mode(EmitterMode.DEBUG)
emit.progress("We need to know!", permanent=True)
emit.progress("Deciding to build a computer or upload it...")
time.sleep(1.5)
with emit.progress_bar("Uploading computer: planetary model", 1788) as progress:
for uploaded in [500, 500, 500, 288]:
progress.advance(uploaded)
time.sleep(1.5)
emit.progress("Asking question...")
time.sleep(1.5)
emit.message("The meaning of life is 42.")
def example_26():
"""Show emitter progress message handover.
This example demonstrates seamless emitter progress message handover
between two craft tools. Handover uses emit.pause() on the local
craft tool before an LXD launched craft tool takes over, and hands back.
"""
emit.set_mode(EmitterMode.BRIEF)
lxd_craft_tool = textwrap.dedent(
"""
import time
from craft_cli import emit, EmitterMode
emit.init(EmitterMode.BRIEF, "subapp", "An example sub application.")
emit.progress("seamless progress #2")
time.sleep(2)
emit.progress("seamless progress #3")
time.sleep(2)
emit.ended_ok()
"""
)
temp_fh, temp_name = tempfile.mkstemp()
with open(temp_fh, "wt", encoding="utf8") as fh:
fh.write(lxd_craft_tool)
emit.message("Application Start.")
emit.progress("seamless progress #1")
time.sleep(2)
with emit.pause():
cmd = [sys.executable, temp_name]
subprocess.run(cmd, env={"PYTHONPATH": os.getcwd()}, capture_output=False, text=True)
os.unlink(temp_name)
emit.progress("seamless progress #4")
time.sleep(2)
emit.message("Application End.")
def _run_noisy_subprocess(mode_name, total_messages, subprocess_code):
"""Capture the output of a noisy subprocess in different modes."""
mode = EmitterMode[mode_name.upper()]
emit.set_mode(mode)
temp_fh, temp_name = tempfile.mkstemp()
with open(temp_fh, "wt", encoding="utf8") as fh:
fh.write(subprocess_code)
emit.progress("About to run a noisy subprocess")
time.sleep(1)
with emit.open_stream("Running custom Python app in unbuffered mode") as stream:
cmd = [sys.executable, "-u", temp_name, str(total_messages)]
subprocess.check_call(cmd, stdout=stream, stderr=stream)
os.unlink(temp_name)
emit.message("All done!")
def example_27(mode_name, total_messages=10):
"""Capture the output of a noisy subprocess in different modes."""
example_test_sub_app = textwrap.dedent(
"""
import sys
import time
from random import random, randint, sample
from string import ascii_lowercase as letters
total = int(sys.argv[1])
for idx in range(total):
short = random() > .2
delay = random() / 4 if short else random() * 2
delay_for_spinner = random() > .9
more = " ".join("".join(sample(letters, randint(3, 8))) for _ in range(randint(5, 30)))
print(f"Noisy message {idx} / {total} -- {more}", flush=True)
time.sleep(delay)
if delay_for_spinner:
time.sleep(5)
"""
)
_run_noisy_subprocess(mode_name, total_messages, example_test_sub_app)
def example_28(mode_name, total_messages=10):
"""Capture the multi-line, tab-containing output of a noisy subprocess."""
example_test_sub_app = textwrap.dedent(
"""
import sys
import time
import textwrap
total = int(sys.argv[1])
for idx in range(total):
#short = random() > .2
delay = 1
delay_for_spinner = False # random() > .9
message = textwrap.dedent(f'''
This first message should never appear.
\tThis second message shouldn't appear either.
\tThis line should appear, preceded ":: " ({idx} / {total}).
''').strip()
print(message,flush=True)
time.sleep(delay)
if delay_for_spinner:
time.sleep(5)
"""
)
_run_noisy_subprocess(mode_name, total_messages, example_test_sub_app)
def example_29(mode_name, streaming_brief):
"""Support some library logging."""
logger = logging.getLogger()
logger.setLevel(0)
mode = EmitterMode[mode_name.upper()]
emit.init(mode, "example_29", "Hi", streaming_brief=bool(int(streaming_brief)))
emit.progress(f"Mode set to {mode}", permanent=True)
emit.progress("Starting up lib1", permanent=False)
_call_lib(logger, 1)
emit.progress("Finished lib1", permanent=True)
emit.progress("Starting up lib2", permanent=False)
_call_lib(logger, 2)
emit.progress("Finished lib2", permanent=True)
emit.progress("Starting up lib3", permanent=False)
_call_lib(logger, 3)
emit.progress("Finished lib3", permanent=True)
def _call_lib(logger, index):
lib = f"lib{index}"
time.sleep(2)
logger.info(f" {lib} INFO 1")
logger.debug(f" {lib} DEBUG 1")
time.sleep(2)
logger.info(f" {lib} INFO 2")
logger.debug(f" {lib} DEBUG 2")
time.sleep(2)
def example_30():
"""Message spamming, noting the different spinner behaviour"""
emit.progress(
"Message spamming example. The same message will be spammed for 10s, but "
"it will appear as one message with a spinner.",
permanent=True,
)
end_time = time.monotonic() + 10
while time.monotonic() < end_time:
emit.progress("SPAM SPAM SPAM SPAM")
time.sleep(0.001)
emit.progress(
"Now two separate messages will be spammed and no spinner appear.", permanent=True
)
end_time = time.monotonic() + 10
while time.monotonic() < end_time:
emit.progress("SPAM SPAM SPAM SPAM")
time.sleep(0.01)
emit.progress("SPAM SPAM SPAM baked beans")
time.sleep(0.01)
emit.progress("And back to the first message!", permanent=True)
end_time = time.monotonic() + 10
while time.monotonic() < end_time:
emit.progress("SPAM SPAM SPAM SPAM")
time.sleep(0.001)
def example_31():
"""Multiline error message."""
emit.progress("Setting up computer for build...")
time.sleep(1)
emit.progress("A long progress message")
time.sleep(6)
raise CraftError("Error 1\nError 2")
# -- end of test cases
if len(sys.argv) < 2:
print(USAGE)
exit()
name = f"example_{int(sys.argv[1]):02d}"
func = globals().get(name)
if func is None:
print(f"ERROR: function {name!r} not found")
exit()
if int(sys.argv[1]) != 29:
emit.init(EmitterMode.BRIEF, "explorator", "Greetings earthlings")
try:
func(*sys.argv[2:])
except CraftError as err:
emit.error(err)
except KeyboardInterrupt as exc:
msg = "User cancelled"
error = CraftError(msg)
error.__cause__ = exc
emit.error(error)
except Exception as exc:
msg = f"Unexpected internal exception: {exc!r}"
error = CraftError(msg)
error.__cause__ = exc
emit.error(error)
else:
emit.ended_ok()