Skip to content

Commit 9207941

Browse files
authored
Selectively Clearing Cache Folders (#1268)
This pull request enhances the `clear-cache` command in the HoloHub CLI by introducing new flags to allow users to selectively clear specific types of cache folders. The changes improve usability and maintain backward compatibility with previous behavior. **CLI Feature Enhancements:** * Added `--build`, `--data`, and `--install` flags to the `clear-cache` command, allowing users to clear only build, data, or install folders, respectively. [[1]](diffhunk://#diff-9c212b6b2b7ad0d4afe9fc076b7d9988417d1b6f1c99de4bbb4e445798e55e4dR436-R440) [[2]](diffhunk://#diff-1ef9cb2fbb55a15400848086c55046b4c552ea60d34e315fd638a0ca0c5396bbR124-R126) **Implementation Details:** * Updated the `handle_clear_cache` method in `holohub.py` to process the new flags, collecting only the relevant directories based on user input. If no flags are provided, all cache folders are cleared to maintain backward compatibility. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * The clear-cache command now supports selective cache clearing with three new options: `--build`, `--data`, and `--install`. Each option restricts cache clearing to the corresponding folder type. When no option is specified, all cache categories are cleared, maintaining backward compatibility with existing workflows. <sub>✏️ Tip: You can customize this high-level summary in your review settings.</sub> <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Signed-off-by: B Hashemian <[email protected]>
1 parent 0a4df14 commit 9207941

File tree

2 files changed

+65
-20
lines changed

2 files changed

+65
-20
lines changed

README.md

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -207,23 +207,36 @@ Make sure you build the application (if applicable) before running it.
207207

208208
## Cleanup
209209

210-
You can run the command below to reset cache directories:
210+
> [!IMPORTANT]
211+
> Many applications use custom container environments with specific build and runtime dependencies. If you encounter issues with missing or broken build tools between different applications, clearing the build cache should be your first troubleshooting step.
211212
212-
```sh
213-
./holohub clear-cache
214-
```
213+
Clear cache directories using the `clear-cache` command:
215214

216-
This removes the `build`, `data`, and `install` directories. You can use `--dryrun` to preview what would be removed.
215+
```sh
216+
# Clear all cache folders (build, data, install)
217+
./holohub clear-cache
217218

218-
In some cases you may want to clear out only the datasets downloaded by applications to the `data` folder:
219+
# Clear specific cache types
220+
./holohub clear-cache --build # Build folders only
221+
./holohub clear-cache --data # Data folders only
222+
./holohub clear-cache --install # Install folders only
219223

220-
```sh
221-
rm -rf ./data
222-
```
224+
# Combine flags to clear multiple types
225+
./holohub clear-cache --build --data
226+
227+
# Preview what would be removed without actually deleting
228+
./holohub clear-cache --dryrun
229+
./holohub clear-cache --build --dryrun
230+
```
223231

224-
Note that many applications supply custom container environments with build and runtime dependencies.
225-
Failing to clean the build cache between different applications may result in unexpected behavior where build
226-
tools or libraries appear to be broken or missing. Clearing the build cache is a good first check to address those issues.
232+
> [!TIP]
233+
> If you encounter permission issues when clearing cache, run the command inside a container with root privileges:
234+
>
235+
> ```sh
236+
> ./holohub run-container --as-root -- ./holohub clear-cache
237+
> ```
238+
>
239+
> This is safer than using `sudo` directly, as it maintains proper container isolation and avoids changing file ownership.
227240
228241
## Contributing
229242

utilities/cli/holohub.py

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,11 @@ def _create_parser(self) -> argparse.ArgumentParser:
436436
clear_cache.add_argument(
437437
"--dryrun", action="store_true", help="Print commands without executing them"
438438
)
439+
clear_cache.add_argument("--build", action="store_true", help="Clear build folders only")
440+
clear_cache.add_argument("--data", action="store_true", help="Clear data folders only")
441+
clear_cache.add_argument(
442+
"--install", action="store_true", help="Clear install folders only"
443+
)
439444
clear_cache.set_defaults(func=self.handle_clear_cache)
440445

441446
# Add vscode command
@@ -2038,21 +2043,48 @@ def handle_install(self, args: argparse.Namespace) -> None:
20382043
extra_args=extra_args,
20392044
)
20402045

2046+
def _collect_cache_dirs(self, patterns: list[str], default_dir=None) -> list:
2047+
"""Helper to collect cache directories matching patterns."""
2048+
dirs = []
2049+
if default_dir is not None:
2050+
dirs.append(default_dir)
2051+
for pattern in patterns:
2052+
for path in HoloHubCLI.HOLOHUB_ROOT.glob(pattern):
2053+
if path.is_dir() and path not in dirs:
2054+
dirs.append(path)
2055+
return dirs
2056+
20412057
def handle_clear_cache(self, args: argparse.Namespace) -> None:
20422058
"""Handle clear-cache command"""
2059+
# Determine which folders to clear
2060+
clear_build = getattr(args, "build", False)
2061+
clear_data = getattr(args, "data", False)
2062+
clear_install = getattr(args, "install", False)
2063+
2064+
# If no flags are provided, clear all (backward compatibility)
2065+
clear_all = not (clear_build or clear_data or clear_install)
2066+
20432067
if args.dryrun:
20442068
print(Color.blue("Would clear cache folders:"))
20452069
else:
20462070
print(Color.blue("Clearing cache..."))
20472071

2048-
cache_dirs = [
2049-
self.DEFAULT_BUILD_PARENT_DIR,
2050-
self.DEFAULT_DATA_DIR,
2051-
]
2052-
for pattern in ["build", "build-*", "data", "data-*", "install"]:
2053-
for path in HoloHubCLI.HOLOHUB_ROOT.glob(pattern):
2054-
if path.is_dir() and path not in cache_dirs:
2055-
cache_dirs.append(path)
2072+
cache_dirs = []
2073+
2074+
# Collect build folders if needed
2075+
if clear_all or clear_build:
2076+
cache_dirs.extend(
2077+
self._collect_cache_dirs(["build", "build-*"], self.DEFAULT_BUILD_PARENT_DIR)
2078+
)
2079+
2080+
# Collect data folders if needed
2081+
if clear_all or clear_data:
2082+
cache_dirs.extend(self._collect_cache_dirs(["data", "data-*"], self.DEFAULT_DATA_DIR))
2083+
2084+
# Collect install folders if needed
2085+
if clear_all or clear_install:
2086+
cache_dirs.extend(self._collect_cache_dirs(["install", "install-*"]))
2087+
20562088
for path in set(cache_dirs):
20572089
if path.exists() and path.is_dir():
20582090
if args.dryrun:

0 commit comments

Comments
 (0)