|
11 | 11 | from textual.widgets import DataTable, Footer, Header, Label, Static |
12 | 12 |
|
13 | 13 |
|
| 14 | +def remove_pycache(path: Path) -> int: |
| 15 | + total_freed_space = 0 |
| 16 | + for pycache_dir in path.rglob("__pycache__"): |
| 17 | + try: |
| 18 | + total_freed_space += get_total_size(pycache_dir) |
| 19 | + shutil.rmtree(pycache_dir) |
| 20 | + except Exception: |
| 21 | + continue |
| 22 | + return total_freed_space |
| 23 | + |
| 24 | + |
14 | 25 | def remove_duplicates(venvs): |
15 | 26 | seen_paths = set() |
16 | 27 | unique_venvs = [] |
@@ -156,6 +167,12 @@ class TableApp(App): |
156 | 167 | description="Delete the selected .venv immediately", |
157 | 168 | show=True, |
158 | 169 | ), |
| 170 | + Binding( |
| 171 | + key="p", |
| 172 | + action="clean_pycache", |
| 173 | + description="Clean __pycache__ directories recursively", |
| 174 | + show=True, |
| 175 | + ), |
159 | 176 | ] |
160 | 177 |
|
161 | 178 | CSS = """ |
@@ -211,6 +228,13 @@ async def find_venvs(self): |
211 | 228 |
|
212 | 229 | self.query_one(Label).update(f"Found {len(venvs)} .venv directories") |
213 | 230 |
|
| 231 | + async def action_clean_pycache(self): |
| 232 | + current_directory = Path.cwd() |
| 233 | + total_freed_space = await asyncio.to_thread(remove_pycache, current_directory) |
| 234 | + self.bytes_release += total_freed_space |
| 235 | + self.query_one(Label).update(f"{format_size(self.bytes_release)} deleted") |
| 236 | + self.bell() |
| 237 | + |
214 | 238 | def action_confirm_delete(self): |
215 | 239 | table = self.query_one(DataTable) |
216 | 240 | for row_index in range(table.row_count): |
|
0 commit comments