scancache.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # Copyright (c) Alibaba, Inc. and its affiliates.
  2. import logging
  3. import os
  4. import time
  5. from argparse import ArgumentParser
  6. from typing import Optional
  7. from modelscope.cli.base import CLICommand
  8. from modelscope.hub.cache_manager import scan_cache_dir
  9. from modelscope.hub.errors import CacheNotFound
  10. from modelscope.utils.logger import get_logger
  11. logger = get_logger(log_level=logging.WARNING)
  12. current_path = os.path.dirname(os.path.abspath(__file__))
  13. def subparser_func(args):
  14. """ Function which will be called for a specific sub parser.
  15. """
  16. return ScanCacheCMD(args)
  17. class ScanCacheCMD(CLICommand):
  18. name = 'scan-cache'
  19. def __init__(self, args):
  20. self.args = args
  21. self.cache_dir: Optional[str] = args.dir
  22. @staticmethod
  23. def define_args(parsers: ArgumentParser):
  24. """ define args for create pipeline template command.
  25. """
  26. parser = parsers.add_parser(ScanCacheCMD.name)
  27. group = parser.add_mutually_exclusive_group()
  28. group.add_argument(
  29. '--dir',
  30. type=str,
  31. default=None,
  32. help=
  33. 'cache directory to scan (optional). Default to the default ModelScope cache.',
  34. )
  35. parser.set_defaults(func=subparser_func)
  36. def execute(self):
  37. try:
  38. t0 = time.time()
  39. cache_info = scan_cache_dir(self.cache_dir)
  40. t1 = time.time()
  41. except CacheNotFound as exc:
  42. cache_dir = exc.cache_dir
  43. print(f'Cache directory not found: {cache_dir}')
  44. return
  45. print(cache_info.export_as_table())
  46. print(
  47. f'\nDone in {round(t1 - t0, 1)}s. Scanned {len(cache_info.repos)} repo(s)'
  48. f' for a total of {cache_info.size_on_disk_str}.')
  49. if len(cache_info.warnings) > 0:
  50. message = f'Got {len(cache_info.warnings)} warning(s) while scanning.'
  51. print(message)
  52. for warning in cache_info.warnings:
  53. print(warning)