Skip to content

Commit cacc7d5

Browse files
committed
chore: du
1 parent 82dd97f commit cacc7d5

1 file changed

Lines changed: 21 additions & 17 deletions

File tree

src/openllm/clean.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,36 @@
55
import questionary
66

77
from openllm.analytic import OpenLLMTyper
8-
from openllm.common import CONFIG_FILE, REPO_DIR, VENV_DIR, VERBOSE_LEVEL, output
8+
from openllm.common import (CONFIG_FILE, REPO_DIR, VENV_DIR, VERBOSE_LEVEL,
9+
output)
910

1011
app = OpenLLMTyper(help='clean up and release disk space used by OpenLLM')
1112

1213
HUGGINGFACE_CACHE = pathlib.Path.home() / '.cache' / 'huggingface' / 'hub'
1314

1415

16+
def _du(path: pathlib.Path) -> int:
17+
seen_paths = set()
18+
used_space = 0
19+
20+
for f in path.rglob('*'):
21+
if os.name == 'nt': # Windows system
22+
# On Windows, directly add file sizes without considering hard links
23+
used_space += f.stat().st_size
24+
else:
25+
# On non-Windows systems, use inodes to avoid double counting
26+
stat = f.stat()
27+
if stat.st_ino not in seen_paths:
28+
seen_paths.add(stat.st_ino)
29+
used_space += stat.st_size
30+
return used_space
31+
32+
1533
@app.command(help='Clean up all the cached models from huggingface')
1634
def model_cache(verbose: bool = False):
1735
if verbose:
1836
VERBOSE_LEVEL.set(20)
19-
used_space = sum(f.stat().st_size for f in HUGGINGFACE_CACHE.rglob('*'))
37+
used_space = _du(HUGGINGFACE_CACHE)
2038
sure = questionary.confirm(
2139
f'This will remove all models cached by Huggingface (~{used_space / 1024 / 1024:.2f}MB), are you sure?'
2240
).ask()
@@ -31,21 +49,7 @@ def venvs(verbose: bool = False):
3149
if verbose:
3250
VERBOSE_LEVEL.set(20)
3351

34-
# Set to store paths of files to avoid double counting
35-
seen_paths = set()
36-
used_space = 0
37-
38-
for f in VENV_DIR.rglob('*'):
39-
if os.name == 'nt': # Windows system
40-
# On Windows, directly add file sizes without considering hard links
41-
used_space += f.stat().st_size
42-
else:
43-
# On non-Windows systems, use inodes to avoid double counting
44-
stat = f.stat()
45-
if stat.st_ino not in seen_paths:
46-
seen_paths.add(stat.st_ino)
47-
used_space += stat.st_size
48-
52+
used_space = _du(VENV_DIR)
4953
sure = questionary.confirm(
5054
f'This will remove all virtual environments created by OpenLLM (~{used_space / 1024 / 1024:.2f}MB), are you sure?'
5155
).ask()

0 commit comments

Comments
 (0)