(
root: Path,
*,
top_n: int,
include_hidden: bool,
follow_symlinks: bool,
ignore: List[str],
)
| 823 | |
| 824 | # ------------------ Folder sizes report (Top-N immediate children) ------------------ |
| 825 | def folder_sizes_report( |
| 826 | root: Path, |
| 827 | *, |
| 828 | top_n: int, |
| 829 | include_hidden: bool, |
| 830 | follow_symlinks: bool, |
| 831 | ignore: List[str], |
| 832 | ) -> List[Tuple[int, Path]]: |
| 833 | items: List[Tuple[int, Path]] = [] |
| 834 | if not root.is_dir(): |
| 835 | st = safe_lstat(root) |
| 836 | if st: |
| 837 | return [(int(st.st_size), root)] |
| 838 | return [] |
| 839 | |
| 840 | try: |
| 841 | with os.scandir(root) as it: |
| 842 | for de in it: |
| 843 | if de.name in (".", ".."): |
| 844 | continue |
| 845 | if not include_hidden and de.name.startswith("."): |
| 846 | continue |
| 847 | p = root / de.name |
| 848 | if match_any(p, de.name, ignore): |
| 849 | continue |
| 850 | try: |
| 851 | st = de.stat(follow_symlinks=follow_symlinks) |
| 852 | except OSError: |
| 853 | continue |
| 854 | if stat.S_ISDIR(st.st_mode): |
| 855 | sz = get_total_size(p, follow_symlinks=follow_symlinks, ignore=ignore, only=[], include_hidden=include_hidden, stats=None) |
| 856 | else: |
| 857 | sz = int(st.st_size) |
| 858 | items.append((int(sz), p)) |
| 859 | except OSError: |
| 860 | return [] |
| 861 | |
| 862 | items.sort(key=lambda x: x[0], reverse=True) |
| 863 | return items[: max(0, top_n)] |
| 864 | |
| 865 | def format_sizes_report(items: List[Tuple[int, Path]]) -> str: |
| 866 | total = sum(sz for sz, _ in items) or 1 |
no test coverage detected