tllocalmgr 11.9 KB
Newer Older
firmicus's avatar
firmicus committed
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
#!/usr/bin/perl 
#===============================================================================
#
#         FILE:  tllocalmgr
#
#        USAGE:  ./tllocalmgr [-a|-s|-c] 
#
#  DESCRIPTION:  
#
#      OPTIONS:  --all (-a)	: update all new packages
#                --skipupdate (-s) : do not download a new version of texlive.tlpdb
#                --clean (-c) : remove the old build tree
#
# REQUIREMENTS:  perl-libwww 
#         BUGS:  ---
#        NOTES:  ---
#       AUTHOR:  Firmicus <firmicus ατ gmx δοτ net>
#      VERSION:  0.1
#      CREATED:  20/09/08 12:11:47 CEST
#     REVISION:  ---
#     TODO : 	- ensure that symlinks to scripts under TEXMFLOCAL/scripts replace 
#                 under TEXMFDIST
#               - List "execute" lines at the end...
#               - Make it behave as a (minimal) Shell.
#     		
#===============================================================================

use strict;
use lib "/home/francois/.texlive/texmf-var/arch/tlpkg";
# this is a slightly modified version of the perl module distributed with TeXLive:
# we simply activated a few accessors to the $tlpdb object
use TeXLive::TLPDB;

use Getopt::Long; #TODO
my $opts = {};
GetOptions( $opts,
			'skipupdate|s',
			'all|a',
			'clean|c',
			'search|s',
			'install|i',
			'info',
			'show'
		);

use File::Path;
use LWP::Simple;
# use YAML::Syck; for debugging
use Term::ANSIColor qw(:constants);
$Term::ANSIColor::AUTORESET = 1; # 

#my $TEXMFVAR=`kpsewhich -var-value=TEXMFVAR`;
my $CTANBASEURL="http://mirror.ctan.org/systems/texlive/tlnet/2008";
my $TLARCHIVE="$CTANBASEURL/archive";
my $TLPDB="$CTANBASEURL/tlpkg/texlive.tlpdb.lzma";
my $ROOT = "$ENV{HOME}/.texlive/texmf-var/arch";
my $LOCALDB = "$ROOT/tlpkg/texlive.tlpdb.lzma";
my $logfile = "$ROOT/tllocalmgr.log";
open LOG, "> $logfile";

my $retrievedb = $opts->{'updatedb'} || 0 ;
my $updateall = $opts->{'all'} || 0 ; 
my $cleantree = $opts->{'clean'} || 0 ;

if ( $cleantree ) {
	rmtree("$ROOT/builds") ;
};

my @dbstats = stat($LOCALDB);

#TODO only retrieve if local DB is older than 12 hours
if ($retrievedb) {
	my $ageofdb = time - $dbstats[9];
	unless ($ageofdb < 3600*12) {
		print "Retrieving new TeXLive database from CTAN...\n";
		print LOG "* Retrieving new TeXLive database from CTAN...\n";
		getstore($TLPDB,$LOCALDB) || die "could not fetch $TLPDB : $!";
		system("mv $ROOT/tlpkg/texlive.tlpdb $ROOT/tlpkg/texlive.tlpdb.old");
		system("unlzma $ROOT/tlpkg/texlive.tlpdb.lzma");
	}
};
print "Parsing ...\n";
my $tlpdb = new TeXLive::TLPDB (root => $ROOT) || die "cannot read $ROOT/tlpkg/texlive.tlpdb";

my @core_colls = qw/ basic basicbin binextra context fontbin genericrecommended fontsrecommended langczechslovak langfrench langgerman langitalian langpolish langportuguese langspanish latex latexrecommended mathextra metapost psutils texinfo xetex / ;

my @core_doc_colls = qw/ documentation-base documentation-bulgarian documentation-chinese 
		documentation-czechslovak documentation-dutch documentation-english documentation-finnish
		documentation-french documentation-german documentation-greek documentation-italian
		documentation-japanese documentation-korean documentation-mongolian documentation-polish
		documentation-portuguese documentation-russian documentation-slovenian documentation-spanish
		documentation-thai documentation-turkish documentation-ukrainian documentation-vietnamese/;

my @other_colls = qw/ bibtexextra fontsextra formatsextra games genericextra htmlxml humanities langcjk langcyrillic langgreek latex3 latexextra music pictures plainextra pstricks publishers science /;
my @langextra_colls = qw/ langafrican langarab langarmenian langcroatian langhebrew langindic langmongolian langtibetan langvietnamese /;

my %tlpackages;
my %tldocpackages;

my @core_additional = qw/ pgf ruhyphen ukrhyph /;
push @{$tlpackages{'core'}}, @core_additional;
push @{$tlpackages{'core-doc'}}, 'pgf';

foreach my $coll (@core_colls) {
		foreach my $d ($tlpdb->depends("collection-$coll")) {
			# avoid packages without content in texmf-dist:
			next if $d =~ /^(a2ping|hyphenex|luatex|pdftex|synctex|texlive\.infra|xindy)$/;
			# avoid also bin- collection- and hyphen- packages
			push @{$tlpackages{'core'}}, $d unless $d =~ /^(bin|collection|hyphen)-/;
			if (($tlpdb->doccontainermd5($d) or $tlpdb->docsize($d)) 
					and $d !~ /^(bin|collection|hyphen)-/) {
				push @{$tlpackages{'core-doc'}}, $d ;
			}
		}
} 

foreach my $coll (@core_doc_colls) {
		foreach my $d ($tlpdb->depends("collection-$coll")) {
			push @{$tlpackages{'core-doc'}}, $d unless $d =~ /^(bin|collection|hyphen)-/;
		}
} 

foreach my $d ($tlpdb->depends("collection-fontsextra")) {
	next if $d =~ /^(aleph|ocherokee|oinuit)$/;
	push @{$tlpackages{'fontsextra'}}, $d unless $d =~ /^(bin|collection|hyphen)-/;
	if (($tlpdb->doccontainermd5($d) or $tlpdb->docsize($d)) 
			and $d !~ /^(bin|collection|hyphen)-/) {
		push @{$tlpackages{'fontsextra-doc'}}, $d ;
	}
}

foreach my $coll (@other_colls) {
	next if $coll eq 'fontsextra';
	foreach my $d ($tlpdb->depends("collection-$coll")) {
		next if ($coll =~ /^pictures/ and $d eq 'pgf');
		next if ($coll =~ /^langcyrillic/ and ($d eq 'ruhyphen' or $d eq 'ukrhyph'));
		push @{$tlpackages{$coll}}, $d unless $d =~ /^(bin|collection|hyphen)-/;
		if (($tlpdb->doccontainermd5($d) or $tlpdb->docsize($d)) 
				and $d !~ /^(bin|collection|hyphen)-/) {
			push @{$tlpackages{"$coll-doc"}}, $d ;
		}
	}
} 

foreach my $coll (@langextra_colls) {
	foreach my $d ($tlpdb->depends("collection-$coll")) {
		next if $d =~ /^(omega-devanagari|otibet)$/;
		push @{$tlpackages{'langextra'}}, $d unless ($d eq 'ebong' or $d =~ /^(bin|collection|hyphen)-/);
		if (($tlpdb->doccontainermd5($d) or $tlpdb->docsize($d)) 
				and $d !~ /^(bin|collection|hyphen)-/) {
			push @{$tlpackages{"langextra-doc"}}, $d ;
		}
	}
} 

# add swebib and finbib to bibtexextra
my @bibtexadd = qw /swebib finbib/;
push @{$tlpackages{'bibtexextra'}}, @bibtexadd;
push @{$tlpackages{'bibtexextra-doc'}}, 'swebib';

my %versions ;

foreach my $coll (keys %tlpackages) {
	my @tmp;
	foreach my $pkg (@{$tlpackages{$coll}}) {
		push @tmp, $tlpdb->package_revision($pkg)
	};
	@tmp = sort {$a <=> $b} @tmp;
	$versions{$coll} = pop @tmp
}


my %installed_pkgs;
my %installed_colversions;
my %installed_pkgversions;
my @installed_lists = <installedpkgs/*.pkgs> ;
my @installed_cols;

foreach (@installed_lists) {
	my ($col, $ver) = $_ =~ m/^.+\/texlive-(.+)\_(.+)\.pkgs$/;
	$installed_colversions{$col} = $ver;
	my @tmp = ();
	open INST, "<$_";
	while (<INST>) {
		next if /^\s*$/;
	    chomp( (my $name, my $ver) = split / /, $_ );
		$installed_pkgversions{$name} = $ver; 
		push @tmp, $name;
	};
	close INST;
	$installed_pkgs{$col} = [ @tmp ];
}

my @updatedpkgs;
my %collectionsbypkg;

print LOG "* List of updated or new packages for each collection:\n";
foreach my $col (sort keys %installed_colversions) {
	#my $installedver = `pacman -Q texlive-$col`;
	#$installedver =~ s/texlive-$col 2008\.([0-9]+)-.+/$1/;
	next unless ( $installed_colversions{$col} < $versions{$col} ) ;
	print LOG " - texlive-$col :\n";
	print BOLD RED "texlive-$col :\n";
	foreach my $pkg (sort @{$tlpackages{$col}}) {
		my $newver = $tlpdb->package_revision($pkg);
		if ( exists $installed_pkgversions{$pkg} ) {
			my $oldver = $installed_pkgversions{$pkg} ;
			if ($newver > $oldver) {
				print CYAN "\t$pkg has been updated ($oldver => $newver)\n";
				print LOG "\t$pkg has been updated ($oldver => $newver)\n";
				push @updatedpkgs, $pkg;
				$collectionsbypkg{$pkg} = $col;
			}
		} else {
			print CYAN "\t$pkg is new!\n";
			print LOG "\t$pkg is new (as of rev $newver)\n";
			push @updatedpkgs, $pkg;
			$collectionsbypkg{$pkg} = $col;
		};
	}
}

my $choice;
unless ($updateall) {
	print BOLD, BLUE, "Which packages do you want to update locally?\n\t> ", RESET ;
	chomp($choice = <STDIN>) ;
} ;
my @toupdate;
if ($updateall or $choice =~ m/all/i) {
	@toupdate = @updatedpkgs
} else {
	@toupdate = split /\s+/, $choice;
};

exit 0 unless @toupdate;

print LOG "* Preparing to update the following packages:\n";
my $updatestr = join(", ", @toupdate); 
print LOG "\t$updatestr\n\n";

#my $TLINSTALL = <<"EOF"
#post_install() {
#	echo    ">>> texlive: updating the filename database..."
#	texconfig-sys rehash
#}
#
#post_upgrade() {
#	echo    ">>> texlive: updating the filename database..."
#	texconfig-sys rehash
#}
#
#post_remove() {
#	echo    ">>> texlive: updating the filename database..."
#	texconfig-sys rehash
#}
#EOF
#;

foreach my $pkg (@toupdate) {
	if ( ! defined $tlpdb->package_revision($pkg) ) {
		print RED "Warning: $pkg does not exist\n";
		print LOG "Warning: $pkg does not exist\n";
		next;
	};
	mkpath( "$ROOT/builds/$pkg", { mode => 0755 });
	open PKGBUILD, "> $ROOT/builds/$pkg/PKGBUILD";
	my $newver = $tlpdb->package_revision($pkg);
	my $col = $collectionsbypkg{$pkg};
	my $pkgmd5 = $tlpdb->containermd5($pkg);
	print PKGBUILD <<"EOF"
# Generated by tllocalmgr
pkgname=texlive-local-$pkg
pkgver=$newver
pkgrel=1
pkgdesc="TeX Live - local installation of CTAN package $pkg"
license=('GPL')
arch=('i686' 'x86_64')
depends=('texlive-core')
conflicts=('texlive-$col>=2008.$newver')
makedepends=('lzma-utils')
url='http://tug.org/texlive/'
source=('$TLARCHIVE/$pkg.tar.lzma')
md5sums=($pkgmd5)

build() {
   cd \$startdir/src
   lzma --force -dc $pkg.tar.lzma | tar xf - || return 1
   install -m755 -d \$startdir/pkg/usr/local/share || return 1
   cd texmf-dist
   find . -type d -exec install -d -m755 \$startdir/pkg/usr/local/share/texmf/'{}' \\; || exit 1
   find . -type f -exec install -m644 '{}' \$startdir/pkg/usr/local/share/texmf/'{}' \\; || exit 1
}
EOF
;
	close PKGBUILD;
	print LOG "* Created $ROOT/builds/$pkg/PKGBUILD\n";
#	open INSTALL, "> $ROOT/builds/$pkg/texlive.install";
#	print INSTALL $TLINSTALL;
#	close INSTALL;
	print LOG "* Running makepkg -ci on $ROOT/builds/$pkg/PKGBUILD\n";
	chdir "$ROOT/builds/$pkg";
	system( "makepkg -ci" );
}

print "\n>>> Finished. Don't forget to run 'texhash' to update your file database.\n";

my @execlines;
foreach my $pkg (@toupdate) {
	push @execlines, $tlpdb->executes($pkg) if $tlpdb->executes($pkg);
}

if (@execlines) {
	print   ">>> Also make sure to update /usr/share/texmf-config/web2c/updmap.cfg in\n";
	print   "    case you have installed new fonts or fonts with a different map file.\n";
	print   "    Look at the log file for details.\n";
	print LOG "* Lines that can be added to /usr/share/texmf-config/web2c/updmap.cfg if needed:\n";
	foreach my $line (@execlines) {
		print LOG "\t", $line, "\n"
	};
	print LOG "(Make sure to run updmap-sys to activate them).\n";
close LOG;
}


__END__
sub action_show {
  foreach my $pkg (@ARGV) {
    my $tlp = $tlpdb->get_package($pkg);
    my $installed = 0; #FIXME
    if ($tlp) {
      print "Package:   ", $tlp->name, "\n";
      print "Category:  ", $tlp->category, "\n";
      print "ShortDesc: ", $tlp->shortdesc, "\n" if ($tlp->shortdesc);
      print "LongDesc:  ", $tlp->longdesc, "\n" if ($tlp->longdesc);
      print "Installed: ", ($installed?"Yes\n":"No\n");
      print "\n";
    } else {
      printf STDERR "Cannot find $pkg\n";
    }
  }
  return;
}


sub action_search {
  my $opt_global = 0;
  my $opt_file = 0;
  Getopt::Long::Configure(qw(no_pass_through));
  GetOptions("global" => \$opt_global,
             "file"   => \$opt_file) or pod2usage(2);
  my $r = shift @ARGV;
  my $ret = "";
  my $tlpdb;
  init_local_db();
  if ($opt_global) {
    $tlmediasrc =  TeXLive::TLMedia->new($location);
    die($loadmediasrcerror . $location) unless defined($tlmediasrc);
    $tlpdb = $tlmediasrc->tlpdb;
  } else {
    $tlpdb = $localtlpdb;
  }
  foreach my $pkg ($tlpdb->list_packages) {
    if ($opt_file) {
      my @ret = grep(m;$r;, $tlpdb->get_package($pkg)->all_files);
      if (@ret) {
        print "$pkg:\n";
        foreach (@ret) {
          print "\t$_\n";
        }
      }
    } else {
      next if ($pkg =~ m/\./);
      my $t = $tlpdb->get_package($pkg)->shortdesc;
      $t |= "";
      my $lt = $tlpdb->get_package($pkg)->longdesc;
      $lt |= "";
      if (($pkg =~ m/$r/) || ($t =~ m/$r/) || ($lt =~ m/$r/)) {
        $ret .= " $pkg - $t\n";
      }
    }
  }
  print "$ret";
  return;
}