Advertisement
Guest User

Untitled

a guest
May 21st, 2011
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.89 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use Switch;
  4.  
  5. sub trim;
  6. sub get_value;
  7. sub get_var;
  8. sub get_field;
  9.  
  10. # trim($str)
  11. # input:    a string
  12. # output:   the trimmed string (without whitespace)
  13. sub trim {
  14.     $ans = $_[0];
  15.     $ans =~ s/^\s+//; #remove leading spaces
  16.     $ans =~ s/\s+$//; #remove trailing spaces
  17.     return $ans;
  18. }
  19.  
  20. # get_value($str)
  21. # input:    a XML tag
  22. # output:   the tag name
  23. sub get_value {
  24.     $start = 1 + index($_[0], ">");
  25.     $end = rindex($_[0], "<");
  26.     return substr($_[0], $start, ($end - $start));
  27. }
  28.  
  29. # get_var($str, $var_name)
  30. # input:    a XML tag and a variable name
  31. # output:   the variable value
  32. sub get_var {
  33.     ($str, $var_name) = @_;
  34.    
  35.     # trim everything until the requested variable's value
  36.     $start = index($str, " ".$var_name."=");
  37.     $ans = substr($str, 3 + length($var_name) + $start);
  38.  
  39.     # trim everything after the value
  40.     $start = index($ans, "\"");
  41.     $ans = substr($ans, 0, $start);
  42.        
  43.     return $ans
  44. }
  45.  
  46. # get_field($str)
  47. # input:    a XML tag
  48. # output:   the tag value
  49. sub get_field {
  50.     $ans =  substr($_[0], 1, (index($_[0], ">") - 1));
  51.     if (index($ans, " ") != -1) {
  52.         $ans =  substr($_[0], 1, (index($_[0], " ") - 1));
  53.     }
  54.     return $ans;
  55. }
  56.  
  57. if ((! -e $ARGV[0]) || (-e $ARGV[1])) {
  58.     print("Usage: rpm2ppm [FILE] [OUTPUT] [DISTRO] [VERSION]\n");
  59.     exit(1);
  60. }
  61.  
  62. open(rpm_package_list, $ARGV[0]);
  63. open(ppm_package_list, ">>$ARGV[1]");
  64.  
  65. while ($line = <rpm_package_list>)
  66. {
  67.     $line = trim($line);
  68.     $field = get_field($line);
  69.  
  70.     switch ($field) {
  71.        
  72.         case "name" {
  73.             $package_name = get_value($line);
  74.             print $package_name." ";  
  75.         }
  76.      
  77.         case "version" {
  78.             $package_version = get_var($line, "ver");
  79.             $package_revision = get_var($line, "rel");
  80.         }
  81.        
  82.         case "summary" {
  83.             $package_description = get_value($line);
  84.         }
  85.        
  86.         case "size" {
  87.             $package_size = get_var($line, "installed")."K";
  88.         }
  89.        
  90.         case "location" {
  91.             $package_path = get_var($line, "href");
  92.             $pos = rindex($package_path, "/");
  93.             $package_file_name = substr($package_path, (1 + $pos));
  94.             $package_path = substr($package_path, 0, $pos);
  95.         }
  96.    
  97.         case "rpm:provides" {
  98.             $package_provides = "";
  99.             $provides = 1;
  100.         }
  101.                        
  102.         case "rpm:requires" {
  103.             $package_dependencies = "";
  104.             $requires = 1;
  105.         }
  106.        
  107.         case "rpm:entry" {
  108.             $entry_name = get_var($line, "name");
  109.             # filter shared libraries, architecture names and files
  110.             if ((index($entry_name, ".so") == -1) && (index($entry_name, "(") == -1) && (index($entry_name, "/") == -1)) {
  111.                 # process packages provided by the package
  112.                 if ($provides == 1) {                  
  113.                     if ($package_provides eq "") {
  114.                         $package_provides = "|".$entry_name."|";
  115.                     } else {
  116.                         $package_provides = $package_provides."|".$entry_name."|";
  117.                     }          
  118.                 } else {
  119.                     # process dependencies
  120.                     if ($requires == 1) {
  121.                         # get rid of the package's dependencies provided by the package itself (e.g mktemp and coreutils)
  122.                         if (($entry_name ne $package_name) && (index($package_provides, "|".$entry_name."|")) == -1) {
  123.                             if ($package_dependencies eq "") {
  124.                                 $package_dependencies = "+".$entry_name;
  125.                             } else {
  126.                                 # make sure dependencies aren't added twice
  127.                                 if ((index($package_dependencies, ",+".$entry_name) == -1) && (index($package_dependencies, "+".$entry_name.",") == -1)) {
  128.                                     $package_dependencies = $package_dependencies.",+".$entry_name;
  129.                                 }
  130.                             }
  131.                         }
  132.                     }
  133.                 }
  134.             }
  135.         }
  136.    
  137.         case "/rpm:provides" {
  138.             $provides = 0;
  139.         }
  140.  
  141.         case "/rpm:requires" {
  142.             $requires = 0;
  143.         }
  144.            
  145.         # the last tag of each entry
  146.         case "/package" {
  147.             print ppm_package_list $package_name."-".$package_version."|".$package_name."|".$package_version."|".$package_revision."|BuildingBlock|".$package_size."|".$package_path."|".$package_file_name."|".$package_dependencies."|".$package_description."|".$ARGV[2]."|".$ARGV[3]."|\n";
  148.         }
  149.        
  150.     }
  151. }
  152.  
  153. print "\n";
  154. close(rpm_package_list);
  155. close(ppm_package_list);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement