multiCMD 1.64__tar.gz → 1.66__tar.gz

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 (43) hide show
  1. {multicmd-1.64 → multicmd-1.66}/PKG-INFO +37 -3
  2. {multicmd-1.64 → multicmd-1.66}/README.md +36 -2
  3. {multicmd-1.64 → multicmd-1.66}/multiCMD.egg-info/PKG-INFO +37 -3
  4. {multicmd-1.64 → multicmd-1.66}/multiCMD.py +110 -47
  5. multicmd-1.66/tests/test_helpers.py +888 -0
  6. {multicmd-1.64 → multicmd-1.66}/tests/test_windows.py +51 -1
  7. multicmd-1.64/tests/test_helpers.py +0 -162
  8. {multicmd-1.64 → multicmd-1.66}/LICENSE +0 -0
  9. {multicmd-1.64 → multicmd-1.66}/multiCMD.egg-info/SOURCES.txt +0 -0
  10. {multicmd-1.64 → multicmd-1.66}/multiCMD.egg-info/dependency_links.txt +0 -0
  11. {multicmd-1.64 → multicmd-1.66}/multiCMD.egg-info/entry_points.txt +0 -0
  12. {multicmd-1.64 → multicmd-1.66}/multiCMD.egg-info/top_level.txt +0 -0
  13. {multicmd-1.64 → multicmd-1.66}/scripts/multiCMD +0 -0
  14. {multicmd-1.64 → multicmd-1.66}/setup.cfg +0 -0
  15. {multicmd-1.64 → multicmd-1.66}/setup.py +0 -0
  16. {multicmd-1.64 → multicmd-1.66}/tests/test_api.py +0 -0
  17. {multicmd-1.64 → multicmd-1.66}/tests/test_async_executor.py +0 -0
  18. {multicmd-1.64 → multicmd-1.66}/tests/test_cli.py +0 -0
  19. {multicmd-1.64 → multicmd-1.66}/tests/test_cli_closed_stdout.py +0 -0
  20. {multicmd-1.64 → multicmd-1.66}/tests/test_cli_failed_stdout.py +0 -0
  21. {multicmd-1.64 → multicmd-1.66}/tests/test_cli_input.py +0 -0
  22. {multicmd-1.64 → multicmd-1.66}/tests/test_cli_summary.py +0 -0
  23. {multicmd-1.64 → multicmd-1.66}/tests/test_display.py +0 -0
  24. {multicmd-1.64 → multicmd-1.66}/tests/test_embed.py +0 -0
  25. {multicmd-1.64 → multicmd-1.66}/tests/test_env.py +0 -0
  26. {multicmd-1.64 → multicmd-1.66}/tests/test_expand_ranges.py +0 -0
  27. {multicmd-1.64 → multicmd-1.66}/tests/test_handover.py +0 -0
  28. {multicmd-1.64 → multicmd-1.66}/tests/test_interrupts.py +0 -0
  29. {multicmd-1.64 → multicmd-1.66}/tests/test_packaging.py +0 -0
  30. {multicmd-1.64 → multicmd-1.66}/tests/test_parsing.py +0 -0
  31. {multicmd-1.64 → multicmd-1.66}/tests/test_pass_fds.py +0 -0
  32. {multicmd-1.64 → multicmd-1.66}/tests/test_ping.py +0 -0
  33. {multicmd-1.64 → multicmd-1.66}/tests/test_pool.py +0 -0
  34. {multicmd-1.64 → multicmd-1.66}/tests/test_process_groups.py +0 -0
  35. {multicmd-1.64 → multicmd-1.66}/tests/test_pty.py +0 -0
  36. {multicmd-1.64 → multicmd-1.66}/tests/test_reader.py +0 -0
  37. {multicmd-1.64 → multicmd-1.66}/tests/test_reader_quit.py +0 -0
  38. {multicmd-1.64 → multicmd-1.66}/tests/test_regressions.py +0 -0
  39. {multicmd-1.64 → multicmd-1.66}/tests/test_run_command.py +0 -0
  40. {multicmd-1.64 → multicmd-1.66}/tests/test_run_commands.py +0 -0
  41. {multicmd-1.64 → multicmd-1.66}/tests/test_sudo.py +0 -0
  42. {multicmd-1.64 → multicmd-1.66}/tests/test_supervision.py +0 -0
  43. {multicmd-1.64 → multicmd-1.66}/tests/test_task.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: multiCMD
3
- Version: 1.64
3
+ Version: 1.66
4
4
  Summary: Run commands simultaneously
5
5
  Home-page: https://github.com/yufei-pan/multiCMD
6
6
  Author: Yufei Pan
@@ -559,6 +559,40 @@ pytest -m "not slow" # skip intentional sleep-heavy cases
559
559
 
560
560
  `test_expand_ranges_perf.py` is a manual performance microbenchmark and is not collected by pytest.
561
561
 
562
+ ## Upgrading to 1.66 from 1.65
563
+
564
+ - Nothing needs doing for `mcmd`, which is unchanged, nor for hpcp 9.92, which checks its sizes itself. hpcp up to 9.91
565
+ hands `-L`, `-F`, `-dis` and `-ddr` to `format_bytes()` as typed and takes its `0` as the size: with 1.66 a value
566
+ with text after the size is `0`, so `-L 10MB/s` there is no limit and `-ddr 100G,50G` a 0-byte partition (1.65
567
+ read 100 G). Upgrade hpcp to 9.92 before or with 1.66, as for 1.65.
568
+ - Changed, library: at a terminal `input_with_timeout_and_countdown()` returns when its timeout runs out, whatever was
569
+ typed: text with no Enter (typed, then one Ctrl-D) is no answer, and a program that loaded `readline` waits no longer
570
+ (1.65: it waited for the line to end). With `readline` loaded, only the terminal's own editing works at this prompt
571
+ ([docs/limitations.md](docs/limitations.md)). A line ended before then, by Enter or by the end of the input, is the
572
+ answer. `timeout` may be a float (1.65: `TypeError`). A stdin that is not a terminal is read as in 1.65: a pipe that
573
+ sent part of a line still holds the call ([docs/limitations.md](docs/limitations.md)). On Windows a key pressed with
574
+ no Enter still holds it ([docs/limitations.md](docs/limitations.md)). A program that read part of a line at a
575
+ terminal itself keeps the rest of it ([docs/limitations.md](docs/limitations.md)).
576
+ - Changed, library: `format_bytes()` takes a string only when all of it is a size: text after the unit (`'5Gb/s'`, `'1e9'`,
577
+ `'5 kB extra'`) or an `i` with no prefix (`'5 iB'`) gives `0` and the "Invalid size format" line, as an unknown unit does
578
+ (1.65: it read the start). Blanks around the size are fine, now before it too. A unit after a number that ends in a point
579
+ is read (`'5.G'`: 5 G; 1.65: 5). With `to_str` such a string comes back as given.
580
+
581
+ ## Upgrading to 1.65 from 1.64
582
+
583
+ - Nothing needs doing for `mcmd`, which is unchanged. hpcp users upgrade hpcp to 9.92 before or with multiCMD 1.65: hpcp up
584
+ to 9.91 takes the `0` below as a size where 1.64 stopped with a `TypeError`, and 9.92 refuses a size with a unit it cannot
585
+ read before anything is touched.
586
+ - Changed, library: `format_bytes()` given a string with a unit it does not know (`'5 foo'`) returns `0` and writes the
587
+ "Invalid size format" line to stderr, as for a string with no number (1.64: `None`). A program that cannot tell this `0`
588
+ from a real one checks the string itself.
589
+ - Changed, library: `input_with_timeout_and_countdown()` counts an ended stdin (Ctrl-D, `/dev/null`), a closed one or none
590
+ as no answer: it returns `None` when the countdown ends, never sooner (1.64: an exception). It goes on asking meanwhile,
591
+ so Ctrl-C still aborts and a line typed before the end is taken. A program that caught `EOFError` to skip the prompt now
592
+ waits the countdown out.
593
+ - Known, planned for 1.66 ([docs/limitations.md](docs/limitations.md)): Ctrl-D after a half-typed line stops the countdown
594
+ until the line is ended, and `format_bytes()` ignores text after the size (`'5Gb/s'` is 5 G).
595
+
562
596
  ## Upgrading to 1.64 from 1.63
563
597
 
564
598
  - Changed, library: once the reader of stdout quit, a shown command whose output keeps coming stops its call: the call's
@@ -572,9 +606,9 @@ pytest -m "not slow" # skip intentional sleep-heavy cases
572
606
  no longer stopped by another write's failure (docs/limitations.md).
573
607
  - Changed, library: a shown dry run whose line finds the reader gone raises `BrokenPipeError` if it waits, whenever it began
574
608
  (1.63: a dry run made after the reader quit returned).
575
- - hpcp 9.89 and 9.90 cloning a disk (`-dd`) with 1.64 installed beside them: when the reader quits, a delayed `e2fsck` that
609
+ - hpcp 9.89 to 9.91 cloning a disk (`-dd`) with 1.64 installed beside them: when the reader quits, a delayed `e2fsck` that
576
610
  prints for longer than 1 s is cut, with the `tune2fs` queued after it, and its call raises before the loop devices are
577
- detached, where 1.63 ran it unseen; a later hpcp runs its cleanup quiet.
611
+ detached, where 1.63 ran it unseen; hpcp 9.92 runs its cleanup quiet.
578
612
  - Changed: `input_with_timeout_and_countdown()` writes its prompt and countdown to stderr, after flushing stdout, and
579
613
  `format_bytes()` its error message (1.63: stdout, where a dead one raised `BrokenPipeError` and `> log` hid the prompt).
580
614
  - Fix: a `sys.stdout` the program closed loses the output and every command runs (1.63: `ValueError`, and nothing ran), and
@@ -533,6 +533,40 @@ pytest -m "not slow" # skip intentional sleep-heavy cases
533
533
 
534
534
  `test_expand_ranges_perf.py` is a manual performance microbenchmark and is not collected by pytest.
535
535
 
536
+ ## Upgrading to 1.66 from 1.65
537
+
538
+ - Nothing needs doing for `mcmd`, which is unchanged, nor for hpcp 9.92, which checks its sizes itself. hpcp up to 9.91
539
+ hands `-L`, `-F`, `-dis` and `-ddr` to `format_bytes()` as typed and takes its `0` as the size: with 1.66 a value
540
+ with text after the size is `0`, so `-L 10MB/s` there is no limit and `-ddr 100G,50G` a 0-byte partition (1.65
541
+ read 100 G). Upgrade hpcp to 9.92 before or with 1.66, as for 1.65.
542
+ - Changed, library: at a terminal `input_with_timeout_and_countdown()` returns when its timeout runs out, whatever was
543
+ typed: text with no Enter (typed, then one Ctrl-D) is no answer, and a program that loaded `readline` waits no longer
544
+ (1.65: it waited for the line to end). With `readline` loaded, only the terminal's own editing works at this prompt
545
+ ([docs/limitations.md](docs/limitations.md)). A line ended before then, by Enter or by the end of the input, is the
546
+ answer. `timeout` may be a float (1.65: `TypeError`). A stdin that is not a terminal is read as in 1.65: a pipe that
547
+ sent part of a line still holds the call ([docs/limitations.md](docs/limitations.md)). On Windows a key pressed with
548
+ no Enter still holds it ([docs/limitations.md](docs/limitations.md)). A program that read part of a line at a
549
+ terminal itself keeps the rest of it ([docs/limitations.md](docs/limitations.md)).
550
+ - Changed, library: `format_bytes()` takes a string only when all of it is a size: text after the unit (`'5Gb/s'`, `'1e9'`,
551
+ `'5 kB extra'`) or an `i` with no prefix (`'5 iB'`) gives `0` and the "Invalid size format" line, as an unknown unit does
552
+ (1.65: it read the start). Blanks around the size are fine, now before it too. A unit after a number that ends in a point
553
+ is read (`'5.G'`: 5 G; 1.65: 5). With `to_str` such a string comes back as given.
554
+
555
+ ## Upgrading to 1.65 from 1.64
556
+
557
+ - Nothing needs doing for `mcmd`, which is unchanged. hpcp users upgrade hpcp to 9.92 before or with multiCMD 1.65: hpcp up
558
+ to 9.91 takes the `0` below as a size where 1.64 stopped with a `TypeError`, and 9.92 refuses a size with a unit it cannot
559
+ read before anything is touched.
560
+ - Changed, library: `format_bytes()` given a string with a unit it does not know (`'5 foo'`) returns `0` and writes the
561
+ "Invalid size format" line to stderr, as for a string with no number (1.64: `None`). A program that cannot tell this `0`
562
+ from a real one checks the string itself.
563
+ - Changed, library: `input_with_timeout_and_countdown()` counts an ended stdin (Ctrl-D, `/dev/null`), a closed one or none
564
+ as no answer: it returns `None` when the countdown ends, never sooner (1.64: an exception). It goes on asking meanwhile,
565
+ so Ctrl-C still aborts and a line typed before the end is taken. A program that caught `EOFError` to skip the prompt now
566
+ waits the countdown out.
567
+ - Known, planned for 1.66 ([docs/limitations.md](docs/limitations.md)): Ctrl-D after a half-typed line stops the countdown
568
+ until the line is ended, and `format_bytes()` ignores text after the size (`'5Gb/s'` is 5 G).
569
+
536
570
  ## Upgrading to 1.64 from 1.63
537
571
 
538
572
  - Changed, library: once the reader of stdout quit, a shown command whose output keeps coming stops its call: the call's
@@ -546,9 +580,9 @@ pytest -m "not slow" # skip intentional sleep-heavy cases
546
580
  no longer stopped by another write's failure (docs/limitations.md).
547
581
  - Changed, library: a shown dry run whose line finds the reader gone raises `BrokenPipeError` if it waits, whenever it began
548
582
  (1.63: a dry run made after the reader quit returned).
549
- - hpcp 9.89 and 9.90 cloning a disk (`-dd`) with 1.64 installed beside them: when the reader quits, a delayed `e2fsck` that
583
+ - hpcp 9.89 to 9.91 cloning a disk (`-dd`) with 1.64 installed beside them: when the reader quits, a delayed `e2fsck` that
550
584
  prints for longer than 1 s is cut, with the `tune2fs` queued after it, and its call raises before the loop devices are
551
- detached, where 1.63 ran it unseen; a later hpcp runs its cleanup quiet.
585
+ detached, where 1.63 ran it unseen; hpcp 9.92 runs its cleanup quiet.
552
586
  - Changed: `input_with_timeout_and_countdown()` writes its prompt and countdown to stderr, after flushing stdout, and
553
587
  `format_bytes()` its error message (1.63: stdout, where a dead one raised `BrokenPipeError` and `> log` hid the prompt).
554
588
  - Fix: a `sys.stdout` the program closed loses the output and every command runs (1.63: `ValueError`, and nothing ran), and
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: multiCMD
3
- Version: 1.64
3
+ Version: 1.66
4
4
  Summary: Run commands simultaneously
5
5
  Home-page: https://github.com/yufei-pan/multiCMD
6
6
  Author: Yufei Pan
@@ -559,6 +559,40 @@ pytest -m "not slow" # skip intentional sleep-heavy cases
559
559
 
560
560
  `test_expand_ranges_perf.py` is a manual performance microbenchmark and is not collected by pytest.
561
561
 
562
+ ## Upgrading to 1.66 from 1.65
563
+
564
+ - Nothing needs doing for `mcmd`, which is unchanged, nor for hpcp 9.92, which checks its sizes itself. hpcp up to 9.91
565
+ hands `-L`, `-F`, `-dis` and `-ddr` to `format_bytes()` as typed and takes its `0` as the size: with 1.66 a value
566
+ with text after the size is `0`, so `-L 10MB/s` there is no limit and `-ddr 100G,50G` a 0-byte partition (1.65
567
+ read 100 G). Upgrade hpcp to 9.92 before or with 1.66, as for 1.65.
568
+ - Changed, library: at a terminal `input_with_timeout_and_countdown()` returns when its timeout runs out, whatever was
569
+ typed: text with no Enter (typed, then one Ctrl-D) is no answer, and a program that loaded `readline` waits no longer
570
+ (1.65: it waited for the line to end). With `readline` loaded, only the terminal's own editing works at this prompt
571
+ ([docs/limitations.md](docs/limitations.md)). A line ended before then, by Enter or by the end of the input, is the
572
+ answer. `timeout` may be a float (1.65: `TypeError`). A stdin that is not a terminal is read as in 1.65: a pipe that
573
+ sent part of a line still holds the call ([docs/limitations.md](docs/limitations.md)). On Windows a key pressed with
574
+ no Enter still holds it ([docs/limitations.md](docs/limitations.md)). A program that read part of a line at a
575
+ terminal itself keeps the rest of it ([docs/limitations.md](docs/limitations.md)).
576
+ - Changed, library: `format_bytes()` takes a string only when all of it is a size: text after the unit (`'5Gb/s'`, `'1e9'`,
577
+ `'5 kB extra'`) or an `i` with no prefix (`'5 iB'`) gives `0` and the "Invalid size format" line, as an unknown unit does
578
+ (1.65: it read the start). Blanks around the size are fine, now before it too. A unit after a number that ends in a point
579
+ is read (`'5.G'`: 5 G; 1.65: 5). With `to_str` such a string comes back as given.
580
+
581
+ ## Upgrading to 1.65 from 1.64
582
+
583
+ - Nothing needs doing for `mcmd`, which is unchanged. hpcp users upgrade hpcp to 9.92 before or with multiCMD 1.65: hpcp up
584
+ to 9.91 takes the `0` below as a size where 1.64 stopped with a `TypeError`, and 9.92 refuses a size with a unit it cannot
585
+ read before anything is touched.
586
+ - Changed, library: `format_bytes()` given a string with a unit it does not know (`'5 foo'`) returns `0` and writes the
587
+ "Invalid size format" line to stderr, as for a string with no number (1.64: `None`). A program that cannot tell this `0`
588
+ from a real one checks the string itself.
589
+ - Changed, library: `input_with_timeout_and_countdown()` counts an ended stdin (Ctrl-D, `/dev/null`), a closed one or none
590
+ as no answer: it returns `None` when the countdown ends, never sooner (1.64: an exception). It goes on asking meanwhile,
591
+ so Ctrl-C still aborts and a line typed before the end is taken. A program that caught `EOFError` to skip the prompt now
592
+ waits the countdown out.
593
+ - Known, planned for 1.66 ([docs/limitations.md](docs/limitations.md)): Ctrl-D after a half-typed line stops the countdown
594
+ until the line is ended, and `format_bytes()` ignores text after the size (`'5Gb/s'` is 5 G).
595
+
562
596
  ## Upgrading to 1.64 from 1.63
563
597
 
564
598
  - Changed, library: once the reader of stdout quit, a shown command whose output keeps coming stops its call: the call's
@@ -572,9 +606,9 @@ pytest -m "not slow" # skip intentional sleep-heavy cases
572
606
  no longer stopped by another write's failure (docs/limitations.md).
573
607
  - Changed, library: a shown dry run whose line finds the reader gone raises `BrokenPipeError` if it waits, whenever it began
574
608
  (1.63: a dry run made after the reader quit returned).
575
- - hpcp 9.89 and 9.90 cloning a disk (`-dd`) with 1.64 installed beside them: when the reader quits, a delayed `e2fsck` that
609
+ - hpcp 9.89 to 9.91 cloning a disk (`-dd`) with 1.64 installed beside them: when the reader quits, a delayed `e2fsck` that
576
610
  prints for longer than 1 s is cut, with the `tune2fs` queued after it, and its call raises before the loop devices are
577
- detached, where 1.63 ran it unseen; a later hpcp runs its cleanup quiet.
611
+ detached, where 1.63 ran it unseen; hpcp 9.92 runs its cleanup quiet.
578
612
  - Changed: `input_with_timeout_and_countdown()` writes its prompt and countdown to stderr, after flushing stdout, and
579
613
  `format_bytes()` its error message (1.63: stdout, where a dead one raised `BrokenPipeError` and `> log` hid the prompt).
580
614
  - Fix: a `sys.stdout` the program closed loses the output and every command runs (1.63: `ValueError`, and nothing ran), and
@@ -29,9 +29,9 @@ import traceback
29
29
  from pprint import pformat
30
30
 
31
31
  #%% global vars
32
- version = '1.64'
32
+ version = '1.66'
33
33
  __version__ = version
34
- COMMIT_DATE = '2026-11-13'
34
+ COMMIT_DATE = '2026-11-14'
35
35
  # fire-and-forget threads (run_commands with wait_for_return=False) that join_threads() waits on. multiCMD takes no
36
36
  # lock for it: each update is one set operation and each snapshot one set.copy(), as for _live_procs below
37
37
  __running_threads = set()
@@ -3250,40 +3250,102 @@ def _run_cli():
3250
3250
  #%% misc functions
3251
3251
  def input_with_timeout_and_countdown(timeout, prompt='Please enter your selection'):
3252
3252
  """
3253
- Read an input from the user with a timeout and a countdown. The prompt and its countdown go to stderr, after what is
3253
+ Read a line from the user, counting down timeout seconds. The prompt and its countdown go to stderr, after what is
3254
3254
  waiting in stdout's buffer ( the caller's question ); one that cannot be written is dropped, and the timeout still applies.
3255
3255
 
3256
3256
  @params:
3257
- timeout: The timeout in seconds
3257
+ timeout: The timeout in seconds, an int or a float ( the countdown shows the seconds left, rounded up )
3258
3258
  prompt: The prompt to display to the user
3259
3259
 
3260
3260
  @returns:
3261
- str: The input from the user or None if no input was received
3261
+ str: The line typed before the timeout ran out, stripped ( Enter ends it, and so does the end of the input after text );
3262
+ or None if no input was received by then: text not ended, and a stdin that has ended, is closed or is missing
3263
+ ( Ctrl-D, /dev/null ), count as no answer, and the call returns when the timeout runs out, never sooner, and at a
3264
+ terminal never later ( a stdin that is not a terminal, and Windows: docs/limitations.md )
3262
3265
 
3263
3266
  """
3264
3267
  # the question the caller printed goes out first; the prompt and its countdown are for the person at the terminal, not for
3265
3268
  # stdout's reader: they go to stderr, as read -p's and rm -i's do, and one that cannot be written is dropped
3266
3269
  _display.flush()
3267
- _write_error(f"{prompt} [{timeout}s]: ", end='')
3270
+ _write_error(f"{prompt} [{math.ceil(timeout)}s]: ", end='')
3268
3271
  # select() cannot watch stdin on Windows; msvcrt can tell whether a key is waiting
3269
3272
  try:
3270
3273
  import msvcrt
3271
3274
  except ImportError:
3272
3275
  msvcrt = None
3273
- # Loop until the timeout
3274
- for remaining in range(timeout, 0, -1):
3275
- # If there is an input, return it
3276
- if msvcrt is not None:
3277
- if msvcrt.kbhit():
3278
- return input().strip()
3279
- elif sys.stdin in select.select([sys.stdin], [], [], 0)[0]:
3280
- return input().strip()
3281
- # Print the remaining time
3282
- _write_error(f"\r{prompt} [{remaining}s]: ", end='')
3283
- # Wait a second
3284
- time.sleep(1)
3285
- # If there is no input, return None
3286
- return None
3276
+ # a stdin that is missing or closed is not asked: the countdown runs out
3277
+ no_stdin = sys.stdin is None or getattr(sys.stdin, 'closed', False)
3278
+ fd = None
3279
+ if msvcrt is None and not no_stdin:
3280
+ # a stdin select() cannot watch ( no descriptor: a StringIO ) raises here: it was not asked, so it is not "no answer"
3281
+ fd = sys.stdin.fileno()
3282
+ # the count is by the clock. A terminal is read by its descriptor, and only as far as select() says there is something:
3283
+ # input() would wait for a whole line with no timeout ( text typed and then Ctrl-D, a program that loaded readline ). In
3284
+ # its line mode it gives at most a line per read, and must be read in whole reads: macOS tells text that a Ctrl-D handed
3285
+ # over from the end of the input only so; out of its line mode ( cbreak, raw ) one read gives all that was typed, the
3286
+ # next line with it, so it is read a byte at a time. Anything else ( a pipe, a file ) is read by Python itself, as in
3287
+ # 1.65: nobody is typing there, and only Python knows what the program's own reading already holds
3288
+ tty = canon = False
3289
+ if fd is not None:
3290
+ try:
3291
+ import termios
3292
+ canon = bool(termios.tcgetattr(fd)[3] & termios.ICANON)
3293
+ tty = True
3294
+ except Exception:
3295
+ # not a terminal, one that is gone, or no termios
3296
+ pass
3297
+ deadline = time.monotonic() + timeout
3298
+ typed = bytearray()
3299
+ shown = None
3300
+ while True:
3301
+ remaining = deadline - time.monotonic()
3302
+ seconds = max(math.ceil(remaining), 0)
3303
+ if seconds and seconds != shown:
3304
+ shown = seconds
3305
+ _write_error(f"\r{prompt} [{seconds}s]: ", end='')
3306
+ # stdin is watched until the next whole second, and asked once more when the countdown ends
3307
+ wait = remaining - (seconds - 1) if seconds else 0
3308
+ # ended: nothing waited for stdin this second ( none to watch, the console, or its end ), so time.sleep() waits
3309
+ ended = fd is None
3310
+ try:
3311
+ if msvcrt is not None:
3312
+ # the console is asked once a second
3313
+ if not no_stdin and msvcrt.kbhit():
3314
+ return input().strip()
3315
+ elif fd is not None and select.select([fd], [], [], wait)[0]:
3316
+ if not tty:
3317
+ return input().strip()
3318
+ data = os.read(fd, 4096 if canon else 1)
3319
+ typed += data
3320
+ if data and b'\n' not in data:
3321
+ # read on at once: the rest of the line is there, or comes. Once the countdown has ended, up to 4096 bytes: the longest
3322
+ # line a terminal's line mode takes in one piece ( Linux 4095 bytes and the newline, macOS 1023; measured ). So a line of
3323
+ # up to that length typed before the end is read to its end, also when that takes many reads ( out of the line mode a
3324
+ # read is a byte; a Ctrl-D splits a line ), and text that keeps coming stops within 4097 reads: more than 4096 bytes with
3325
+ # no end of the line is no answer
3326
+ if remaining <= 0 and len(typed) > 4096:
3327
+ return None
3328
+ continue
3329
+ if typed:
3330
+ # Enter, or the end of the input after text ( Ctrl-D twice )
3331
+ line = bytes(typed).split(b'\n', 1)[0]
3332
+ # decoded as stdin itself decodes: bytes that are not text are kept under the C locale ( surrogateescape ), and a
3333
+ # strict stdin raises, as in 1.65 and as input() does for any other stdin
3334
+ return line.decode(getattr(sys.stdin, 'encoding', None) or 'utf-8', getattr(sys.stdin, 'errors', None) or 'strict').strip()
3335
+ # a Ctrl-D: no answer this second
3336
+ ended = True
3337
+ except io.UnsupportedOperation:
3338
+ # a stdin that cannot be read ( one the program opened for writing ) was not asked: not "no answer", as in 1.65
3339
+ raise
3340
+ except (EOFError, OSError):
3341
+ # no answer this second, as in 1.65. EOFError: input() at the end of the input ( msvcrt's Ctrl-Z too ); OSError: the
3342
+ # descriptor closed, or its terminal gone
3343
+ ended = True
3344
+ if remaining <= 0:
3345
+ return None
3346
+ # an ended stdin is asked again at the next second, not in a busy loop: a terminal can answer after a Ctrl-D
3347
+ if ended:
3348
+ time.sleep(max(deadline - (seconds - 1) - time.monotonic(), 0))
3287
3349
 
3288
3350
  def pretty_format_table(data, delimiter=None, header=None, full=False, remove_empty_columns=False):
3289
3351
  import re
@@ -3611,24 +3673,10 @@ def format_bytes(size, use_1024_bytes=None, to_int=False, to_str=False, str_form
3611
3673
  if isinstance(size, int):
3612
3674
  return size
3613
3675
  elif isinstance(size, str):
3614
- match = re.match(r"(\d+(\.\d+)?)\s*([a-zA-Z]*)", size)
3615
- if not match:
3616
- if to_str:
3617
- return size
3618
- _write_error("Invalid size format. Expected format: 'number [unit]', e.g., '1.5 GiB' or '1.5GiB'\n" f"Got: {size}")
3619
- return 0
3620
- number, _, unit = match.groups()
3621
- number = float(number)
3622
- unit = unit.strip().lower().rstrip("b")
3623
- if unit.endswith("i"):
3624
- use_1024_bytes = True
3625
- elif use_1024_bytes is None:
3626
- use_1024_bytes = False
3627
- unit = unit.rstrip("i")
3628
- if use_1024_bytes:
3629
- power = 2**10
3630
- else:
3631
- power = 10**3
3676
+ # the whole string is a size: a number, then a unit or none ( K to Y, an i after it for 1024s, a B at the end ), with
3677
+ # blanks around them; anything else, text after the unit too ( '5Gb/s', '1e9' ), is not one ( 1.65 read its start ).
3678
+ # The blanks around go by strip(): a \s* at each end of the pattern would retry every split of a long run of them
3679
+ match = re.fullmatch(r"(\d+(\.\d*)?)\s*((?:[kmgtpezyKMGTPEZY][iI]?)?[bB]?)", size.strip())
3632
3680
  unit_labels = {
3633
3681
  "": 0,
3634
3682
  "k": 1,
@@ -3640,18 +3688,33 @@ def format_bytes(size, use_1024_bytes=None, to_int=False, to_str=False, str_form
3640
3688
  "z": 7,
3641
3689
  "y": 8,
3642
3690
  }
3643
- if unit not in unit_labels:
3691
+ if match:
3692
+ number, _, unit = match.groups()
3693
+ number = float(number)
3694
+ unit = unit.lower().rstrip("b")
3695
+ if unit.endswith("i"):
3696
+ use_1024_bytes = True
3697
+ elif use_1024_bytes is None:
3698
+ use_1024_bytes = False
3699
+ unit = unit.rstrip("i")
3700
+ # not a size: no number, a unit it does not know ( 1.64 returned None ), or text after it
3701
+ if not match:
3644
3702
  if to_str:
3645
3703
  return size
3704
+ _write_error("Invalid size format. Expected format: 'number [unit]', e.g., '1.5 GiB' or '1.5GiB'\n" f"Got: {size}")
3705
+ return 0
3706
+ if use_1024_bytes:
3707
+ power = 2**10
3646
3708
  else:
3647
- if to_str:
3648
- return format_bytes(
3649
- size=int(number * (power**unit_labels[unit])),
3650
- use_1024_bytes=use_1024_bytes,
3651
- to_str=True,
3652
- str_format=str_format,
3653
- )
3654
- return int(number * (power**unit_labels[unit]))
3709
+ power = 10**3
3710
+ if to_str:
3711
+ return format_bytes(
3712
+ size=int(number * (power**unit_labels[unit])),
3713
+ use_1024_bytes=use_1024_bytes,
3714
+ to_str=True,
3715
+ str_format=str_format,
3716
+ )
3717
+ return int(number * (power**unit_labels[unit]))
3655
3718
  else:
3656
3719
  try:
3657
3720
  return int(size)