Control.pm 6.7 KB

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