diff --git a/CHANGELOG.md b/CHANGELOG.md index fa83c93..903a490 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,21 @@ All notable changes to this project are documented here. Format: [Keep a Changel ## [Unreleased] +### Added +- Undo: developer build folders and uninstall remnants go to the freedesktop Trash; `marmot undo` (or the Undo button) restores the last batch. Every action is logged to `~/.local/state/marmot/history.jsonl` (`marmot history`). +- Dry-run for every cleaner item (Preview button, `marmot clean --dry-run`). +- Package backends for dnf (dnf4/dnf5), zypper and apk, plus Flatpak apps in the uninstaller. +- Command line: `marmot status|scan|clean|undo|history`, with `--json` output. +- Cleaner: per-folder `~/.cache` selection, orphaned packages, unused Flatpak runtimes, Docker/Podman unused data, old Snap revisions. +- Disk Analyzer: cached directory sizes (instant back/forward), progress, cancel on navigation, percentage bars, stays on one filesystem. + +### Changed +- Scans, cleaning and uninstalling run off the UI thread, so the TUI no longer freezes during a pkexec prompt. + +### Fixed +- Operation logs printed raw `[success]` markup instead of colours. +- Journal size was always reported as 0 when `journalctl` printed sizes like `1.2G`. + ## [1.1.0] ### Added diff --git a/README.md b/README.md index 1629a61..11f2f76 100644 --- a/README.md +++ b/README.md @@ -44,14 +44,32 @@ Upgrade with `uv tool upgrade marmot-tui`, remove with `uv tool uninstall marmot **Supported distributions** -| Family | Package backend | Status | -|---|---|---| -| Debian, Ubuntu, Mint, Pop!_OS, Kali | apt / dpkg | full | -| Arch, Manjaro, EndeavourOS, CachyOS | pacman | full | -| Others (Fedora, openSUSE, Alpine...) | none yet | Monitor, Cleaner (no package cache), Analyzer, Optimizer | +| Family | Package backend | +|---|---| +| Debian, Ubuntu, Mint, Pop!_OS, Kali | apt / dpkg | +| Arch, Manjaro, EndeavourOS, CachyOS | pacman | +| Fedora, RHEL, Rocky, Alma | dnf (dnf4 and dnf5) | +| openSUSE Tumbleweed / Leap | zypper | +| Alpine | apk | +| any distro | Flatpak apps (uninstall, unused runtimes) | + +Docker/Podman and Snap cleanup appear automatically when those tools are installed. Journal cleaning and service reset need systemd. +### Command line + +Every cleaning action also works without the TUI (scripts, cron, SSH): + +```bash +marmot status [--json] # CPU, RAM, disks, battery +marmot scan [--json] # what can be cleaned, with item ids +marmot clean user_cache dev_junk --dry-run +marmot clean user_cache --keep-cache mozilla yay --yes +marmot undo # restore what the last action moved to Trash +marmot history +``` + ### Keyboard Navigation & Controls - `TAB` / `Shift+TAB` : Switch between active Tabs (`Monitor`, `Cleaner`, `Uninstaller`, `Disk Analyzer`, `Optimizer`) diff --git a/pyproject.toml b/pyproject.toml index e39d70b..e5bbf4a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -19,7 +19,7 @@ Homepage = "https://github.com/aquistusfami/Marmot" Issues = "https://github.com/aquistusfami/Marmot/issues" [project.scripts] -marmot = "marmot.app:main" +marmot = "marmot.cli:main" [dependency-groups] dev = ["pytest>=8", "pytest-cov>=5", "ruff>=0.6"] diff --git a/src/marmot/__main__.py b/src/marmot/__main__.py index 02ed752..c4b6232 100644 --- a/src/marmot/__main__.py +++ b/src/marmot/__main__.py @@ -1,3 +1,5 @@ -from marmot.app import main +import sys -main() +from marmot.cli import main + +sys.exit(main()) diff --git a/src/marmot/analyzer.py b/src/marmot/analyzer.py index 7a680d4..ca6094d 100644 --- a/src/marmot/analyzer.py +++ b/src/marmot/analyzer.py @@ -1,16 +1,29 @@ import os +import stat +from collections.abc import Callable from pathlib import Path +class ScanCancelled(Exception): + pass + + class DiskAnalyzer: + """ncdu-like sizes. Every directory size computed is cached, so going into a child or back + to the parent after a scan is instant. `invalidate()` drops the cache (Rescan, after cleaning).""" + def __init__(self): self.current_path = Path.home() - + self._cache: dict[str, int] = {} + def set_path(self, path_str: str): path = Path(path_str) if path.exists() and path.is_dir(): self.current_path = path + def invalidate(self) -> None: + self._cache.clear() + def format_size(self, size_bytes: int) -> str: if size_bytes == 0: return "0 B" @@ -21,50 +34,74 @@ def format_size(self, size_bytes: int) -> str: i += 1 return f"{size_bytes:.2f} {size_name[i]}" - def _recursive_size(self, path_str: str) -> int: - import stat - total = 0 + def _recursive_size(self, path_str: str, cancelled: Callable[[], bool] | None = None) -> int: + """Apparent size of a file, or of a directory tree (symlinks count 0, other filesystems skipped).""" + cached = self._cache.get(path_str) + if cached is not None: + return cached try: st = os.lstat(path_str) - if stat.S_ISREG(st.st_mode): - return st.st_size - elif stat.S_ISLNK(st.st_mode): - return 0 - - for root, _dirs, files in os.walk(path_str): - for f in files: + except OSError: + return 0 + if stat.S_ISLNK(st.st_mode): + return 0 + if not stat.S_ISDIR(st.st_mode): + return st.st_size + return self._dir_size(path_str, st.st_dev, cancelled) + + def _dir_size(self, path_str: str, dev: int, cancelled) -> int: + cached = self._cache.get(path_str) + if cached is not None: + return cached + if cancelled and cancelled(): + raise ScanCancelled + total = 0 + try: + with os.scandir(path_str) as it: + for entry in it: try: - fp = os.path.join(root, f) - fst = os.lstat(fp) - if not stat.S_ISLNK(fst.st_mode): - total += fst.st_size + if entry.is_symlink(): + continue + if entry.is_dir(follow_symlinks=False): + # like `du -x`: don't wander into /proc, network mounts, other disks + if entry.stat(follow_symlinks=False).st_dev == dev: + total += self._dir_size(entry.path, dev, cancelled) + else: + total += entry.stat(follow_symlinks=False).st_size except OSError: pass - except Exception: + except OSError: pass + self._cache[path_str] = total return total - def scan_current(self) -> list: + def scan_current(self, progress: Callable[[int, int, str], None] | None = None, + cancelled: Callable[[], bool] | None = None) -> list: + """Sizes of the current directory's entries, biggest first. + `progress(done, total, name)` is called per entry; raises ScanCancelled if `cancelled()` turns true.""" items = [] try: - for entry in os.scandir(self.current_path): - try: - is_directory = entry.is_dir(follow_symlinks=False) - if is_directory: - size = self._recursive_size(entry.path) - else: - size = entry.stat().st_size - - items.append({ - 'name': entry.name, - 'path': entry.path, - 'size': size, - 'size_str': self.format_size(size), - 'is_dir': is_directory - }) - except OSError: - pass - items.sort(key=lambda x: x['size'], reverse=True) + entries = list(os.scandir(self.current_path)) except Exception: - pass + return items + for n, entry in enumerate(entries): + if progress: + progress(n, len(entries), entry.name) + try: + is_directory = entry.is_dir(follow_symlinks=False) + size = self._recursive_size(entry.path, cancelled) if is_directory else entry.stat().st_size + items.append({ + 'name': entry.name, + 'path': entry.path, + 'size': size, + 'size_str': self.format_size(size), + 'is_dir': is_directory + }) + except OSError: + pass + items.sort(key=lambda x: x['size'], reverse=True) + total = sum(i['size'] for i in items) or 1 + for i in items: + i['percent'] = i['size'] * 100 / total + self._cache[str(self.current_path)] = sum(i['size'] for i in items) return items diff --git a/src/marmot/app.py b/src/marmot/app.py index 50cbf8b..622ddb6 100644 --- a/src/marmot/app.py +++ b/src/marmot/app.py @@ -15,8 +15,10 @@ TabbedContent, TabPane, ) +from textual.worker import get_current_worker -from marmot.analyzer import DiskAnalyzer +from marmot import trash +from marmot.analyzer import DiskAnalyzer, ScanCancelled from marmot.cleaner import SystemCleaner # Import Marmot Modules @@ -165,7 +167,8 @@ def __init__(self, package_info: dict): self.package_info = package_info def compose(self) -> ComposeResult: - yield Label(f"[bold cyan]{self.package_info['name']}[/bold cyan] ([dim]{self.package_info['version']}[/dim])\n[dim]{self.package_info['summary']}[/dim]") + tag = " [magenta]flatpak[/]" if self.package_info.get('source') == 'flatpak' else "" + yield Label(f"[bold cyan]{self.package_info['name']}[/bold cyan]{tag} ([dim]{self.package_info['version']}[/dim])\n[dim]{self.package_info['summary']}[/dim]") class DiskListItem(ListItem): @@ -174,9 +177,14 @@ def __init__(self, item_info: dict): self.item_info = item_info def compose(self) -> ComposeResult: - prefix = "[DIR] " if self.item_info['is_dir'] else " " - name_style = "[bold cyan]" if self.item_info['is_dir'] else "[white]" - yield Label(f"{prefix}{name_style}{self.item_info['name']}[/] - [magenta]{self.item_info['size_str']}[/]") + info = self.item_info + pct = info.get('percent', 0) + filled = round(pct / 10) + bar = f"[magenta]{'█' * filled}[/][dim]{'░' * (10 - filled)}[/]" + prefix = "[DIR] " if info['is_dir'] else " " + name_style = "[bold cyan]" if info['is_dir'] else "[white]" + yield Label(f"{bar} {pct:5.1f}% [magenta]{info['size_str']:>10}[/] {prefix}{name_style}{info['name']}[/]", + markup=True) class MarmotApp(App): @@ -222,24 +230,27 @@ def compose(self) -> ComposeResult: with TabPane("Cleaner", id="pane-cleaner"): with Horizontal(): with Vertical(id="cleaner-options-panel", classes="half-width-pad-right"): - yield Label("[bold]Select areas to clean:[/bold]\n") - yield Checkbox("User Caches (~/.cache)", value=True, id="chk-user-cache") - yield Checkbox("System Trash Bin", value=True, id="chk-trash") - pm = self.cleaner.pm - if pm: - yield Checkbox(f"{pm['label']} Package Cache", value=False, id="chk-pkg-cache") - yield Checkbox("Systemd Journal Logs", value=False, id="chk-systemd-logs") - yield Checkbox("Developer Build Files (node_modules, target...)", value=False, id="chk-dev-junk") - - with Horizontal(classes="margin-top-2"): - yield Button("Scan Storage", variant="primary", id="btn-clean-scan") - yield Button("Clean Selected", variant="error", id="btn-clean-run", classes="margin-left-2") - + yield Label("[bold]Areas to clean[/bold] [dim](press Scan to measure)[/dim]") + self.clean_items = SelectionList(id="list-clean-items") + yield self.clean_items + self.clean_cache_lbl = Label("[bold]Cache folders[/bold] [dim](untick to keep)[/dim]") + self.clean_cache_lbl.display = False + yield self.clean_cache_lbl + self.clean_cache = SelectionList(id="list-clean-cache") + self.clean_cache.display = False + yield self.clean_cache + yield Checkbox("Move developer files to Trash (undoable)", value=True, id="chk-use-trash") + with Horizontal(classes="margin-top-1 button-row"): + yield Button("Scan", variant="primary", id="btn-clean-scan") + yield Button("Preview", id="btn-clean-preview") + yield Button("Clean", variant="error", id="btn-clean-run") + yield Button("Undo", id="btn-clean-undo") + with Vertical(classes="half-width"): yield Label("[bold]Operation Logs:[/bold]") - self.clean_log = RichLog(highlight=True, classes="log-view") + self.clean_log = RichLog(highlight=True, markup=True, classes="log-view") yield self.clean_log - + # 3. Smart Uninstaller with TabPane("Uninstaller", id="pane-uninstaller"): with Horizontal(): @@ -261,7 +272,7 @@ def compose(self) -> ComposeResult: self.uninstall_btn = Button("Purge Package & Configuration Remnants", variant="error", id="btn-uninstall-purge", disabled=True) yield self.uninstall_btn yield Label("[bold]Uninstaller Execution Log:[/bold]", classes="margin-top-1") - self.uninstall_log = RichLog(highlight=True, classes="log-view") + self.uninstall_log = RichLog(highlight=True, markup=True, classes="log-view") yield self.uninstall_log # 4. Disk Analyzer @@ -270,7 +281,7 @@ def compose(self) -> ComposeResult: self.analyzer_path_lbl = Label(f"Current Directory: {self.analyzer.current_path}", id="lbl-analyzer-path") yield self.analyzer_path_lbl with Horizontal(classes="analyzer-header"): - yield Button("Scan Current Directory", variant="primary", id="btn-analyzer-scan") + yield Button("Rescan", variant="primary", id="btn-analyzer-scan") yield Button("Go to Parent (..)", variant="default", id="btn-analyzer-up", classes="margin-left-2") self.analyzer_list = ListView(id="list-analyzer") yield self.analyzer_list @@ -287,7 +298,7 @@ def compose(self) -> ComposeResult: yield Button("Reclaim Swap Space", id="btn-opt-swap-reset", variant="primary") with Vertical(classes="half-width"): yield Label("[bold]Optimizer Execution Log:[/bold]") - self.optimize_log = RichLog(highlight=True, classes="log-view") + self.optimize_log = RichLog(highlight=True, markup=True, classes="log-view") yield self.optimize_log yield Footer() @@ -344,6 +355,10 @@ def on_button_pressed(self, event: Button.Pressed) -> None: self.run_clean_scan() elif button_id == "btn-clean-run": self.run_clean_execution() + elif button_id == "btn-clean-preview": + self.run_clean_execution(dry_run=True) + elif button_id == "btn-clean-undo": + self.run_undo() elif button_id == "btn-opt-drop-caches": self.run_tweak("drop_caches") elif button_id == "btn-opt-flush-dns": @@ -355,6 +370,7 @@ def on_button_pressed(self, event: Button.Pressed) -> None: elif button_id == "btn-uninstall-purge": self.run_purge_execution() elif button_id == "btn-analyzer-scan": + self.analyzer.invalidate() self.refresh_analyzer() elif button_id == "btn-analyzer-up": # Navigate to parent @@ -364,57 +380,81 @@ def on_button_pressed(self, event: Button.Pressed) -> None: self.analyzer_path_lbl.update(f"Current Directory: {self.analyzer.current_path}") self.refresh_analyzer() + # Scan/clean can take long (du of ~/.cache, pkexec password prompt): run them off the UI thread. + def _set_cleaner_busy(self, busy: bool) -> None: + for bid in ("btn-clean-scan", "btn-clean-preview", "btn-clean-run", "btn-clean-undo"): + self.query_one(f"#{bid}", Button).disabled = busy + def run_clean_scan(self) -> None: - self.clean_log.write("Starting storage scan...") - self.clean_scan_data = self.cleaner.scan() - self.clean_log.write("Scan completed successfully.\n") - - # Display sizes inside checkboxes - for key, val in self.clean_scan_data.items(): - self.clean_log.write(f"- [bold]{val['name']}[/]: {val['size_str']} ({val['desc']})") - - # Update check label with size info dynamically - if key == 'user_cache': - self.query_one("#chk-user-cache", Checkbox).label = f"User Caches: {val['size_str']}" - elif key == 'trash': - self.query_one("#chk-trash", Checkbox).label = f"System Trash Bin: {val['size_str']}" - elif key == 'pkg_cache': - self.query_one("#chk-pkg-cache", Checkbox).label = f"{val['name']}: {val['size_str']}" - elif key == 'systemd_logs': - self.query_one("#chk-systemd-logs", Checkbox).label = f"Systemd Journal Logs: {val['size_str']}" - elif key == 'dev_junk': - self.query_one("#chk-dev-junk", Checkbox).label = f"Developer Build Files: {val['size_str']}" - - def run_clean_execution(self) -> None: + self._set_cleaner_busy(True) + self.clean_log.write("Scanning storage...") + self.run_worker(self._clean_scan_worker, thread=True, exclusive=True, group="cleaner") + + def _clean_scan_worker(self) -> None: + data = self.cleaner.scan() + self.call_from_thread(self._show_clean_scan, data) + + def _show_clean_scan(self, data: dict) -> None: + previously = set(self.clean_items.selected) if self.clean_scan_data else {"user_cache", "trash"} + self.clean_scan_data = data + self.clean_items.clear_options() + self.clean_items.add_options([ + (f"{v['name']}: {v['size_str']}" + (" [dim](root)[/dim]" if v['requires_sudo'] else ""), k, k in previously) + for k, v in data.items()]) + children = data.get('user_cache', {}).get('children', []) + self.clean_cache.clear_options() + self.clean_cache.add_options([(f"{c['name']}: {c['size_str']}", c['path'], True) for c in children[:40]]) + self.clean_cache.display = self.clean_cache_lbl.display = bool(children) + for v in data.values(): + self.clean_log.write(f"- [bold]{v['name']}[/]: {v['size_str']} [dim]({v['desc']})[/]") + self.clean_log.write("Scan completed.\n") + self._set_cleaner_busy(False) + + def _clean_args(self) -> dict: + cache = list(self.clean_cache.selected) + children = self.clean_scan_data.get('user_cache', {}).get('children', []) + # folders beyond the 40 shown are cleaned only when every shown one is ticked + if len(children) > 40 and len(cache) == min(40, len(children)): + cache = [c['path'] for c in children] + return {'selected': cache, 'use_trash': self.query_one("#chk-use-trash", Checkbox).value} + + def run_clean_execution(self, dry_run: bool = False) -> None: if not self.clean_scan_data: - self.clean_log.write("[warning]Please scan storage first before cleaning.[/]") + self.clean_log.write("[yellow]Please scan storage first before cleaning.[/]") return - - chk_mapping = { - 'user_cache': "#chk-user-cache", - 'trash': "#chk-trash", - 'pkg_cache': "#chk-pkg-cache", - 'systemd_logs': "#chk-systemd-logs", - 'dev_junk': "#chk-dev-junk" - } - + items = [i for i in self.clean_items.selected if i in self.clean_scan_data] + if not items: + self.clean_log.write("[yellow]Nothing selected.[/]") + return + self._set_cleaner_busy(True) + args = self._clean_args() + self.run_worker(lambda: self._clean_worker(items, dry_run, args), thread=True, exclusive=True, group="cleaner") + + def _clean_worker(self, items: list, dry_run: bool, args: dict) -> None: cleaned_any = False - for item_id, selector in chk_mapping.items(): - if item_id not in self.clean_scan_data: - continue # e.g. no package cache on an unsupported distro - chk = self.query_one(selector, Checkbox) - if chk.value: - self.clean_log.write(f"Cleaning {self.clean_scan_data[item_id]['name']}...") - success, msg = self.cleaner.clean(item_id, self.clean_scan_data) - if success: - self.clean_log.write(f"[success]✓ {msg}[/]") - cleaned_any = True - else: - self.clean_log.write(f"[danger]✗ {msg}[/]") - + for item_id in items: + name = self.clean_scan_data[item_id]['name'] + self.call_from_thread(self.clean_log.write, f"{'Previewing' if dry_run else 'Cleaning'} {name}...") + success, msg = self.cleaner.clean( + item_id, self.clean_scan_data, dry_run=dry_run, use_trash=args['use_trash'], + selected=args['selected'] if item_id == 'user_cache' else None) + style = "green" if success else "red" + self.call_from_thread(self.clean_log.write, f"[{style}]{'✓' if success else '✗'} {msg}[/]") + cleaned_any |= success and not dry_run if cleaned_any: - # Refresh Scan data - self.run_clean_scan() + self.analyzer.invalidate() + self._clean_scan_worker() # refresh sizes (still in this worker thread) + else: + self.call_from_thread(self._set_cleaner_busy, False) + + def run_undo(self) -> None: + entry, restored, errors = trash.undo_last() + if entry is None: + self.clean_log.write("[yellow]Nothing to undo (only actions that moved files to Trash can be undone).[/]") + return + self.clean_log.write(f"[green]↶ Undid '{entry['action']}' from {entry['ts']}: restored {len(restored)} item(s)[/]") + for e in errors: + self.clean_log.write(f"[red]✗ {e}[/]") # --- UNINSTALLER TAB LOGIC --- def on_input_changed(self, event: Input.Changed) -> None: @@ -433,7 +473,8 @@ def on_list_view_selected(self, event: ListView.Selected) -> None: if item and hasattr(item, "package_info"): pkg = item.package_info - removal = self.uninstaller.preview_removal(pkg['name']) + source = pkg.get('source', 'system') + removal = self.uninstaller.preview_removal(pkg['name'], source) if removal is None: removal_text = "\n\n[bold red]Cannot preview removal (package required by others?). Purge disabled.[/]" else: @@ -442,12 +483,13 @@ def on_list_view_selected(self, event: ListView.Selected) -> None: if extra: removal_text += f"\n[bold yellow]Warning: {len(extra)} other package(s) will also be removed.[/]" - remnants = self.uninstaller.find_remnants(pkg['name']) + remnants = self.uninstaller.find_remnants(pkg['name'], source) self.uninstall_remnants.clear_options() self.uninstall_remnants.add_options([(f"{r['path']} ({r['size_str']})", r['path']) for r in remnants]) self.uninstall_remnants.display = bool(remnants) remnant_text = ("\n\n[bold yellow]Remnant directories found (tick to delete them too):[/]" - if remnants else "\n\nNo config/cache remnants detected.") + if remnants else "\n\nNo config/cache remnants detected." + if source == 'system' else "\n\nApp data in ~/.var/app is removed with it.") info = ( f"[bold]{pkg['name']}[/bold]\n" @@ -458,6 +500,7 @@ def on_list_view_selected(self, event: ListView.Selected) -> None: ) self.uninstall_info.update(info) self.selected_package = pkg['name'] + self.selected_source = source self.uninstall_btn.disabled = removal is None elif event.list_view.id == "list-analyzer": @@ -472,56 +515,67 @@ def on_list_view_selected(self, event: ListView.Selected) -> None: def run_purge_execution(self) -> None: if not hasattr(self, "selected_package"): return - + self.uninstall_btn.disabled = True self.uninstall_log.write(f"Executing purge on {self.selected_package}...") - success, msg = self.uninstaller.uninstall(self.selected_package, list(self.uninstall_remnants.selected)) - + args = (self.selected_package, list(self.uninstall_remnants.selected), self.selected_source) + self.run_worker(lambda: self._purge_worker(*args), thread=True, exclusive=True, group="uninstall") + + def _purge_worker(self, package: str, remnants: list, source: str) -> None: + success, msg = self.uninstaller.uninstall(package, remnants, source) + self.call_from_thread(self._purge_done, success, msg) + + def _purge_done(self, success: bool, msg: str) -> None: if success: - self.uninstall_log.write(f"[success]✓ {msg}[/]") + self.uninstall_log.write(f"[green]✓ {msg}[/]") self.uninstall_info.update("Select a package from the list to view details.") - self.uninstall_btn.disabled = True self.uninstall_remnants.clear_options() self.uninstall_remnants.display = False - # Refresh package list self.refresh_packages_list(self.uninstall_search.value) else: - self.uninstall_log.write(f"[danger]✗ {msg}[/]") + self.uninstall_log.write(f"[red]✗ {msg}[/]") + self.uninstall_btn.disabled = False # --- OPTIMIZER TAB LOGIC --- def run_tweak(self, tweak_id: str) -> None: self.optimize_log.write(f"Running optimization task: {tweak_id}...") success, msg = self.optimizer.run_optimization(tweak_id) if success: - self.optimize_log.write(f"[success]✓ {msg}[/]\n") + self.optimize_log.write(f"[green]✓ {msg}[/]\n") else: - self.optimize_log.write(f"[danger]✗ {msg}[/]\n") + self.optimize_log.write(f"[red]✗ {msg}[/]\n") # --- ANALYZER TAB LOGIC --- def refresh_analyzer(self) -> None: self.analyzer_list.clear() self.analyzer_path_lbl.update(f"Current Directory: [bold]{self.analyzer.current_path}[/bold] [yellow](Scanning...)[/yellow]") - - # Insert a visual loading feedback message into the list (wrapped in ListItem so clear() can remove it) self.analyzer_list.append(ListItem(Label(" Scanning directory contents, please wait...", classes="analyzer-loading-msg"))) - - # Offload the blocking filesystem scan to a background thread worker - self.run_worker(self._scan_analyzer_worker, thread=True, exclusive=True) - + # exclusive=True cancels the previous scan when the user navigates before it finishes + self.run_worker(self._scan_analyzer_worker, thread=True, exclusive=True, group="analyzer") + def _scan_analyzer_worker(self) -> None: - # Executes in a background thread to prevent UI freezing - items = self.analyzer.scan_current() - + worker = get_current_worker() + path = self.analyzer.current_path + + def progress(done: int, total: int, name: str) -> None: + self.call_from_thread(self.analyzer_path_lbl.update, + f"Current Directory: [bold]{path}[/bold] [yellow]({done}/{total}: {name})[/yellow]") + try: + items = self.analyzer.scan_current(progress=progress, cancelled=lambda: worker.is_cancelled) + except ScanCancelled: + return + def update_ui(): self.analyzer_list.clear() - self.analyzer_path_lbl.update(f"Current Directory: [bold]{self.analyzer.current_path}[/bold]") + total = self.analyzer.format_size(sum(i['size'] for i in items)) + self.analyzer_path_lbl.update(f"Current Directory: [bold]{path}[/bold] [dim]total {total}[/dim]") if not items: self.analyzer_list.append(ListItem(Label(" (Empty Directory)", classes="analyzer-loading-label"))) else: for it in items: self.analyzer_list.append(DiskListItem(it)) - - # Safely schedule the UI updates back on the main thread - self.call_from_thread(update_ui) + + if not worker.is_cancelled: + self.call_from_thread(update_ui) def main() -> None: diff --git a/src/marmot/cleaner.py b/src/marmot/cleaner.py index 931cd14..17a1eba 100644 --- a/src/marmot/cleaner.py +++ b/src/marmot/cleaner.py @@ -1,10 +1,12 @@ import os +import re +import shlex import shutil import subprocess import sys from pathlib import Path -from marmot import pkgmgr +from marmot import pkgmgr, trash # the virtualenv Marmot itself runs from (repo .venv, pipx/uv tool env): never offered as junk RUNNING_VENV = Path(sys.prefix).resolve() @@ -19,19 +21,44 @@ "build.gradle", "build.gradle.kts", "package.json"), } +UNKNOWN = -1 # size could not be measured (orphans, flatpak runtimes) +_UNITS = {"": 1, "B": 1, "K": 1024, "M": 1024**2, "G": 1024**3, "T": 1024**4} + +# remove disabled snap revisions in one pkexec call; names/revisions are passed as argv, never interpolated +_SNAP_REMOVE = 'while [ $# -gt 1 ]; do snap remove "$1" --revision="$2" || exit 1; shift 2; done' + + +def parse_size(text: str) -> int: + """'1.2GB', '120.4M', '3.5 GiB', '0B' -> bytes (binary units); UNKNOWN if unparsable.""" + m = re.match(r"\s*([\d.]+)\s*([KMGT]?)i?B?\b", text, re.I) + return int(float(m[1]) * _UNITS[m[2].upper()]) if m else UNKNOWN + + +def run_root(cmd: list) -> tuple[bool, str, str]: + """pkexec first, then passwordless sudo. Returns (ok, " (via sudo)" or "", stdout-or-error).""" + err = "" + for prefix, suffix in ((["pkexec"], ""), (["sudo", "-n"], " (via sudo)")): + try: + res = subprocess.run([*prefix, *cmd], check=True, capture_output=True) + return True, suffix, res.stdout.decode("utf-8", errors="ignore").strip() + except Exception as e: + err = str(e) + return False, "", err + class SystemCleaner: def __init__(self, backend: str | None = None): self.home = Path.home() self.pm = pkgmgr.get(backend) - + # backend pinned (tests) -> do not probe the host for extra tools + self.probe_host = backend is None + def get_dir_size(self, path: Path) -> int: total_size = 0 try: if path.is_file(): return path.stat().st_size for root, _dirs, files in os.walk(path): - # Handle permission errors gracefully for f in files: fp = os.path.join(root, f) try: @@ -44,6 +71,8 @@ def get_dir_size(self, path: Path) -> int: return total_size def format_size(self, size_bytes: int) -> str: + if size_bytes == UNKNOWN: + return "?" if size_bytes == 0: return "0 B" size_name = ("B", "KB", "MB", "GB", "TB") @@ -53,195 +82,287 @@ def format_size(self, size_bytes: int) -> str: i += 1 return f"{size_bytes:.2f} {size_name[i]}" + def _item(self, name: str, path: str, size: int, desc: str, sudo: bool, **extra) -> dict: + return {'name': name, 'path': path, 'size': size, 'size_str': self.format_size(size), + 'desc': desc, 'requires_sudo': sudo, **extra} + + # ---- scan ---------------------------------------------------------------- + def scan(self) -> dict: results = {} - - # 1. User Cache (~/.cache) + + # 1. User cache, per application folder so the user can keep e.g. a browser cache cache_path = self.home / ".cache" - cache_size = self.get_dir_size(cache_path) - results['user_cache'] = { - 'name': "User Caches", - 'path': str(cache_path), - 'size': cache_size, - 'size_str': self.format_size(cache_size), - 'desc': "Temporary application cache files (Chrome, Firefox, Spotify, etc.)", - 'requires_sudo': False - } - - # 2. Trash (~/.local/share/Trash) - trash_path = self.home / ".local" / "share" / "Trash" + children = [] + try: + for entry in cache_path.iterdir(): + if entry.is_symlink(): + continue + sz = self.get_dir_size(entry) + children.append({'name': entry.name, 'path': str(entry), 'size': sz, 'size_str': self.format_size(sz)}) + except OSError: + pass + children.sort(key=lambda c: c['size'], reverse=True) + cache_size = sum(c['size'] for c in children) if children else self.get_dir_size(cache_path) + results['user_cache'] = self._item( + "User Caches", str(cache_path), cache_size, + "Temporary application cache files (Chrome, Firefox, Spotify, etc.)", False, children=children) + + # 2. Trash + trash_path = trash.trash_dir() trash_size = self.get_dir_size(trash_path) - results['trash'] = { - 'name': "System Trash", - 'path': str(trash_path), - 'size': trash_size, - 'size_str': self.format_size(trash_size), - 'desc': "Files in your trash bin", - 'requires_sudo': False - } - - # 3. Package manager cache (apt: .deb archives, pacman: /var/cache/pacman/pkg) - if self.pm: - cache_path = self.pm['cache_dir'] - cache_size = 0 + results['trash'] = self._item("System Trash", str(trash_path), trash_size, + "Files in your trash bin (deleted permanently)", False) + + # 3. Package manager cache + if self.pm and self.pm['cache_dir'] is not None: + cache_dir = self.pm['cache_dir'] + size = 0 try: - for f in cache_path.glob(self.pm['cache_glob']): + for f in cache_dir.glob(self.pm['cache_glob']): try: - cache_size += f.stat().st_size + size += f.stat().st_size except OSError: pass except OSError: pass # missing/unreadable cache dir (Python < 3.13 raises from glob) - results['pkg_cache'] = { - 'name': f"{self.pm['label']} Package Cache", - 'path': str(cache_path), - 'size': cache_size, - 'size_str': self.format_size(cache_size), - 'desc': f"Downloaded installation packages in {cache_path}", - 'requires_sudo': True - } - - # 4. Systemd Journal Logs + results['pkg_cache'] = self._item(f"{self.pm['label']} Package Cache", str(cache_dir), size, + f"Downloaded installation packages in {cache_dir}", True) + + # 4. Orphaned packages (dependencies nothing needs any more) + if self.pm and self.pm.get('orphans_cmd'): + orphans = pkgmgr.query(self.pm['orphans_cmd'], self.pm['parse_orphans']) or [] + if orphans: + results['orphans'] = self._item( + f"Orphaned Packages ({len(orphans)})", "", UNKNOWN, + f"Unneeded dependencies: {', '.join(orphans[:8])}{'...' if len(orphans) > 8 else ''}", + True, packages=orphans) + + # 5. Systemd journal + journal_size = 0 try: out = subprocess.check_output(["journalctl", "--disk-usage"], stderr=subprocess.DEVNULL) - # Example output: "Archived and active journals take up 1.2GB in the file system." - parts = out.decode("utf-8").split() - # Try to find a size part - for part in parts: - if "take" in part or "up" in part: - continue - if "B" in part or "KB" in part or "MB" in part or "GB" in part: - # found size string - results['systemd_logs'] = { - 'name': "Systemd Journal Logs", - 'path': "/var/log/journal", - 'size': 1, # placeholder greater than 0 - 'size_str': part, - 'desc': "System services and boot logs", - 'requires_sudo': True - } - break + # "Archived and active journals take up 1.2G in the file system." + m = re.search(r"take up (\S+)", out.decode("utf-8", errors="ignore")) + journal_size = parse_size(m[1]) if m else 0 + size_str = m[1] if m else "0 B" except Exception: - pass - - if 'systemd_logs' not in results: - results['systemd_logs'] = { - 'name': "Systemd Journal Logs", - 'path': "/var/log/journal", - 'size': 0, - 'size_str': "0 B", - 'desc': "System services and boot logs", - 'requires_sudo': True - } - - # 5. Developer Junk (Scan first level of user folders for node_modules, target, .venv) - dev_size = 0 - dev_paths = [] - # To keep scan fast, we search in standard locations + size_str = "0 B" + results['systemd_logs'] = self._item("Systemd Journal Logs", "/var/log/journal", max(journal_size, 0), + "System services and boot logs (keeps the last day)", True) + results['systemd_logs']['size_str'] = size_str + + # 6. Developer junk + dev_paths = self._find_dev_junk() + dev_size = sum(self.get_dir_size(Path(p)) for p in dev_paths) + results['dev_junk'] = self._item( + "Developer Build Artifacts", f"{len(dev_paths)} folders found", dev_size, + f"Build artifacts (node_modules, target, .venv) in Projects/Home. Detected: " + f"{', '.join([Path(p).parent.name + '/' + Path(p).name for p in dev_paths[:3]])}...", + False, paths_list=dev_paths) + + if self.probe_host: + results.update(self._scan_extras()) + return results + + def _find_dev_junk(self) -> list: search_dirs = [self.home / "Projects", self.home / "workspace", self.home / "Downloads", self.home] - - found_folders = [] + found = [] for sdir in search_dirs: if sdir.exists() and sdir.is_dir(): try: - # Scan depth 2 to keep it responsive for entry in sdir.iterdir(): if entry.is_dir() and not entry.name.startswith("."): - # Check standard build folders for sub, markers in DEV_JUNK_MARKERS.items(): path = entry / sub # "build"/"target" are only junk next to a project file that produced them if path.exists() and path.is_dir() and path.resolve() != RUNNING_VENV and ( markers is None or any((entry / m).exists() for m in markers)): - found_folders.append(path) + if str(path) not in found: + found.append(str(path)) except Exception: pass - - # Scan inside projects too if they are top level - for folder in found_folders: - sz = self.get_dir_size(folder) - dev_size += sz - dev_paths.append(str(folder)) - - results['dev_junk'] = { - 'name': "Developer Build Artifacts", - 'path': f"{len(dev_paths)} folders found", - 'size': dev_size, - 'size_str': self.format_size(dev_size), - 'desc': f"Build artifacts (node_modules, target, .venv) in Projects/Home. Detected: {', '.join([Path(p).parent.name + '/' + Path(p).name for p in dev_paths[:3]])}...", - 'paths_list': dev_paths, - 'requires_sudo': False - } - + return found + + def _scan_extras(self) -> dict: + """Optional sources that exist only on some hosts: flatpak, podman/docker, snap.""" + results = {} + if shutil.which("flatpak"): + results['flatpak_unused'] = self._item( + "Unused Flatpak Runtimes", "", UNKNOWN, "Runtimes and extensions no installed app uses", False) + + for tool in ("podman", "docker"): + if not shutil.which(tool): + continue + try: + res = subprocess.run([tool, "system", "df", "--format", "{{.Type}}\t{{.Reclaimable}}"], + capture_output=True, timeout=30) + except (OSError, subprocess.SubprocessError): + continue + if res.returncode != 0: + continue # e.g. no permission on the docker socket + size = 0 + for line in res.stdout.decode(errors="ignore").splitlines(): + kind, _, rec = line.partition("\t") + if "volume" not in kind.lower(): # prune without --volumes keeps volumes + size += max(parse_size(rec), 0) + results['containers'] = self._item( + f"{tool.capitalize()} Unused Data", tool, size, + "Stopped containers, dangling images, unused networks, build cache (volumes kept). Size is an upper bound.", + False, tool=tool) + break + + if shutil.which("snap"): + revs = self._disabled_snaps() + if revs: + size = sum(r['size'] for r in revs) + results['snap_old'] = self._item( + f"Old Snap Revisions ({len(revs)})", "/var/lib/snapd/snaps", size, + ", ".join(f"{r['name']} r{r['rev']}" for r in revs[:6]), True, revisions=revs) return results - def clean(self, item_id: str, scan_details: dict) -> tuple[bool, str]: - """Cleans specific item, returns (success, message)""" + def _disabled_snaps(self) -> list: + try: + out = subprocess.run(["snap", "list", "--all"], capture_output=True, timeout=30).stdout.decode() + except (OSError, subprocess.SubprocessError): + return [] + return parse_disabled_snaps(out) + + # ---- clean --------------------------------------------------------------- + + def clean(self, item_id: str, scan_details: dict, dry_run: bool = False, selected: list | None = None, + use_trash: bool = True) -> tuple[bool, str]: + """Clean one scanned item. `selected` narrows user_cache to some folders. + `use_trash` moves user-owned data (dev junk) to the Trash so `marmot undo` can restore it.""" details = scan_details.get(item_id) if not details: return False, "Item not found in scan details" - + path_str = details.get('path') - - # 1. User Cache + if item_id == 'user_cache': cache_path = Path(path_str) - if cache_path.exists(): + if not cache_path.exists(): + return False, "Cache directory does not exist" + children = details.get('children') or [] + targets = [c for c in children if selected is None or c['path'] in selected] + if not children: # scan found no folders (or old scan format): wipe the whole cache dir + if dry_run: + return True, f"Would delete everything in {cache_path}" shutil.rmtree(cache_path, ignore_errors=True) os.makedirs(cache_path, exist_ok=True) + trash.record("clean:user_cache", freed=details.get('size', 0)) return True, "User caches cleaned successfully" - return False, "Cache directory does not exist" - - # 2. Trash + freed = sum(c['size'] for c in targets) + if dry_run: + return True, (f"Would delete {len(targets)} cache folder(s), {self.format_size(freed)}: " + f"{', '.join(c['name'] for c in targets)}") + for c in targets: + p = Path(c['path']) + if p.parent != cache_path: # defence in depth: only direct children of ~/.cache + continue + if p.is_dir() and not p.is_symlink(): + shutil.rmtree(p, ignore_errors=True) + else: + p.unlink(missing_ok=True) + trash.record("clean:user_cache", freed=freed, detail=", ".join(c['name'] for c in targets)) + return True, f"User caches cleaned successfully ({len(targets)} folder(s), {self.format_size(freed)})" + elif item_id == 'trash': trash_path = Path(path_str) - if trash_path.exists(): - # delete files under Trash/files and Trash/info - shutil.rmtree(trash_path, ignore_errors=True) - os.makedirs(trash_path / "files", exist_ok=True) - os.makedirs(trash_path / "info", exist_ok=True) - return True, "Trash bin emptied successfully" - return False, "Trash directory does not exist" - - # 3. Package manager cache + if not trash_path.exists(): + return False, "Trash directory does not exist" + if dry_run: + return True, f"Would permanently delete {details.get('size_str', '?')} from {trash_path}" + shutil.rmtree(trash_path, ignore_errors=True) + os.makedirs(trash_path / "files", exist_ok=True) + os.makedirs(trash_path / "info", exist_ok=True) + trash.record("clean:trash", freed=details.get('size', 0)) + return True, "Trash bin emptied successfully" + elif item_id == 'pkg_cache': if not self.pm: - return False, "Unsupported distribution: no apt or pacman found." - label = self.pm['label'] - try: - subprocess.run(["pkexec", *self.pm['clean_cmd']()], check=True, capture_output=True) - return True, f"{label} packages cache cleaned successfully" - except Exception: - try: - subprocess.run(["sudo", "-n", *self.pm['clean_cmd']()], check=True, capture_output=True) - return True, f"{label} packages cache cleaned successfully (via sudo)" - except Exception as e: - return False, f"Failed to execute. Sudo privileges required. Error: {str(e)}" + return False, "Unsupported distribution: no supported package manager found." + return self._root_action(self.pm['clean_cmd'](), dry_run, item_id, details, + f"{self.pm['label']} packages cache cleaned successfully") + + elif item_id == 'orphans': + names = details.get('packages') or [] + return self._root_action(self.pm['remove_orphans_cmd'](names), dry_run, item_id, details, + f"Removed {len(names)} orphaned package(s)") - # 4. Systemd Logs elif item_id == 'systemd_logs': - try: - # Vacuum journalctl to 1 day - cmd = ["pkexec", "journalctl", "--vacuum-time=1d"] - res = subprocess.run(cmd, check=True, capture_output=True) - return True, f"Systemd logs vacuumed: {res.stdout.decode('utf-8').strip()}" - except Exception: - try: - res = subprocess.run(["sudo", "-n", "journalctl", "--vacuum-time=1d"], check=True, capture_output=True) - return True, f"Systemd logs vacuumed (via sudo): {res.stdout.decode('utf-8').strip()}" - except Exception as e: - return False, f"Failed to execute. Sudo privileges required. Error: {str(e)}" - - # 5. Developer Junk + return self._root_action(["journalctl", "--vacuum-time=1d"], dry_run, item_id, details, + "Systemd logs vacuumed", show_output=True) + + elif item_id == 'snap_old': + argv = [x for r in details.get('revisions', []) for x in (r['name'], str(r['rev']))] + return self._root_action(["sh", "-c", _SNAP_REMOVE, "sh", *argv], dry_run, item_id, details, + f"Removed {len(argv) // 2} old snap revision(s)") + + elif item_id == 'flatpak_unused': + return self._user_action(["flatpak", "uninstall", "--unused", "-y"], dry_run, item_id, details, + "Unused Flatpak runtimes removed") + + elif item_id == 'containers': + return self._user_action([details['tool'], "system", "prune", "-f"], dry_run, item_id, details, + f"{details['tool'].capitalize()} unused data pruned") + elif item_id == 'dev_junk': paths = details.get('paths_list', []) - cleaned_count = 0 + if dry_run: + where = "to Trash" if use_trash else "permanently" + return True, f"Would move {len(paths)} build folder(s) {where}: {', '.join(paths)}" + trashed, cleaned_count = [], 0 for p in paths: try: - shutil.rmtree(p, ignore_errors=True) + if use_trash: + trashed.append({'name': trash.move_to_trash(p), 'path': p}) + else: + shutil.rmtree(p, ignore_errors=True) cleaned_count += 1 except Exception: pass + trash.record("clean:dev_junk", freed=0 if use_trash else details.get('size', 0), trashed=trashed) + if use_trash: + return True, f"Moved {cleaned_count} build folder(s) to Trash (marmot undo restores them)" return True, f"Cleaned {cleaned_count} build folder(s)" - + return False, "Unknown item" + + def _root_action(self, cmd: list, dry_run: bool, item_id: str, details: dict, ok_msg: str, + show_output: bool = False) -> tuple[bool, str]: + if dry_run: + return True, f"Would run as root: {shlex.join(cmd)}" + ok, via, out = run_root(cmd) + if not ok: + return False, f"Failed to execute. Sudo privileges required. Error: {out}" + trash.record(f"clean:{item_id}", freed=max(details.get('size', 0), 0)) + return True, f"{ok_msg}{via}" + (f": {out}" if show_output and out else "") + + def _user_action(self, cmd: list, dry_run: bool, item_id: str, details: dict, ok_msg: str) -> tuple[bool, str]: + if dry_run: + return True, f"Would run: {shlex.join(cmd)}" + try: + res = subprocess.run(cmd, capture_output=True, timeout=600) + except (OSError, subprocess.SubprocessError) as e: + return False, f"Failed: {e}" + if res.returncode != 0: + return False, f"Failed: {res.stderr.decode(errors='ignore').strip()[-300:]}" + trash.record(f"clean:{item_id}", freed=max(details.get('size', 0), 0)) + return True, ok_msg + + +def parse_disabled_snaps(out: str) -> list: + """`snap list --all` rows whose Notes column says `disabled` -> [{name, rev, size}].""" + revs = [] + for line in out.splitlines()[1:]: + cols = line.split() + if len(cols) >= 4 and "disabled" in cols[-1].split(","): + name, rev = cols[0], cols[2] + try: + size = Path(f"/var/lib/snapd/snaps/{name}_{rev}.snap").stat().st_size + except OSError: + size = 0 + revs.append({'name': name, 'rev': rev, 'size': size}) + return revs diff --git a/src/marmot/cli.py b/src/marmot/cli.py new file mode 100644 index 0000000..c34fa16 --- /dev/null +++ b/src/marmot/cli.py @@ -0,0 +1,152 @@ +"""Command line entry point: `marmot` opens the TUI, subcommands work headless (scripts, cron, SSH).""" +import argparse +import json +import sys +import time + +from marmot import __version__, trash + + +def _fmt(n: int) -> str: + if n < 0: + return "?" + for unit in ("B", "KB", "MB", "GB", "TB"): + if n < 1024 or unit == "TB": + return f"{n:.0f} {unit}" if unit == "B" else f"{n:.2f} {unit}" + n /= 1024 + return "" + + +def cmd_status(args) -> int: + from marmot.monitor import SystemMonitor + mon = SystemMonitor() + mon.get_cpu_usage() # first sample primes the counters + time.sleep(0.3) + data = {"cpu_percent": round(mon.get_cpu_usage(), 1), "temperature_c": mon.get_temperature(), + "memory": mon.get_memory_info(), "disks": mon.get_disk_usage(), "battery": mon.get_battery_info()} + if args.json: + print(json.dumps(data, indent=2)) + return 0 + m = data["memory"] + temp = f" {data['temperature_c']:.0f}°C" if data["temperature_c"] else "" + print(f"CPU {data['cpu_percent']:5.1f}%{temp}") + print(f"RAM {m['percent']:5.1f}% {_fmt(m['used'])} / {_fmt(m['total'])} swap {_fmt(m['swap_used'])}") + for d in data["disks"]: + print(f"Disk {d['percent']:5.1f}% {_fmt(d['used'])} / {_fmt(d['total'])} {d['mountpoint']}") + b = data["battery"] + if b.get("present"): + print(f"Bat {b['capacity']}% {b['status']}") + return 0 + + +def _scan(): + from marmot.cleaner import SystemCleaner + cleaner = SystemCleaner() + return cleaner, cleaner.scan() + + +def cmd_scan(args) -> int: + _, data = _scan() + if args.json: + print(json.dumps(data, indent=2)) + return 0 + width = max(len(k) for k in data) + for key, v in data.items(): + root = " (root)" if v["requires_sudo"] else "" + print(f"{key:<{width}} {v['size_str']:>10} {v['name']}{root}") + print("\nClean with: marmot clean ... [--dry-run]") + return 0 + + +def cmd_clean(args) -> int: + cleaner, data = _scan() + unknown = [i for i in args.items if i not in data] + if unknown: + print(f"Unknown or unavailable item(s): {', '.join(unknown)}. Available: {', '.join(data)}", file=sys.stderr) + return 2 + + selected = None + if args.keep_cache: + selected = [c["path"] for c in data["user_cache"].get("children", []) if c["name"] not in args.keep_cache] + + if not args.dry_run and not args.yes: + if not sys.stdin.isatty(): + print("Refusing to clean without --yes when not interactive (use --dry-run to preview).", file=sys.stderr) + return 2 + for i in args.items: + print(f" {data[i]['name']}: {data[i]['size_str']}") + if input("Clean these? [y/N] ").strip().lower() not in ("y", "yes"): + print("Aborted.") + return 1 + + rc = 0 + for item_id in args.items: + ok, msg = cleaner.clean(item_id, data, dry_run=args.dry_run, use_trash=not args.permanent, + selected=selected if item_id == "user_cache" else None) + print(f"{'✓' if ok else '✗'} {msg}") + rc |= not ok + return rc + + +def cmd_undo(args) -> int: + entry, restored, errors = trash.undo_last() + if entry is None: + print("Nothing to undo (only actions that moved files to Trash can be undone).") + return 1 + print(f"Undid '{entry['action']}' from {entry['ts']}:") + for p in restored: + print(f" ↶ {p}") + for e in errors: + print(f" ✗ {e}", file=sys.stderr) + return 1 if errors else 0 + + +def cmd_history(args) -> int: + for e in trash.load_history()[-args.n:]: + extra = f" trashed {len(e['trashed'])}" if e["trashed"] else "" + freed = f" freed {_fmt(e['freed'])}" if e.get("freed") else "" + print(f"{e['ts']} {e['action']:<20}{freed}{extra} {e.get('detail', '')}".rstrip()) + return 0 + + +def build_parser() -> argparse.ArgumentParser: + p = argparse.ArgumentParser(prog="marmot", description="Run without a command to open the TUI.") + p.add_argument("--version", action="version", version=f"marmot {__version__}") + sub = p.add_subparsers(dest="command") + + s = sub.add_parser("status", help="CPU, memory, disks, battery") + s.add_argument("--json", action="store_true") + s.set_defaults(func=cmd_status) + + s = sub.add_parser("scan", help="measure what can be cleaned") + s.add_argument("--json", action="store_true") + s.set_defaults(func=cmd_scan) + + s = sub.add_parser("clean", help="clean items listed by `marmot scan`") + s.add_argument("items", nargs="+", metavar="ID") + s.add_argument("-n", "--dry-run", action="store_true", help="only show what would happen") + s.add_argument("-y", "--yes", action="store_true", help="do not ask for confirmation") + s.add_argument("--permanent", action="store_true", help="delete developer files instead of moving them to Trash") + s.add_argument("--keep-cache", nargs="+", metavar="NAME", default=[], help="~/.cache folders to keep") + s.set_defaults(func=cmd_clean) + + s = sub.add_parser("undo", help="restore what the last clean/uninstall moved to Trash") + s.set_defaults(func=cmd_undo) + + s = sub.add_parser("history", help="show recent actions") + s.add_argument("-n", type=int, default=20) + s.set_defaults(func=cmd_history) + return p + + +def main(argv: list | None = None) -> int: + args = build_parser().parse_args(argv) + if args.command is None: + from marmot.app import MarmotApp # textual is only imported for the TUI + MarmotApp().run() + return 0 + return args.func(args) + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/src/marmot/marmot.tcss b/src/marmot/marmot.tcss index 1935b61..8803b5e 100644 --- a/src/marmot/marmot.tcss +++ b/src/marmot/marmot.tcss @@ -268,3 +268,22 @@ ProgressBar.-complete > .bar--bar { text-style: italic; } + +#list-clean-items { + height: auto; + max-height: 14; +} + +#list-clean-cache { + height: auto; + max-height: 12; +} + +.button-row { + height: auto; +} + +.button-row Button { + margin-right: 1; + min-width: 10; +} diff --git a/src/marmot/pkgmgr.py b/src/marmot/pkgmgr.py index 67b963d..24d4dd3 100644 --- a/src/marmot/pkgmgr.py +++ b/src/marmot/pkgmgr.py @@ -1,9 +1,17 @@ -"""Package manager backends (apt, pacman). Each backend is plain data + small parsers.""" +"""Package manager backends. Each backend is plain data + small output parsers. + +Keys every backend has: + label, list_cmd/parse_list, preview_cmd/parse_preview, remove_cmd, privileged, + cache_dir/cache_glob/clean_cmd (cache_dir None = no package cache to clean), + orphans_cmd/parse_orphans/remove_orphans_cmd (orphans_cmd None = not supported). +""" import shutil +import subprocess from pathlib import Path -def _parse_dpkg(out: str) -> list: +def _parse_tabbed(out: str) -> list: + """`nameversionsummary` lines (dpkg-query, rpm --qf, flatpak --columns).""" pkgs = [] for line in out.splitlines(): parts = line.split("\t") @@ -27,8 +35,19 @@ def _parse_pacman_qi(out: str) -> list: return pkgs +def _parse_apk_info(out: str) -> list: + """`apk info -vv`: `name-1.2.3-r0 - description`.""" + pkgs = [] + for line in out.splitlines(): + nv, sep, desc = line.partition(" - ") + parts = nv.rsplit("-", 2) + if sep and len(parts) == 3: + pkgs.append({'name': parts[0], 'version': f"{parts[1]}-{parts[2]}", 'summary': desc}) + return pkgs + + def _parse_apt_sim(out: str) -> list: - # `apt-get -s purge` prints one "Purg [...]" line per package it would remove + # `apt-get -s purge|autoremove` prints one "Purg [...]" / "Remv " line per removed package return [ln.split()[1] for ln in out.splitlines() if ln.startswith(("Purg ", "Remv "))] @@ -36,17 +55,64 @@ def _parse_lines(out: str) -> list: return [ln.split()[0] for ln in out.splitlines() if ln.strip()] +def _parse_dnf_remove(out: str) -> list: + """`dnf remove --assumeno`: package rows (leading space, >=5 columns) under the "Removing..." headers.""" + names, on = [], False + for ln in out.splitlines(): + if ln.startswith("Transaction Summary"): + break + if ln.startswith("Removing"): + on = True + elif on and ln.startswith(" ") and len(ln.split()) >= 5: + names.append(ln.split()[0]) + return names + + +def _parse_zypper_remove(out: str) -> list: + """`zypper rm --dry-run`: indented, space separated names after "...going to be REMOVED:".""" + names, on = [], False + for ln in out.splitlines(): + if "going to be REMOVED:" in ln: + on = True + elif on: + if not ln.strip(): + break + names.extend(ln.split()) + return names + + +def _parse_zypper_table(out: str) -> list: + """`zypper packages --unneeded`: `S | Repository | Name | Version | Arch` rows.""" + rows = [[c.strip() for c in ln.split("|")] for ln in out.splitlines() if ln.count("|") >= 4] + return [r[2] for r in rows if r[2] and r[2] != "Name"] + + +def _parse_apk_del(out: str) -> list: + # `apk del --simulate`: "(1/9) Purging curl (8.23.0-r0)" + return [ln.split()[2] for ln in out.splitlines() if ln.startswith("(") and len(ln.split()) >= 3] + + +def _first_existing(*paths: str) -> Path: + return next((Path(p) for p in paths if Path(p).exists()), Path(paths[0])) + + +_RPM_LIST = ["rpm", "-qa", "--qf", "%{NAME}\t%{VERSION}-%{RELEASE}\t%{SUMMARY}\n"] + BACKENDS = { 'apt': { 'label': "Apt", 'list_cmd': ["dpkg-query", "-W", "-f=${Package}\t${Version}\t${binary:Summary}\n"], - 'parse_list': _parse_dpkg, + 'parse_list': _parse_tabbed, 'preview_cmd': lambda p: ["apt-get", "-s", "purge", p], 'parse_preview': _parse_apt_sim, 'remove_cmd': lambda p: ["apt-get", "purge", "-y", p], + 'privileged': True, 'cache_dir': Path("/var/cache/apt/archives"), 'cache_glob': "*.deb", 'clean_cmd': lambda: ["apt-get", "clean"], + 'orphans_cmd': ["apt-get", "-s", "autoremove"], + 'parse_orphans': _parse_apt_sim, + 'remove_orphans_cmd': lambda names: ["apt-get", "autoremove", "--purge", "-y"], }, 'pacman': { 'label': "Pacman", @@ -56,19 +122,80 @@ def _parse_lines(out: str) -> list: 'preview_cmd': lambda p: ["pacman", "-Rsp", "--print-format", "%n", p], 'parse_preview': _parse_lines, 'remove_cmd': lambda p: ["pacman", "-Rns", "--noconfirm", p], + 'privileged': True, 'cache_dir': Path("/var/cache/pacman/pkg"), 'cache_glob': "*.pkg.tar.*", # keep the newest version of each package so downgrades stay possible 'clean_cmd': lambda: ["paccache", "-rk1"] if shutil.which("paccache") else ["pacman", "-Sc", "--noconfirm"], + 'orphans_cmd': ["pacman", "-Qdtq"], + 'parse_orphans': _parse_lines, + 'remove_orphans_cmd': lambda names: ["pacman", "-Rns", "--noconfirm", *names], + }, + 'dnf': { + 'label': "DNF", + 'list_cmd': _RPM_LIST, + 'parse_list': _parse_tabbed, + 'preview_cmd': lambda p: ["dnf", "remove", "--assumeno", p], + 'parse_preview': _parse_dnf_remove, + 'remove_cmd': lambda p: ["dnf", "remove", "-y", p], + 'privileged': True, + 'cache_dir': _first_existing("/var/cache/libdnf5", "/var/cache/dnf"), + 'cache_glob': "**/*.rpm", + 'clean_cmd': lambda: ["dnf", "clean", "packages"], + 'orphans_cmd': ["dnf", "repoquery", "--unneeded", "--qf", "%{name}\n"], + 'parse_orphans': _parse_lines, + 'remove_orphans_cmd': lambda names: ["dnf", "autoremove", "-y"], + }, + 'zypper': { + 'label': "Zypper", + 'list_cmd': _RPM_LIST, + 'parse_list': _parse_tabbed, + 'preview_cmd': lambda p: ["zypper", "--non-interactive", "rm", "--dry-run", "--clean-deps", p], + 'parse_preview': _parse_zypper_remove, + 'remove_cmd': lambda p: ["zypper", "--non-interactive", "rm", "--clean-deps", p], + 'privileged': True, + 'cache_dir': Path("/var/cache/zypp/packages"), + 'cache_glob': "**/*.rpm", + 'clean_cmd': lambda: ["zypper", "clean"], + 'orphans_cmd': ["zypper", "--quiet", "--non-interactive", "packages", "--unneeded"], + 'parse_orphans': _parse_zypper_table, + 'remove_orphans_cmd': lambda names: ["zypper", "--non-interactive", "rm", "--clean-deps", *names], + }, + 'apk': { + 'label': "APK", + 'list_cmd': ["apk", "info", "-vv"], + 'parse_list': _parse_apk_info, + 'preview_cmd': lambda p: ["apk", "del", "--simulate", p], + 'parse_preview': _parse_apk_del, + 'remove_cmd': lambda p: ["apk", "del", p], + 'privileged': True, + # apk only keeps a package cache when /etc/apk/cache is configured; apk del already drops orphans + 'cache_dir': Path("/etc/apk/cache") if Path("/etc/apk/cache").is_dir() else None, + 'cache_glob': "*.apk", + 'clean_cmd': lambda: ["apk", "cache", "clean"], + 'orphans_cmd': None, }, } +# Flatpak apps sit beside the distro backend; flatpak asks polkit itself for system installs. +FLATPAK = { + 'label': "Flatpak", + 'list_cmd': ["flatpak", "list", "--app", "--columns=application,version,name"], + 'parse_list': _parse_tabbed, + 'preview_cmd': None, # removing an app never takes other apps with it + 'remove_cmd': lambda app_id: ["flatpak", "uninstall", "-y", "--delete-data", app_id], + 'privileged': False, +} + +# Detection order matters: dnf before zypper (some systems ship both rpm tools). +_DETECT = [('apt', ("dpkg-query", "apt-get")), ('pacman', ("pacman",)), ('dnf', ("dnf", "rpm")), + ('zypper', ("zypper", "rpm")), ('apk', ("apk",))] + def detect() -> str | None: - if shutil.which("dpkg-query") and shutil.which("apt-get"): - return 'apt' - if shutil.which("pacman"): - return 'pacman' + for name, tools in _DETECT: + if all(shutil.which(t) for t in tools): + return name return None @@ -76,3 +203,15 @@ def get(name: str | None = None) -> dict | None: """Backend dict for `name`, or the detected one; None when the distro is unsupported.""" return BACKENDS.get(name or detect() or "") + +def flatpak() -> dict | None: + return FLATPAK if shutil.which("flatpak") else None + + +def query(cmd: list, parser) -> list | None: + """Run a read-only command and parse its stdout; None when it cannot run or yields nothing.""" + try: + res = subprocess.run(cmd, capture_output=True, timeout=120) + except (OSError, subprocess.SubprocessError): + return None + return parser(res.stdout.decode("utf-8", errors="ignore")) or None diff --git a/src/marmot/trash.py b/src/marmot/trash.py new file mode 100644 index 0000000..726087a --- /dev/null +++ b/src/marmot/trash.py @@ -0,0 +1,121 @@ +"""Undoable deletion: freedesktop.org Trash + an action history for `marmot undo`. + +Trashed items show up in every file manager's Trash and can be restored there too. +""" +import json +import os +import shutil +import time +import uuid +from pathlib import Path +from urllib.parse import quote, unquote + + +def trash_dir() -> Path: + return Path(os.environ.get("XDG_DATA_HOME", Path.home() / ".local/share")) / "Trash" + + +def history_file() -> Path: + return Path(os.environ.get("XDG_STATE_HOME", Path.home() / ".local/state")) / "marmot" / "history.jsonl" + + +def move_to_trash(path: str | Path) -> str: + """Move `path` into the user Trash (spec-compliant .trashinfo). Returns the name inside Trash/files.""" + src = Path(path).absolute() + if not src.exists() and not src.is_symlink(): + raise FileNotFoundError(src) + files, info = trash_dir() / "files", trash_dir() / "info" + files.mkdir(parents=True, exist_ok=True) + info.mkdir(parents=True, exist_ok=True) + + base, n = src.name, 1 + while True: + name = base if n == 1 else f"{base}.{n}" + try: + # O_EXCL reserves the name atomically, as the spec requires + fd = os.open(info / f"{name}.trashinfo", os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600) + except FileExistsError: + n += 1 + continue + if (files / name).exists(): + os.close(fd) + os.unlink(info / f"{name}.trashinfo") + n += 1 + continue + break + with os.fdopen(fd, "w") as f: + f.write(f"[Trash Info]\nPath={quote(str(src))}\nDeletionDate={time.strftime('%Y-%m-%dT%H:%M:%S')}\n") + try: + # shortcut: files on another filesystem are copied into the home Trash instead of $topdir/.Trash-$uid; + # fine for Marmot's targets (all under $HOME), revisit if it ever trashes other mounts. + shutil.move(str(src), str(files / name)) + except BaseException: + os.unlink(info / f"{name}.trashinfo") + raise + return name + + +def restore_from_trash(name: str) -> Path: + """Move a trashed entry back to its original path. Refuses to overwrite.""" + info = trash_dir() / "info" / f"{name}.trashinfo" + original = None + for line in info.read_text().splitlines(): + if line.startswith("Path="): + original = Path(unquote(line[5:])) + if original is None: + raise ValueError(f"{info} has no Path=") + if original.exists() or original.is_symlink(): + raise FileExistsError(f"{original} already exists, not overwriting") + original.parent.mkdir(parents=True, exist_ok=True) + shutil.move(str(trash_dir() / "files" / name), str(original)) + info.unlink() + return original + + +# ---- history ------------------------------------------------------------- + +def record(action: str, *, freed: int = 0, trashed: list | None = None, detail: str = "", + undoes: str | None = None) -> dict: + """Append one action. `trashed` = [{"name": , "path": }].""" + entry = {"id": uuid.uuid4().hex[:12], "ts": time.strftime("%Y-%m-%d %H:%M:%S"), "action": action, + "freed": freed, "trashed": trashed or [], "detail": detail, "undoes": undoes} + hf = history_file() + hf.parent.mkdir(parents=True, exist_ok=True) + with hf.open("a") as f: + f.write(json.dumps(entry) + "\n") + return entry + + +def load_history() -> list: + try: + lines = history_file().read_text().splitlines() + except FileNotFoundError: + return [] + out = [] + for ln in lines: + try: + out.append(json.loads(ln)) + except ValueError: + pass # a torn line from a crash must not hide the rest + return out + + +def last_undoable() -> dict | None: + hist = load_history() + undone = {e.get("undoes") for e in hist} + return next((e for e in reversed(hist) if e["trashed"] and e["id"] not in undone), None) + + +def undo_last() -> tuple[dict | None, list, list]: + """Restore everything the last undoable action trashed. Returns (entry, restored, errors).""" + entry = last_undoable() + if entry is None: + return None, [], [] + restored, errors = [], [] + for t in entry["trashed"]: + try: + restored.append(str(restore_from_trash(t["name"]))) + except (OSError, ValueError) as e: + errors.append(f"{t['path']}: {e}") + record("undo", detail=f"undo {entry['action']}", undoes=entry["id"]) + return entry, restored, errors diff --git a/src/marmot/uninstaller.py b/src/marmot/uninstaller.py index 18e4853..5f97619 100644 --- a/src/marmot/uninstaller.py +++ b/src/marmot/uninstaller.py @@ -2,7 +2,7 @@ import subprocess from pathlib import Path -from marmot import pkgmgr +from marmot import pkgmgr, trash # Only direct children of these dirs (relative to $HOME) may be deleted as remnants. REMNANT_PARENTS = (".config", ".local/share", ".cache") @@ -12,39 +12,42 @@ class PackageUninstaller: def __init__(self, backend: str | None = None): self.home = Path.home() self.pm = pkgmgr.get(backend) + self.flatpak = pkgmgr.flatpak() if backend is None else None + + def _list(self, pm: dict, source: str) -> list: + process = subprocess.Popen(pm['list_cmd'], stdout=subprocess.PIPE, stderr=subprocess.DEVNULL) + out, _ = process.communicate() + return [{**p, 'source': source} for p in pm['parse_list'](out.decode("utf-8", errors="ignore"))] def list_installed_packages(self, search_query: str = "") -> list: packages = [] - if not self.pm: - return packages - try: - process = subprocess.Popen(self.pm['list_cmd'], stdout=subprocess.PIPE, stderr=subprocess.DEVNULL) - out, _ = process.communicate() - q = search_query.lower() - for pkg in self.pm['parse_list'](out.decode("utf-8", errors="ignore")): - if not q or q in pkg['name'].lower() or q in pkg['summary'].lower(): - packages.append(pkg) - packages.sort(key=lambda x: x['name']) - except Exception: - pass + q = search_query.lower() + for pm, source in ((self.pm, 'system'), (self.flatpak, 'flatpak')): + if not pm: + continue + try: + packages += [p for p in self._list(pm, source) + if not q or q in p['name'].lower() or q in p['summary'].lower()] + except Exception: + pass + packages.sort(key=lambda x: x['name']) return packages[:60] # limit for smooth TUI scrolling - def preview_removal(self, package_name: str) -> list | None: + def preview_removal(self, package_name: str, source: str = 'system') -> list | None: """Packages the package manager would remove (dependants/orphans included), or None on error.""" + if source == 'flatpak': + return [package_name] if self.flatpak else None if not self.pm: return None - try: - res = subprocess.run(self.pm['preview_cmd'](package_name), capture_output=True) - if res.returncode != 0: - return None - return self.pm['parse_preview'](res.stdout.decode("utf-8", errors="ignore")) - except Exception: - return None + # exit codes are not trusted (dnf --assumeno always "fails"): an empty parse means it would not work + return pkgmgr.query(self.pm['preview_cmd'](package_name), self.pm['parse_preview']) def _remnant_parents(self) -> list: return [self.home / d for d in REMNANT_PARENTS] - def find_remnants(self, package_name: str) -> list: + def find_remnants(self, package_name: str, source: str = 'system') -> list: + if source == 'flatpak': + return [] # `flatpak uninstall --delete-data` already removes ~/.var/app/ # Exact (case-insensitive) name match only: a substring match made "git" hit ~/.config/gitkraken. want = package_name.lower() folders = [] @@ -87,11 +90,18 @@ def format_size(self, size_bytes: int) -> str: i += 1 return f"{size_bytes:.2f} {size_name[i]}" - def uninstall(self, package_name: str, delete_remnants: list = None) -> tuple[bool, str]: + def uninstall(self, package_name: str, delete_remnants: list = None, source: str = 'system') -> tuple[bool, str]: success = False msg = "" - if not self.pm: - return False, "Unsupported distribution: no apt or pacman found." + pm = self.flatpak if source == 'flatpak' else self.pm + if not pm: + return False, "Unsupported distribution: no supported package manager found." + if not pm['privileged']: + res = subprocess.run(pm['remove_cmd'](package_name), capture_output=True) + if res.returncode != 0: + return False, f"Failed to remove: {res.stderr.decode('utf-8', errors='ignore').strip()}" + trash.record("uninstall", detail=f"{source}:{package_name}") + return True, f"{pm['label']} app {package_name} removed (with its data)." try: cmd = ["pkexec", *self.pm['remove_cmd'](package_name)] res = subprocess.run(cmd, capture_output=True) @@ -109,18 +119,18 @@ def uninstall(self, package_name: str, delete_remnants: list = None) -> tuple[bo except Exception as e: msg = f"Error during purge execution: {str(e)}" - if success and delete_remnants: - import shutil - deleted_paths = [] - for rpath in delete_remnants: + if success: + trashed = [] + for rpath in delete_remnants or []: if not self.is_safe_remnant(rpath): continue try: - shutil.rmtree(rpath, ignore_errors=True) - deleted_paths.append(rpath) + trashed.append({'name': trash.move_to_trash(rpath), 'path': rpath}) except Exception: pass - if deleted_paths: - msg += f" Cleared user config remnants: {', '.join([os.path.basename(p) for p in deleted_paths])}." - + if trashed: + msg += (f" Moved remnants to Trash (marmot undo restores them): " + f"{', '.join(os.path.basename(t['path']) for t in trashed)}.") + trash.record("uninstall", trashed=trashed, detail=f"system:{package_name}") + return success, msg diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..66161f2 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,8 @@ +import pytest + + +@pytest.fixture(autouse=True) +def isolated_xdg(tmp_path, monkeypatch): + """Never touch the real Trash or ~/.local/state/marmot from tests.""" + monkeypatch.setenv("XDG_DATA_HOME", str(tmp_path / "data")) + monkeypatch.setenv("XDG_STATE_HOME", str(tmp_path / "state")) diff --git a/tests/test_analyzer.py b/tests/test_analyzer.py index 73dadad..de8bf17 100644 --- a/tests/test_analyzer.py +++ b/tests/test_analyzer.py @@ -21,29 +21,29 @@ def test_set_path_invalid(self, mock_exists): self.analyzer.set_path('/invalid/path') self.assertEqual(self.analyzer.current_path, old_path) - @patch('os.lstat') - @patch('os.walk') - def test_recursive_size(self, mock_walk, mock_lstat): - import stat - # Mock file lstat - file_stat = MagicMock() - file_stat.st_mode = stat.S_IFREG - file_stat.st_size = 500 - - # Mock dir lstat - dir_stat = MagicMock() - dir_stat.st_mode = stat.S_IFDIR - dir_stat.st_size = 4096 - - mock_lstat.side_effect = lambda path: file_stat if 'file' in str(path) else dir_stat - - mock_walk.return_value = [ - ('/tmp/test', (), ('file1.txt', 'file2.txt')), - ] - - size = self.analyzer._recursive_size('/tmp/test') - # 2 files * 500 = 1000 bytes - self.assertEqual(size, 1000) + def test_recursive_size(self): + import tempfile + with tempfile.TemporaryDirectory() as d: + Path(d, "sub").mkdir() + Path(d, "a.bin").write_bytes(b"x" * 600) + Path(d, "sub", "b.bin").write_bytes(b"x" * 400) + Path(d, "link").symlink_to(Path(d, "a.bin")) # symlinks are not counted twice + self.assertEqual(self.analyzer._recursive_size(d), 1000) + # cached: deleting a file does not change the answer until invalidate() + Path(d, "a.bin").unlink() + self.assertEqual(self.analyzer._recursive_size(d), 1000) + self.analyzer.invalidate() + self.assertEqual(self.analyzer._recursive_size(d), 400) + + def test_scan_cancel(self): + import tempfile + + from marmot.analyzer import ScanCancelled + with tempfile.TemporaryDirectory() as d: + Path(d, "sub").mkdir() + self.analyzer.set_path(d) + with self.assertRaises(ScanCancelled): + self.analyzer.scan_current(cancelled=lambda: True) @patch('os.scandir') @patch('marmot.analyzer.DiskAnalyzer._recursive_size', return_value=1200) diff --git a/tests/test_app.py b/tests/test_app.py index 2da0595..9dcc12e 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -1,6 +1,7 @@ -"""Headless smoke test: the TUI mounts and every tab renders (no privileged action is triggered).""" +"""Headless TUI tests (no privileged action and no real cleaning is ever triggered).""" import asyncio import unittest +from unittest.mock import patch from textual.widgets import TabbedContent @@ -8,10 +9,23 @@ TABS = ["pane-monitor", "pane-cleaner", "pane-uninstaller", "pane-analyzer", "pane-optimizer"] +FAKE_SCAN = { + 'user_cache': {'name': "User Caches", 'path': "/nonexistent/.cache", 'size': 300, 'size_str': "300 B", + 'desc': "", 'requires_sudo': False, + 'children': [{'name': "a", 'path': "/nonexistent/.cache/a", 'size': 200, 'size_str': "200 B"}, + {'name': "b", 'path': "/nonexistent/.cache/b", 'size': 100, 'size_str': "100 B"}]}, + 'trash': {'name': "System Trash", 'path': "/nonexistent/Trash", 'size': 0, 'size_str': "0 B", + 'desc': "", 'requires_sudo': False}, +} + + +def run(coro): + return asyncio.run(coro) + class TestAppSmoke(unittest.TestCase): def test_mount_and_switch_tabs(self): - async def run(): + async def go(): app = MarmotApp() async with app.run_test(size=(140, 45)) as pilot: tabs = app.query_one(TabbedContent) @@ -19,11 +33,36 @@ async def run(): tabs.active = tab await pilot.pause() self.assertEqual(tabs.active, tab) - # the package-cache checkbox exists only when a backend was detected - self.assertEqual(bool(app.query("#chk-pkg-cache")), app.cleaner.pm is not None) await pilot.press("q") + run(go()) + + def test_cleaner_scan_then_preview(self): + async def go(): + app = MarmotApp() + calls = [] - asyncio.run(run()) + def fake_clean(item_id, data, dry_run=False, selected=None, use_trash=True): + calls.append((item_id, dry_run, selected)) + return True, "would do it" + + with patch.object(app.cleaner, "scan", return_value=FAKE_SCAN), \ + patch.object(app.cleaner, "clean", side_effect=fake_clean): + async with app.run_test(size=(140, 45)) as pilot: + app.query_one(TabbedContent).active = "pane-cleaner" + await pilot.pause() + await pilot.click("#btn-clean-scan") + await app.workers.wait_for_complete() + await pilot.pause() + # defaults: user cache + trash ticked, every cache folder ticked + self.assertEqual(set(app.clean_items.selected), {"user_cache", "trash"}) + self.assertEqual(len(app.clean_cache.selected), 2) + app.clean_cache.deselect("/nonexistent/.cache/b") + await pilot.click("#btn-clean-preview") + await app.workers.wait_for_complete() + await pilot.pause() + self.assertIn(("user_cache", True, ["/nonexistent/.cache/a"]), calls) + self.assertTrue(all(dry for _, dry, _ in calls)) + run(go()) if __name__ == "__main__": diff --git a/tests/test_cleaner.py b/tests/test_cleaner.py index 410bf80..7f6b27f 100644 --- a/tests/test_cleaner.py +++ b/tests/test_cleaner.py @@ -85,7 +85,7 @@ def test_clean_dev_junk(self, mock_rmtree): 'paths_list': ['/home/user/Projects/test/node_modules', '/home/user/Projects/test/.venv'] } } - success, msg = self.cleaner.clean('dev_junk', scan_details) + success, msg = self.cleaner.clean('dev_junk', scan_details, use_trash=False) self.assertTrue(success) self.assertEqual(msg, "Cleaned 2 build folder(s)") self.assertEqual(mock_rmtree.call_count, 2) diff --git a/tests/test_cleaner_extras.py b/tests/test_cleaner_extras.py new file mode 100644 index 0000000..6be8a55 --- /dev/null +++ b/tests/test_cleaner_extras.py @@ -0,0 +1,118 @@ +from pathlib import Path +from unittest.mock import MagicMock, patch + +from marmot import trash +from marmot.cleaner import SystemCleaner, parse_disabled_snaps, parse_size + + +def test_parse_size(): + assert parse_size("120.4MB") == int(120.4 * 1024**2) + assert parse_size("1.2G") == int(1.2 * 1024**3) + assert parse_size("3.5 GiB") == int(3.5 * 1024**3) + assert parse_size("0B (0%)") == 0 + assert parse_size("n/a") == -1 + + +def test_parse_disabled_snaps(): + out = """Name Version Rev Tracking Publisher Notes +core22 20240111 1122 latest/stable canonical✓ base,disabled +core22 20240408 1380 latest/stable canonical✓ base +firefox 125.0 4173 latest/stable mozilla✓ disabled +""" + assert [(r['name'], r['rev']) for r in parse_disabled_snaps(out)] == [("core22", "1122"), ("firefox", "4173")] + + +def _home(tmp_path): + h = tmp_path / "home" + (h / ".cache" / "big").mkdir(parents=True) + (h / ".cache" / "big" / "f").write_bytes(b"x" * 500) + (h / ".cache" / "keep").mkdir() + (h / ".cache" / "keep" / "f").write_bytes(b"x" * 100) + return h + + +def test_user_cache_per_folder_and_dry_run(tmp_path): + h = _home(tmp_path) + with patch('pathlib.Path.home', return_value=h), patch('subprocess.check_output', side_effect=OSError): + c = SystemCleaner(backend="pacman") + data = c.scan() + kids = data['user_cache']['children'] + assert [k['name'] for k in kids] == ["big", "keep"] and data['user_cache']['size'] == 600 + + ok, msg = c.clean('user_cache', data, dry_run=True, selected=[str(h / ".cache" / "big")]) + assert ok and "Would delete 1" in msg and (h / ".cache" / "big").exists() + + ok, _ = c.clean('user_cache', data, selected=[str(h / ".cache" / "big")]) + assert ok and not (h / ".cache" / "big").exists() and (h / ".cache" / "keep").exists() + + +def test_user_cache_refuses_paths_outside_cache(tmp_path): + h = _home(tmp_path) + precious = tmp_path / "precious" + precious.mkdir() + data = {'user_cache': {'path': str(h / ".cache"), 'size': 1, + 'children': [{'name': "x", 'path': str(precious), 'size': 1}]}} + with patch('pathlib.Path.home', return_value=h): + SystemCleaner(backend="pacman").clean('user_cache', data) + assert precious.exists() + + +def test_dev_junk_goes_to_trash_and_undo_restores(tmp_path): + h = tmp_path / "home" + nm = h / "web" / "node_modules" + nm.mkdir(parents=True) + (h / "web" / "package.json").touch() + with patch('pathlib.Path.home', return_value=h), patch('subprocess.check_output', side_effect=OSError): + c = SystemCleaner(backend="pacman") + data = c.scan() + ok, msg = c.clean('dev_junk', data) + assert ok and "Trash" in msg and not nm.exists() + entry, restored, errors = trash.undo_last() + assert entry['action'] == "clean:dev_junk" and restored == [str(nm)] and nm.exists() and not errors + + +def test_dry_run_never_runs_commands(tmp_path): + c = SystemCleaner(backend="apt") + data = {'pkg_cache': {'size': 1}, 'orphans': {'size': -1, 'packages': ["a"]}, + 'systemd_logs': {'size': 1}, 'containers': {'size': 1, 'tool': "podman"}, + 'flatpak_unused': {'size': -1}, 'snap_old': {'size': 1, 'revisions': [{'name': "x", 'rev': "1", 'size': 1}]}} + with patch('subprocess.run') as run: + for item in data: + ok, msg = c.clean(item, data, dry_run=True) + assert ok and msg.startswith("Would"), (item, msg) + run.assert_not_called() + + +def test_orphans_and_snap_commands(): + c = SystemCleaner(backend="pacman") + with patch('subprocess.run', return_value=MagicMock(stdout=b"")) as run: + c.clean('orphans', {'orphans': {'size': -1, 'packages': ["a", "b"]}}) + assert run.call_args.args[0] == ["pkexec", "pacman", "-Rns", "--noconfirm", "a", "b"] + revs = [{'name': "core22", 'rev': "1122", 'size': 0}, {'name': "evil;rm -rf ~", 'rev': "1", 'size': 0}] + c.clean('snap_old', {'snap_old': {'size': 0, 'revisions': revs}}) + cmd = run.call_args.args[0] + # names are separate argv entries after the script, never spliced into it + assert cmd[:3] == ["pkexec", "sh", "-c"] and cmd[-4:] == ["core22", "1122", "evil;rm -rf ~", "1"] + + +def test_scan_reports_orphans(tmp_path): + with patch('pathlib.Path.home', return_value=tmp_path), patch('subprocess.check_output', side_effect=OSError), \ + patch('marmot.pkgmgr.query', return_value=["libfoo", "libbar"]): + data = SystemCleaner(backend="pacman").scan() + assert data['orphans']['packages'] == ["libfoo", "libbar"] and data['orphans']['size_str'] == "?" + + +def test_containers_scan_skips_volumes(): + df = b"Images\t1GB (50%)\nContainers\t0B (0%)\nLocal Volumes\t5GB (100%)\nBuild Cache\t512MB\n" + c = SystemCleaner(backend="pacman") + with patch('shutil.which', side_effect=lambda t: t == "podman"), \ + patch('subprocess.run', return_value=MagicMock(returncode=0, stdout=df)): + extras = c._scan_extras() + assert extras['containers']['tool'] == "podman" + assert extras['containers']['size'] == 1024**3 + 512 * 1024**2 + + +def test_trash_item_uses_xdg_trash(tmp_path): + with patch('pathlib.Path.home', return_value=tmp_path), patch('subprocess.check_output', side_effect=OSError): + data = SystemCleaner(backend="pacman").scan() + assert Path(data['trash']['path']) == trash.trash_dir() diff --git a/tests/test_cli.py b/tests/test_cli.py new file mode 100644 index 0000000..9f65753 --- /dev/null +++ b/tests/test_cli.py @@ -0,0 +1,53 @@ +import json +from unittest.mock import MagicMock, patch + +from marmot import cli + +FAKE = {'trash': {'name': "System Trash", 'size': 10, 'size_str': "10 B", 'requires_sudo': False, 'desc': ""}} + + +def _cleaner(ok=True): + c = MagicMock() + c.scan.return_value = FAKE + c.clean.return_value = (ok, "Would permanently delete 10 B") + return c + + +def test_scan_json(capsys): + with patch('marmot.cli._scan', return_value=(_cleaner(), FAKE)): + assert cli.main(["scan", "--json"]) == 0 + assert json.loads(capsys.readouterr().out)['trash']['size'] == 10 + + +def test_clean_unknown_item_is_usage_error(capsys): + with patch('marmot.cli._scan', return_value=(_cleaner(), FAKE)): + assert cli.main(["clean", "nope"]) == 2 + assert "Available: trash" in capsys.readouterr().err + + +def test_clean_refuses_non_interactive_without_yes(): + c = _cleaner() + with patch('marmot.cli._scan', return_value=(c, FAKE)), patch('sys.stdin.isatty', return_value=False): + assert cli.main(["clean", "trash"]) == 2 + c.clean.assert_not_called() + + +def test_clean_dry_run_passes_flag(): + c = _cleaner() + with patch('marmot.cli._scan', return_value=(c, FAKE)): + assert cli.main(["clean", "trash", "--dry-run"]) == 0 + assert c.clean.call_args.kwargs['dry_run'] is True + + +def test_clean_failure_exit_code(): + with patch('marmot.cli._scan', return_value=(_cleaner(ok=False), FAKE)): + assert cli.main(["clean", "trash", "--yes"]) == 1 + + +def test_status_json(capsys): + assert cli.main(["status", "--json"]) == 0 + assert "memory" in json.loads(capsys.readouterr().out) + + +def test_undo_nothing(): + assert cli.main(["undo"]) == 1 diff --git a/tests/test_pkgmgr.py b/tests/test_pkgmgr.py index 932e47f..dfddf45 100644 --- a/tests/test_pkgmgr.py +++ b/tests/test_pkgmgr.py @@ -98,3 +98,89 @@ def test_skips_marmot_own_venv(self): if __name__ == "__main__": unittest.main() + + +DNF_REMOVE = """Package Arch Version Repository Size +Removing: + python3-requests noarch 0:2.34.2-2.fc45 fedora 679.2 KiB +Removing unused dependencies: + python3-idna noarch 0:3.18-2.fc45 fedora 523.7 KiB + python3-urllib3+socks noarch 0:2.8.0-3.fc45 updates 0.0 B + +Transaction Summary: + Removing: 3 packages +""" + +ZYPPER_REMOVE = """Reading installed packages... +The following 3 packages are going to be REMOVED: + libexpat1 python313-requests + python313-urllib3 + +3 packages to remove. +""" + +ZYPPER_UNNEEDED = """S | Repository | Name | Version | Arch +---+------------+-------------+---------+------- +i | repo-oss | libfoo1 | 1.0-1.1 | x86_64 +i | repo-oss | python3-bar | 2.0-2.1 | noarch +""" + + +class TestMoreBackends(unittest.TestCase): + def test_dnf_remove_preview(self): + self.assertEqual(pkgmgr._parse_dnf_remove(DNF_REMOVE), + ["python3-requests", "python3-idna", "python3-urllib3+socks"]) + + def test_dnf_failed_preview_is_empty(self): + out = "Failed to resolve the transaction:\nProblem 1: ... protected packages: dnf5\n" + self.assertEqual(pkgmgr._parse_dnf_remove(out), []) + + def test_zypper(self): + self.assertEqual(pkgmgr._parse_zypper_remove(ZYPPER_REMOVE), + ["libexpat1", "python313-requests", "python313-urllib3"]) + self.assertEqual(pkgmgr._parse_zypper_table(ZYPPER_UNNEEDED), ["libfoo1", "python3-bar"]) + + def test_apk(self): + info = "alpine-baselayout-3.7.2-r1 - Alpine base dir structure\nca-certificates-bundle-20250911-r0 - Certs\n" + pkgs = pkgmgr._parse_apk_info(info) + self.assertEqual([(p['name'], p['version']) for p in pkgs], + [("alpine-baselayout", "3.7.2-r1"), ("ca-certificates-bundle", "20250911-r0")]) + self.assertEqual(pkgmgr._parse_apk_del("(1/2) Purging curl (8.23.0-r0)\n(2/2) Purging libcurl (8)\nOK: 8 MiB\n"), + ["curl", "libcurl"]) + + def test_detect_order(self): + with patch('shutil.which', side_effect=lambda t: t in ("dnf", "rpm", "zypper")): + self.assertEqual(pkgmgr.detect(), "dnf") + with patch('shutil.which', side_effect=lambda t: t == "apk"): + self.assertEqual(pkgmgr.detect(), "apk") + with patch('shutil.which', return_value=None): + self.assertIsNone(pkgmgr.detect()) + + def test_query_empty_is_none(self): + with patch('subprocess.run', return_value=MagicMock(stdout=b"")): + self.assertIsNone(pkgmgr.query(["x"], pkgmgr._parse_lines)) + with patch('subprocess.run', side_effect=OSError): + self.assertIsNone(pkgmgr.query(["x"], pkgmgr._parse_lines)) + + +class TestFlatpak(unittest.TestCase): + def _un(self): + un = PackageUninstaller(backend="pacman") + un.flatpak = pkgmgr.FLATPAK + return un + + @patch('subprocess.Popen') + def test_lists_both_sources(self, mock_popen): + outs = iter([(PACMAN_QI.encode(), None), (b"org.gimp.GIMP\t3.0.4\tGNU Image Manipulation Program\n", None)]) + mock_popen.return_value.communicate.side_effect = lambda: next(outs) + pkgs = self._un().list_installed_packages() + self.assertEqual({p['name']: p['source'] for p in pkgs}, + {"git": "system", "niri": "system", "org.gimp.GIMP": "flatpak"}) + + @patch('subprocess.run') + def test_flatpak_uninstall_has_no_pkexec(self, mock_run): + mock_run.return_value = MagicMock(returncode=0) + ok, msg = self._un().uninstall("org.gimp.GIMP", source="flatpak") + self.assertTrue(ok) + self.assertEqual(mock_run.call_args.args[0], ["flatpak", "uninstall", "-y", "--delete-data", "org.gimp.GIMP"]) + self.assertEqual(self._un().preview_removal("org.gimp.GIMP", "flatpak"), ["org.gimp.GIMP"]) diff --git a/tests/test_trash.py b/tests/test_trash.py new file mode 100644 index 0000000..577dd74 --- /dev/null +++ b/tests/test_trash.py @@ -0,0 +1,71 @@ +from pathlib import Path + +import pytest + +from marmot import trash + + +def test_trash_and_restore_roundtrip(tmp_path): + victim = tmp_path / "proj" / "node_modules" + victim.mkdir(parents=True) + (victim / "x.js").write_text("hi") + + name = trash.move_to_trash(victim) + assert not victim.exists() + info = (trash.trash_dir() / "info" / f"{name}.trashinfo").read_text() + assert f"Path={victim}" in info and "DeletionDate=" in info + + assert trash.restore_from_trash(name) == victim + assert (victim / "x.js").read_text() == "hi" + assert not (trash.trash_dir() / "info" / f"{name}.trashinfo").exists() + + +def test_name_collisions_get_suffix(tmp_path): + names = [] + for i in range(3): + d = tmp_path / str(i) / ".venv" + d.mkdir(parents=True) + names.append(trash.move_to_trash(d)) + assert names == [".venv", ".venv.2", ".venv.3"] + + +def test_restore_never_overwrites(tmp_path): + f = tmp_path / "a.txt" + f.write_text("old") + name = trash.move_to_trash(f) + f.write_text("new") + with pytest.raises(FileExistsError): + trash.restore_from_trash(name) + assert f.read_text() == "new" + + +def test_undo_last_restores_batch_once(tmp_path): + paths = [tmp_path / "a", tmp_path / "b"] + for p in paths: + p.mkdir() + trashed = [{"name": trash.move_to_trash(p), "path": str(p)} for p in paths] + trash.record("clean:dev_junk", trashed=trashed) + trash.record("clean:trash", freed=10) # not undoable, must be skipped + + entry, restored, errors = trash.undo_last() + assert entry["action"] == "clean:dev_junk" and not errors + assert sorted(restored) == sorted(map(str, paths)) and all(p.exists() for p in paths) + assert trash.undo_last() == (None, [], []) # already undone + + +def test_history_survives_torn_line(): + trash.record("a") + with trash.history_file().open("a") as f: + f.write('{"broken": \n') + trash.record("b") + assert [e["action"] for e in trash.load_history()] == ["a", "b"] + + +def test_missing_path_raises(tmp_path): + with pytest.raises(FileNotFoundError): + trash.move_to_trash(tmp_path / "nope") + assert not list((trash.trash_dir() / "info").glob("*")) if (trash.trash_dir() / "info").exists() else True + + +def test_trash_dir_follows_xdg(tmp_path): + assert trash.trash_dir() == Path(tmp_path / "data" / "Trash") diff --git a/tests/test_uninstaller.py b/tests/test_uninstaller.py index 1b590be..3ef1647 100644 --- a/tests/test_uninstaller.py +++ b/tests/test_uninstaller.py @@ -68,17 +68,18 @@ def custom_is_dir(self_path): Path.is_dir = orig_is_dir @patch('subprocess.run') - @patch('shutil.rmtree') - def test_uninstall_success(self, mock_rmtree, mock_run): + @patch('marmot.trash.move_to_trash', return_value="curl") + def test_uninstall_success(self, mock_trash, mock_run): mock_res = MagicMock() mock_res.returncode = 0 mock_run.return_value = mock_res - - success, msg = self.uninstaller.uninstall("curl", delete_remnants=[str(Path.home() / ".config" / "curl")]) + remnant = str(Path.home() / ".config" / "curl") + + success, msg = self.uninstaller.uninstall("curl", delete_remnants=[remnant]) self.assertTrue(success) self.assertIn("purged successfully", msg) - self.assertIn("Cleared user config remnants", msg) - mock_rmtree.assert_called_once_with(str(Path.home() / ".config" / "curl"), ignore_errors=True) + self.assertIn("Moved remnants to Trash", msg) + mock_trash.assert_called_once_with(remnant) @patch('subprocess.run') def test_uninstall_failure(self, mock_run): @@ -163,12 +164,12 @@ def test_uninstall_exception(self, mock_run): self.assertIn("Error during purge execution", msg) @patch('subprocess.run') - @patch('shutil.rmtree', side_effect=Exception("Delete failed")) - def test_uninstall_remnant_exception(self, mock_rmtree, mock_run): + @patch('marmot.trash.move_to_trash', side_effect=OSError("Delete failed")) + def test_uninstall_remnant_exception(self, mock_trash, mock_run): mock_res = MagicMock() mock_res.returncode = 0 mock_run.return_value = mock_res - + success, msg = self.uninstaller.uninstall("curl", delete_remnants=[str(Path.home() / ".config" / "curl")]) self.assertTrue(success) - self.assertNotIn("Cleared user config remnants", msg) + self.assertNotIn("Moved remnants", msg)