-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnixtat.py
More file actions
executable file
·229 lines (190 loc) · 8.48 KB
/
Copy pathnixtat.py
File metadata and controls
executable file
·229 lines (190 loc) · 8.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/usr/bin/env python3
import os
import sys
import re
import subprocess
import argparse
import glob
from collections import defaultdict
# Configuration des arguments, -h est réservé donc on utilise --human-readable et -H
parser = argparse.ArgumentParser(description="Analyze disk space used by packages in a nix store.")
parser.add_argument('--with-version', action='store_true', help="Keep version numbers in package names.")
parser.add_argument('-H', '--human-readable', action='store_true', help="Display sizes in human-readable format, add headers and a progress bar (Default).")
parser.add_argument('-v', '--verbose', action='store_true', help="Display errors as they occur.")
parser.add_argument('--sort', choices=['size', 'count', 'name'], default='size', help="Sort column.")
parser.add_argument('-r', '--reverse', action='store_true', help="Reverse sort order.")
parser.add_argument('-n', type=int, help="Number of lines to display (mode -H).")
parser.add_argument('--full', action='store_true', help="Display all lines (mode -H).")
parser.add_argument('--path', default='/nix/store', help="Path to the nix store (default: /nix/store).")
parser.add_argument('--simplify', action='store_true', help="Use simple output format (machine readable).")
args = parser.parse_args()
# Rich management for display (only if -H is enabled)
USE_RICH = not args.simplify
if USE_RICH:
try:
from rich.console import Console
from rich.table import Table
from rich.progress import Progress, SpinnerColumn, BarColumn, TextColumn, TimeRemainingColumn, MofNCompleteColumn
except ImportError:
print("Error: The 'rich' module is required for the default output. Install it with 'pip install rich' or use --simplify.", file=sys.stderr)
sys.exit(1)
def get_human_size(size_in_kb):
"""Converts a size in KB to a readable unit (MB, GB)."""
for unit in ['KB', 'MB', 'GB', 'TB']:
if size_in_kb < 1024.0:
return f"{size_in_kb:.2f} {unit}"
size_in_kb /= 1024.0
return f"{size_in_kb:.2f} PB"
def parse_package_name(path, keep_version):
"""
Extracts the package name from the full path.
Example: /nix/store/hash-go-1.25.4 -> go-1.25.4 (or 'go' if keep_version is False)
"""
base_name = os.path.basename(path)
# Regex 1: Remove the hash (32 alphanumeric characters followed by a dash at the beginning)
# Nix store hash is in base32 (a-z0-9)
match_hash = re.match(r'^[a-z0-9]{32}-(.*)$', base_name)
if not match_hash:
return base_name # Fallback if the format is not standard
name_with_version = match_hash.group(1)
if keep_version:
return name_with_version
# Regex 2: Remove the version
# Look for the last dash followed by a digit, which usually indicates the start of the version.
# Ex: go-1.25.4 -> go
# Ex: python3-3.9 -> python3
match_version = re.match(r"^(.*?)(?:-[0-9].*)?$", name_with_version)
if match_version:
return match_version.group(1)
return name_with_version
def main():
store_path = args.path
if not os.path.exists(store_path):
print(f"Error: The directory {store_path} does not exist.", file=sys.stderr)
sys.exit(1)
# Retrieve the list of directories
# We use glob to list, but we will pass these paths to `du`
try:
all_paths = glob.glob(os.path.join(store_path, '*'))
# Keep only directories
all_paths = [p for p in all_paths if os.path.isdir(p)]
except Exception as e:
print(f"Error reading the store: {e}", file=sys.stderr)
sys.exit(1)
# Dictionary for aggregation: { "package_name": {"size": 0, "count": 0} }
stats = defaultdict(lambda: {"size": 0, "count": 0})
encountered_errors = set()
# Process in batches to avoid "Argument list too long" with subprocess
CHUNK_SIZE = max(100, min(1000, len(all_paths) // 100)) # Adjusts chunk size based on total size
# Initialize progress bar if necessary
progress = None
task_id = None
console_err = None
if USE_RICH:
console_err = Console(stderr=True)
progress = Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
BarColumn(),
MofNCompleteColumn(),
TimeRemainingColumn(),
console=console_err,
transient=True
)
progress.start()
task_id = progress.add_task("Analyzing the store", total=len(all_paths))
for i in range(0, len(all_paths), CHUNK_SIZE):
chunk = all_paths[i:i + CHUNK_SIZE]
if not chunk:
continue
# Call `du -s -k` (in Kilobytes for consistency)
# We use -k to force KB, as the default behavior of du varies by OS
cmd = ['du', '-s', '-k'] + chunk
# Execute du and capture output
result = subprocess.run(cmd, capture_output=True, text=True, check=False)
if result.stderr:
for line in result.stderr.splitlines():
encountered_errors.add(line)
if args.verbose:
if progress:
progress.console.print(line, style="red")
else:
print(line, file=sys.stderr)
for line in result.stdout.splitlines():
parts = line.split('\t')
if len(parts) != 2:
continue
try:
size_kb = int(parts[0])
except ValueError:
continue
path = parts[1]
# Extract and clean the name
pkg_name = parse_package_name(path, args.with_version)
# Aggregation
stats[pkg_name]["size"] += size_kb
stats[pkg_name]["count"] += 1
if progress:
progress.update(task_id, advance=1)
if progress:
progress.stop()
if encountered_errors:
print(f"\nError summary ({len(encountered_errors)} error types):", file=sys.stderr)
for err in sorted(encountered_errors):
print(f" {err}", file=sys.stderr)
# Sort results
key_map = {
'size': lambda item: item[1]['size'],
'count': lambda item: item[1]['count'],
'name': lambda item: item[0]
}
sorted_stats = sorted(stats.items(), key=key_map[args.sort], reverse=args.reverse)
# Calculate percentages and cumulative values
total_size = sum(item[1]['size'] for item in sorted_stats)
cumulative_size = 0
processed_stats = []
for name, data in sorted_stats:
cumulative_size += data['size']
perc = (data['size'] / total_size * 100) if total_size > 0 else 0
cum_perc = (cumulative_size / total_size * 100) if total_size > 0 else 0
processed_stats.append((name, data, perc, cum_perc))
# Affichage
if USE_RICH:
console = Console()
limit = len(sorted_stats)
if not args.full:
if args.n is not None:
limit = args.n
else:
limit = int(console.size.height * 0.8)
special_names_re = re.compile(r"^(source|system-path|nixos($|-.*))")
table = Table(title=None, expand=True, row_styles=["", "on color(236)"])
table.add_column("Package Name", style="cyan", no_wrap=True)
table.add_column("Size", justify="right", style="green")
table.add_column("Occurrences", justify="right", style="magenta")
table.add_column("%", justify="right", style="blue")
table.add_column("% Cumul.", justify="right", style="yellow")
subset = processed_stats[-limit:] if limit > 0 else []
for name, data, perc, cum_perc in subset:
row_style = None
if special_names_re.match(name):
row_style = "on color(54)" # Dark magenta background for special packages
table.add_row(
name,
get_human_size(data['size']),
str(data['count']),
f"{perc:.2f}%",
f"{cum_perc:.2f}%",
style=row_style
)
console.print(table)
else:
# Standard format: name size(KB) count % %cumul
for name, data, perc, cum_perc in processed_stats:
print(f"{name} {data['size']} {data['count']} {perc:.2f}% {cum_perc:.2f}%")
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("\nInterrupted by user.", file=sys.stderr)
sys.exit(0)