home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / sbin / aspell-autobuildhash < prev    next >
Encoding:
Text File  |  2006-12-19  |  8.6 KB  |  288 lines

  1. #!/usr/bin/perl -w
  2. # $Id: aspell-autobuildhash,v 1.10 2006/10/29 21:23:19 agmartin Exp $
  3. #
  4. #  script for aspell hash autorebuild in Debian systems
  5. #
  6. # (c) 2004-2006 Agustin Martin Domingo <agmartin@debian.org>
  7. #
  8. #
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; either version 2 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program; if not, write to the Free Software
  21. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  22. #
  23.  
  24. sub usage {
  25.     print STDERR "\nUsage:\taspell-autobuildhash [--debug] [--force]\n"
  26.     . "\n"
  27.     . "Options:\n"
  28.     . "\t--debug         Show debugging information\n"
  29.     . "\t--force         Do the job regardless of versions comparisons\n";
  30. }
  31.  
  32. sub debugprint {
  33.     print STDERR "@_\n" if $debug;
  34. }
  35.  
  36. sub mymessage{
  37.     my $message  = join(" ",@_);
  38.     my $question = "dictionaries-common/ispell-autobuildhash-message";
  39.     my $hashfile = '';
  40.     
  41.     if ( $lang ) {
  42.     $hashfile = "$lang";
  43.     } else {
  44.     $hashfile = "dictionaries-common";
  45.     }
  46.  
  47.     subst($question,"xxpell","aspell");
  48.     subst($question,"XXpell","Aspell");
  49.     subst($question,"hashfile",$hashfile);
  50.     subst($question,"errormsg",$message);
  51.     fset ($question,"seen","false");
  52.     title("dictionaries-common: Running aspell-autobuildhash");
  53.     input("critical",$question);
  54.     go ();
  55. }
  56.  
  57. sub myerror {
  58.     mymessage @_;
  59.     exit 1;
  60. }
  61.  
  62. # ---------------------------------------------------------------------
  63. #      Handle autorebuilding
  64. # ---------------------------------------------------------------------
  65.  
  66. sub autorebuild {
  67.     my $lang      = shift ||                              # The dictionary name
  68.     myerror "No argument passed to function autorebuild";
  69.     my $data      = "/usr/lib/aspell";                    # The data/lib dir
  70.     my $langsfile = "/usr/share/aspell/$lang.contents";   # The subdicts file
  71.     my @sublangs  = ();
  72.     my $options   = "";
  73.     
  74.     $options      = "--dont-validate-affixes" unless $debug;
  75.     
  76.     myerror "aspell data dir $data does not exist" unless ( -d $data );
  77.     
  78.     if ( -e $langsfile ){
  79.     open (LANGSFILE, "< $langsfile") || die "Could not open $langsfile for reading";
  80.     @sublangs = <LANGSFILE>;
  81.     close LANGSFILE;
  82.     } else {
  83.     push @sublangs, $lang;
  84.     }
  85.     
  86.     foreach ( @sublangs ){
  87.     
  88.     next if m/^[\t\s]*$/;
  89.     chomp;
  90.     s/^[\s\t]*//;
  91.     s/[\s\t]*$//;
  92.     next if m/^\#/;
  93.     
  94.     my $sublang = $_;
  95.     my $base    = "/usr/share/aspell/$sublang";     # the wordlist basename
  96.     my $hash    = "/var/lib/aspell/$sublang.rws";   # the hash file
  97.     my $msg     = '';
  98.     my $unpack  = '';
  99.     
  100.     print STDERR "aspell-autobuildhash: processing: $lang [$sublang]\n";
  101.     
  102.  
  103.     if ( -e "$base.mwl.gz" ){
  104.         $unpack = "zcat $base.mwl.gz";
  105.     } elsif ( -e "$base.wl.gz") {
  106.         $unpack = "zcat $base.wl.gz";
  107.     } elsif ( -e "$base.cwl.gz") {
  108.         $unpack = "zcat $base.cwl.gz | precat";
  109.     } else {
  110.         mymessage "Could not find any of $base.{mwl,wl,cwl}.gz";
  111.         return 0;
  112.     }
  113.     
  114.     #$unpack = "$unpack | aspell clean strict";
  115.     system ("$unpack | aspell $options --local-data-dir=$data --lang=$lang create master $hash") == 0
  116.         or $msg = "Could not build the hash file for $sublang" ;
  117.     
  118.     if ( $msg ){             # Do not break postinst if hash cannot be built
  119.         mymessage ($msg);    # Just inform about that
  120.         return 0;
  121.     }
  122.     }
  123.     return 1;
  124. }
  125.  
  126. # ---------------------------------------------------------------------
  127. #                   Get aspell compat version
  128. # ---------------------------------------------------------------------
  129.  
  130. sub get_aspell_compat {
  131.     
  132.     my $aspell_compat = '';
  133.     
  134.     if ( -e $aspellcompatfile ){
  135.     open (COMPAT,"$aspellcompatfile");
  136.     chomp ( $aspell_compat = <COMPAT> );
  137.     close COMPAT;
  138.     } else {
  139.     $force = "yes";
  140.     }
  141.     return $aspell_compat;
  142. }
  143.  
  144. # ---------------------------------------------------------------------
  145. #                        The main program
  146. # ---------------------------------------------------------------------
  147.  
  148. die "$0: You must run this as root.\n" if ($> != 0);
  149.  
  150. use Debconf::Client::ConfModule q(:all);
  151. use Getopt::Long;
  152.  
  153. $compatdir        = "/var/lib/aspell";
  154. $aspellcompatfile = "/usr/share/aspell/aspell.compat";
  155.  
  156. $force        = '';
  157. $debug        = '';
  158.  
  159. GetOptions ('debug' => \$debug, 
  160.         'force' => \$force) or usage();
  161.  
  162. if ( -x "/usr/bin/aspell" ){
  163.  
  164.     $aspell_compat = get_aspell_compat();
  165.     
  166.     foreach $compat ( <$compatdir/*.compat> ){
  167.     my $build_hash  = '';
  168.     my $lang_compat = '';
  169.  
  170.     $lang = $compat;
  171.     $lang =~ s/\.compat$//;
  172.     $lang =~ s/.*\///;
  173.     
  174.     open (COMPAT,"$compat");
  175.     $lang_compat = <COMPAT>;
  176.     close COMPAT;
  177.     $lang_compat = 0     if not $lang_compat;
  178.     chomp $lang_compat;
  179.  
  180.     $build_hash  = "yes" if ( $aspell_compat ne $lang_compat );
  181.     $build_hash  = "yes" if $force;
  182.     
  183.     if ( $build_hash ){
  184.         debugprint "$lang => aspell_compat: [$aspell_compat]; lang_compat: [$lang_compat]";
  185.         if ( autorebuild($lang) ){
  186.         debugprint " +++ Updating $compat";
  187.         open (COMPAT,">","$compat");
  188.         if ( $aspell_compat ){
  189.             print COMPAT "$aspell_compat\n";
  190.         } else {
  191.             print COMPAT "0\n";
  192.         }
  193.         close COMPAT;
  194.         } else {
  195.         debugprint " --- $compat not updated because of an error";
  196.         }
  197.     }
  198.     }
  199. } else {
  200.     debugprint " aspell is not installed. Doing nothing";
  201. }
  202.  
  203. __END__
  204.  
  205. =head1 NAME
  206.  
  207. B<aspell-autobuildhash> - Autobuilding aspell hash files for some dicts
  208.  
  209. =head1 SYNOPSIS
  210.  
  211.  aspell-autobuildhash [--force]
  212.  
  213.    Options:
  214.     --debug      Show some extra information.
  215.     --force      Rebuild the hash file for all dicts providing a 
  216.                  compat file skipping the test.
  217.  
  218. =head1 DESCRIPTION
  219.  
  220. B<aspell-autobuildhash> is a script that will manage aspell hash files 
  221. autobuild, intended to be called from the dictionaries-common tools. 
  222. Depending on the aspell 
  223. compatibility level and on the compatibility level used for the hash file 
  224. if present, will decide whether it must be rebuilt or not. This script will 
  225. only work on aspell packages prepared to use it, it will do nothing for other 
  226. aspell dict packages.
  227.  
  228. =head1 OPTIONS
  229.  
  230.  --debug      Show some extra information.
  231.  --force      Rebuild the hash file for all dicts providing a compat 
  232.               file regardless of the compatibility levels found.
  233.  
  234. =head1 PACKAGE MAINTAINERS
  235.  
  236. To use this system, just provide a F<$lang.compat> file in F</var/lib/aspell> 
  237. (I<$lang> stands for the lang basename with variant if any, e.g. I<gl-minimos>
  238. or I<en>). Put a "0" in it or just create an empty one with touch.
  239.  
  240. Wordlists should previously be compressed either with gzip
  241. (and their extensions set as F<.mwl.gz> or F<.wl.gz>) or preferably
  242. first with aspell prezip and then gzipped (with F<.cwl.gz> extension).
  243. This applies both for plain wordlists and munched wordlists
  244. (in the ispell way) if you use affix compression.
  245.  
  246. If your package will provide a single hash, install prezipped+gzipped
  247. wordlist as F</usr/share/aspell/$lang.cwl.gz> or, if prezip is not used,
  248. as F</usr/share/aspell/$lang.mwl.gz>.
  249.  
  250. If your package will provide more than one aspell hash for the same $lang,
  251. you will need to place each compressed wordlist as e.g.
  252. F</usr/share/aspell/$subdict.cwl.gz>, and the common F<$lang.compat> as
  253. above. Then create a F</usr/share/aspell/$lang.contents> file with the
  254. base names of the subdicts, one in a line. For English that will contain,
  255. amongst other possible lines
  256.  
  257.  en-common
  258.  en-variant_0
  259.  en-variant_1
  260.  en-variant_2
  261.  en_CA-w_accents-only
  262.  
  263. No need to use this file if a single hash is being created.
  264.  
  265. Dictionaries-common scripts will call internally this script and create a 
  266. single hash file at F</var/lib/ispell/$lang.rws>, or hash files at
  267. F</var/lib/ispell/$subdict.rws>. You must set a symlink to that 
  268. files from F</usr/lib/aspell/$lang.rws> or
  269. F</usr/lib/aspell/$subdict.rws> as appropriate.
  270. You are also suggested to create empty files at
  271. F</var/lib/aspell/$lang.rws> or for all of the
  272. F</var/lib/aspell/$subdict.rws> in the install target of 
  273. your package build process. This empty file will be overwritten when the 
  274. real hash is created, but will make the hash be removed at package 
  275. removal without any magic being done in the postrm and will also help to
  276. keep track about which package owns that file.
  277.  
  278. B<aspell> maintainer should also call this script from package postinst. 
  279. When comparing versions it will get the aspell version from file
  280. F</usr/share/aspell/aspell.compat>.
  281.  
  282. =head1 AUTHORS
  283.  
  284. Agustin Martin <agmartin@debian.org>
  285.  
  286. =cut
  287.  
  288.