multiCMD 1.68__tar.gz → 1.69__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.68 → multicmd-1.69}/PKG-INFO +16 -1
  2. {multicmd-1.68 → multicmd-1.69}/README.md +15 -0
  3. {multicmd-1.68 → multicmd-1.69}/multiCMD.egg-info/PKG-INFO +16 -1
  4. {multicmd-1.68 → multicmd-1.69}/multiCMD.py +89 -41
  5. {multicmd-1.68 → multicmd-1.69}/tests/test_interrupts.py +22 -4
  6. {multicmd-1.68 → multicmd-1.69}/tests/test_terminal_width.py +181 -8
  7. {multicmd-1.68 → multicmd-1.69}/LICENSE +0 -0
  8. {multicmd-1.68 → multicmd-1.69}/multiCMD.egg-info/SOURCES.txt +0 -0
  9. {multicmd-1.68 → multicmd-1.69}/multiCMD.egg-info/dependency_links.txt +0 -0
  10. {multicmd-1.68 → multicmd-1.69}/multiCMD.egg-info/entry_points.txt +0 -0
  11. {multicmd-1.68 → multicmd-1.69}/multiCMD.egg-info/top_level.txt +0 -0
  12. {multicmd-1.68 → multicmd-1.69}/scripts/multiCMD +0 -0
  13. {multicmd-1.68 → multicmd-1.69}/setup.cfg +0 -0
  14. {multicmd-1.68 → multicmd-1.69}/setup.py +0 -0
  15. {multicmd-1.68 → multicmd-1.69}/tests/test_api.py +0 -0
  16. {multicmd-1.68 → multicmd-1.69}/tests/test_async_executor.py +0 -0
  17. {multicmd-1.68 → multicmd-1.69}/tests/test_cli.py +0 -0
  18. {multicmd-1.68 → multicmd-1.69}/tests/test_cli_closed_stdout.py +0 -0
  19. {multicmd-1.68 → multicmd-1.69}/tests/test_cli_failed_stdout.py +0 -0
  20. {multicmd-1.68 → multicmd-1.69}/tests/test_cli_input.py +0 -0
  21. {multicmd-1.68 → multicmd-1.69}/tests/test_cli_summary.py +0 -0
  22. {multicmd-1.68 → multicmd-1.69}/tests/test_display.py +0 -0
  23. {multicmd-1.68 → multicmd-1.69}/tests/test_embed.py +0 -0
  24. {multicmd-1.68 → multicmd-1.69}/tests/test_env.py +0 -0
  25. {multicmd-1.68 → multicmd-1.69}/tests/test_expand_ranges.py +0 -0
  26. {multicmd-1.68 → multicmd-1.69}/tests/test_handover.py +0 -0
  27. {multicmd-1.68 → multicmd-1.69}/tests/test_helpers.py +0 -0
  28. {multicmd-1.68 → multicmd-1.69}/tests/test_packaging.py +0 -0
  29. {multicmd-1.68 → multicmd-1.69}/tests/test_parsing.py +0 -0
  30. {multicmd-1.68 → multicmd-1.69}/tests/test_pass_fds.py +0 -0
  31. {multicmd-1.68 → multicmd-1.69}/tests/test_ping.py +0 -0
  32. {multicmd-1.68 → multicmd-1.69}/tests/test_pool.py +0 -0
  33. {multicmd-1.68 → multicmd-1.69}/tests/test_process_groups.py +0 -0
  34. {multicmd-1.68 → multicmd-1.69}/tests/test_pty.py +0 -0
  35. {multicmd-1.68 → multicmd-1.69}/tests/test_reader.py +0 -0
  36. {multicmd-1.68 → multicmd-1.69}/tests/test_reader_quit.py +0 -0
  37. {multicmd-1.68 → multicmd-1.69}/tests/test_regressions.py +0 -0
  38. {multicmd-1.68 → multicmd-1.69}/tests/test_run_command.py +0 -0
  39. {multicmd-1.68 → multicmd-1.69}/tests/test_run_commands.py +0 -0
  40. {multicmd-1.68 → multicmd-1.69}/tests/test_sudo.py +0 -0
  41. {multicmd-1.68 → multicmd-1.69}/tests/test_supervision.py +0 -0
  42. {multicmd-1.68 → multicmd-1.69}/tests/test_task.py +0 -0
  43. {multicmd-1.68 → multicmd-1.69}/tests/test_windows.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: multiCMD
3
- Version: 1.68
3
+ Version: 1.69
4
4
  Summary: Run commands simultaneously
5
5
  Home-page: https://github.com/yufei-pan/multiCMD
6
6
  Author: Yufei Pan
@@ -558,6 +558,21 @@ pytest -m "not slow" # skip intentional sleep-heavy cases
558
558
 
559
559
  `test_expand_ranges_perf.py` is a manual performance microbenchmark and is not collected by pytest.
560
560
 
561
+ ## Upgrading to 1.69 from 1.68
562
+
563
+ - Nothing needs doing. `mcmd` is unchanged.
564
+ - Changed, library: a `print_progress_bar()` frame with a total keeps a bar of at least 10 cells (from 12 columns up; the first
565
+ frame holds ` Calculating... ` or blanks in its place). Beside it the percent
566
+ gives way first, then the prefix from its end down to its first field, then the suffix from its start, then that first
567
+ field, a whole field at a time, so a number keeps its unit and its label (1.68 shrank the bar to two cells, then dropped
568
+ the percent and the prefix whole). At 80 columns hpcp's copy bar shows its count and its time left (1.68: no count).
569
+ - Fix, library: the first frame at or past the total ends its line, so the program's next output does not join a count
570
+ that stepped over its total (1.68 ended the line only at a frame equal to the total). A count that goes on past its
571
+ total draws its later frames on the next line, and the last stays open: the next output joins it.
572
+ - Fix, library: a control character in a frame's prefix or suffix (a tab, a newline, an escape: a file name can hold one)
573
+ is drawn as `?` (1.68: a newline started a row, so each frame scrolled, and a tab could wrap the frame). A prefix or
574
+ suffix coloured with escape codes shows them as text (`?[32m`).
575
+
561
576
  ## Upgrading to 1.68 from 1.67
562
577
 
563
578
  - Nothing needs doing.
@@ -533,6 +533,21 @@ 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.69 from 1.68
537
+
538
+ - Nothing needs doing. `mcmd` is unchanged.
539
+ - Changed, library: a `print_progress_bar()` frame with a total keeps a bar of at least 10 cells (from 12 columns up; the first
540
+ frame holds ` Calculating... ` or blanks in its place). Beside it the percent
541
+ gives way first, then the prefix from its end down to its first field, then the suffix from its start, then that first
542
+ field, a whole field at a time, so a number keeps its unit and its label (1.68 shrank the bar to two cells, then dropped
543
+ the percent and the prefix whole). At 80 columns hpcp's copy bar shows its count and its time left (1.68: no count).
544
+ - Fix, library: the first frame at or past the total ends its line, so the program's next output does not join a count
545
+ that stepped over its total (1.68 ended the line only at a frame equal to the total). A count that goes on past its
546
+ total draws its later frames on the next line, and the last stays open: the next output joins it.
547
+ - Fix, library: a control character in a frame's prefix or suffix (a tab, a newline, an escape: a file name can hold one)
548
+ is drawn as `?` (1.68: a newline started a row, so each frame scrolled, and a tab could wrap the frame). A prefix or
549
+ suffix coloured with escape codes shows them as text (`?[32m`).
550
+
536
551
  ## Upgrading to 1.68 from 1.67
537
552
 
538
553
  - Nothing needs doing.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: multiCMD
3
- Version: 1.68
3
+ Version: 1.69
4
4
  Summary: Run commands simultaneously
5
5
  Home-page: https://github.com/yufei-pan/multiCMD
6
6
  Author: Yufei Pan
@@ -558,6 +558,21 @@ pytest -m "not slow" # skip intentional sleep-heavy cases
558
558
 
559
559
  `test_expand_ranges_perf.py` is a manual performance microbenchmark and is not collected by pytest.
560
560
 
561
+ ## Upgrading to 1.69 from 1.68
562
+
563
+ - Nothing needs doing. `mcmd` is unchanged.
564
+ - Changed, library: a `print_progress_bar()` frame with a total keeps a bar of at least 10 cells (from 12 columns up; the first
565
+ frame holds ` Calculating... ` or blanks in its place). Beside it the percent
566
+ gives way first, then the prefix from its end down to its first field, then the suffix from its start, then that first
567
+ field, a whole field at a time, so a number keeps its unit and its label (1.68 shrank the bar to two cells, then dropped
568
+ the percent and the prefix whole). At 80 columns hpcp's copy bar shows its count and its time left (1.68: no count).
569
+ - Fix, library: the first frame at or past the total ends its line, so the program's next output does not join a count
570
+ that stepped over its total (1.68 ended the line only at a frame equal to the total). A count that goes on past its
571
+ total draws its later frames on the next line, and the last stays open: the next output joins it.
572
+ - Fix, library: a control character in a frame's prefix or suffix (a tab, a newline, an escape: a file name can hold one)
573
+ is drawn as `?` (1.68: a newline started a row, so each frame scrolled, and a tab could wrap the frame). A prefix or
574
+ suffix coloured with escape codes shows them as text (`?[32m`).
575
+
561
576
  ## Upgrading to 1.68 from 1.67
562
577
 
563
578
  - Nothing needs doing.
@@ -29,9 +29,9 @@ import traceback
29
29
  from pprint import pformat
30
30
 
31
31
  #%% global vars
32
- version = '1.68'
32
+ version = '1.69'
33
33
  __version__ = version
34
- COMMIT_DATE = '2026-11-16'
34
+ COMMIT_DATE = '2026-11-20'
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()
@@ -3589,11 +3589,17 @@ def _cells(text):
3589
3589
  return len(text)
3590
3590
  return sum(map(_char_width, text))
3591
3591
 
3592
- def _fit(text, width, from_start=False):
3592
+ def _cuts_at(text, i):
3593
3593
  '''
3594
- text cut to width terminal columns ( internal ): its end, back to the last blank that fits and follows neither a digit
3595
- nor a colon, so a number keeps its label and its unit ( format_bytes writes '1.23 K' ); from_start, its start, at the
3596
- column
3594
+ Whether text can be cut at its index i ( internal ): at a blank that follows neither a digit nor a colon. A blank after a digit
3595
+ may sit between a number and its unit ( format_bytes writes '1.23 K' ), one after a colon between a label and its number
3596
+ '''
3597
+ return text[i] == ' ' and not (i and (text[i - 1].isdigit() or text[i - 1] == ':'))
3598
+
3599
+ def _fit(text, width, from_start=False, whole=False):
3600
+ '''
3601
+ text cut to width terminal columns ( internal ): its end, back to the last blank it can be cut at ( _cuts_at ), so a number
3602
+ keeps its label and its unit, else at the column ( with whole, not at all: '' ); from_start, its start, at the column
3597
3603
  '''
3598
3604
  used = 0
3599
3605
  for i in (range(len(text) - 1, -1, -1) if from_start else range(len(text))):
@@ -3601,14 +3607,58 @@ def _fit(text, width, from_start=False):
3601
3607
  if used > width:
3602
3608
  if from_start:
3603
3609
  return text[i + 1:]
3604
- # a blank after a digit may sit between a number and its unit ( '1.23 K' ), one after a colon between a label and
3605
- # its number: neither is a place to cut
3606
3610
  blank = i
3607
- while blank > 0 and (text[blank] != ' ' or text[blank - 1].isdigit() or text[blank - 1] == ':'):
3611
+ while blank > 0 and not _cuts_at(text, blank):
3608
3612
  blank -= 1
3609
- return text[:blank] if blank > 0 else text[:i]
3613
+ return text[:blank] if blank > 0 else '' if whole else text[:i]
3610
3614
  return text
3611
3615
 
3616
+ # The least bar of a frame with a total, in cells, whatever the total, so it reads at a glance. Its text gives way before the bar
3617
+ # shrinks below it ( 1.68 shrank the bar first, down to two cells, then dropped the percent and the prefix whole: hpcp's copy
3618
+ # bar at 80 columns showed no count )
3619
+ _LEAST_BAR = 10
3620
+
3621
+ # the control characters ( C0, DEL, C1 ), each a '?' in a frame's text: a newline starts a row, a tab jumps to a tab stop, an
3622
+ # escape starts a sequence the terminal acts on, so the frame cannot be measured ( a file name may hold any of them )
3623
+ _CONTROLS = dict.fromkeys(list(range(0x20)) + list(range(0x7f, 0xa0)), '?')
3624
+
3625
+ def _cut_prefix(prefix, width):
3626
+ '''
3627
+ A frame's prefix in width terminal columns ( internal ): whole, or its start, cut after a whole field ( _fit ) and joined
3628
+ to the bar by ' |' again; '' where not even its first field fits
3629
+ '''
3630
+ if _cells(prefix) <= width:
3631
+ return prefix
3632
+ kept = _fit(prefix, width - 2, whole=True).rstrip(' |')
3633
+ return kept + ' |' if kept else ''
3634
+
3635
+ def _first_field(prefix):
3636
+ '''A frame's prefix cut after its first field ( internal; see _cut_prefix ), or '' if it holds only the joint to the bar'''
3637
+ for i in range(1, len(prefix) + 1):
3638
+ if i == len(prefix) or _cuts_at(prefix, i):
3639
+ kept = prefix[:i].rstrip(' |')
3640
+ if kept:
3641
+ return kept + ' |'
3642
+ return ''
3643
+
3644
+ def _cut_suffix(suffix, width):
3645
+ '''
3646
+ A frame's suffix in width terminal columns ( internal ): whole, or its end, joined to the bar by '| ' again, from a blank it
3647
+ can be cut at ( _cuts_at ) before a number, a label ( a word that ends in a colon ) or a '|': a word after a number's unit
3648
+ ( '1.23 M Files', '5.00 KF/s Limit' ) never starts it without its number. '' where not even its last such part fits
3649
+ '''
3650
+ if _cells(suffix) <= width:
3651
+ return suffix
3652
+ # the first character of the end that fits beside the '| '
3653
+ start = len(suffix) - len(_fit(suffix, width - 2, from_start=True))
3654
+ for i in range(max(start - 1, 0), len(suffix)):
3655
+ if _cuts_at(suffix, i):
3656
+ word = suffix[i + 1:].lstrip(' ').split(' ', 1)[0]
3657
+ if word[:1].isdigit() or word[:1] == '|' or word.endswith(':'):
3658
+ kept = suffix[i + 1:].lstrip('| ')
3659
+ return '| ' + kept if kept else ''
3660
+ return ''
3661
+
3612
3662
  def _genrate_progress_bar(iteration, total, prefix='', suffix='',columns=240):
3613
3663
  '''
3614
3664
  Generate a progress bar string: one line, within one column less than columns ( a line that reaches the last column
@@ -3624,10 +3674,8 @@ def _genrate_progress_bar(iteration, total, prefix='', suffix='',columns=240):
3624
3674
  @returns:
3625
3675
  str: the progress bar string
3626
3676
  '''
3627
- noPrefix = False
3628
- noSuffix = False
3629
- noPercent = False
3630
- noBar = False
3677
+ # a control character would break the frame over rows or columns it cannot count: it is drawn as a '?'
3678
+ prefix, suffix = prefix.translate(_CONTROLS), suffix.translate(_CONTROLS)
3631
3679
  width = columns - 1
3632
3680
  # if total is 0, we don't want to divide by 0: the iteration, padded to clear the last frame. What does not fit gives way
3633
3681
  # from the prefix's start ( hpcp's is the end of a path ), then from the end
@@ -3636,23 +3684,23 @@ def _genrate_progress_bar(iteration, total, prefix='', suffix='',columns=240):
3636
3684
  line = _fit(_fit(prefix, width - _cells(rest), from_start=True) + rest, width)
3637
3685
  return line + ' ' * (width - _cells(line))
3638
3686
  percent = f'|{("{0:.1f}").format(100 * (iteration / float(total)))}% '
3687
+ # a bar is length + 1 wide, full or not ( see below ), so a frame is at most columns - 2 wide. Beside a bar of _LEAST_BAR
3688
+ # cells, what does not fit gives way from the bar outward: the percent, the prefix from its end down to its first field, the
3689
+ # suffix from its start, then that first field, each a whole field at a time. The line keeps what it starts and ends with
3690
+ # longest: hpcp's count, and its time left
3691
+ room = columns - 2 - _LEAST_BAR
3639
3692
  prefix_cells, suffix_cells = _cells(prefix), _cells(suffix)
3640
- # a bar is length + 1 wide, full or not ( see below ), so a frame is at most columns - 2 wide
3641
- length = columns - prefix_cells - suffix_cells - len(percent) - 3
3642
- if length <= 0:
3643
- length = columns - prefix_cells - suffix_cells - 3
3644
- noPercent = True
3645
- if length <= 0:
3646
- length = columns - suffix_cells - 3
3647
- noPrefix = True
3648
- if length <= 0:
3649
- length = columns - 3
3650
- noSuffix = True
3693
+ if prefix_cells + suffix_cells + len(percent) > room:
3694
+ percent = ''
3695
+ if prefix_cells + suffix_cells > room:
3696
+ prefix = _cut_prefix(prefix, room - suffix_cells) or _first_field(prefix)
3697
+ if _cells(prefix) > room:
3698
+ prefix = ''
3699
+ suffix = _cut_suffix(suffix, room - _cells(prefix))
3700
+ length = columns - _cells(prefix) - _cells(suffix) - len(percent) - 3
3651
3701
  if length <= 0:
3652
3702
  # 3 columns or fewer: no room for a bar
3653
3703
  return ''
3654
- if iteration == 0:
3655
- noBar = True
3656
3704
  filled_length = int(length * iteration // total)
3657
3705
  progress_chars = _BAR_BLOCKS
3658
3706
  fractional_progress = (length * iteration / total) - filled_length
@@ -3664,19 +3712,11 @@ def _genrate_progress_bar(iteration, total, prefix='', suffix='',columns=240):
3664
3712
  bar = progress_chars[-1] * (length + 1)
3665
3713
  else:
3666
3714
  bar = progress_chars[-1] * filled_length + bar_char + '_' * (length - filled_length)
3667
- lineOut = ''
3668
- if not noPrefix:
3669
- lineOut += prefix
3670
- if not noBar:
3671
- lineOut += f'{bar}'
3672
- if not noPercent:
3673
- lineOut += percent
3674
- else:
3675
- if length >= 16:
3676
- lineOut += ' Calculating... '
3677
- if not noSuffix:
3678
- lineOut += suffix
3679
- return lineOut
3715
+ if iteration == 0:
3716
+ # the place of the bar ( length + 1 cells ): ' Calculating... ' when it fits, else blanks, so the first frame is cut as a frame
3717
+ # under 10% is ( a percent as wide as its own ), and the prefix's joint and the suffix's never stand side by side ( '||' )
3718
+ return prefix + (' Calculating... ' if length + 1 >= 16 else ' ' * (length + 1)) + suffix
3719
+ return prefix + bar + percent + suffix
3680
3720
 
3681
3721
  def _writable(text, stream):
3682
3722
  '''
@@ -3700,6 +3740,9 @@ def _writable(text, stream):
3700
3740
  text = text.translate(_ASCII_BAR)
3701
3741
  return text.encode(encoding, 'replace').decode(encoding)
3702
3742
 
3743
+ # the total of the bar whose last frame was at or past it, else None ( see print_progress_bar )
3744
+ _bar_done = [None]
3745
+
3703
3746
  def print_progress_bar(iteration, total, prefix='', suffix=''):
3704
3747
  '''
3705
3748
  Call in a loop to create terminal progress bar
@@ -3724,8 +3767,13 @@ def print_progress_bar(iteration, total, prefix='', suffix=''):
3724
3767
  # that wraps or leaves the one before it showing
3725
3768
  prefix, suffix = _writable(prefix, sys.stdout), _writable(suffix, sys.stdout)
3726
3769
  _display._write(_writable(f'\r{_genrate_progress_bar(iteration, total, prefix, suffix, columns)}', sys.stdout))
3727
- if iteration == total and total > 0:
3770
+ # the frame at the total ends its line, each time, and so does the first one past it ( a total that was an estimate, a
3771
+ # count that steps over it ), so the program's next output starts a line of its own. A later frame past the total is
3772
+ # drawn on the line after it, and left open: ending each would scroll a line per frame
3773
+ done = total > 0 and iteration >= total
3774
+ if done and (iteration == total or _bar_done[0] != total):
3728
3775
  _display._write('\n')
3776
+ _bar_done[0] = total if done else None
3729
3777
  except Exception:
3730
3778
  if iteration % 5 == 0:
3731
3779
  _display._write(_writable(_genrate_progress_bar(iteration, total, prefix, suffix), sys.stdout) + '\n')
@@ -603,13 +603,28 @@ def handler(signum, frame):
603
603
  armed[0] = False
604
604
  raise KeyboardInterrupt
605
605
  signal.signal(signal.SIGALRM, handler)
606
+ def reap_strays():
607
+ # a stop that lands as Popen's fork returns leaves a child that no Popen knows ( docs/limitations.md, "A Ctrl-C inside
608
+ # Popen during a serial start ..." ), a zombie until the probe exits: in 5,000 stops one run reaped 770 ( 3.6 ), 782 ( 3.9 ),
609
+ # 1,821 ( 3.11 ), 1,822 ( 3.12 ) and 2,032 ( 3.14 ), so on 3.11 and later a process limit of 1,024 ( Docker's default ) runs
610
+ # out, and the next start fails with EAGAIN. The test asserts that some were reaped: a probe that reaps none passes wherever
611
+ # the process limit is above its strays
612
+ count = 0
613
+ while True:
614
+ try:
615
+ pid, _ = os.waitpid(-1, os.WNOHANG)
616
+ except ChildProcessError:
617
+ return count
618
+ if not pid:
619
+ return count
620
+ count += 1
606
621
  times = []
607
622
  for _ in range(50):
608
623
  start = time.perf_counter()
609
624
  multiCMD.run_command(['true'], quiet=True)
610
625
  times.append(time.perf_counter() - start)
611
626
  window = sorted(times)[len(times) // 2] * 1.5
612
- stops = others = 0
627
+ stops = others = strays = 0
613
628
  for i in range(n):
614
629
  try:
615
630
  try:
@@ -630,12 +645,13 @@ for i in range(n):
630
645
  print('LEAK: threading._active_limbo_lock held after %d calls' % (i + 1), flush=True)
631
646
  os._exit(2)
632
647
  threading._active_limbo_lock.release()
648
+ strays += reap_strays()
633
649
  done[0] = i + 1
634
650
  assert multiCMD.run_command(['true'], quiet=True, return_code_only=True) == 0
635
651
  # ( Python 3.6 - 3.8 block-buffer a redirected stderr, where the stops that landed in a finalizer are told: a result line
636
652
  # printed while a flush at 8 KB had left half a line there would land on the end of it, so the line is told after a flush )
637
653
  sys.stderr.flush()
638
- print('%d calls, %d stopped, %d other errors' % (n, stops, others), flush=True)
654
+ print('%d calls, %d stopped, %d other errors, %d strays reaped' % (n, stops, others, strays), flush=True)
639
655
  '''
640
656
 
641
657
 
@@ -646,13 +662,15 @@ def test_stops_in_serial_commands_never_jam_thread_starts(tmp_path):
646
662
  # left threading's _active_limbo_lock held, and every later thread start hung: up to 1.59 on 3.14 this probe hung in 6 of
647
663
  # 6 runs of 20,000 calls, after 30 to 9,000 calls ( three runs by the plan, three by the build ). 3.6 never hung so in
648
664
  # 60,000 calls. 5,000 calls take about 75 s on 3.6 ( a call and the sleep after it ): 600 s leaves eight times that
649
- # under -n auto
665
+ # under -n auto. The probe reaps the zombies such stops leave ( 1.69: 1.68's probe failed under a process limit of 1,024 )
650
666
  probe = tmp_path / 'probe.py'
651
667
  probe.write_text(STOP_PROBE)
652
668
  run = subprocess.run([sys.executable, '-I', str(probe), str(PACKAGE_ROOT), '5000'], stdout=subprocess.PIPE,
653
669
  stderr=subprocess.STDOUT, universal_newlines=True, timeout=600)
654
670
  # ( the probe flushes stderr before its result line: see there )
655
- assert run.returncode == 0 and re.search(r'^5000 calls, \d+ stopped, 0 other errors$', run.stdout, re.M), run.stdout[-4000:]
671
+ result = re.search(r'^5000 calls, \d+ stopped, 0 other errors, (\d+) strays reaped$', run.stdout, re.M)
672
+ # ( 770 to 2,032 were reaped in 5,000 stops on the five Pythons: a probe that reaps none has lost its reaper )
673
+ assert run.returncode == 0 and result and int(result.group(1)) > 0, run.stdout[-4000:]
656
674
 
657
675
 
658
676
  def _dummy_threads():
@@ -1,5 +1,6 @@
1
1
  '''A progress line fits its terminal (1.68): a terminal of no size counts as unknown, and a frame is cut to one column less
2
- than the width, in terminal columns.'''
2
+ than the width, in terminal columns. A bar with a total keeps 10 cells, and its text gives way a field at a time; a bar past
3
+ its total ends its line; a control character in a frame's text is a '?' (1.69).'''
3
4
  import os
4
5
  import re
5
6
  import select
@@ -29,6 +30,13 @@ def fb(count):
29
30
  REMOVAL = f'Removed: {fb(1230)} Folders: {fb(45)} | Overall: {fb(1230000)} Files'
30
31
  LISTING = f'Files: {fb(1230)} Links: {fb(12)} Folders: {fb(45)} Size: {multiCMD.format_bytes(1.15 * 2 ** 30)}B | Overall: {fb(1230000)} Files'
31
32
  WIDE_PATH = '/数据/项目/二〇二六/运行记录/输出结果/检查点/第一千二百步/模型权重文件.safetensors'
33
+ # hpcp 9.96's copy bar ( Adaptive_Progress_Bar.print_progress, read, not imported ), with this module's format_bytes: its count,
34
+ # size and jobs, then its rates, the last jobs' rates and the time left; with a rate limit set, the limits in the jobs' place
35
+ COPY_PREFIX = f'{fb(123456)}F ({multiCMD.format_bytes(45.6 * 2 ** 30)}B) Copied | 16 Scheduled {4.0:0>3.1f} F/Job '
36
+ COPY = (f'{multiCMD.format_bytes(1.23 * 2 ** 30)}B/s {fb(2345.6)}F/s | {2.5:.1f}s: {multiCMD.format_bytes(1.1 * 2 ** 30)}B/s '
37
+ f'{fb(2100)}F/s | 1h2m3s')
38
+ COPY_LIMITED = (f'{multiCMD.format_bytes(1.23 * 2 ** 30)}B/s {fb(2345.6)}F/s | {multiCMD.format_bytes(2 ** 30)}B/s Limit | '
39
+ f'{fb(5000)}F/s Limit | 1h2m3s')
32
40
 
33
41
 
34
42
  def cells(text):
@@ -89,6 +97,10 @@ def test_the_test_strings_are_what_format_bytes_writes():
89
97
  # '1.23K' and cut 'Size: 123.45 GiB' after its number )
90
98
  assert REMOVAL == 'Removed: 1.23 K Folders: 45.00 | Overall: 1.23 M Files'
91
99
  assert LISTING == 'Files: 1.23 K Links: 12.00 Folders: 45.00 Size: 1.15 GiB | Overall: 1.23 M Files'
100
+ # and hpcp 9.96's copy bar ( the 1.69 tests )
101
+ assert COPY_PREFIX == '123.46 KF (45.60 GiB) Copied | 16 Scheduled 4.0 F/Job '
102
+ assert COPY == '1.23 GiB/s 2.35 KF/s | 2.5s: 1.10 GiB/s 2.10 KF/s | 1h2m3s'
103
+ assert COPY_LIMITED == '1.23 GiB/s 2.35 KF/s | 1.00 GiB/s Limit | 5.00 KF/s Limit | 1h2m3s'
92
104
 
93
105
 
94
106
  @pytest.mark.parametrize('reported, environ, expected', [
@@ -314,9 +326,9 @@ def test_a_frame_never_ends_inside_a_field_at_any_width(prefix, suffix):
314
326
  ('x' * 300 + '\u4e2d', 302),
315
327
  ])
316
328
  def test_cells_counts_a_character_by_its_unicode_width(text, expected):
317
- # the module's own count, by Unicode's width of each character: no terminal measures it ( a tab or a newline counts one:
318
- # docs/limitations.md, deferred to 1.69 ). ASCII and Latin-1, the usual text, is counted by length: a fast path that must give
319
- # what the count by character gives
329
+ # the module's own count, by Unicode's width of each character: no terminal measures it ( a tab or a newline counts one, and a
330
+ # frame draws each as a '?' before it counts: 1.69 ). ASCII and Latin-1, the usual text, is counted by length: a fast path that
331
+ # must give what the count by character gives
320
332
  assert multiCMD._cells(text) == expected
321
333
 
322
334
 
@@ -343,9 +355,13 @@ def test_a_bar_of_a_known_total_at_a_terminal_too_narrow_for_it():
343
355
  assert _genrate_progress_bar(5, 10, 'p |', '| s', 5) == '\N{FULL BLOCK}\N{LOWER ONE EIGHTH BLOCK}_'
344
356
 
345
357
 
346
- def test_a_bar_of_a_known_total_drops_its_percent_before_it_shrinks():
347
- # 15 columns: no room for the percent, so the bar takes its place and the prefix and suffix stay
348
- assert _genrate_progress_bar(5, 10, 'p |', '| s', 15) == 'p |' + '\N{FULL BLOCK}' * 3 + '\N{LOWER ONE EIGHTH BLOCK}' + '_' * 3 + '| s'
358
+ def test_a_bar_of_a_known_total_keeps_10_cells_before_its_text():
359
+ # 15 columns: the bar keeps its 10 cells, the percent goes, then the suffix, and the prefix's first field stays ( 1.68: a bar
360
+ # of 7 cells and both ). 12 columns: the bar alone, of 10; fewer: what there is
361
+ half = '\N{FULL BLOCK}' * 4 + '\N{LOWER HALF BLOCK}' + '_' * 5
362
+ assert _genrate_progress_bar(5, 10, 'p |', '| s', 15) == 'p |' + half
363
+ assert _genrate_progress_bar(5, 10, 'p |', '| s', 12) == half
364
+ assert _genrate_progress_bar(5, 10, 'p |', '| s', 8) == '\N{FULL BLOCK}' * 2 + '\N{LOWER HALF BLOCK}' + '_' * 3
349
365
 
350
366
 
351
367
  def test_a_bar_of_a_known_total_drops_a_long_prefix_and_its_percent():
@@ -368,10 +384,167 @@ def test_the_last_frame_of_a_bar_covers_the_one_before_it(columns):
368
384
  line = []
369
385
  for frame in drawn:
370
386
  line[:len(frame)] = list(frame)
371
- assert ''.join(line) == drawn[-1] and drawn[-1].endswith('| 3.2 F/s | 00:00:00')
387
+ # ( at 40 columns '3.2 F/s' gives way, to keep a bar of 10 cells or more, and the bar takes the room: 1.68 drew '3.2 F/s' beside
388
+ # a bar of 8 cells there )
389
+ tail = '|100.0% | 3.2 F/s | 00:00:00' if columns > 40 else '| 00:00:00'
390
+ assert ''.join(line) == drawn[-1] == '10 files |' + '\N{FULL BLOCK}' * (columns - 2 - 10 - len(tail)) + tail
372
391
 
373
392
 
374
393
  def test_print_progress_bar_draws_at_one_column_less_than_the_terminal(monkeypatch, capsys):
375
394
  monkeypatch.setattr(multiCMD, 'get_terminal_size', lambda: os.terminal_size((80, 24)))
376
395
  print_progress_bar(7, 0, PATH[-50:], REMOVAL)
377
396
  assert capsys.readouterr().out == '\r' + ('tensors | iteration:7 | ' + REMOVAL).ljust(79)
397
+
398
+
399
+ # ---- 1.69: a bar with a total keeps 10 cells; a bar past its total ends its line; a control character is a '?'
400
+
401
+ BAR = re.compile('[' + multiCMD._BAR_BLOCKS + '_]+')
402
+
403
+
404
+ def parts(frame):
405
+ '''A frame of a known total as ( what is before its bar, the bar's cells, what is after it ).'''
406
+ found = BAR.search(frame)
407
+ return frame[:found.start()], found.end() - found.start(), frame[found.end():]
408
+
409
+
410
+ def a_broken_start(kept, whole):
411
+ '''Why a frame's text that keeps the end of whole, from kept on, starts badly, else None: inside a word, on a unit without
412
+ its number, on a number without its label, or on a word whose number went ( '| Files' of '1.23 M Files' ): it starts with
413
+ a number, a label or a '|' ( the mirror of a_broken_end ).'''
414
+ if not whole.endswith(kept):
415
+ return 'is not the end of the line'
416
+ before = whole[:len(whole) - len(kept)]
417
+ if before and before[-1] != ' ':
418
+ return 'starts inside a word'
419
+ before = before.split()
420
+ if before and re.fullmatch(r'\d+(\.\d+)?', before[-1]):
421
+ return 'starts on a unit without its number'
422
+ if before and before[-1].endswith(':'):
423
+ return 'starts on a number without its label'
424
+ first = kept.split()[0]
425
+ if before and not (first[0].isdigit() or first[0] == '|' or first.endswith(':')):
426
+ return 'starts on a word whose number went'
427
+ return None
428
+
429
+
430
+ @pytest.mark.parametrize('columns, suffix, shown', [
431
+ # 1.68 dropped the percent and the whole prefix: ( '', 18, '| ' + COPY ), with no count, size or percent
432
+ (80, COPY, ('123.46 KF |', 18, '| 2.35 KF/s | 2.5s: 1.10 GiB/s 2.10 KF/s | 1h2m3s')),
433
+ (80, COPY_LIMITED, ('123.46 KF |', 10, '| 2.35 KF/s | 1.00 GiB/s Limit | 5.00 KF/s Limit | 1h2m3s')),
434
+ (100, COPY, ('123.46 KF (45.60 GiB) |', 15, '| ' + COPY)),
435
+ (100, COPY_LIMITED, ('123.46 KF |', 19, '| ' + COPY_LIMITED)),
436
+ # 1.68: the whole prefix and a bar of 2 cells
437
+ (120, COPY, ('123.46 KF (45.60 GiB) Copied | 16 Scheduled |', 13, '| ' + COPY)),
438
+ (160, COPY, (COPY_PREFIX + ' |', 35, '|50.0% | ' + COPY)),
439
+ ], ids=['80', '80-limited', '100', '100-limited', '120', '160'])
440
+ def test_hpcp_copy_bar_keeps_its_count_and_its_time_left(columns, suffix, shown):
441
+ # the bar keeps 10 cells; beside it the percent gives way, then the prefix from its end down to its count, then the suffix
442
+ # from its start: the line keeps what it starts and ends with ( the count, the time left )
443
+ frame = _genrate_progress_bar(5, 10, COPY_PREFIX + ' |', '| ' + suffix, columns)
444
+ assert parts(frame) == shown and cells(frame) == columns - 2, frame
445
+
446
+
447
+ @pytest.mark.parametrize('suffix', [COPY, COPY_LIMITED, REMOVAL], ids=['copy', 'copy-limited', 'removal'])
448
+ @pytest.mark.parametrize('prefix', [COPY_PREFIX, PATH[-50:], WIDE_PATH[-50:]], ids=['copy', 'path', 'wide-path'])
449
+ def test_a_frame_of_a_known_total_keeps_whole_fields_beside_10_cells_at_any_width(prefix, suffix):
450
+ # at 12 to 200 columns: a bar of 10 cells or more, the start of the prefix and the end of the suffix, each cut only between
451
+ # fields ( a number keeps its unit, a label its number ), and the prefix's first field and the suffix's last as long as
452
+ # there is room for them
453
+ # ( a path has no blank: it is its one field )
454
+ first = '123.46 KF |' if prefix == COPY_PREFIX else prefix + ' |'
455
+ last = '| Overall: 1.23 M Files' if suffix == REMOVAL else '| 1h2m3s'
456
+ prefix, suffix = prefix + ' |', '| ' + suffix
457
+ for columns in range(12, 201):
458
+ frame = _genrate_progress_bar(5, 10, prefix, suffix, columns)
459
+ head, bar, tail = parts(frame)
460
+ if tail.startswith('|50.0% '):
461
+ tail = tail[len('|50.0% '):]
462
+ assert cells(frame) == columns - 2 and bar >= 10, (columns, frame)
463
+ assert head == '' or head == prefix or head.endswith(' |') and a_broken_end(head[:-2], prefix) is None, (columns, frame)
464
+ assert tail == '' or tail == suffix or tail.startswith('| ') and a_broken_start(tail[2:], suffix) is None, (columns, frame)
465
+ if cells(first) + 10 <= columns - 2:
466
+ assert head.startswith(first[:-2]), (columns, frame)
467
+ if cells(head) + cells(last) + 10 <= columns - 2:
468
+ assert tail.endswith(last), (columns, frame)
469
+
470
+
471
+ @pytest.mark.parametrize('suffix', [COPY, COPY_LIMITED, REMOVAL], ids=['copy', 'copy-limited', 'removal'])
472
+ @pytest.mark.parametrize('prefix', [COPY_PREFIX, PATH[-50:], WIDE_PATH[-50:]], ids=['copy', 'path', 'wide-path'])
473
+ def test_the_first_frame_of_a_known_total_has_the_place_of_its_bar_and_no_double_joint(prefix, suffix):
474
+ # iteration 0 ( hpcp's first copy bar frame ) is cut as the frames after it, and its text stands where theirs does: the bar's
475
+ # place holds ' Calculating... ' when it fits, else blanks ( 1.69 first cut it for a bar it never drew and joined prefix and
476
+ # suffix with nothing between: '0.00 F || 0.00 F/s | ∞' at 40 columns ). 1 of 1000 has a percent as long as 0's
477
+ prefix, suffix = prefix + ' |', '| ' + suffix
478
+ for columns in range(12, 201):
479
+ first = _genrate_progress_bar(0, 1000, prefix, suffix, columns)
480
+ head, bar, tail = parts(_genrate_progress_bar(1, 1000, prefix, suffix, columns))
481
+ if tail.startswith('|0.1% '):
482
+ tail = tail[len('|0.1% '):]
483
+ place = ' Calculating... ' if bar >= 16 else ' ' * bar
484
+ assert first == head + place + tail, (columns, first)
485
+ assert '||' not in first and cells(first) <= columns - 2, (columns, first)
486
+
487
+
488
+ def test_a_bar_past_its_total_ends_its_line(monkeypatch, capsys):
489
+ # 1.68 ended the line only at a frame equal to the total: a count that stepped over it left the line open, and the program's
490
+ # next output joined the bar ( '...|110.0% | snext' ). The first frame at or past the total ends it now; the frame at the
491
+ # total still ends it each time; a later frame past it is drawn on the next line and left open, so it does not scroll
492
+ monkeypatch.setattr(multiCMD, 'get_terminal_size', lambda: os.terminal_size((40, 24)))
493
+ monkeypatch.setattr(multiCMD, '_bar_done', [None])
494
+ print_progress_bar(9, 10, 'p', 's')
495
+ print_progress_bar(11, 10, 'p', 's')
496
+ print('next')
497
+ shown = capsys.readouterr().out.split('\n')
498
+ assert len(shown) == 3 and shown[0].endswith('|110.0% | s') and shown[1:] == ['next', ''], shown
499
+ for frames, lines in [
500
+ # a count that steps over the total, then goes on: one line end, after the first frame past it
501
+ ([9, 12, 13, 15], 1),
502
+ # a frame at the total, each time ( as 1.68 )
503
+ ([5, 10, 10], 2),
504
+ # at the total, then past it: as 1.68
505
+ ([9, 10, 11], 1),
506
+ # a new bar of the same total: a frame below it starts it again
507
+ ([12, 3, 12], 2),
508
+ ]:
509
+ for iteration in frames:
510
+ print_progress_bar(iteration, 10, 'p', 's')
511
+ assert capsys.readouterr().out.count('\n') == lines, frames
512
+ print_progress_bar(0, 10, 'p', 's')
513
+ capsys.readouterr()
514
+
515
+
516
+ @posix_only
517
+ def test_the_output_after_a_bar_past_its_total_starts_a_line_of_its_own():
518
+ # at a terminal of no size ( 80x24 ): the bar's line ends with the first frame past the total, and 'next' starts the line after it
519
+ code = ('import multiCMD\n'
520
+ 'for i in (5, 11): multiCMD.print_progress_bar(i, 10, "p", "s")\n'
521
+ 'print("next")')
522
+ shown = at_a_pty(code, 0, 0)
523
+ assert shown.endswith('|110.0% | s\nnext\n') and shown.count('\n') == 2, shown
524
+
525
+
526
+ CONTROL = re.compile('[\x00-\x1f\x7f-\x9f]')
527
+
528
+
529
+ @pytest.mark.parametrize('name', ['run\tlog', 'run\nlog', 'run\rlog', 'run\x1b[2Jlog', 'run\x7flog', 'run\x9b2Jlog', 'run\x00log'],
530
+ ids=['tab', 'newline', 'carriage-return', 'escape', 'delete', 'c1-csi', 'nul'])
531
+ def test_a_control_character_in_a_frame_is_a_question_mark(name):
532
+ # a file name may hold any of them ( hpcp's removal and listing lines show the end of a path ): 1.68 wrote a newline as a
533
+ # new row, so each redraw scrolled, and a tab to the next tab stop, so the frame could reach the last column and wrap
534
+ shown = CONTROL.sub('?', name)
535
+ assert shown.startswith('run?') and shown.endswith('log')
536
+ frame = _genrate_progress_bar(7, 0, '/data/' + name + ' |', '| Removed: 1.23 K', 80)
537
+ assert frame == ('/data/' + shown + ' | iteration:7 | Removed: 1.23 K').ljust(79), frame
538
+ frame = _genrate_progress_bar(5, 10, name + ' |', '| ' + name, 80)
539
+ assert not CONTROL.search(frame) and cells(frame) == 78 and frame.startswith(shown + ' |') and frame.endswith('| ' + shown)
540
+
541
+
542
+ @posix_only
543
+ def test_a_name_with_a_tab_and_a_newline_stays_on_one_row():
544
+ # at an 80-column terminal: three frames, no row of their own and no tab ( 1.68: a row more per frame, each scrolling )
545
+ code = ('import multiCMD\n'
546
+ 'for i in range(3): multiCMD.print_progress_bar(i, 0, "/data/run\\tlog\\nold", "Removed: 1.23 K")\n')
547
+ shown = at_a_pty(code, 24, 80)
548
+ assert '\n' not in shown and '\t' not in shown and len(frames(shown)) == 3, shown
549
+ assert [frame.rstrip() for frame in frames(shown)] == ['/data/run?log?old | iteration:%d | Removed: 1.23 K' % i for i in range(3)]
550
+ assert all(cells(frame) == 79 for frame in frames(shown)), shown
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes