Info.pm 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. # Copyright © 2007-2010 Raphaël Hertzog <hertzog@debian.org>
  2. # Copyright © 2009, 2012-2015 Guillem Jover <guillem@debian.org>
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package Dpkg::Control::Info;
  17. use strict;
  18. use warnings;
  19. our $VERSION = '1.01';
  20. use Dpkg::Control;
  21. use Dpkg::ErrorHandling;
  22. use Dpkg::Gettext;
  23. use parent qw(Dpkg::Interface::Storable);
  24. use overload
  25. '@{}' => sub { return [ $_[0]->{source}, @{$_[0]->{packages}} ] };
  26. =encoding utf8
  27. =head1 NAME
  28. Dpkg::Control::Info - parse files like debian/control
  29. =head1 DESCRIPTION
  30. It provides an object to access data of files that follow the same
  31. syntax as F<debian/control>.
  32. =head1 METHODS
  33. =over 4
  34. =item $c = Dpkg::Control::Info->new(%opts)
  35. Create a new Dpkg::Control::Info object. Loads the file from the filename
  36. option, if no option is specified filename defaults to F<debian/control>.
  37. If a scalar is passed instead, it will be used as the filename. If filename
  38. is "-", it parses the standard input. If filename is undef no loading will
  39. be performed.
  40. =cut
  41. sub new {
  42. my ($this, @args) = @_;
  43. my $class = ref($this) || $this;
  44. my $self = {
  45. source => undef,
  46. packages => [],
  47. };
  48. bless $self, $class;
  49. my %opts;
  50. if (scalar @args == 0) {
  51. $opts{filename} = 'debian/control';
  52. } elsif (scalar @args == 1) {
  53. $opts{filename} = $args[0];
  54. } else {
  55. %opts = @args;
  56. }
  57. $self->load($opts{filename}) if $opts{filename};
  58. return $self;
  59. }
  60. =item $c->reset()
  61. Resets what got read.
  62. =cut
  63. sub reset {
  64. my $self = shift;
  65. $self->{source} = undef;
  66. $self->{packages} = [];
  67. }
  68. =item $c->load($file)
  69. Load the content of $file. Exits in case of errors. If file is "-", it
  70. loads from the standard input.
  71. =item $c->parse($fh, $description)
  72. Parse a control file from the given filehandle. Exits in case of errors.
  73. $description is used to describe the filehandle, ideally it's a filename
  74. or a description of where the data comes from. It is used in error messages.
  75. The data in the object is reset before parsing new control files.
  76. =cut
  77. sub parse {
  78. my ($self, $fh, $desc) = @_;
  79. $self->reset();
  80. my $cdata = Dpkg::Control->new(type => CTRL_INFO_SRC);
  81. return if not $cdata->parse($fh, $desc);
  82. $self->{source} = $cdata;
  83. unless (exists $cdata->{Source}) {
  84. $cdata->parse_error($desc, g_('first block lacks a Source field'));
  85. }
  86. while (1) {
  87. $cdata = Dpkg::Control->new(type => CTRL_INFO_PKG);
  88. last if not $cdata->parse($fh, $desc);
  89. push @{$self->{packages}}, $cdata;
  90. unless (exists $cdata->{Package}) {
  91. $cdata->parse_error($desc, g_("block lacks the '%s' field"),
  92. 'Package');
  93. }
  94. unless (exists $cdata->{Architecture}) {
  95. $cdata->parse_error($desc, g_("block lacks the '%s' field"),
  96. 'Architecture');
  97. }
  98. }
  99. }
  100. =item $c->[0]
  101. =item $c->get_source()
  102. Returns a Dpkg::Control object containing the fields concerning the
  103. source package.
  104. =cut
  105. sub get_source {
  106. my $self = shift;
  107. return $self->{source};
  108. }
  109. =item $c->get_pkg_by_idx($idx)
  110. Returns a Dpkg::Control object containing the fields concerning the binary
  111. package numbered $idx (starting at 1).
  112. =cut
  113. sub get_pkg_by_idx {
  114. my ($self, $idx) = @_;
  115. return $self->{packages}[--$idx];
  116. }
  117. =item $c->get_pkg_by_name($name)
  118. Returns a Dpkg::Control object containing the fields concerning the binary
  119. package named $name.
  120. =cut
  121. sub get_pkg_by_name {
  122. my ($self, $name) = @_;
  123. foreach my $pkg (@{$self->{packages}}) {
  124. return $pkg if ($pkg->{Package} eq $name);
  125. }
  126. return;
  127. }
  128. =item $c->get_packages()
  129. Returns a list containing the Dpkg::Control objects for all binary packages.
  130. =cut
  131. sub get_packages {
  132. my $self = shift;
  133. return @{$self->{packages}};
  134. }
  135. =item $c->output($filehandle)
  136. Dump the content into a filehandle.
  137. =cut
  138. sub output {
  139. my ($self, $fh) = @_;
  140. my $str;
  141. $str .= $self->{source}->output($fh);
  142. foreach my $pkg (@{$self->{packages}}) {
  143. print { $fh } "\n" if defined $fh;
  144. $str .= "\n" . $pkg->output($fh);
  145. }
  146. return $str;
  147. }
  148. =item "$c"
  149. Return a string representation of the content.
  150. =item @{$c}
  151. Return a list of Dpkg::Control objects, the first one is corresponding to
  152. source information and the following ones are the binary packages
  153. information.
  154. =back
  155. =head1 CHANGES
  156. =head2 Version 1.01 (dpkg 1.18.0)
  157. New argument: The $c->new() constructor accepts an %opts argument.
  158. =head2 Version 1.00 (dpkg 1.15.6)
  159. Mark the module as public.
  160. =cut
  161. 1;