# $Id$ # TeXLive::TLPDB.pm - module for using tlpdb files # Copyright 2007 Norbert Preining # # This file is licensed under the GNU General Public License version 2 # or any later version. package TeXLive::TLPDB; =pod =head1 NAME C -- A database of TeX Live Packages =head1 SYNOPSIS use TeXLive::TLPDB; TeXLive::TLPDB->new (); TeXLive::TLPDB->new (root => "/path/to/texlive/installation/root"); $tlpdb->root("/path/to/root/of/texlive/installation"); $tlpdb->from_file($filename); $tlpdb->writeout; $tlpdb->writeout(FILEHANDLE); $tlpdb->save; $tlpdb->available_architectures(); $tlpdb->add_tlpcontainer($pkg, $ziploc [, $archrefs [, $dest ]] ); $tlpdb->add_tlpobj($tlpobj); $tlpdb->needed_by($pkg); $tlpdb->remove_package($pkg); $tlpdb->get_package("packagename"); $tlpdb->list_packages; $tlpdb->find_file("filename"); $tlpdb->collections; $tlpdb->schemes; $tlpdb->updmap_cfg_lines; $tlpdb->fmtutil_cnf_lines; $tlpdb->language_dat_lines; $tlpdb->package_revision("packagename"); $tlpdb->location; $tlpdb->config_src_container; $tlpdb->config_doc_container; $tlpdb->config_container_format; $tlpdb->config_release; TeXLive::TLPDB->listdir([$dir]); $tlpdb->generate_listfiles([$destdir]); =head1 DESCRIPTION =cut use TeXLive::TLConfig qw($CategoriesRegexp $DefaultCategory $InfraLocation $DatabaseName $MetaCategoriesRegexp); use TeXLive::TLUtils qw(dirname mkdirhier tllog member); use TeXLive::TLPOBJ; my $_listdir; =pod =over 6 =item C<< TeXLive::TLPDB->new >> =item C<< TeXLive::TLPDB->new( [root => "$path"] ) >> C<< TeXLive::TLPDB->new >> creates a new C object. If the argument C is given it will be initialized from the respective location within $path. If C<$path> begins with C or C, the program C is used to download the file. =cut sub new { my $class = shift; my %params = @_; my $self = { root => $params{'root'}, tlps => $params{'tlps'} }; $_listdir = $params{'listdir'} if defined($params{'listdir'}); bless $self, $class; if (defined($self->{'root'})) { $self->from_file("$self->{'root'}/$InfraLocation/$DatabaseName"); } return $self; } =pod =item C<< $tlpdb->add_tlpobj($tlpobj) >> The C adds an object of the type TLPOBJ to the TLPDB. =cut sub add_tlpobj { my ($self,$tlp) = @_; $self->{'tlps'}{$tlp->name} = $tlp; } =pod =item C<< $tlpdb->needed_by($pkg) >> Returns an array of package names depending on $pkg. =cut sub needed_by { my ($self,$pkg) = @_; my @ret; # we only check collections and schemes ... foreach my $p ($self->list_packages) { my $tlp = $self->get_package($p); if ($tlp->category =~ m/$MetaCategoriesRegexp/) { foreach my $d ($tlp->depends) { if ($d =~ m/^(.*)\.win32$/) { if (member("win32", $self->available_architectures)) { if ($d eq $pkg) { push @ret, $p; last; } } } else { if ($d eq $pkg) { push @ret, $p; last; } } } } } return @ret; } =pod =item C<< $tlpdb->remove_package($pkg) >> Remove the package named C<$pkg> from the tlpdb. Gives a warning if the package is not present =cut sub remove_package { my ($self,$pkg) = @_; if (defined($self->{'tlps'}{$pkg})) { delete $self->{'tlps'}{$pkg}; } else { warn "Cannot find package $pkg for removal in tlpdb!"; } } =pod =item C<< $tlpdb->from_file($filename) >> The C function initializes the C in case the root was not given at generation time. If C<$filename> begins with C or C, the program C is used to download the file. =cut sub from_file { my ($self, $path) = @_; if (@_ != 2) { die("Need a filename for initialization!"); } my $root_from_path = dirname(dirname($path)); if (defined($self->{'root'})) { if ($self->{'root'} ne $root_from_path) { printf STDERR "root=$self->{'root'}, root_from_path=$root_from_path\n"; printf STDERR "Initialisation from different location as originally given.\nHope you are sure!\n"; } } else { $self->root($root_from_path); } if ($path =~ /^(http|ftp):\/\//) { tllog($::LOG_DDDEBUG, "TLPDB.pm: trying to initialize from $path\n"); open(TMP, "wget -q --output-document=- $path|") || die("Cannot open tlpdb file: $path"); } else { open(TMP,"<$path") || die("Cannot open tlpdb file: $path"); } my $found = 0; do { my $tlp = TeXLive::TLPOBJ->new; tllog($::LOG_DDDEBUG, "creating tlp from $path...\n"); $found = $tlp->from_fh(\*TMP,1); if ($found) { $self->add_tlpobj($tlp); } } until (!$found); } =pod =item C<< $tlpdb->writeout >> =item C<< $tlpdb->writeout(FILEHANDLE) >> The C function writes the database to C, or the file handle given as argument. =cut sub writeout { my $self = shift; my $fd = (@_ ? $_[0] : STDOUT); foreach (sort keys %{$self->{'tlps'}}) { tllog($::LOG_DDDEBUG, "tlpname = $_\n"); tllog($::LOG_DDDEBUG, "foo: ", $self->{'tlps'}{$_}->name, "\n"); $self->{'tlps'}{$_}->writeout($fd); print $fd "\n"; } } =pod =item C<< $tlpdb->save >> The C functions saves the C to the file which has been set as location. If the location is undefined, die. =cut sub save { my $self = shift; my $path = $self->location; mkdirhier(dirname($path)); open(FOO,">$path") || die("Cannot open $path for writing: $!"); $self->writeout(\*FOO); close(FOO); } =pod =item C<< $tlpdb->available_architectures >> The C functions returns the list of available architectures by checking for binfiles in the "bin-tex" TLPOBJ. If this package does not exist, die. =cut sub available_architectures { my $self = shift; my @packs = $self->list_packages; my @archs; # map { s@^$tltree/@@ ; push @nf, $_; } @files; map { s/^bin-tex\.// ; push @archs, $_ ; } grep(/^bin-tex\.(.*)$/, @packs); return @archs; } =pod =item C<< $tlpdb->add_tlpcontainer($pkg, $ziploc [, $archrefs [, $dest ]] ) >> Installs the package C<$pkg> from the container files in C<$ziploc>. If C<$archrefs> is given then it must be a reference to a list of architectures to be installed. If the normal (arch=all) package is architecture dependent then all arch packages in this list are installed. If C<$dest> is given then the files are installed into it, otherwise into the location of the TLPDB. Note that this procedure does NOT check for dependencies. So if your package adds new dependencies they are not necessarily fulfilled. =cut sub add_tlpcontainer { my ($self, $package, $ziplocation, $archrefs, $dest) = @_; my @archs; require Cwd; if (defined($archrefs)) { @archs = @$archrefs; } my $cwd = getcwd(); if ($ziplocation !~ m,^/,) { $ziplocation = "$cwd/$ziplocation"; } my $tlpobj = $self->_add_tlpcontainer($package, $ziplocation, "all", $dest); if ($tlpobj->is_arch_dependent) { foreach (@$archrefs) { $self->_add_tlpcontainer($package, $ziplocation, $_, $dest); } } } sub _add_tlpcontainer { my ($self, $package, $ziplocation, $arch, $dest) = @_; my $unpackprog; my $args; require Cwd; # WARNING: If you change the location of the texlive.tlpdb this # has to be changed, too!! if (not(defined($dest))) { $dest = $self->{'root'}; } my $container = "$ziplocation/$package"; if ($arch ne "all") { $container .= ".$arch"; } if (-r "$container.zip") { $container .= ".zip"; $unpackprog="unzip"; $args="-o -qq $container -d $dest"; } elsif (-r "$container.lzma") { $container .= ".lzma"; $unpackprog="NO_IDEA_HOW_TO_UNPACK_LZMA"; $args="NO IDEA WHAT ARGS IT NEEDS"; die("lzma is checked for but not implemented, please edit TLPDB.pm\n"); } else { die "Cannot find a package $container (.zip or .lzma) in $ziplocation\n"; } warn "Huuu, this needs testing and error checking!\n"; warn "Should we use -a -- adapt line endings etc?\n"; `$unpackprog $args`; # we only create/add tlpobj for arch eq "all" if ($arch eq "all") { my $tlpobj = new TeXLive::TLPOBJ; $tlpobj->from_file("$dest/$TeXLive::TLConfig::InfraLocation/tlpobj/$package.tlpobj"); $self->add_tlpobj($tlpobj); return $tlpobj; } } =pod =item C<< $tlpdb->get_package("packagename") >> The C function returns a reference to a C object in case its name the the argument name coincide. =cut sub get_package { my ($self,$pkg) = @_; if (defined($self->{'tlps'}{$pkg})) { return($self->{'tlps'}{$pkg}); } else { return(undef); } } =pod =item C<< $tlpdb->list_packages >> The C function returns the list of all included packages. =cut sub list_packages { my $self = shift; return (sort keys %{$self->{'tlps'}}); } =pod =item C<< $tlpdb->find_file("filename") >> The C returns a list of packages:filename containing a file named C. =cut sub find_file { my ($self,$fn) = @_; my @ret; foreach my $pkg ($self->list_packages) { my @foo = $self->get_package($pkg)->contains_file($fn); foreach my $f ($self->get_package($pkg)->contains_file($fn)) { push @ret, "$pkg:$f"; } } return(@ret); } =pod =item C<< $tlpdb->collections >> The C function returns the list of all collections. =cut sub collections { my $self = shift; my @ret; foreach my $p ($self->list_packages) { if ($self->get_package($p)->category eq "Collection") { push @ret, $p; } } return @ret; } =pod =item C<< $tlpdb->schemes >> The C function returns the list of all schemes. =cut sub schemes { my $self = shift; my @ret; foreach my $p ($self->list_packages) { if ($self->get_package($p)->category eq "Scheme") { push @ret, $p; } } return @ret; } =pod =item C<< $tlpdb->package_revision("packagename") >> The C function returns the revision number of the package named in the first argument. =cut sub package_revision { my ($self,$pkg) = @_; if (defined($self->{'tlps'}{$pkg})) { return($self->{'tlps'}{$pkg}->revision); } else { return(undef); } } =pod =item C<< $tlpdb->generate_packagelist >> The C prints TeX Live package names in the object database, together with their revisions, to the file handle given in the first (optional) argument, or C by default. It also outputs all available architectures as packages with revision number -1. =cut sub generate_packagelist { my $self = shift; my $fd = (@_ ? $_[0] : STDOUT); foreach (sort keys %{$self->{'tlps'}}) { print $fd $self->{'tlps'}{$_}->name, " ", $self->{'tlps'}{$_}->revision, "\n"; } foreach ($self->available_architectures) { print $fd "$_ -1\n"; } } =pod =item C<< $tlpdb->generate_listfiles >> =item C<< $tlpdb->generate_listfiles($destdir) >> The C generates the list files for the old installers. This function will probably go away. =cut sub generate_listfiles { my ($self,$destdir) = @_; if (not(defined($destdir))) { $destdir = TeXLive::TLPDB->listdir; } foreach (sort keys %{$self->{'tlps'}}) { $tlp = $self->{'tlps'}{$_}; $self->_generate_listfile($tlp, $destdir); } } sub _generate_listfile { my ($self,$tlp,$destdir) = @_; my $listname = $tlp->name; my @files = $tlp->all_files; @files = TeXLive::TLUtils::sort_uniq(@files); &mkpath("$destdir") if (! -d "$destdir"); my (@lop, @lot); foreach my $d ($tlp->depends) { my $subtlp = $self->get_package($d); if (defined($subtlp)) { if ($subtlp->is_meta_package) { push @lot, $d; } else { push @lop, $d; } } else { # speudo dependencies on $Package.ARCH can be ignored if ($d !~ m/\.ARCH$/) { warn "Strange: $tlp->name depends on $d, but this package does not exists!\n"; } } } open(TMP, ">$destdir/$listname") or die "Cannot open $destdir/$listname!"; # title and size information for collections and schemes in the # first two lines, marked with * if ($tlp->category eq "Collection") { print TMP "*Title: ", $tlp->shortdesc, "\n"; # collections references Packages, we have to collect the sizes of # all the Package-tlps included # What is unclear for me is HOW the size is computed for bin-* # packages. The collection-basicbin contains quite a lot of # bin-files, but the sizes for the different archs differ. # I guess we have to take the maximum? my $s = 0; foreach my $p (@lop) { my $subtlp = $self->get_package($p); if (!defined($subtlp)) { warn "Strange: $listname references $p, but it is not in tlpdb"; } $s += $subtlp->total_size; } # in case the collection itself ships files ... $s += $tlp->runsize + $tlp->srcsize + $tlp->docsize; print TMP "*Size: $s\n"; } elsif ($tlp->category eq "Scheme") { print TMP "*Title: ", $tlp->shortdesc, "\n"; my $s = 0; # schemes size includes ONLY those packages which are directly # included and direclty included files, not the size of the # included collections. But if a package is included in one of # the called for collections AND listed directly, we don't want # to count its size two times my (@inccol,@incpkg,@collpkg); # first we add all the packages tlps that are directly included @incpkg = @lop; # now we select all collections, and for all collections we # again select all packages of type Documentation and Package foreach my $c (@lot) { my $coll = $self->get_package($c); foreach my $d ($coll->depends) { my $subtlp = $self->get_package($d); if (defined($subtlp)) { if (!($subtlp->is_meta_package)) { TeXLive::TLUtils::push_uniq(\@collpkg,$d); } } else { warn "Strange: $coll->name depends on $d, but this does not exists!\n"; } } } # finally go through all packages and add the ->total_size foreach my $p (@incpkg) { if (!TeXLive::TLUtils::member($p,@collpkg)) { $s += $self->get_package($p)->total_size; } } $s += $tlp->runsize + $tlp->srcsize + $tlp->docsize; print TMP "*Size: $s\n"; } # dependencies and inclusion of packages foreach my $t (@lot) { # strange, schemes mark included collections via -, while collections # themself mark deps on other collections with +. collection are # never referenced in Packages if ($listname =~ m/^scheme/) { print TMP "-"; } else { print TMP "+"; } print TMP "$t\n"; } foreach my $t (@lop) { print TMP "+$t\n"; } # included files foreach my $f (@files) { print TMP "$f\n"; } # also print the listfile itself print TMP "$destdir/$listname\n"; # execute statements foreach my $e ($tlp->executes) { print TMP "!$e\n"; } # finish close(TMP); } =pod =item C<< $tlpdb->root([ "/path/to/installation" ]) >> The function C allows to read and set the root of the installation. =cut sub root { my $self = shift; if (@_) { $self->{'root'} = shift } return $self->{'root'}; } =pod =item C<< $tlpdb->location >> The function C returns the location of the actual C file used. Note that this is a read-only function, you cannot change the root of the TLPDB using this function. =cut sub location { my $self = shift; return "$self->{'root'}/$InfraLocation/$DatabaseName"; } =pod =item C<< $tlpdb->listdir >> The function C allows to read and set the packages variable specifiying where generated list files are created. =cut sub listdir { my $self = shift; if (@_) { $_listdir = $_[0] } return $_listdir; } =pod =item C<< $tlpdb->config_src_container >> Returns 1 if the the texlive config option for src files splitting on container level is set. See Options below. =cut sub config_src_container { my $self = shift; if (defined($self->{'tlps'}{'00texlive.config'})) { foreach my $d ($self->{'tlps'}{'00texlive.config'}->depends) { if ($d eq "container_split_src_files") { return 1; } } } return 0; } =pod =item C<< $tlpdb->config_doc_container >> Returns 1 if the the texlive config option for doc files splitting on container level is set. See Options below. =cut sub config_doc_container { my $self = shift; if (defined($self->{'tlps'}{'00texlive.config'})) { foreach my $d ($self->{'tlps'}{'00texlive.config'}->depends) { if ($d eq "container_split_doc_files") { return 1; } } } return 0; } =pod =item C<< $tlpdb->config_doc_container >> Returns the currently set default container format. See Options below. =cut sub config_container_format { my $self = shift; if (defined($self->{'tlps'}{'00texlive.config'})) { foreach my $d ($self->{'tlps'}{'00texlive.config'}->depends) { if ($d =~ m!^container_format/(.*)$!) { return "$1"; } } } return ""; } =pod =item C<< $tlpdb->config_release >> Returns the currently set release. See Options below. =cut sub config_release { my $self = shift; if (defined($self->{'tlps'}{'00texlive.config'})) { foreach my $d ($self->{'tlps'}{'00texlive.config'}->depends) { if ($d =~ m!^release/(.*)$!) { return "$1"; } } } return ""; } =pod =item C<< $tlpdb->option_XXXXX >> Need to be documented =cut sub _option_value { my ($self,$key) = @_; if (defined($self->{'tlps'}{'00texlive-installation.config'})) { foreach my $d ($self->{'tlps'}{'00texlive-installation.config'}->depends) { if ($d =~ m!^$key/(.*)$!) { return "$1"; } } return ""; } return; } sub _option_setting { my ($self,$key) = @_; if (defined($self->{'tlps'}{'00texlive-installation.config'})) { foreach my $d ($self->{'tlps'}{'00texlive-installation.config'}->depends) { if ($d =~ m!^$key$!) { return 1; } } return 0; } return; } sub option_symlinks { my $self = shift; return $self->_option_setting("symlinks"); } sub option_docfiles { my $self = shift; return $self->_option_setting("docfiles"); } sub option_srcfiles { my $self = shift; return $self->_option_setting("srcfiles"); } sub option_formats { my $self = shift; return $self->_option_setting("formats"); } sub option_letter { my $self = shift; return $self->_option_setting("letter"); } sub option_location { my $self = shift; return $self->_option_value("location"); } sub option_sys_bin { my $self = shift; return $self->_option_value("sys_bin"); } sub option_sys_man { my $self = shift; return $self->_option_value("sys_man"); } sub option_sys_info { my $self = shift; return $self->_option_value("sys_info"); } sub option_platform { my $self = shift; return $self->_option_value("platform"); } =pod =item C<< $tlpdb->fmtutil_cnf_lines >> The function C returns the list of a fmtutil.cnf file containing only those formats present in the installation. =cut sub fmtutil_cnf_lines { my $self = shift; my %fmtcnffiles; foreach my $p ($self->list_packages) { my $obj = $self->get_package ($p); die "No TeX Live package named $p, strange" if ! $obj; foreach my $e ($obj->executes) { if ($e =~ m/BuildFormat (.*)$/) { $fmtcnffiles{$1} = 1; } # others are ignored here } } my @formatlines; open(INFILE,"<$self->{'root'}/texmf/fmtutil/fmtutil-hdr.cnf") or die("Cannot open $self->{'root'}/texmf/fmtutil/fmtutil-hdr.cnf\n"); my @tmp = ; close (INFILE); push @formatlines, @tmp; foreach my $f (keys %fmtcnffiles) { open(INFILE,"<$self->{'root'}/texmf/fmtutil/format.$f.cnf") or die("Cannot open $self->{'root'}/texmf/fmtutil/format.$f.cnf"); @tmp = ; close(INFILE); push @formatlines, @tmp; } return(@formatlines); } =item C<< $tlpdb->updmap_cfg_lines >> The function C returns the list of a updmap.cfg file containing only those maps present in the installation. =cut sub updmap_cfg_lines { my $self = shift; my %maps; foreach my $p ($self->list_packages) { my $obj = $self->get_package ($p); die "No TeX Live package named $p, strange" if ! $obj; foreach my $e ($obj->executes) { if ($e =~ m/addMap (.*)$/) { $maps{$1} = 1; } elsif ($e =~ m/addMixedMap (.*)$/) { $maps{$1} = 2; } # others are ignored here } } my @updmaplines; open(UPDHDR,"<$self->{'root'}/texmf/web2c/updmap-hdr.cfg") or die("Cannot find $self->{'root'}/texmf/web2c/updmap-hdr.cfg!\n"); my @tmp = ; close(UPDHDR); push @updmaplines, @tmp; foreach (sort keys %maps) { if ($maps{$_} == 2) { push @updmaplines, "MixedMap $_\n"; } else { push @updmaplines, "Map $_\n"; } } return(@updmaplines); } =item C<< $tlpdb->language_dat_lines >> The function C returns the concatenation of all files specified in C directives in the present TLPDB. Exception: the result always begins with the contents of C file. This and all the other files are looked for in C. =cut sub language_dat_lines { my $self = shift; my %langfiles; foreach my $p ($self->list_packages) { my $obj = $self->get_package ($p); die "No TeX Live package named $p, too strange" if ! $obj; foreach my $e ($obj->executes) { if ($e =~ m/BuildLanguageDat (.*)$/) { $langfiles{$1} = 1; } # others are ignored here } } my @langlines = (); # must start with language.us. local *LANHDR; $LANHDR = "<$self->{'root'}/texmf/tex/generic/config/language.us"; open (LANHDR) or die "open($LANHDR) failed: $!"; my @tmp = ; close (LANHDR); push @langlines, @tmp; local *INFILE; foreach my $f (keys %langfiles) { $INFILE = "<$self->{'root'}/texmf/tex/generic/config/language.$f.dat"; open (INFILE) or die "open($INFILE) failed: $!"; @tmp = ; close (INFILE); push @langlines, @tmp; } return @langlines; } =back =pod =head1 OPTIONS Options regarding the full TeX Live installation to be described are saved in a package C<00texlive.config> as values of C lines. This special package C<00texlive.config> does not contain any files, only depend lines which set one or more of the following options: =over 6 =item C =item C These options specify that at container generation time the source and documentation files for a package have been put into a separate container named C and C. =item C> This option specifies a format for containers. The currently supported formats are C and C. But note that C is untested. =back To set these options the respective lines should be added to C<00texlive.config.tlpsrc>. =head1 SEE ALSO The modules L, L, L, L and the document L and the specification in the TeX Live repository trunk/Master/tlpkg/doc/. =head1 AUTHORS AND COPYRIGHT This script and its documentation were written for the TeX Live distribution (L) and both are licensed under the GNU General Public License Version 2 or later. =cut 1; ### Local Variables: ### perl-indent-level: 2 ### tab-width: 2 ### indent-tabs-mode: nil ### End: # vim:set tabstop=2 expandtab: #