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 / popularity-contest < prev    next >
Encoding:
Text File  |  2007-02-05  |  3.6 KB  |  130 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. # Popcon release
  61. my $popconver=`dpkg-query --showformat='\${version}' --show popularity-contest`;
  62.  
  63. # Initialise time computations
  64.  
  65. my $now = time;
  66. my $daylen = 24 * 60 * 60;
  67. my $monthlen = $daylen * 30;
  68. my $lastmonth = $now - $monthlen;
  69.  
  70. my %popcon=();
  71.  
  72. #Read dpkg database of installed packages
  73.  
  74. open PACKAGES, "dpkg-query --show --showformat='\${status} \${package}\\n'|";
  75. while (<PACKAGES>)
  76. {
  77.   /^.*installed *(.+)$/ or next;
  78.   my $pkg=$1;
  79.   $popcon{$pkg}=[0,0,$pkg,"<NOFILES>"];
  80.   open FILES, "$dpkg_db/$pkg.list";
  81.   my $bestatime = undef;
  82.   while (<FILES>)
  83.   {
  84.     chop;
  85.     m{/bin/|/sbin/|^/usr/games/|\.[ah]$|\.pm|^/boot/System\.map-} or next;
  86.     -f $_ or next;
  87.     my($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
  88.                       $atime,$mtime,$ctime,$blksize,$blocks)
  89.                           = stat;
  90.     print STDERR if (!defined($atime));
  91.     if (!defined($bestatime) || $atime >= $bestatime)
  92.     {
  93.       $bestatime=$atime;
  94.       if ($atime < $lastmonth)
  95.       { 
  96.         # Not accessed since more than 30 days.
  97.         $popcon{$pkg}=[$atime,$ctime,$pkg,$_,"<OLD>"];
  98.       }
  99.       elsif ($ctime > $lastmonth && $atime-$ctime < $daylen)
  100.       {
  101.         # Installed/upgraded less than a month ago and not used after 
  102.         # install/upgrade day.
  103.         $popcon{$pkg}=[$atime,$ctime,$pkg,$_,"<RECENT-CTIME>"];
  104.       }
  105.       else
  106.       {
  107.         # Else we `vote' for the package.
  108.         $popcon{$pkg}=[$atime,$ctime,$pkg,$_];
  109.       }
  110.     }
  111.   }
  112.   close FILES;
  113. }
  114.  
  115. close PACKAGES;
  116.  
  117. # We're not done yet.  Sort the output in reverse by atime, and
  118. # add a header/footer.
  119.     
  120. print "POPULARITY-CONTEST-0 TIME:",time," ID:$HOSTID ".
  121.     "ARCH:$debarch POPCONVER:$popconver\n";
  122.  
  123. for (sort { $popcon{$b}[0] <=> $popcon{$a}[0] } keys %popcon)
  124. {
  125.   print join(' ',@{$popcon{$_}}),"\n";
  126. }
  127.  
  128. print "END-POPULARITY-CONTEST-0 TIME:",time,"\n";
  129.