Info.pm 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. # Copyright © 2007 Raphaël Hertzog <hertzog@debian.org>
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 2 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. package Dpkg::Control::Info;
  16. use strict;
  17. use warnings;
  18. use Dpkg::Control;
  19. use Dpkg::ErrorHandling;
  20. use Dpkg::Gettext;
  21. use base qw(Dpkg::Interface::Storable);
  22. =head1 NAME
  23. Dpkg::Control::Info - parse files like debian/control
  24. =head1 DESCRIPTION
  25. It provides an object to access data of files that follow the same
  26. syntax than debian/control.
  27. =head1 FUNCTIONS
  28. =over 4
  29. =item $c = Dpkg::Control::Info->new($file)
  30. Create a new Dpkg::Control::Info object for $file. If $file is omitted, it
  31. loads debian/control. If file is "-", it parses the standard input.
  32. =cut
  33. sub new {
  34. my ($this, $arg) = @_;
  35. my $class = ref($this) || $this;
  36. my $self = {
  37. 'source' => undef,
  38. 'packages' => [],
  39. };
  40. bless $self, $class;
  41. if ($arg) {
  42. if ($arg eq "-") {
  43. $self->parse(\*STDIN, _g("<standard input>"));
  44. } else {
  45. $self->load($arg);
  46. }
  47. } else {
  48. $self->load("debian/control");
  49. }
  50. return $self;
  51. }
  52. =item $c->reset()
  53. Resets what got read.
  54. =cut
  55. sub reset {
  56. my $self = shift;
  57. $self->{source} = undef;
  58. $self->{packages} = [];
  59. }
  60. =item $c->load($file)
  61. Load the content of $file. Exits in case of errors.
  62. =item $c->parse($fh, $description)
  63. Parse a control file from the given filehandle. Exits in case of errors.
  64. $description is used to describe the filehandle, ideally it's a filename
  65. or a description of where the data comes from. It's used in error
  66. messages.
  67. =cut
  68. sub parse {
  69. my ($self, $fh, $desc) = @_;
  70. $self->reset();
  71. my $cdata = Dpkg::Control->new(type => CTRL_INFO_SRC);
  72. return if not $cdata->parse($fh, $desc);
  73. $self->{source} = $cdata;
  74. unless (exists $cdata->{Source}) {
  75. syntaxerr($desc, _g("first block lacks a source field"));
  76. }
  77. while (1) {
  78. $cdata = Dpkg::Control->new(type => CTRL_INFO_PKG);
  79. last if not $cdata->parse($fh, $desc);
  80. push @{$self->{packages}}, $cdata;
  81. unless (exists $cdata->{Package}) {
  82. syntaxerr($desc, _g("block lacks a package field"));
  83. }
  84. }
  85. }
  86. =item $c->get_source()
  87. Returns a Dpkg::Control object containing the fields concerning the
  88. source package.
  89. =cut
  90. sub get_source {
  91. my $self = shift;
  92. return $self->{source};
  93. }
  94. =item $c->get_pkg_by_idx($idx)
  95. Returns a Dpkg::Control object containing the fields concerning the binary
  96. package numbered $idx (starting at 1).
  97. =cut
  98. sub get_pkg_by_idx {
  99. my ($self, $idx) = @_;
  100. return $self->{packages}[--$idx];
  101. }
  102. =item $c->get_pkg_by_name($name)
  103. Returns a Dpkg::Control object containing the fields concerning the binary
  104. package named $name.
  105. =cut
  106. sub get_pkg_by_name {
  107. my ($self, $name) = @_;
  108. foreach my $pkg (@{$self->{packages}}) {
  109. return $pkg if ($pkg->{Package} eq $name);
  110. }
  111. return undef;
  112. }
  113. =item $c->get_packages()
  114. Returns a list containing the Dpkg::Control objects for all binary packages.
  115. =cut
  116. sub get_packages {
  117. my $self = shift;
  118. return @{$self->{packages}};
  119. }
  120. =item $c->output($filehandle)
  121. Dump the content into a filehandle.
  122. =cut
  123. sub output {
  124. my ($self, $fh) = @_;
  125. my $str;
  126. $str .= $self->{source}->output($fh);
  127. foreach my $pkg (@{$self->{packages}}) {
  128. print $fh "\n";
  129. $str .= "\n" . $pkg->output($fh);
  130. }
  131. return $str;
  132. }
  133. =back
  134. =head1 AUTHOR
  135. Raphael Hertzog <hertzog@debian.org>.
  136. =cut
  137. 1;