home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / sbin / popularity-contest < prev    next >
Encoding:
Text File  |  2006-05-11  |  4.0 KB  |  144 lines

  1. #!/usr/bin/perl -w
  2. #
  3. # Copyright (C) 2003 by Bill Allombert <ballombe@debian.org>
  4.  
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18.  
  19. # based on a design and a bash/gawk script 
  20. # Copyright (C) 1998,2000 by Avery Pennarun, for the Debian Project.
  21. # Use, modify, and redistribute modified or unmodified versions in any
  22. # way you wish.
  23.  
  24. use strict;
  25. use 5.6.0;  
  26.  
  27. my $dpkg_db="/var/lib/dpkg/info";
  28. my $popcon_conf="/etc/popularity-contest.conf";
  29.  
  30. my %opts=();
  31.  
  32. # $popcon_conf is in shell-script format
  33. my $HOSTID = qx(unset MY_HOSTID; . $popcon_conf; echo \$MY_HOSTID );
  34.  
  35. chomp $HOSTID;
  36.  
  37. if ( $HOSTID eq "")
  38. {
  39.   print STDERR "You must set MY_HOSTID in $popcon_conf!\n";
  40.   exit 1;
  41. }
  42.  
  43. if ( $HOSTID eq "d41d8cd98f00b204e9800998ecf8427e")
  44. {
  45.   print STDERR "Warning: MY_HOSTID is the md5sum of the empty file!\n";
  46.   print STDERR "Please change it to the md5sum of a random file in $popcon_conf!\n";
  47. }
  48.  
  49. if ( $HOSTID !~ /^([a-f0-9]{32})$/)
  50. {
  51.   print STDERR "Error: MY_HOSTID does not match ^([a-f0-9]{32})\$\n";
  52.   print STDERR "Please edit $popcon_conf to use a valid md5sum value\n";
  53.   exit 1;
  54. }
  55.  
  56. # Architecture.
  57. my $debarch = `dpkg --print-installation-architecture`;
  58. chomp $debarch;
  59.  
  60. # Set if dpkg package version is older than 1.10, thus missing dpkg-query.
  61. my $olddpkg = 0;
  62. if ( ! -x "/usr/bin/dpkg-query" ) {
  63.     $olddpkg = 1;
  64. }
  65.  
  66. # Popcon release
  67. my $popconver;
  68. if ($olddpkg) {
  69.     $popconver = `dpkg-awk "Package: *popularity-contest.*" -- Version|grep ^Version|sed 's/^Version: //'`;
  70. } else {
  71.     $popconver=`dpkg-query --showformat='\${version}' --show popularity-contest 2>/dev/null`;
  72. }
  73.  
  74. # Initialise time computations
  75.  
  76. my $now = time;
  77. my $daylen = 24 * 60 * 60;
  78. my $monthlen = $daylen * 30;
  79. my $lastmonth = $now - $monthlen;
  80.  
  81. my %popcon=();
  82.  
  83. #Read dpkg database of installed packages
  84. if ($olddpkg) {
  85.     open PACKAGES, "dpkg-awk 'Status: .* installed' -- Package | grep '^Package:' | sed 's/^Package: /install ok installed /'|";
  86. } else {
  87.     open PACKAGES, "dpkg-query --show --showformat='\${status} \${package}\\n'|";
  88. }
  89. while (<PACKAGES>)
  90. {
  91.   /^.*installed *(.+)$/ or next;
  92.   my $pkg=$1;
  93.   $popcon{$pkg}=[0,0,$pkg,"<NOFILES>"];
  94.   open FILES, "$dpkg_db/$pkg.list";
  95.   my $bestatime = undef;
  96.   while (<FILES>)
  97.   {
  98.     chop;
  99.     m{/bin/|/sbin/|^/usr/games/|\.[ah]$|\.pm$} or next;
  100.     -f $_ or next;
  101.     my($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
  102.                       $atime,$mtime,$ctime,$blksize,$blocks)
  103.                           = stat;
  104.     print STDERR if (!defined($atime));
  105.     if (!defined($bestatime) || $atime >= $bestatime)
  106.     {
  107.       $bestatime=$atime;
  108.       if ($atime < $lastmonth)
  109.       { 
  110.         # Not accessed since more than 30 days.
  111.         $popcon{$pkg}=[$atime,$ctime,$pkg,$_,"<OLD>"];
  112.       }
  113.       elsif ($ctime > $lastmonth && $atime-$ctime < $daylen)
  114.       {
  115.         # Installed/upgraded less than a month ago and not used after 
  116.         # install/upgrade day.
  117.         $popcon{$pkg}=[$atime,$ctime,$pkg,$_,"<RECENT-CTIME>"];
  118.       }
  119.       else
  120.       {
  121.         # Else we `vote' for the package.
  122.         $popcon{$pkg}=[$atime,$ctime,$pkg,$_];
  123.       }
  124.     }
  125.   }
  126.   close FILES;
  127. }
  128.  
  129. close PACKAGES;
  130.  
  131. # We're not done yet.  Sort the output in reverse by atime, and
  132. # add a header/footer.
  133.     
  134. print "POPULARITY-CONTEST-0 TIME:",time," ID:$HOSTID ".
  135.     "ARCH:$debarch POPCONVER:$popconver\n";
  136.  
  137. for (sort { $popcon{$b}[0] <=> $popcon{$a}[0] } keys %popcon)
  138. {
  139.   print join(' ',@{$popcon{$_}}),"\n";
  140. }
  141.  
  142. print "END-POPULARITY-CONTEST-0 TIME:",time,"\n";
  143.