Skip to content

Commit

Permalink
dev(narugo): add ls_repo into the commands
Browse files Browse the repository at this point in the history
  • Loading branch information
narugo1992 committed May 16, 2024
1 parent d5f0207 commit 131abe4
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/ir_repos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ name: Irregular Repos
on:
workflow_dispatch:
schedule:
- cron: '0 * * * 0'
- cron: '0 12 * * 0'

jobs:
check_irregular_repo:
Expand Down
4 changes: 4 additions & 0 deletions hfutils/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from .entry import hfutilscli

Check warning on line 1 in hfutils/__main__.py

View check run for this annotation

Codecov / codecov/patch

hfutils/__main__.py#L1

Added line #L1 was not covered by tests

if __name__ == '__main__':
hfutilscli()

Check warning on line 4 in hfutils/__main__.py

View check run for this annotation

Codecov / codecov/patch

hfutils/__main__.py#L3-L4

Added lines #L3 - L4 were not covered by tests
2 changes: 2 additions & 0 deletions hfutils/entry/cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from .dispatch import hfutilcli
from .download import _add_download_subcommand
from .ls import _add_ls_subcommand
from .ls_repo import _add_ls_repo_subcommand
from .upload import _add_upload_subcommand
from .whoami import _add_whoami_subcommand

Expand All @@ -9,6 +10,7 @@
_add_upload_subcommand,
_add_ls_subcommand,
_add_whoami_subcommand,
_add_ls_repo_subcommand,
]

cli = hfutilcli
Expand Down
74 changes: 74 additions & 0 deletions hfutils/entry/ls_repo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import fnmatch
from typing import Optional

import click
from huggingface_hub.utils import LocalTokenNotFoundError

from .base import CONTEXT_SETTINGS, ClickErrorException
from ..operate.base import REPO_TYPES, get_hf_client


class NoLocalAuthentication(ClickErrorException):
"""
Exception raised when there is no local authentication token.
"""
exit_code = 0x31


def _add_ls_repo_subcommand(cli: click.Group) -> click.Group:
"""
Add the ls_repo subcommand to the CLI.
:param cli: The click Group object.
:type cli: click.Group
:return: The updated click Group object.
:rtype: click.Group
"""

@cli.command('ls_repo', help='List repositories from HuggingFace.\n\n'
'Set environment $HF_TOKEN to use your own access token.',
context_settings=CONTEXT_SETTINGS)
@click.option('-a', '--author', 'author', type=str, default=None,
help='Author of the repositories. Search my repositories when not given.')
@click.option('-t', '--type', 'repo_type', type=click.Choice(REPO_TYPES), default='dataset',
help='Type of the HuggingFace repository.', show_default=True)
@click.option('-p', '--pattern', 'pattern', type=str, default='*',
help='Pattern of the repository names.', show_default=True)
def ls(author: Optional[str], repo_type: str, pattern: str):
"""
List repositories from HuggingFace.
:param author: Author of the repositories.
:type author: Optional[str]
:param repo_type: Type of the HuggingFace repository.
:type repo_type: str
:param pattern: Pattern of the repository names.
:type pattern: str
"""
hf_client = get_hf_client()
if not author:
try:
info = hf_client.whoami()
author = author or info['name']
except LocalTokenNotFoundError:
raise NoLocalAuthentication(
'Authentication failed.\n'
'Make sure you have set the correct Huggingface token.\n'
'Or if need to use this with guest mode, please explicitly set the `-a` option.'
)

if repo_type == 'model':
r = hf_client.list_models(author=author)
elif repo_type == 'dataset':
r = hf_client.list_datasets(author=author)
elif repo_type == 'space':
r = hf_client.list_spaces(author=author)
else:
raise ValueError(f'Unknown repository type - {repo_type!r}.') # pragma: no cover

for repo_item in r:
if fnmatch.fnmatch(repo_item.id, pattern):
print(repo_item.id)

return cli
60 changes: 60 additions & 0 deletions test/entry/test_ls_repo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import os
from unittest.mock import patch

import click
import pytest
from hbutils.testing import simulate_entry
from huggingface_hub import HfApi

from hfutils.entry import hfutilscli


@pytest.fixture()
def no_hf_token():
def _get_hf_client():
return HfApi(token='')

with patch('hfutils.entry.ls_repo.get_hf_client', _get_hf_client), \
patch.dict(os.environ, {'HF_TOKEN': ''}):
yield


@pytest.mark.unittest
class TestEntryLsRepo:
def test_ls_repo(self):
result = simulate_entry(hfutilscli, [
'hfutils', 'ls_repo',
])
assert result.exitcode == 0
repos = click.unstyle(result.stdout).splitlines(keepends=False)
assert 'narugo/manual_packs' in repos
assert 'narugo/csip_v1_info' in repos

def test_ls_repo_space(self):
result = simulate_entry(hfutilscli, [
'hfutils', 'ls_repo', '-t', 'space',
])
assert result.exitcode == 0
repos = click.unstyle(result.stdout).splitlines(keepends=False)
assert 'narugo/jupyterlab' in repos
assert 'narugo/CDC_anime_demo' in repos

def test_ls_repo_model(self):
result = simulate_entry(hfutilscli, [
'hfutils', 'ls_repo', '-t', 'model',
])
assert result.exitcode == 0
repos = click.unstyle(result.stdout).splitlines(keepends=False)
assert 'narugo/gchar_models' in repos
assert 'narugo/test_v1.5_kristen' in repos
assert 'narugo/test_v1.5_nian' in repos

def test_ls_repo_anonymous(self, no_hf_token):
result = simulate_entry(hfutilscli, [
'hfutils', 'ls_repo',
])
assert result.exitcode == 0x31
stdout_lines = click.unstyle(result.stdout).splitlines(keepends=False)
assert len(stdout_lines) == 0
stderr_lines = click.unstyle(result.stderr).splitlines(keepends=False)
assert 'Authentication failed.' in stderr_lines

0 comments on commit 131abe4

Please sign in to comment.