Debian Codegraph @

mirror-size

raw
1 #!/usr/bin/perl
2
3 # This script can be used on a mirror (or e.g. coccia.debian.org) to
4 # produce an overview of the size of the archive. The numbers reflect
5 # the "raw" size of the archive: the size of packages and source files.
6 # It does not include the size of files containing meta data, nor of
7 # various separate directories.
8
9 # Copyright (c) 2009 Frans Pop <[email protected]>
10
11 use warnings;
12 use strict;
13 use Cwd;
14 use Getopt::Long;
15 use File::Temp qw/ tempfile /;
16 use Compress::Zlib;
17 use List::Util qw(max);
18
19 our $root="/srv/ftp.debian.org/ftp/dists";
20 our @arches= qw(source all amd64 arm64 armel armhf i386 loong64 mips64el mipsel ppc64el riscv64 s390x);
21 our @comps= qw(main contrib non-free non-free-firmware main/debian-installer);
22 our @suites= qw(oldoldstable oldstable stable testing unstable experimental);
23 our %dists;
24 our (@source_files, @package_files);
25 our (%files, %sizes, %total, %width);
26
27 GetOptions ("root=s" => \$root) or die "Error in command line arguments\n";
28
29 chdir($root) or die "chdir $root: $!";
30
31 my ($tfh, $tfile) = tempfile();
32 END { unlink $tfile if $tfile }
33
34 ### Collect the data
35
36 foreach my $suite (@suites) {
37 next unless -f "$root/$suite/Release";
38 if (open my $release, "<", "$root/$suite/Release") {
39 while (<$release>) {
40 # Describe suites using their Codename, except for experimental; its
41 # Codename is technically "rc-buggy", but it's rarely called that in
42 # practice and we want something shorter anyway to make the table look
43 # nicer.
44 if ($suite eq "experimental") {
45 $dists{$suite} = "exp";
46 } elsif (/^Codename:/) {
47 ($dists{$suite}) = m/^Codename:\s+(.*)/i;
48 last;
49 }
50 }
51 close $release;
52 }
53
54 foreach my $comp (@comps) {
55 next unless -d "$root/$suite/$comp";
56 print(STDERR "Processing $suite $comp\n");
57 foreach my $arch (@arches) {
58 my $file;
59 if ($arch eq "source") {
60 $file = "source/Sources.xz";
61 next unless -f "$root/$suite/$comp/$file";
62 parse_sources($file, $suite, $comp);
63 } else {
64 $file = "binary-$arch/Packages.xz";
65 next unless -f "$root/$suite/$comp/$file";
66 parse_packages($file, $suite, $comp, $arch);
67 }
68 }
69 }
70 }
71
72 ### Print the tables
73
74 foreach my $suite (@suites) {
75 next unless exists $dists{$suite};
76 $width{$suite} = exists $sizes{"d$suite"} ? 16 : 6;
77 $width{$suite} = max(length($dists{$suite}), $width{$suite});
78 }
79
80 print("Total archive size (binary + source) per component:\n\n");
81 printf("%10s ", "(in MiB)");
82 foreach my $suite (@suites) {
83 next unless exists $dists{$suite};
84 printf("| %$width{$suite}s ", $dists{$suite});
85 }
86 printf("| %7s\n", "all");
87 print_ruler();
88 foreach my $comp (@comps) {
89 next unless exists $sizes{all}{$comp};
90 if ($comp eq "main/debian-installer") {
91 printf("%-10s", "main/d-i");
92 } elsif ($comp eq "non-free-firmware") {
93 printf("%-10s", "non-free-f");
94 } else {
95 printf("%-10s", $comp);
96 }
97 foreach my $suite (@suites) {
98 next unless exists $dists{$suite};
99 if (exists $sizes{$suite}{$comp}) {
100 $total{$suite} += $sizes{$suite}{$comp};
101 my $strsize = sprintf("%6i", int((1 + $sizes{$suite}{$comp}) /1024/1024));
102 if (exists $sizes{"d$suite"}{$comp}) {
103 $total{"d$suite"} += $sizes{"d$suite"}{$comp};
104 $strsize .= sprintf(" (+%6i)", int((1 + $sizes{"d$suite"}{$comp}) /1024/1024));
105 }
106 printf(" | %$width{$suite}s", $strsize);
107 } else {
108 print(" | " . (" " x $width{$suite}));
109 }
110 }
111 printf(" | %7i\n", int((1 + $sizes{all}{$comp}) /1024/1024));
112 $total{all} += $sizes{all}{$comp};
113 }
114 print_ruler();
115 printf("%-9s ", "total");
116 foreach my $suite (@suites) {
117 next unless exists $dists{$suite};
118 my $strsize = sprintf("%6i", int((1 + $total{$suite}) /1024/1024));
119 $strsize .= sprintf(" (+%6i)", int((1 + $total{"d$suite"}) /1024/1024)) if exists $total{"d$suite"};
120 printf(" | %$width{$suite}s", $strsize);
121 }
122 printf(" | %6i", int((1 + $total{all}) /1024/1024)."\n");
123
124 print("\n\n");
125 print("Archive size per architecture (source and arch=all packages are shown separately):\n\n");
126 printf("%10s ", "(in MiB)");
127 foreach my $suite (@suites) {
128 next unless exists $dists{$suite};
129 printf("| %$width{$suite}s ", $dists{$suite});
130 }
131 printf("| %7s\n", "all");
132 print_ruler();
133 foreach my $arch (@arches) {
134 next unless exists $sizes{all}{$arch};
135 my $parch = $arch;
136 $parch =~ s/kfree/k/;
137 printf("%-10s", $parch);
138 foreach my $suite (@suites) {
139 next unless exists $dists{$suite};
140 if (exists $sizes{$suite}{$arch}) {
141 my $strsize = sprintf("%6i", int((1 + $sizes{$suite}{$arch}) /1024/1024));
142 $strsize .= sprintf(" (+%6i)", int((1 + $sizes{"d$suite"}{$arch}) /1024/1024)) if exists $sizes{"d$suite"}{$arch};
143 printf(" | %$width{$suite}s", $strsize);
144 } else {
145 printf(" | " . (" " x $width{$suite}));
146 }
147 }
148 printf(" | %7i\n", int((1 + $sizes{all}{$arch}) /1024/1024));
149 }
150
151 my @ts=gmtime(time());
152 printf("\nAll numbers reflect the state of the archive per %i %s %i.\n", $ts[3],
153 (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec))[$ts[4]], $ts[5] + 1900);
154
155 ### Functions
156
157 sub print_ruler {
158 print("-" x 11);
159 foreach my $suite (@suites) {
160 next unless exists $dists{$suite};
161 print("|" . "-" x ($width{$suite} + 2));
162 }
163 print("|" . "-" x 8 . "\n");
164 return;
165 }
166
167 sub parse_packages {
168 my ($file, $suite, $comp, $arch) = @_;
169 my ($line, $res, $size, $filename, $architecture);
170 local $/ = "\n\n";
171 system_redirect_io("unxz", "$root/$suite/$comp/$file", "$tfile");
172 open(my $tfh, "<", $tfile) or die "$tfile: $!";
173 for (;;) {
174 my $buf;
175 unless (defined( $buf = <$tfh> )) {
176 last if eof;
177 die "$file: $!" if $!;
178 }
179 $_ = $buf;
180 ($filename) = m/^Filename:\s+(.*)/im;
181 $filename =~ s:/+:/:; # remove redundant slashes in paths
182 ($architecture) = m/^Architecture:\s+(.*)/im;
183 ($size) = m/^Size:\s+(\d+)/im;
184
185 if (! exists $files{$filename}{$suite}) {
186 $sizes{$suite}{$comp} += $size;
187 $sizes{$suite}{$architecture} += $size;
188 if (($suite eq "oldstable" && exists $dists{oldoldstable} &&
189 ! exists $files{$filename}{oldoldstable}) ||
190 ($suite eq "stable" && exists $dists{oldstable} &&
191 ! exists $files{$filename}{oldstable}) ||
192 ($suite eq "testing" && exists $dists{stable} &&
193 ! exists $files{$filename}{stable}) ||
194 ($suite eq "unstable" && exists $dists{testing} &&
195 ! exists $files{$filename}{testing})) {
196 $sizes{"d$suite"}{$comp} += $size;
197 $sizes{"d$suite"}{$architecture} += $size;
198 }
199 }
200 if (! exists $files{$filename}{x}) {
201 $sizes{all}{$comp} += $size;
202 $sizes{all}{$architecture} += $size;
203 }
204 $files{$filename}{x} = 1;
205 $files{$filename}{$suite} = 1;
206 }
207 close($tfh);
208 return;
209 }
210 sub parse_sources {
211 my ($file, $suite, $comp) = @_;
212 my ($line, $res, $directory, $checksums, $hash, $size, $filename);
213 local $/ = "\n\n";
214 system_redirect_io("unxz", "$root/$suite/$comp/$file", "$tfile");
215 open(my $tfh, "<", $tfile) or die "$tfile: $!";
216 for (;;) {
217 my $buf;
218 unless (defined( $buf = <$tfh> )) {
219 last if eof;
220 die "$file: $!" if $!;
221 }
222 $_ = $buf;
223 ($directory) = m/^Directory:\s+(.*)/im;
224 ($checksums) = m/^Checksums-Sha256:\n((?: .*\n)+)/m;
225 foreach my $checksum_line (split /^/m, $checksums) {
226 ($hash, $size, $filename)=split(' ', $checksum_line, 3);
227 $filename = "$directory/$filename";
228 $filename =~ s:/+:/:; # remove redundant slashes in paths
229
230 if (! exists $files{$filename}{$suite}) {
231 $sizes{$suite}{$comp} += $size;
232 $sizes{$suite}{source} += $size;
233 if (($suite eq "oldstable" && exists $dists{oldoldstable} &&
234 ! exists $files{$filename}{oldoldstable}) ||
235 ($suite eq "stable" && exists $dists{oldstable} &&
236 ! exists $files{$filename}{oldstable}) ||
237 ($suite eq "testing" && exists $dists{stable} &&
238 ! exists $files{$filename}{stable}) ||
239 ($suite eq "unstable" && exists $dists{testing} &&
240 ! exists $files{$filename}{testing})) {
241 $sizes{"d$suite"}{$comp} += $size;
242 $sizes{"d$suite"}{source} += $size;
243 }
244 }
245 if (! exists $files{$filename}{x}) {
246 $sizes{all}{$comp} += $size;
247 $sizes{all}{source} += $size;
248 }
249 $files{$filename}{x} = 1;
250 $files{$filename}{$suite} = 1;
251 }
252 }
253 close($tfh);
254 return;
255 }
256
257 # run system() with stdin and stdout redirected to files
258 # unlinks stdout target file first to break hard links
259 sub system_redirect_io {
260 my ($command, $fromfile, $tofile) = @_;
261
262 if (-f $tofile) {
263 unlink($tofile) or die "unlink($tofile) failed: $!";
264 }
265 system("$command <$fromfile >$tofile");
266 return;
267 }