Info.pm 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. # Copyright © 2007-2010 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. use overload
  23. '@{}' => sub { return [ $_[0]->{source}, @{$_[0]->{packages}} ] };
  24. =head1 NAME
  25. Dpkg::Control::Info - parse files like debian/control
  26. =head1 DESCRIPTION
  27. It provides an object to access data of files that follow the same
  28. syntax than debian/control.
  29. =head1 FUNCTIONS
  30. =over 4
  31. =item $c = Dpkg::Control::Info->new($file)
  32. Create a new Dpkg::Control::Info object for $file. If $file is omitted, it
  33. loads debian/control. If file is "-", it parses the standard input.
  34. =cut
  35. sub new {
  36. my ($this, $arg) = @_;
  37. my $class = ref($this) || $this;
  38. my $self = {
  39. 'source' => undef,
  40. 'packages' => [],
  41. };
  42. bless $self, $class;
  43. if ($arg) {
  44. if ($arg eq "-") {
  45. $self->parse(\*STDIN, _g("<standard input>"));
  46. } else {
  47. $self->load($arg);
  48. }
  49. } else {
  50. $self->load("debian/control");
  51. }
  52. return $self;
  53. }
  54. =item $c->reset()
  55. Resets what got read.
  56. =cut
  57. sub reset {
  58. my $self = shift;
  59. $self->{source} = undef;
  60. $self->{packages} = [];
  61. }
  62. =item $c->load($file)
  63. Load the content of $file. Exits in case of errors.
  64. =item $c->parse($fh, $description)
  65. Parse a control file from the given filehandle. Exits in case of errors.
  66. $description is used to describe the filehandle, ideally it's a filename
  67. or a description of where the data comes from. It's used in error
  68. messages.
  69. =cut
  70. sub parse {
  71. my ($self, $fh, $desc) = @_;
  72. $self->reset();
  73. my $cdata = Dpkg::Control->new(type => CTRL_INFO_SRC);
  74. return if not $cdata->parse($fh, $desc);
  75. $self->{source} = $cdata;
  76. unless (exists $cdata->{Source}) {
  77. syntaxerr($desc, _g("first block lacks a source field"));
  78. }
  79. while (1) {
  80. $cdata = Dpkg::Control->new(type => CTRL_INFO_PKG);
  81. last if not $cdata->parse($fh, $desc);
  82. push @{$self->{packages}}, $cdata;
  83. unless (exists $cdata->{Package}) {
  84. syntaxerr($desc, _g("block lacks a package field"));
  85. }
  86. }
  87. }
  88. =item $c->[0]
  89. =item $c->get_source()
  90. Returns a Dpkg::Control object containing the fields concerning the
  91. source package.
  92. =cut
  93. sub get_source {
  94. my $self = shift;
  95. return $self->{source};
  96. }
  97. =item $c->get_pkg_by_idx($idx)
  98. Returns a Dpkg::Control object containing the fields concerning the binary
  99. package numbered $idx (starting at 1).
  100. =cut
  101. sub get_pkg_by_idx {
  102. my ($self, $idx) = @_;
  103. return $self->{packages}[--$idx];
  104. }
  105. =item $c->get_pkg_by_name($name)
  106. Returns a Dpkg::Control object containing the fields concerning the binary
  107. package named $name.
  108. =cut
  109. sub get_pkg_by_name {
  110. my ($self, $name) = @_;
  111. foreach my $pkg (@{$self->{packages}}) {
  112. return $pkg if ($pkg->{Package} eq $name);
  113. }
  114. return undef;
  115. }
  116. =item $c->get_packages()
  117. Returns a list containing the Dpkg::Control objects for all binary packages.
  118. =cut
  119. sub get_packages {
  120. my $self = shift;
  121. return @{$self->{packages}};
  122. }
  123. =item $c->output($filehandle)
  124. Dump the content into a filehandle.
  125. =cut
  126. sub output {
  127. my ($self, $fh) = @_;
  128. my $str;
  129. $str .= $self->{source}->output($fh);
  130. foreach my $pkg (@{$self->{packages}}) {
  131. print $fh "\n";
  132. $str .= "\n" . $pkg->output($fh);
  133. }
  134. return $str;
  135. }
  136. =item "$c"
  137. Return a string representation of the content.
  138. =item @{$c}
  139. Return a list of Dpkg::Control objects, the first one is corresponding to
  140. source information and the following ones are the binary packages
  141. information.
  142. =back
  143. =head1 AUTHOR
  144. Raphael Hertzog <hertzog@debian.org>.
  145. =cut
  146. 1;