Control.pm 7.2 KB

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