rpm2ppm 3.7 KB

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