Control.pm 5.9 KB

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