summaryrefslogtreecommitdiff
path: root/Master/tlpkg/bin/tl-update-containers
blob: f99f7b5b2d31c288c2f857e4e27030d53920fdbe (plain)
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
#!/usr/bin/env perl
# Copyright 2008 Norbert Preining
# This file is licensed under the GNU General Public License version 2
# or any later version.
# 
# Generate archive file(s) for the packages specified on the cmdline
# (doesn't really work, not documented), or all if none specified.

BEGIN {
  $vc_id = '$Id$';
  $^W = 1;
  ($mydir = $0) =~ s,/[^/]*$,,;
  unshift (@INC, "$mydir/..");
}

use strict;
use TeXLive::TLConfig;
use TeXLive::TLPOBJ;
use TeXLive::TLPDB;
use TeXLive::TLUtils;
use Getopt::Long;
use Pod::Usage;
use File::Path;

# packages matching these re's will not be updated without --all.
my @critical_pkg_list = qw/bin-texlive texlive\.infra texlive-en/;

our ($mydir, $vc_id);
my $opt_location = ".";
my $opt_relative = 0;
my $opt_recreate = 0;
my $opt_help = 0;
my $opt_version = 0;
my $opt_all = 0;

TeXLive::TLUtils::process_logging_options();
GetOptions(
  "location=s"  => \$opt_location, 
  "recreate"    => \$opt_recreate,
  "all|a"       => \$opt_all,
  "version"	=> \$opt_version,
  "help|?"      => \$opt_help) or pod2usage(1);

pod2usage(-exitstatus => 0, -verbose => 2) if $opt_help;
if ($opt_version) { print "$vc_id\n"; exit 0; } 

exit (&main());

# the real work
sub main
{
  # get the db.
  chomp(my $Master = `cd $mydir/../.. && pwd`);  # xx TLPDB should default
  my $tlpdb = TeXLive::TLPDB->new("root" => $Master);
  die "cannot find tlpdb in $Master" unless defined($tlpdb);
  my @packs = @ARGV ? @ARGV : $tlpdb->list_packages;

  # get configuration of package splitting
  my $srcsplit = $tlpdb->config_src_container;
  my $docsplit = $tlpdb->config_doc_container;
  my $format = $tlpdb->config_container_format;
  my $type = "lzma";
  if ($format eq "lzma" || $format eq "zip") {
    $type = $format;
  } else {
    warn "$0: unknown container format specified in 00texlive.config: $format"
         . "; ignoring and continuing with $type";
  }

  debug("format=$type srcsplit=$srcsplit docsplit=$docsplit\n");

  my @todopacks = ();
  my @removepacks = ();
  my $opt_containerdir = "$opt_location/$TeXLive::TLConfig::Archive";
  my %count;
  my $nettlpdb;

  if ($opt_recreate) {
    # remake everything.
    @todopacks = $tlpdb->list_packages();
    $nettlpdb = $tlpdb->copy;
    $nettlpdb->root($opt_location);

  } else {
    $nettlpdb = TeXLive::TLPDB->new(root => $opt_location);
    if (!defined($nettlpdb)) {
      die "Cannot init tlpdb from $opt_location ...";
    }
    my %archiverevs;
    foreach my $pkg ($nettlpdb->list_packages()) {
      $archiverevs{$pkg} = $nettlpdb->get_package($pkg)->revision();
      if (!defined($tlpdb->get_package($pkg))) {
        # $pkg has disappeared, removing it
        push @removepacks, $pkg;
      }
    }

    # collect packages to be updated.
    $count{"new"} = $count{"removed"} = $count{"updated"} = $count{"unchanged"}
                  = 0;
 
    for my $pkg (@packs) {
      # by definition, any 00texlive... package does not need containers.
      next if $pkg =~ /00texlive.*\./;
      my $oldrev = 0;
      if (-r "$opt_containerdir/$pkg.tar.$type"
          && defined($archiverevs{$pkg})) {
        $oldrev = $archiverevs{$pkg};
      } else {
        info("$pkg is new\n");
        $count{"new"}++;
      }

      my $tlp = $tlpdb->get_package($pkg);
      my $newrev = 0;
      if (defined($tlp)) {
        $newrev = $tlp->revision;
      } else {
        # that cannot happen!!! we are looping over the packages of
        # $tlpdb so the above ->get_package will succeed (or something
        # stranged happened!
        die ("failed to get package $pkg, that should not happen!");
      }

      if ($oldrev == $newrev) {
        debug("$pkg up to date\n");
        # check for the existence of all containers, might got missing
        if (($tlp->runfiles && ! -r "$opt_containerdir/$pkg.tar.$type") ||
            ($srcsplit && $tlp->srcfiles && ! -r "$opt_containerdir/$pkg.source.tar.$type") ||
            ($docsplit && $tlp->docfiles && ! -r "$opt_containerdir/$pkg.doc.tar.$type")) {
          info("container(s) for $pkg have disappeared, recreating them.\n");
          push @todopacks, $pkg;
          $count{"updated"}++;
        } else {
          $count{"unchanged"}++;
        }
      } elsif ($oldrev < $newrev) {
        push @todopacks, $pkg;
        $count{"updated"}++ if $oldrev;
      } else {
        # This can happen when packages get renamed or files get
        # shuffled from one package to another.
        warn "The revision of $pkg in texlive.tlpdb"
            . "($newrev) is OLDER than the one in"
            . "$opt_location/tlpkg/texlive.tlpdb ($oldrev)";
        push @todopacks, $pkg;
      }
    }
  }

  # set up the programs ...
  &TeXLive::TLUtils::setup_programs("$Master/tlpkg/installer");

  # get list of packages.
  PACKS: for my $pkg (sort @todopacks) {
    next if ($pkg eq "00texlive.config");
    next if ($pkg eq "00texlive.installer");
    foreach my $manualpkgre (@critical_pkg_list) {
      # we match the initial string of the package name, so that all the
      # .arch packages are skipped, too
      if ($pkg =~ m/^$manualpkgre/) {
        if ($opt_all || $opt_recreate) {
          tlwarn ("$0: updating critical $pkg due to -all\n");
          last; # of the manualpkgre checks
        } else {
          tlwarn ("$0: skipping critical $pkg");
          # we assume that the packages in @critical_pkg_list always
          # exist, so if they are there then the number of updated packages
          # should be reduced.
          $count{'updated'}--;
          # the following line skips all other regexp checks on critical
          # packages and skips everything below this big for list (the
          # part which actually builds the containers) and continues with
          # the next package (the PACKS: for my $pkg (sort @todopacks) line
          next PACKS;
        }
      }
    }
    my $obj = $tlpdb->get_package ($pkg);
    die "$0: no TL package named $pkg in $Master" if ! $obj;
    debug("updating $pkg containers ...\n");
    # we have to make a copy otherwise the src/doc files in the original
    # tlpobj are removed, and thus also in the tlpdb to be saved!!!
    my $objcopy = $obj->copy;
    if ($srcsplit) {
      my $objsrc = $obj->srcfiles_package;
      $objcopy->clear_srcfiles;
      if ($objsrc) {
        my ($s,$m) = $objsrc->make_container($type, $Master, $opt_containerdir,
                                            "$pkg.source", $opt_relative);
        if ($s > 0) {
          # something was created 
          # important, we have to add it to the original $obj
          $obj->srccontainersize($s);
        }
        if ($m ne "") {
          $obj->srccontainermd5($m);
        }
      }
    }
    if ($docsplit) {
      my $objdoc = $obj->docfiles_package;
      $objcopy->clear_docfiles;
      if ($objdoc) {
        my ($s,$m) = $objdoc->make_container($type, $Master, $opt_containerdir,
                                             "$pkg.doc", $opt_relative);
        if ($s > 0) {
          # something was created
          $obj->doccontainersize($s);
        }
        if ($m ne "") {
          $obj->doccontainermd5($m);
        }
      }
    }
    my ($s,$m) = $objcopy->make_container($type, $Master, $opt_containerdir,
                                          $pkg, $opt_relative);
    if ($s > 0) {
      $obj->containersize($s);
    }
    if ($m ne "") {
      $obj->containermd5($m);
    }
    # add the updated (or new) TLPOBJ to NET TLPDB
    # that way the other container sizes are not destroyed
    $nettlpdb->add_tlpobj($obj);
  }

  # next we remove those containers which have been gone!
  for my $op (@removepacks) {
    info("$op has disappeared, removing its containers\n");
    `rm $opt_containerdir/$op.*`;
    $nettlpdb->remove_package($op);
    $count{"removed"}++;
  }

  if (@todopacks && ! $opt_recreate) {
    # we updated something
    print "$0: $count{new} new, $count{removed} removed, "
          . "$count{updated} updated, $count{unchanged} unchanged.\n";
  } else {
    print "$0: nothing to be done.\n";
  }

  # STRANGE: It seems that calling -recreate did not save the
  # docfiles into the texlive.tlpdb, no idea why. So update should now
  # do that.
  $nettlpdb->save;
  system("lzma --force -k -z $opt_location/tlpkg/texlive.tlpdb");
  
  # do a last check that all the containers are actually present
  foreach my $p ($tlpdb->list_packages) {
    next if $p =~ /00texlive.*\./;
    if (! -r "$opt_containerdir/$p.tar.lzma") {
      info ("container for $p is missing, strange!\n");
    }
  }
}


__END__

=head1 NAME

tl-update-containers - create, update, or remove TL containers

=head1 SYNOPSIS

tl-update-containers [I<option>]...

=head1 OPTIONS

=over 4

=item B<-location> I</container/dir>

The location to find the previously generated containers;
default is C<./archive>.  

=item B<-recreate>

Does a full rebuild of all packages.

=item B<-all|-a>

Will update also packages which are exempted due to possible breakage
(currently texlive.infra and bin-texlive).

=item B<-help>

Print this documentation and exit.

=back

The standard options B<-q>, B<-v>, and B<-logfile>=I<file> are also
accepted; see the C<process_logging_options> function in
L<TeXLive::TLUtils> for details.

The format of the containers and the splitting of source and
documentation files are controlled by the TLPDB options in the
pseudo-package C<00texlive.config>.  See the documentation for TLPDB.


=head1 DESCRIPTION

The B<tl-update-containers> program compares the revision of each
package found in C<containerdir> with the revision of the same package
in C<texlive.tlpdb>, found relative to this script. Where the latter is
newer (i.e., a higher number), the container(s) for the package are
updated.  Where the container(s) do not exist, they are created.  When
the package no longer exists in tlpdb, the container(s) are removed.


=head1 AUTHORS AND COPYRIGHT

This script and its documentation were written for the TeX Live
distribution (L<http://tug.org/texlive>) and both are licensed under the
GNU General Public License Version 2 or later.

=cut

### Local Variables:
### perl-indent-level: 2
### tab-width: 2
### indent-tabs-mode: nil
### End:
# vim:set tabstop=2 expandtab: #