File Coverage

blib/lib/Sman/Autoconfig.pm
Criterion Covered Total %
statement 12 41 29.2
branch 0 10 0.0
condition n/a
subroutine 4 5 80.0
pod 0 1 0.0
total 16 57 28.0


line stmt bran cond sub pod time code
1             package Sman::Autoconfig;
2 1     1   1313 use Sman::Man::Convert;
  1         2  
  1         27  
3 1     1   540 use Storable;
  1         2194  
  1         44  
4              
5              
6             #$Id$
7              
8 1     1   5 use strict;
  1         1  
  1         16  
9 1     1   3 use warnings;
  1         1  
  1         331  
10              
11             # this package finds which man command works best on this system
12             # Chooses 'best' man command for Sman.
13             # Our logic is that either 'man %F' or 'man %S %C' will work
14             # given a list of manfiles, we deterministically pick
15             # representatives, and see which man command works best
16             # on each, XML-wise.
17             # this works for most linuxes we've tested
18             #MANCMD man -c %F
19             # this works for freebsd 4.4 and Mac OS X up to 10.3
20             #MANCMD man -c %S %C
21              
22             # these are the man commands we try
23             #my @tries = ( 'man -c %F', 'man -c %S %C', 'cat %F | gunzip -f --stdout | man -c' );
24             # the last option above does not work, so it's been removed. (it needs tmp file in the middle.)
25             # we once left it in anyway, it won't get used if doesn't work, but it causes warnings under cron
26             my @tries = (
27             'man %F', # debian 4.0 needs this, the simplest one, which none supported for years.
28             'man -c %F',
29             'man -c %S %C',
30             );
31             # man -c means to reparse manpage input (and not use the manpage cache)
32             # gunzip -f means just cat it if it's not compressed
33             # gunzip --stdout means put the output to stdout (I think this is the default)
34              
35              
36             sub GetBestManCommand {
37 0     0 0   my ($smanconfig, $manfilesref) = @_;
38            
39 0           my %converters = ();
40 0           for my $cmd (@tries) {
41 0           my $newconfig = Storable::dclone($smanconfig);
42 0           $newconfig->SetConfigData("MANCMD", $cmd);
43 0           $newconfig->SetConfigData("AUTOCONFIGURING", 1); # internal flag
44 0           $converters{ $cmd } = new Sman::Man::Convert($newconfig, { nocache=>1 } );
45             }
46 0           my $numfiles = 10; # number of files to test
47 0           my @testfiles = (); # the files we'll be testing
48 0 0         if (scalar(@$manfilesref) < $numfiles) { $numfiles = scalar(@$manfilesref); }
  0            
49 0           for (my $i=0; $i < $numfiles; $i++) {
50 0           push(@testfiles, $manfilesref->[ int( $i / $numfiles * scalar(@$manfilesref) ) ] );
51             }
52              
53 0           my %cmdwins = (); # hash of cmd -> sum of lengths of output for this command
54 0           for my $file (@testfiles) {
55 0 0         warn "Testing $file" if $smanconfig->GetConfigData("VERBOSE");
56 0           my ($maxlen, $winningcmd) = (0, "");
57 0           for my $mancmd (keys(%converters)) { # go through the converters
58 0           my ($parser, $contentref) = $converters{$mancmd}->ConvertManfile($file);
59 0 0         printf( "$0: Got %d bytes from %s\n", length($$contentref), $mancmd ) if $smanconfig->GetConfigData( "DEBUG" );
60 0 0         if (length($$contentref) > $maxlen) { # record the largest output and its cmd
61 0           $maxlen = length($$contentref);
62 0           $winningcmd = $mancmd;
63             }
64             }
65 0           $cmdwins{$winningcmd}++; # whichever cmd had largest output gets a point
66             }
67 0           my @wins = sort { $cmdwins{$b} <=> $cmdwins{$a} } keys(%cmdwins);
  0            
68 0 0         if (scalar(@wins)) { return $wins[0]; }
  0            
69 0           return 'man %S %C'; # or 'man %F'
70             }
71              
72             1;
73             __END__