Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add --sort arg to delete-cache to sort by size #2815

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

AlpinDale
Copy link

@AlpinDale AlpinDale commented Jan 30, 2025

This PR adds the --sort flag to the delete-cache command. This should allow users to sort the repos by size, including all revisions. The default mode is in descending order, but this behaviour can be changed by passing the ascending argument to the flag.

The flag takes 4 arguments: size, alphabetical, lastUpdated, and lastUsed. The size is currently in descending order.

@Wauplin
Copy link
Contributor

Wauplin commented Jan 31, 2025

x-linking #1065

Copy link
Contributor

@Wauplin Wauplin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @AlpinDale thank you for this very clean PR! I appreciate the code quality and having tests already implemented 🤗 I've left a high-level comment about the CLI interface. I'd prefer something like this:

huggingface-cli delete-cache --sort=size

Let me know what you think !

src/huggingface_hub/commands/delete_cache.py Outdated Show resolved Hide resolved
@AlpinDale
Copy link
Author

Thanks for the review, @Wauplin. I've added the requested changes, including the alphabetical, lastUpdated, lastUsed sorting options. Please take a look again.

Copy link
Contributor

@Wauplin Wauplin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @AlpinDale ! Didn't expect that much but it makes the PR really great! :) I've left some minor comments to address.

@@ -18,6 +18,7 @@
huggingface-cli delete-cache
huggingface-cli delete-cache --disable-tui
huggingface-cli delete-cache --dir ~/.cache/huggingface/hub
huggingface-cli delete-cache --sort
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
huggingface-cli delete-cache --sort
huggingface-cli delete-cache --sort=size

(to showcase how to use it)

"--sort",
nargs="?",
choices=["size", "alphabetical", "lastUpdated", "lastUsed"],
const="size",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const="size",

By default, let's not sort

delete_cache_parser.set_defaults(func=DeleteCacheCommand)

def __init__(self, args: Namespace) -> None:
self.cache_dir: Optional[str] = args.dir
self.disable_tui: bool = args.disable_tui
self.sort_by: Optional[str] = args.sort
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you define a SortingOption_T as the beginning of the module?

SortingOption_T = Literal["size", "alphabetical", "lastUpdated", "lastUsed"]

and then use it throughout the module like this?

Suggested change
self.sort_by: Optional[str] = args.sort
self.sort_by: Optional[SortingOption_T] = args.sort

for repo in sorted(hf_cache_info.repos, key=_repo_sorting_order):
sorted_repos = sorted(
hf_cache_info.repos,
key=lambda repo: -repo.size_on_disk if sort_by == "size" else 1,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move the sorting logic defined above in the TUI part to a separate method so that it can be reused here? So that the "no tui" path can also support all sorting options.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants