clearcache.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # Copyright (c) Alibaba, Inc. and its affiliates.
  2. import os
  3. import shutil
  4. from argparse import ArgumentParser
  5. from pathlib import Path
  6. from modelscope.cli.base import CLICommand
  7. from modelscope.hub.constants import TEMPORARY_FOLDER_NAME
  8. from modelscope.hub.utils.utils import get_model_masked_directory
  9. def subparser_func(args):
  10. """ Function which will be called for a specific sub parser.
  11. """
  12. return ClearCacheCMD(args)
  13. class ClearCacheCMD(CLICommand):
  14. name = 'clear-cache'
  15. def __init__(self, args):
  16. self.args = args
  17. self.cache_dir = os.getenv(
  18. 'MODELSCOPE_CACHE',
  19. Path.home().joinpath('.cache', 'modelscope'))
  20. @staticmethod
  21. def define_args(parsers: ArgumentParser):
  22. """ define args for clear-cache command.
  23. """
  24. parser = parsers.add_parser(ClearCacheCMD.name)
  25. group = parser.add_mutually_exclusive_group()
  26. group.add_argument(
  27. '--model',
  28. type=str,
  29. help=
  30. 'The id of the model whose cache will be cleared. For clear-cache, '
  31. 'if neither model or dataset id is provided, entire cache will be cleared.'
  32. )
  33. group.add_argument(
  34. '--dataset',
  35. type=str,
  36. help=
  37. 'The id of the dataset whose cache will be cleared. For clear-cache, '
  38. 'if neither model or dataset id is provided, entire cache will be cleared.'
  39. )
  40. parser.set_defaults(func=subparser_func)
  41. def execute(self):
  42. self._execute_with_confirmation()
  43. def _execute_with_confirmation(self):
  44. all = False
  45. single_model = False
  46. prompt = '\nYou are about to delete '
  47. if self.args.model or self.args.dataset:
  48. if self.args.model:
  49. id = self.args.model
  50. single_model = True
  51. prompt = prompt + f'local cache for model {id}. '
  52. else:
  53. id = self.args.dataset
  54. prompt = prompt + f'local cache for dataset {id}. '
  55. else:
  56. prompt = prompt + f'entire ModelScope cache at {self.cache_dir}, including ALL models and dataset.\n'
  57. all = True
  58. user_input = input(
  59. prompt
  60. + '\nPlease press Y or y to proceed, any other key to abort.\n'
  61. ).strip().upper()
  62. if user_input == 'Y':
  63. if all:
  64. self._remove_directory(self.cache_dir)
  65. print('Cache cleared.')
  66. else:
  67. entity_directory = os.path.join(
  68. self.cache_dir, 'hub' if single_model else 'datasets', id)
  69. temp_directory = os.path.join(
  70. self.cache_dir, 'hub' if single_model else 'datasets',
  71. TEMPORARY_FOLDER_NAME, id)
  72. entity_removed = self._remove_directory(entity_directory)
  73. temp_removed = self._remove_directory(temp_directory)
  74. if (not entity_removed) and (not temp_removed):
  75. if single_model:
  76. print(
  77. f'Cache for Model {id} not found. Nothing to do.')
  78. else:
  79. print(
  80. f'Cache for Dataset {id} not found. Nothing to do.'
  81. )
  82. else:
  83. print('Cache cleared.')
  84. else:
  85. print('Operation aborted.')
  86. return
  87. def _remove_directory(self, path):
  88. if os.path.exists(path):
  89. try:
  90. if os.path.islink(path):
  91. shutil.rmtree(os.readlink(path))
  92. os.remove(path)
  93. print(f'Cache and link for {path} removed.')
  94. else:
  95. shutil.rmtree(path)
  96. print(f'Cache folder {path} removed.')
  97. return True
  98. except Exception as e:
  99. print(f'An error occurred while clearing cache at {path}: {e}')
  100. return False