Info.pm 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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.00';
  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 FUNCTIONS
  33. =over 4
  34. =item $c = Dpkg::Control::Info->new($file)
  35. Create a new Dpkg::Control::Info object for $file. If $file is omitted, it
  36. loads F<debian/control>. If file is "-", it parses the standard input.
  37. =cut
  38. sub new {
  39. my ($this, $arg) = @_;
  40. my $class = ref($this) || $this;
  41. my $self = {
  42. source => undef,
  43. packages => [],
  44. };
  45. bless $self, $class;
  46. if ($arg) {
  47. $self->load($arg);
  48. } else {
  49. $self->load('debian/control');
  50. }
  51. return $self;
  52. }
  53. =item $c->reset()
  54. Resets what got read.
  55. =cut
  56. sub reset {
  57. my $self = shift;
  58. $self->{source} = undef;
  59. $self->{packages} = [];
  60. }
  61. =item $c->load($file)
  62. Load the content of $file. Exits in case of errors. If file is "-", it
  63. loads from the standard input.
  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. $cdata->parse_error($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. $cdata->parse_error($desc, g_("block lacks the '%s' field"),
  85. 'Package');
  86. }
  87. unless (exists $cdata->{Architecture}) {
  88. $cdata->parse_error($desc, g_("block lacks the '%s' field"),
  89. 'Architecture');
  90. }
  91. }
  92. }
  93. =item $c->[0]
  94. =item $c->get_source()
  95. Returns a Dpkg::Control object containing the fields concerning the
  96. source package.
  97. =cut
  98. sub get_source {
  99. my $self = shift;
  100. return $self->{source};
  101. }
  102. =item $c->get_pkg_by_idx($idx)
  103. Returns a Dpkg::Control object containing the fields concerning the binary
  104. package numbered $idx (starting at 1).
  105. =cut
  106. sub get_pkg_by_idx {
  107. my ($self, $idx) = @_;
  108. return $self->{packages}[--$idx];
  109. }
  110. =item $c->get_pkg_by_name($name)
  111. Returns a Dpkg::Control object containing the fields concerning the binary
  112. package named $name.
  113. =cut
  114. sub get_pkg_by_name {
  115. my ($self, $name) = @_;
  116. foreach my $pkg (@{$self->{packages}}) {
  117. return $pkg if ($pkg->{Package} eq $name);
  118. }
  119. return;
  120. }
  121. =item $c->get_packages()
  122. Returns a list containing the Dpkg::Control objects for all binary packages.
  123. =cut
  124. sub get_packages {
  125. my $self = shift;
  126. return @{$self->{packages}};
  127. }
  128. =item $c->output($filehandle)
  129. Dump the content into a filehandle.
  130. =cut
  131. sub output {
  132. my ($self, $fh) = @_;
  133. my $str;
  134. $str .= $self->{source}->output($fh);
  135. foreach my $pkg (@{$self->{packages}}) {
  136. print { $fh } "\n";
  137. $str .= "\n" . $pkg->output($fh);
  138. }
  139. return $str;
  140. }
  141. =item "$c"
  142. Return a string representation of the content.
  143. =item @{$c}
  144. Return a list of Dpkg::Control objects, the first one is corresponding to
  145. source information and the following ones are the binary packages
  146. information.
  147. =back
  148. =head1 CHANGES
  149. =head2 Version 1.00
  150. Mark the module as public.
  151. =head1 AUTHOR
  152. Raphaël Hertzog <hertzog@debian.org>.
  153. =cut
  154. 1;