Info.pm 4.2 KB

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