Control.pm 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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.
  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. $self->parse($arg);
  40. } else {
  41. $self->parse("debian/control");
  42. }
  43. return $self;
  44. }
  45. =item $c->reset()
  46. Resets what got read.
  47. =cut
  48. sub reset {
  49. my $self = shift;
  50. $self->{source} = undef;
  51. $self->{packages} = [];
  52. }
  53. =item $c->parse($file)
  54. Parse the content of $file. Exits in case of errors.
  55. =cut
  56. sub parse {
  57. my ($self, $file) = @_;
  58. $self->reset();
  59. # Parse
  60. open(CDATA, "<", $file) || syserr(_g("cannot read %s"), $file);
  61. my $cdata = parsecdata(\*CDATA, $file);
  62. return if not defined $cdata;
  63. $self->{source} = $cdata;
  64. unless (exists $cdata->{Source}) {
  65. syntaxerr($file, _g("first block lacks a source field"));
  66. }
  67. while (1) {
  68. $cdata = parsecdata(\*CDATA, $file);
  69. last if not defined $cdata;
  70. push @{$self->{packages}}, $cdata;
  71. unless (exists $cdata->{Package}) {
  72. syntaxerr($file, _g("block lacks a package field"));
  73. }
  74. }
  75. close(CDATA);
  76. }
  77. =item $c->get_source()
  78. Returns a reference to a hash containing the fields concerning the
  79. source package. The hash is tied to Dpkg::Fields::Object.
  80. =cut
  81. sub get_source {
  82. my $self = shift;
  83. return $self->{source};
  84. }
  85. =item $c->get_pkg_by_idx($idx)
  86. Returns a reference to a hash containing the fields concerning the binary
  87. package numbered $idx (starting at 1). The hash is tied to
  88. Dpkg::Fields::Object.
  89. =cut
  90. sub get_pkg_by_idx {
  91. my ($self, $idx) = @_;
  92. return $self->{packages}[--$idx];
  93. }
  94. =item $c->get_pkg_by_name($name)
  95. Returns a reference to a hash containing the fields concerning the binary
  96. package named $name. The hash is tied to Dpkg::Fields::Object.
  97. =cut
  98. sub get_pkg_by_name {
  99. my ($self, $name) = @_;
  100. foreach my $pkg (@{$self->{packages}}) {
  101. return $pkg if ($pkg->{Package} eq $name);
  102. }
  103. return undef;
  104. }
  105. =item $c->get_packages()
  106. Returns a list containing the hashes for all binary packages.
  107. =cut
  108. sub get_packages {
  109. my $self = shift;
  110. return @{$self->{packages}};
  111. }
  112. =item $c->dump($filehandle)
  113. Dump the content into a filehandle.
  114. =cut
  115. sub dump {
  116. my ($self, $fh) = @_;
  117. tied(%{$self->{source}})->dump($fh);
  118. foreach my $pkg (@{$self->{packages}}) {
  119. print $fh "\n";
  120. tied(%{$pkg})->dump($fh);
  121. }
  122. }
  123. =back
  124. =head1 AUTHOR
  125. Raphael Hertzog <hertzog@debian.org>.
  126. =cut
  127. 1;