Control.pm 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. # Copyright © 2007-2009 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;
  16. use strict;
  17. use warnings;
  18. our $VERSION = "1.00";
  19. use Dpkg::Gettext;
  20. use Dpkg::ErrorHandling;
  21. use Dpkg::Control::Types;
  22. use Dpkg::Control::Hash;
  23. use Dpkg::Control::Fields;
  24. use base qw(Dpkg::Control::Hash Exporter);
  25. our @EXPORT = qw(CTRL_UNKNOWN CTRL_INFO_SRC CTRL_INFO_PKG CTRL_INDEX_SRC
  26. CTRL_INDEX_PKG CTRL_PKG_SRC CTRL_PKG_DEB CTRL_FILE_CHANGES
  27. CTRL_FILE_VENDOR CTRL_FILE_STATUS CTRL_CHANGELOG);
  28. =encoding utf8
  29. =head1 NAME
  30. Dpkg::Control - parse and manipulate official control-like information
  31. =head1 DESCRIPTION
  32. The Dpkg::Control object is a smart version of Dpkg::Control::Hash.
  33. It associates a type to the control information. That type can be
  34. used to know what fields are allowed and in what order they must be
  35. output.
  36. The types are constants that are exported by default. Here's the full
  37. list:
  38. =over 4
  39. =item CTRL_UNKNOWN
  40. This type is the default type, it indicates that the type of control
  41. information is not yet known.
  42. =item CTRL_INFO_SRC
  43. Corresponds to the first block of information in a debian/control file in
  44. a Debian source package.
  45. =item CTRL_INFO_PKG
  46. Corresponds to subsequent blocks of information in a debian/control file
  47. in a Debian source package.
  48. =item CTRL_INDEX_SRC
  49. Corresponds to an entry in a Sources file of a source package
  50. repository.
  51. =item CTRL_INDEX_PKG
  52. Corresponds to an entry in a Packages file of a binary package
  53. repository.
  54. =item CTRL_PKG_SRC
  55. Corresponds to a .dsc file of a Debian source package.
  56. =item CTRL_PKG_DEB
  57. Corresponds to the control file generated by dpkg-gencontrol
  58. (DEBIAN/control) and to the same file inside .deb packages.
  59. =item CTRL_FILE_CHANGES
  60. Corresponds to a .changes file.
  61. =item CTRL_FILE_VENDOR
  62. Corresponds to a vendor file in /etc/dpkg/origins/.
  63. =item CTRL_FILE_STATUS
  64. Corresponds to an entry in dpkg's status file (/var/lib/dpkg/status).
  65. =item CTRL_CHANGELOG
  66. Corresponds to the output of dpkg-parsechangelog.
  67. =back
  68. =head1 FUNCTIONS
  69. All the methods of Dpkg::Control::Hash are available. Those listed below
  70. are either new or overridden with a different behaviour.
  71. =over 4
  72. =item my $c = Dpkg::Control->new(%opts)
  73. If the "type" option is given, it's used to setup default values
  74. for other options. See set_options() for more details.
  75. =cut
  76. sub new {
  77. my ($this, %opts) = @_;
  78. my $class = ref($this) || $this;
  79. my $self = Dpkg::Control::Hash->new();
  80. bless $self, $class;
  81. $self->set_options(%opts);
  82. return $self;
  83. }
  84. =item $c->set_options(%opts)
  85. Changes the value of one or more options. If the "type" option is changed,
  86. it is used first to define default values for others options. The option
  87. "allow_pgp" is set to 1 for CTRL_PKG_SRC and CTRL_FILE_CHANGES and to 0
  88. otherwise. The option "drop_empty" is set to 0 for CTRL_INFO_PKG and
  89. CTRL_INFO_SRC and to 1 otherwise. The option "name" is set to a textual
  90. description of the type of control information.
  91. The output order is also set to match the ordered list returned by
  92. Dpkg::Control::Fields::field_ordered_list($type).
  93. =cut
  94. sub set_options {
  95. my ($self, %opts) = @_;
  96. if (exists $opts{'type'}) {
  97. my $t = $opts{'type'};
  98. $$self->{'allow_pgp'} = ($t & (CTRL_PKG_SRC | CTRL_FILE_CHANGES)) ? 1 : 0;
  99. $$self->{'drop_empty'} = ($t & (CTRL_INFO_PKG | CTRL_INFO_SRC)) ? 0 : 1;
  100. if ($t == CTRL_INFO_SRC) {
  101. $$self->{'name'} = _g("general section of control info file");
  102. } elsif ($t == CTRL_INFO_PKG) {
  103. $$self->{'name'} = _g("package's section of control info file");
  104. } elsif ($t == CTRL_CHANGELOG) {
  105. $$self->{'name'} = _g("parsed version of changelog");
  106. } elsif ($t == CTRL_INDEX_SRC) {
  107. $$self->{'name'} = sprintf(_g("entry in repository's %s file"), "Sources");
  108. } elsif ($t == CTRL_INDEX_PKG) {
  109. $$self->{'name'} = sprintf(_g("entry in repository's %s file"), "Packages");
  110. } elsif ($t == CTRL_PKG_SRC) {
  111. $$self->{'name'} = sprintf(_g("%s file"), ".dsc");
  112. } elsif ($t == CTRL_PKG_DEB) {
  113. $$self->{'name'} = _g("control info of a .deb package");
  114. } elsif ($t == CTRL_FILE_CHANGES) {
  115. $$self->{'name'} = sprintf(_g("%s file"), ".changes");
  116. } elsif ($t == CTRL_FILE_VENDOR) {
  117. $$self->{'name'} = _g("vendor file");
  118. } elsif ($t == CTRL_FILE_STATUS) {
  119. $$self->{'name'} = _g("entry in dpkg's status file");
  120. }
  121. $self->set_output_order(field_ordered_list($opts{'type'}));
  122. }
  123. # Options set by the user override default values
  124. $$self->{$_} = $opts{$_} foreach keys %opts;
  125. }
  126. =item $c->get_type()
  127. Returns the type of control information stored. See the type parameter
  128. set during new().
  129. =cut
  130. sub get_type {
  131. my ($self) = @_;
  132. return $$self->{'type'};
  133. }
  134. =back
  135. =head1 AUTHOR
  136. Raphaël Hertzog <hertzog@debian.org>.
  137. =cut
  138. 1;