summaryrefslogtreecommitdiff
path: root/Tools/scripts/release/oneshot.pl
blob: dfb34a3ff6af2ac959cda4121568a50a0ccf9852 (plain) (blame)
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#!/usr/bin/perl
# $FreeBSD$

use File::Basename;
use File::Find;
use File::Path;

my $debug	= 1;
my $fake	= 0;
my $pkg_ext     = "tbz";

my %discs;
my %distfiles;
my %index;
my %unplaced;

sub copyDist($$) {
    my $disc = shift;
    my $name = shift;

    my $distfile = "$disc/distfiles/$name";
    my $dir = dirname($distfile);
    mkpath($dir);

    debugPrint("copying $distfile");
    if ($fake) {
        system("touch $distfile");
    } else {
        system("cp $distDir/$name $distfile");
    }
}

sub copyPkg($$) {
    my $disc = shift;
    my $name = shift;
    my $pkg  = $index{$name};
    my $base = $pkg->{base};
    my $text = $pkg->{INDEX};

  # Copy over the package.
    debugPrint("copying $disc/packages/All/$name.$pkg_ext");
    mkpath("$disc/packages/All");
    if ($fake) {
        system("touch $disc/packages/All/$name.$pkg_ext");
    } else {
        system("cp -f $pkgDir/All/$name.$pkg_ext $disc/packages/All");
    }

  # Create symlinks in each category.
    foreach $cat (@{$pkg->{cats}}) {
        debugPrint("creating $disc/packages/$cat/$name.$pkg_ext");
        mkpath("$disc/packages/$cat");
        symlink("../All/$name.$pkg_ext", "$disc/packages/$cat/$name.$pkg_ext");
    }

  # If required created the Latest symlink.
    mkpath("$disc/packages/Latest");
    if ($pkg->{latest}) {
        symlink("../All/$name.$pkg_ext", "$disc/packages/Latest/$base.$pkg_ext");
    }

  # Add the package to INDEX.
    open(BAR, ">>$disc/packages/INDEX");
    print(BAR "$text\n");
    close(BAR);
}

sub debugPrint($) {
    my $line = shift;
    print STDERR $line . "\n" if $debug;
}

sub addDistfile() {
    return if -d;

    my $name = $File::Find::name;
    my $size = (stat($name))[7];

  # Don't record distfiles that are of zero length.
    if ($size == 0) {
        debugPrint("zero length distfile: $name");
        return;
    }

    $name =~ s!$distDir/!!;
    $distfiles{$name} = $size;
}

sub doDistfiles() {
    my %clumps;

  # Get a list of all the disfiles and their sizes.
    find(\&addDistfile, $distDir);

  # xxx Need to clump distfiles so that all the ones for a port end up
  # xxx on the same disc.  For now each distfile is it's own clump. :(
    foreach $distfile (sort keys %distfiles) {
        my @a = ($distfile);
        $clumps{$distfiles{$distfile}} = \@a;
    }

  # Place as many clumps as we can fit on each disc.
    foreach $disc (sort keys %discs) {
        next if ! $discs{$disc}->{distFlag};

        my $d = $discs{$disc};
        my @keys = sort { $a <=> $b } (keys %clumps);

        debugPrint("placing distfiles on $disc ...");

        while ($d->{avail} > 0) {
            my $workDone = 0;

            foreach $size (@keys) {
                last if ($size > $d->{avail});
                placeDist($disc, $size, @{$clumps{$size}});
                delete $clumps{$size};
                $workDone++;
            }

            debugPrint("workDone = $workDone");
            last if (!$workDone);
        }
    }

  # Put the remaining clumps on the scratch disc.
    foreach $size (keys %clumps) {
        placeDist('scratch', $size, @{$clumps{$size}});
    }
}

sub doPackages() {
  # Place as many packages as we can fit on each disc.
    foreach $disc (sort keys %discs) {
        next if ! $discs{$disc}->{pkgFlag};

        my $d = $discs{$disc};

        debugPrint("placing packages on $disc ...");
        my $d = $discs{$disc};

        while ($d->{avail} > 0) {
            redoImpact($disc);

            my $workDone = 0;
            my @keys = sort { $impact{$a} <=> $impact{$b} } (keys %unplaced);

            foreach $name (@keys) {
                if ($impact{$name} <= $d->{avail}) {
                    placePkg($disc, $name, 0);
                    $workDone = 1;
                    last;
                }
            }

            last if (!$workDone);
        }
    }

  # Put all the unplaced packages on the scratch disc.
    redoImpact('scratch');
    foreach $name (keys %unplaced) {
        debugPrint("uplaced package: $name");
        placePkg('scratch', $name, 0);
    }
}

sub isLatest($$) {
    my ($name, $base) = @_;
    my $link = "$pkgDir/Latest/$base.$pkg_ext";
    my $pkg = "$pkgDir/All/$name.$pkg_ext";

    return 0 if ! -l $link;

    my ($dev1, $ino1) = stat($pkg);
    my ($dev2, $ino2) = stat($pkg);

    return $dev1 == $dev2 && $ino1 == $ino2;
}

sub placeDist($$@) {
    my $disc      = shift;
    my $size      = shift;
    my @distfiles = @_;
    my $d         = $discs{$disc};

  # Adjust sizes of disc based on the impact of this package.
    $d->{used} += $size;
    $d->{avail} -= $size;

  # Add each of the distfiles to this disc.
    foreach $name (@distfiles) {
        debugPrint("placing $name on $disc");
        $d->{distfiles}->{$name}++;
    }
}

sub placeNeeded() {
    local *NEEDED;

    debugPrint("reading $neededPkgs and placing packages ...");
    open(NEEDED, $neededPkgs)
      or die "unable to read $neededPkgs: $!\n";

    my $disc;

    $_ = <NEEDED>;
    chomp;

    if (/^disc (\w+)/) {
        $disc = $1;
    } else {
        die "invalid first line in $neededPkgs: $_";
    }

    while (<NEEDED>) {
        chomp;

        if (/^disc (\w+)/) {
            $disc = $1;
            next;
        }

        if (defined($index{$_})) {
            placePkg($disc, $_, 1);
        } else {
          # die "missing needed package: $_";
        }
    }

    close(NEEDED);
}

sub placePkg($$$) {
    my $disc   = shift;
    my $name   = shift;
    my $nodeps = shift;

    debugPrint(" placing $name on $disc");

    my $size;

    if ($nodeps) {
        $size = $index{$name}->{size};
    } else {
        $size = $impact{$name};
    }

  # Adjust sizes of disc based on the impact of this package.
    my $d = $discs{$disc};

    $d->{used} += $size;
    $d->{avail} -= $size;

  # Remove this package from unplaced and place it on this disc.
    delete $unplaced{$name};
    $d->{pkgs}->{$name}++;

  # Now do the same thing for the dependencies.
    return if $nodeps;

    foreach (@{$index{$name}->{rdeps}}) {
        delete $unplaced{$_};
        $d->{pkgs}->{$_}++;
    }
}

sub readDiscInfo() {
    local *INFO;

  # Make sure we have at least disc that will be the place overflow will go.
    my $disc = {
      'avail'           => 0,
      'distFlag'	=> 0,
      'distfiles'       => {},
      'name'            => 'scratch',
      'pkgFlag'		=> 0,
      'pkgs'            => {},
      'size'            => 0,
      'used'            => 0
    };
    $discs{'scratch'} = $disc;

    debugPrint("reading $discInfo ...");
    open(INFO, $discInfo)
      or die "unable to read $discInfo: $!";

    while (<INFO>) {
        chomp;
	next if (/^#/);

        my @f = split(/\s+/);
        next if (@f != 4);

        my $disc = {
          'avail'	=> $f[3],
          'distFlag'	=> $f[2],
          'distfiles'	=> {},
          'name'	=> $f[0],
          'pkgFlag'	=> $f[1],
          'pkgs'	=> {},
          'size'	=> $f[3],
          'used'	=> 0
        };

        $discs{$f[0]} = $disc;
    }

    close(INFO);
}

sub readIndex() {
    local *INDEX;

    debugPrint("reading $indexFilename ...");
    open(INDEX, $indexFilename)
      or die "unable to read $indexFilename: $!";

    while (<INDEX>) {
        chomp;

        my @f    = split(/\|/);
        my $name = $f[0];
        my $base = $name;

        $base =~ s/-[^-]*$//;

        my $pkg = {
          'INDEX'	=> $_,
          'base'	=> $base,
          'cats'	=> [split(/\s+/, $f[6])],
          'distfiles'	=> {},
          'latest'	=> isLatest($name, $base),
          'name'	=> $name,
          'rdeps'	=> [split(/\s+/, $f[8])],
          'size'	=> (stat("$pkgDir/All/$name.$pkg_ext"))[7],
        };

      # debugPrint("adding " . $pkg->{name});
      # die "duplicate package: $name" if ($debug && defined($index{$name}));
        $index{$name} = $pkg;
        $unplaced{$name} = 1;
    }

    close(INDEX);
}

sub _impact($$) {
    my $disc = shift;
    my $name = shift;

  # Return previously calculated value if we have it.
    return $impact{$name} if defined($impact{$name});

    my $pkg   = $index{$name};
    my @rdeps = @{$pkg->{rdeps}};
    my $size  = $pkg->{size};

  # If no dependencies then the impact is the size of this package.
    return $impact{$name} = $size if (!@rdeps);

  # Otherwise the impact is this package's size plus any dependencies
  # that are not already on this disc.
    foreach (@rdeps) {
        $size += $index{$_}->{size} if !defined($discs{$disc}->{pkgs}->{$_});
    }

    return $impact{$name} = $size;
}

sub redoImpact($) {
    my $disc = shift;

  # Reset the impact hashtable.
    %impact = undef;

  # Calculate the impact for each unplaced package.
    foreach $pkg (keys %unplaced) {
        $impact{$pkg} = _impact($disc, $pkg);
    }
}

MAIN: {
    if ($#ARGV != 5) {
        die "$0 <disc_info> <needed_pkgs> <INDEX> <ports_dir> <pkg_dir> <dist_dir>";
    }

    $discInfo      = shift(@ARGV);
    $neededPkgs    = shift(@ARGV);
    $indexFilename = shift(@ARGV);
    $portsDir      = shift(@ARGV);
    $pkgDir        = shift(@ARGV);
    $distDir       = shift(@ARGV);

    readDiscInfo();
    readIndex();
    placeNeeded();

  # bail if any of the discs have overflowed.
    foreach (keys %discs) {
        my $avail = $discs{$_}->{avail};
        die "disc $_ has overflowed: avail = $avail\n" if ($avail < 0);
        print "avail for disc $_ = $avail\n" if ($debug);
    }

    doPackages();
    doDistfiles();

  # Now that we know where everything should go.  Make it so.
    foreach $disc (sort keys %discs) {
        debugPrint("copying packages to $disc ...");
        foreach $name (keys %{$discs{$disc}->{pkgs}}) {
            copyPkg($disc, $name);
        }

        debugPrint("copying distfiles to $disc ...");
        foreach $name (keys %{$discs{$disc}->{distfiles}}) {
            copyDist($disc, $name);
        }
    }

    foreach $disc (sort keys %discs) {
        debugPrint("$disc: avail = " . $discs{$disc}->{avail});
    }
}