Control.pm 3.9 KB

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