home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / system-tools-backends-2.0 / scripts / Utils / Platform.pm < prev    next >
Encoding:
Perl POD Document  |  2009-04-09  |  14.5 KB  |  462 lines

  1. #!/usr/bin/perl
  2. #-*- Mode: perl; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  3.  
  4. # Determine the platform we're running on.
  5. #
  6. # Copyright (C) 2000-2001 Ximian, Inc.
  7. #
  8. # Authors: Arturo Espinosa <arturo@ximian.com>
  9. #          Hans Petter Jansson <hpj@ximian.com>
  10. #
  11. # This program is free software; you can redistribute it and/or modify
  12. # it under the terms of the GNU Library General Public License as published
  13. # by the Free Software Foundation; either version 2 of the License, or
  14. # (at your option) any later version.
  15. #
  16. # This program is distributed in the hope that it will be useful,
  17. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. # GNU Library General Public License for more details.
  20. #
  21. # You should have received a copy of the GNU Library General Public License
  22. # along with this program; if not, write to the Free Software
  23. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  24.  
  25. package Utils::Platform;
  26.  
  27. use Utils::Parse;
  28. use Utils::Backend;
  29. use Utils::File;
  30. use Utils::Replace;
  31.  
  32. # --- System guessing --- #
  33.  
  34. my $PLATFORM_INFO = {
  35.   "debian-3.0"      => [ "Debian GNU/Linux", "3.0", "Woody" ],
  36.   "debian-3.1"      => [ "Debian GNU/Linux", "3.1", "Sarge" ],
  37.   "debian-4.0"      => [ "Debian GNU/Linux", "4.0", "Etch" ],
  38.   "debian-5.0"      => [ "Debian GNU/Linux", "5.0", "Lenny" ],
  39.   "debian-testing"  => [ "Debian GNU/Linux", "testing/unstable", "Lenny/Sid" ],
  40.   "ubuntu-5.04"     => [ "Ubuntu Linux", "5.04", "Hoary" ],
  41.   "ubuntu-5.10"     => [ "Ubuntu Linux", "5.10", "Breezy" ],
  42.   "ubuntu-6.06"     => [ "Ubuntu Linux", "6.06", "Dapper" ],
  43.   "ubuntu-6.10"     => [ "Ubuntu Linux", "6.10", "Edgy" ],
  44.   "ubuntu-7.04"     => [ "Ubuntu Linux", "7.04", "Feisty" ],
  45.   "ubuntu-7.10"     => [ "Ubuntu Linux", "7.10", "Gutsy" ],
  46.   "ubuntu-8.04"     => [ "Ubuntu Linux", "8.04", "Hardy" ],
  47.   "redhat-5.2"      => [ "Red Hat Linux", "5.2", "Apollo" ],
  48.   "redhat-6.0"      => [ "Red Hat Linux", "6.0", "Hedwig" ],
  49.   "redhat-6.1"      => [ "Red Hat Linux", "6.1", "Cartman" ],
  50.   "redhat-6.2"      => [ "Red Hat Linux", "6.2", "Zoot" ],
  51.   "redhat-7.0"      => [ "Red Hat Linux", "7.0", "Guinness" ],
  52.   "redhat-7.1"      => [ "Red Hat Linux", "7.1", "Seawolf" ],
  53.   "redhat-7.2"      => [ "Red Hat Linux", "7.2", "Enigma" ],
  54.   "redhat-7.3"      => [ "Red Hat Linux", "7.3", "Valhalla" ],
  55.   "redhat-8.0"      => [ "Red Hat Linux", "8.0", "Psyche" ],
  56.   "redhat-9"        => [ "Red Hat Linux", "9.0", "Shrike" ],
  57.   "openna-1.0"      => [ "OpenNA Linux", "1.0", "VSLC" ],
  58.   "mandrake-7.1"    => [ "Linux Mandrake", "7.1" ],
  59.   "mandrake-7.2"    => [ "Linux Mandrake", "7.2", "Odyssey" ],
  60.   "mandrake-8.0"    => [ "Linux Mandrake", "8.0", "Traktopel" ],
  61.   "mandrake-9.0"    => [ "Linux Mandrake", "9.0", "Dolphin" ],
  62.   "mandrake-9.1"    => [ "Linux Mandrake", "9.1", "Bamboo" ],
  63.   "mandrake-9.2"    => [ "Linux Mandrake", "9.2", "FiveStar" ],
  64.   "mandrake-10.0"   => [ "Linux Mandrake", "10.0" ],
  65.   "mandrake-10.1"   => [ "Linux Mandrake", "10.1" ],
  66.   "mandrake-10.2"   => [ "Linux Mandrake", "2005" ],
  67.   "mandriva-2006.0" => [ "Mandriva Linux", "2006.0" ],
  68.   "mandriva-2006.1" => [ "Mandriva Linux", "2006.1" ],
  69.   "yoper-2.2"       => [ "Yoper Linux", "2.2" ],
  70.   "blackpanther-4.0" => [ "Black Panther OS", "4.0", "" ],
  71.   "conectiva-9"     => [ "Conectiva Linux", "9", "" ],
  72.   "conectiva-10"    => [ "Conectiva Linux", "10", "" ],
  73.   "suse-9.0"        => [ "SuSE Linux", "9.0", "" ],
  74.   "suse-9.1"        => [ "SuSE Linux", "9.1", "" ],
  75.   "slackware-9.1.0" => [ "Slackware", "9.1.0", "" ],
  76.   "slackware-10.0.0" => [ "Slackware", "10.0.0", "" ],
  77.   "slackware-10.1.0" => [ "Slackware", "10.1.0", "" ],
  78.   "slackware-10.2.0" => [ "Slackware", "10.2.0", "" ],
  79.   "slackware-11.0.0" => [ "Slackware", "11.0.0", "" ],
  80.   "slackware-12.0.0" => [ "Slackware", "12.0.0", "" ],
  81.   "bluewhite64-12.0.0" => [ "Bluewhite64", "12.0.0", "" ],
  82.   "freebsd-4"       => [ "FreeBSD", "4", "" ],
  83.   "freebsd-5"       => [ "FreeBSD", "5", "" ],
  84.   "freebsd-6"       => [ "FreeBSD", "6", "" ],
  85.   "freebsd-7"       => [ "FreeBSD", "7", "" ],
  86.   "gentoo"          => [ "Gentoo Linux", "", "" ],
  87.   "vlos-1.2"        => [ "Vida Linux OS", "1.2" ],
  88.   "archlinux"       => [ "Arch Linux", "", "" ],
  89.   "pld-1.0"         => [ "PLD", "1.0", "Ra" ],
  90.   "pld-1.1"         => [ "PLD", "1.1", "Ra" ],
  91.   "pld-1.99"        => [ "PLD", "1.99", "Ac-pre" ],
  92.   "pld-2.99"        => [ "PLD", "1.99", "Th-pre" ],
  93.   "vine-3.0"        => [ "Vine Linux", "3.0", "" ],
  94.   "vine-3.1"        => [ "Vine Linux", "3.1", "" ],
  95.   "fedora-1"        => [ "Fedora Core", "1", "Yarrow" ],
  96.   "fedora-2"        => [ "Fedora Core", "2", "Tettnang" ],
  97.   "fedora-3"        => [ "Fedora Core", "3", "Heidelberg" ],
  98.   "fedora-4"        => [ "Fedora Core", "4", "Stentz" ],
  99.   "rpath"           => [ "rPath Linux" ],
  100.   "ark"             => [ "Ark Linux" ],
  101.   "solaris-2.11"    => [ "Solaris / OpenSolaris", "2.11", "Nevada" ],
  102.   "nexenta-1.0"     => [ "Nexenta GNU/Solaris", "1.0", "Ellate" ],
  103.   "yellowdog-4.1"   => [ "Yellow Dog Linux", "4.1", "Sagitta" ],
  104.   "guadalinex-v4"   => [ "Guadalinex", "v4", "Toro" ],
  105. };
  106.  
  107. sub get_platform_info
  108. {
  109.   return $PLATFORM_INFO;
  110. }
  111.  
  112. sub ensure_distro_map
  113. {
  114.   my ($distro) = @_;
  115.  
  116.   # This is a distro metamap, if one distro
  117.   # behaves *exactly* like another, just specify it here
  118.   my %metamap =
  119.     (
  120.      "blackpanther-4.0" => "mandrake-9.0",
  121.      "conectiva-10"     => "conectiva-9",
  122.      "debian-3.1"       => "debian-3.0",
  123.      "mandrake-7.1"     => "redhat-6.2",
  124.      "mandrake-7.2"     => "redhat-6.2",
  125.      "mandrake-9.1"     => "mandrake-9.0",
  126.      "mandrake-9.2"     => "mandrake-9.0",
  127.      "mandrake-10.0"    => "mandrake-9.0",
  128.      "mandrake-10.1"    => "mandrake-9.0",
  129.      "mandrake-10.2"    => "mandrake-9.0",
  130.      "mandriva-2006.0"  => "mandrake-9.0",
  131.      "mandriva-2006.1"  => "mandrake-9.0",
  132.      "fedora-1"         => "redhat-7.2",
  133.      "fedora-2"         => "redhat-7.2",
  134.      "fedora-3"         => "redhat-7.2",
  135.      "fedora-4"         => "redhat-7.2",
  136.      "fedora-5"         => "redhat-7.2",
  137.      "freebsd-6"        => "freebsd-5",
  138.      "freebsd-7"        => "freebsd-5",
  139.      "openna-1.0"       => "redhat-6.2",
  140.      "pld-1.1"          => "pld-1.0",
  141.      "pld-1.99"         => "pld-1.0",
  142.      "pld-2.99"         => "pld-1.0",
  143.      "redhat-9"         => "redhat-8.0",
  144.      "rpath"            => "redhat-7.2",
  145.      "yellowdog-4.1"    => "redhat-7.2",
  146.      "slackware-10.0.0" => "slackware-9.1.0",
  147.      "slackware-10.1.0" => "slackware-9.1.0",
  148.      "slackware-10.2.0" => "slackware-9.1.0",
  149.      "slackware-11.0.0" => "slackware-9.1.0",
  150.      "slackware-12.0.0" => "slackware-9.1.0",
  151.      "bluewhite64-12.0.0" => "slackware-9.1.0",
  152.      "suse-9.1"         => "suse-9.0",
  153.      "ubuntu-5.04"      => "debian-3.0",
  154.      "ubuntu-5.10"      => "debian-3.0",
  155.      "ubuntu-6.06"      => "debian-3.0",
  156.      "ubuntu-6.10"      => "debian-3.0",
  157.      "ubuntu-7.10"      => "ubuntu-7.04",
  158.      "ubuntu-8.04"      => "ubuntu-7.04",
  159.      "vine-3.1"         => "vine-3.0",
  160.      "vlos-1.2"         => "gentoo",
  161.      "nexenta-1.0"      => "solaris-2.11",
  162.      "guadalinex-v4"    => "debian-3.0",
  163.      );
  164.  
  165.   return $metamap{$distro} if ($metamap{$distro});
  166.   return $distro;
  167. }
  168.   
  169. sub check_lsb
  170. {
  171.   my ($ver, $dist, %distmap);
  172.  
  173.   %distmap = {
  174.     "gnu_solaris" => "nexenta"
  175.   };
  176.  
  177.   $dist = lc (&Utils::Parse::get_sh ("/etc/lsb-release", "DISTRIB_ID"));
  178.   $ver = lc (&Utils::Parse::get_sh ("/etc/lsb-release", "DISTRIB_RELEASE"));
  179.  
  180.   $dist = $$distmap{$dist} if exists $$distmap{$dist};
  181.  
  182.   return -1 if ($dist eq "") || ($ver eq "");
  183.   return "$dist-$ver";
  184. }
  185.  
  186. sub check_yoper
  187. {
  188.    open YOPER, "$gst_prefix/etc/yoper-release" or return -1;
  189.    while (<YOPER>)
  190.    {
  191.      $ver = $_;
  192.      chomp ($ver);
  193.      if ($ver =~ m/Yoper (\S+)/)
  194.      {
  195.        close YOPER;
  196.        # find the first digit of our release
  197.        $mystring= ~m/(\d)/;
  198.        #store it in $fdigit
  199.        $fdigit= $1;
  200.        # the end of the release is marked with -2 so find the -
  201.        $end = index($ver,"-");
  202.        $start = index($ver,$fdigit);
  203.        # extract the substring into $newver
  204.        $newver= substr($ver,$start,$end-$start);
  205.        print $newver;
  206.        return "yoper-$newver";
  207.      }
  208.    }
  209.    close YOPER;
  210.    return -1;
  211. }
  212.  
  213. sub check_rpath
  214. {
  215.   open RPATH, "$gst_prefix/etc/distro-release" or return -1;
  216.  
  217.   while (<RPATH>)
  218.   {
  219.     $ver = $_;
  220.     chomp ($ver);
  221.  
  222.     if ($ver =~ /^rPath Linux/)
  223.     {
  224.       close RPATH;
  225.       return "rpath";
  226.     }
  227.     elsif ($ver =~ /Foresight/)
  228.     {
  229.       close RPATH;
  230.       return "rpath";
  231.     }
  232.   }
  233.  
  234.   close RPATH;
  235.   return -1;
  236. }
  237.  
  238. sub check_ark
  239. {
  240.   open ARK, "$gst_prefix/etc/ark-release" or return -1;
  241.   while (<ARK>)
  242.   {
  243.     $ver = $_;
  244.     chomp ($ver);
  245.  
  246.     if ($ver =~ /^Ark Linux/)
  247.     {
  248.       close ARK;
  249.       return "ark";
  250.     }
  251.   }
  252.  
  253.   close ARK;
  254.   return -1;
  255. }
  256.  
  257. sub check_freebsd
  258. {
  259.   my ($sysctl_cmd, @output);
  260.  
  261.   $sysctl_cmd = &Utils::File::locate_tool ("sysctl");
  262.   @output = (readpipe("$sysctl_cmd -n kern.version"));
  263.   foreach (@output)
  264.   {
  265.     chomp;
  266.     if (/^FreeBSD\s([0-9]+)\.\S+.*/)
  267.     {
  268.       return "freebsd-$1";
  269.     }
  270.   }
  271.   return -1;
  272. }
  273.  
  274. sub check_solaris
  275. {
  276.   my ($fd, $dist);
  277.  
  278.   $fd = &Utils::File::run_pipe_read ("uname -r");
  279.   return -1 if $fd eq undef;
  280.   chomp ($dist = <$fd>);
  281.   &Utils::File::close_file ($fd);
  282.  
  283.   if ($dist =~ /^5\.(\d+)/) { return "solaris-2.$1" }
  284.   return -1;
  285. }
  286.  
  287. sub check_distro_file
  288. {
  289.   my ($file, $dist, $re, $map) = @_;
  290.   my ($ver);
  291.   local *FILE;
  292.  
  293.   open FILE, "$gst_prefix/$file" or return -1;
  294.  
  295.   while (<FILE>)
  296.   {
  297.     chomp;
  298.  
  299.     if ($_ =~ "$re")
  300.     {
  301.       $ver = $1;
  302.       $ver = $$map{$ver} if ($$map{$ver});
  303.  
  304.       close FILE;
  305.       return "$dist-$ver";
  306.     }
  307.   }
  308.  
  309.   close FILE;
  310.   return -1;
  311. }
  312.  
  313. sub check_file_exists
  314. {
  315.   my ($file, $distro) = @_;
  316.  
  317.   return $distro if stat ("$gst_prefix/$file");
  318.   return -1;
  319. }
  320.  
  321. sub get_system
  322. {
  323.   # get the output of 'uname -s', it returns the system we are running
  324.   $Utils::Backend::tool{"system"} = &Utils::File::run_backtick ("uname -s");
  325.   chomp ($Utils::Backend::tool{"system"});
  326. }
  327.  
  328. sub set_platform
  329. {
  330.   my ($platform) = @_;
  331.   my ($p);
  332.  
  333.   if (&ensure_platform ($platform))
  334.   {
  335.     $platform = &ensure_distro_map ($platform);
  336.  
  337.     $Utils::Backend::tool{"platform"} = $gst_dist = $platform;
  338.     &Utils::Report::do_report ("platform_success", $platform);
  339.     &Utils::Report::end ();
  340.     return;
  341.   }
  342.  
  343.   &set_platform_unsupported ($object);
  344.   &Utils::Report::do_report ("platform_unsup", $platform);
  345.   &Utils::Report::end ();
  346. }
  347.  
  348. sub set_platform_unsupported
  349. {
  350.   my ($object) = @_;
  351.  
  352.   $Utils::Backend::tool{"platform"} = $gst_dist = undef;
  353.   #&Net::DBus::Object::emit_signal ($object, "noPlatformDetected");
  354. }
  355.  
  356. sub ensure_platform
  357. {
  358.   my ($platform) = @_;
  359.  
  360.   return $platform if ($$PLATFORM_INFO{$platform} ne undef);
  361.   return undef;
  362. }
  363.  
  364. sub guess
  365. {
  366.   my ($object) = @_;
  367.   my ($distro, $func);
  368.   my ($checks, $check);
  369.  
  370.   my %platform_checks = (
  371.     "Linux"   => [[ \&check_lsb ],
  372.                   [ \&check_distro_file, "/etc/debian_version", "debian", "(.*)", { "testing/unstable" => "sarge", "lenny/sid" => "testing" } ],
  373.                   [ \&check_distro_file, "/etc/SuSE-release", "suse", "VERSION\\s*=\\s*(\\S+)" ],
  374.                   [ \&check_distro_file, "/etc/blackPanther-release", "blackpanther", "^Linux Black Panther release (\\S+)" ],
  375.                   [ \&check_distro_file, "/etc/blackPanther-release", "blackpanther", "^Black Panther ( L|l)inux release ([\\d\\.]+)" ],
  376.                   [ \&check_distro_file, "/etc/vine-release", "vine", "^Vine Linux (\\S+)" ],
  377.                   [ \&check_distro_file, "/etc/fedora-release", "fedora", "^Fedora Core release (\\S+)" ],
  378.                   [ \&check_distro_file, "/etc/mandrake-release", "mandrake", "^Linux Mandrake release (\\S+)" ],
  379.                   [ \&check_distro_file, "/etc/mandrake-release", "mandrake", "^Mandrake( L|l)inux release (\\S+)" ],
  380.                   [ \&check_distro_file, "/etc/mandriva-release", "mandriva", "^Linux Mandriva release (\\S+)" ],
  381.                   [ \&check_distro_file, "/etc/mandriva-release", "mandriva", "^Mandriva( L|l)inux release (\\S+)" ],
  382.                   [ \&check_distro_file, "/etc/conectiva-release", "conectiva", "^Conectiva Linux (\\S+)" ],
  383.                   [ \&check_distro_file, "/etc/redhat-release", "redhat", "^Red Hat Linux.*\\s+(\\S+)" ],
  384.                   [ \&check_distro_file, "/etc/openna-release", "openna", "^OpenNA (\\S+)" ],
  385.                   [ \&check_distro_file, "/etc/slackware-version", "slackware", "^Slackware (\\S+)" ],
  386.                   [ \&check_distro_file, "/etc/bluewhite64-version", "bluewhite64", "^Bluewhite64 (\\S+)" ],
  387.                   [ \&check_distro_file, "/etc/vlos-release", "vlos", "^VLOS.*\\s+(\\S+)" ],
  388.                   [ \&check_file_exists, "/usr/portage", "gentoo" ],
  389.                   [ \&check_distro_file, "/etc/pld-release", "pld", "^(\\S+) PLD Linux" ],
  390.                   [ \&check_rpath ],
  391.                   [ \&check_file_exists, "/etc/arch-release", "archlinux" ],
  392.                   [ \&check_ark ],
  393.                   [ \&check_yoper ],
  394.                   [ \&check_distro_file, "/etc/yellowdog-release", "yellowdog", "^Yellow Dog Linux release (\\S+)" ],
  395.                  ],
  396.     "FreeBSD" => [[ \&check_freebsd ]],
  397.     "SunOS"   => [[ \&check_solaris ]]
  398.   );
  399.  
  400.   $distro = $Utils::Backend::tool{"system"};
  401.   $checks = $platform_checks{$distro};
  402.  
  403.   foreach $check (@$checks) {
  404.     $func = shift (@$check);
  405.     $dist = &$func (@$check);
  406.  
  407.     if ($dist != -1 && &ensure_platform ($dist))
  408.     {
  409.       $dist = &ensure_distro_map ($dist);
  410.       $Utils::Backend::tool{"platform"} = $gst_dist = $dist;
  411.       &Utils::Report::do_report ("platform_success", $dist);
  412.       return;
  413.     }
  414.   }
  415.  
  416.   &set_platform_unsupported ($tool, $object);
  417.   &Utils::Report::do_report ("platform_unsup", $platform);
  418.   &Utils::Report::end ();
  419. }
  420.  
  421. sub get_cached_platform
  422. {
  423.   my ($file, $platform);
  424.  
  425.   $file = &Utils::File::get_base_path() . "/detected-platform";
  426.   $platform = &Utils::Parse::get_first_line ($file);
  427.  
  428.   if (&ensure_platform ($platform))
  429.   {
  430.     $Utils::Backend::tool{"platform"} = $gst_dist = $platform;
  431.     &Utils::Report::do_report ("platform_success", $platform);
  432.     return 1;
  433.   }
  434.  
  435.   return 0;
  436. }
  437.  
  438. sub cache_platform
  439. {
  440.   my ($file, $platform);
  441.  
  442.   $file = &Utils::File::get_base_path() . "/detected-platform";
  443.   &Utils::Replace::set_first_line ($file, $gst_dist);
  444. }
  445.  
  446. sub init
  447. {
  448.   &get_system ();
  449.  
  450.   # FIXME: need to figure out whether the underlying platform has
  451.   # been updated or changed, until now it's safer to ignore the cache,
  452.   # this function is called just once in all the executable lifecicle,
  453.   # so I don't expect any noticeable performance decrease.
  454.   #if (!&get_cached_platform ())
  455.   #{
  456.     &guess ($self) if !$Utils::Backend::tool{"platform"};
  457.     &cache_platform ();
  458.   #}
  459. }
  460.  
  461. 1;
  462.