Entry.pm 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. # Copyright © 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::Changelog::Entry;
  16. use strict;
  17. use warnings;
  18. our $VERSION = '1.00';
  19. use Dpkg::Gettext;
  20. use Dpkg::ErrorHandling;
  21. use Dpkg::Control::Changelog;
  22. use overload
  23. '""' => \&output,
  24. 'eq' => sub { defined($_[1]) and "$_[0]" eq "$_[1]" },
  25. fallback => 1;
  26. =encoding utf8
  27. =head1 NAME
  28. Dpkg::Changelog::Entry - represents a changelog entry
  29. =head1 DESCRIPTION
  30. This object represents a changelog entry. It is composed
  31. of a set of lines with specific purpose: an header line, changes lines, a
  32. trailer line. Blank lines can be between those kind of lines.
  33. =head1 FUNCTIONS
  34. =over 4
  35. =item my $entry = Dpkg::Changelog::Entry->new()
  36. Creates a new object. It doesn't represent a real changelog entry
  37. until one has been successfully parsed or built from scratch.
  38. =cut
  39. sub new {
  40. my ($this) = @_;
  41. my $class = ref($this) || $this;
  42. my $self = {
  43. header => undef,
  44. changes => [],
  45. trailer => undef,
  46. blank_after_header => [],
  47. blank_after_changes => [],
  48. blank_after_trailer => [],
  49. };
  50. bless $self, $class;
  51. return $self;
  52. }
  53. =item my $str = $entry->output()
  54. =item "$entry"
  55. Get a string representation of the changelog entry.
  56. =item $entry->output($fh)
  57. Print the string representation of the changelog entry to a
  58. filehandle.
  59. =cut
  60. sub _format_output_block {
  61. my $lines = shift;
  62. return join('', map { $_ . "\n" } @{$lines});
  63. }
  64. sub output {
  65. my ($self, $fh) = @_;
  66. my $str = '';
  67. $str .= $self->{header} . "\n" if defined($self->{header});
  68. $str .= _format_output_block($self->{blank_after_header});
  69. $str .= _format_output_block($self->{changes});
  70. $str .= _format_output_block($self->{blank_after_changes});
  71. $str .= $self->{trailer} . "\n" if defined($self->{trailer});
  72. $str .= _format_output_block($self->{blank_after_trailer});
  73. print $fh $str if defined $fh;
  74. return $str;
  75. }
  76. =item $entry->get_part($part)
  77. Return either a string (for a single line) or an array ref (for multiple
  78. lines) corresponding to the requested part. $part can be
  79. "header, "changes", "trailer", "blank_after_header",
  80. "blank_after_changes", "blank_after_trailer".
  81. =cut
  82. sub get_part {
  83. my ($self, $part) = @_;
  84. internerr('invalid part of changelog entry: %s') unless exists $self->{$part};
  85. return $self->{$part};
  86. }
  87. =item $entry->set_part($part, $value)
  88. Set the value of the corresponding part. $value can be a string
  89. or an array ref.
  90. =cut
  91. sub set_part {
  92. my ($self, $part, $value) = @_;
  93. internerr('invalid part of changelog entry: %s') unless exists $self->{$part};
  94. if (ref($self->{$part})) {
  95. if (ref($value)) {
  96. $self->{$part} = $value;
  97. } else {
  98. $self->{$part} = [ $value ];
  99. }
  100. } else {
  101. $self->{$part} = $value;
  102. }
  103. }
  104. =item $entry->extend_part($part, $value)
  105. Concatenate $value at the end of the part. If the part is already a
  106. multi-line value, $value is added as a new line otherwise it's
  107. concatenated at the end of the current line.
  108. =cut
  109. sub extend_part {
  110. my ($self, $part, $value, @rest) = @_;
  111. internerr('invalid part of changelog entry: %s') unless exists $self->{$part};
  112. if (ref($self->{$part})) {
  113. if (ref($value)) {
  114. push @{$self->{$part}}, @$value;
  115. } else {
  116. push @{$self->{$part}}, $value;
  117. }
  118. } else {
  119. if (defined($self->{$part})) {
  120. if (ref($value)) {
  121. $self->{$part} = [ $self->{$part}, @$value ];
  122. } else {
  123. $self->{$part} .= $value;
  124. }
  125. } else {
  126. $self->{$part} = $value;
  127. }
  128. }
  129. }
  130. =item $is_empty = $entry->is_empty()
  131. Returns 1 if the changelog entry doesn't contain anything at all.
  132. Returns 0 as soon as it contains something in any of its non-blank
  133. parts.
  134. =cut
  135. sub is_empty {
  136. my ($self) = @_;
  137. return !(defined($self->{header}) || defined($self->{trailer}) ||
  138. scalar(@{$self->{changes}}));
  139. }
  140. =item $entry->normalize()
  141. Normalize the content. Strip whitespaces at end of lines, use a single
  142. empty line to separate each part.
  143. =cut
  144. sub normalize {
  145. my ($self) = @_;
  146. if (defined($self->{header})) {
  147. $self->{header} =~ s/\s+$//g;
  148. $self->{blank_after_header} = [''];
  149. } else {
  150. $self->{blank_after_header} = [];
  151. }
  152. if (scalar(@{$self->{changes}})) {
  153. s/\s+$//g foreach @{$self->{changes}};
  154. $self->{blank_after_changes} = [''];
  155. } else {
  156. $self->{blank_after_changes} = [];
  157. }
  158. if (defined($self->{trailer})) {
  159. $self->{trailer} =~ s/\s+$//g;
  160. $self->{blank_after_trailer} = [''];
  161. } else {
  162. $self->{blank_after_trailer} = [];
  163. }
  164. }
  165. =item my $src = $entry->get_source()
  166. Return the name of the source package associated to the changelog entry.
  167. =cut
  168. sub get_source {
  169. return;
  170. }
  171. =item my $ver = $entry->get_version()
  172. Return the version associated to the changelog entry.
  173. =cut
  174. sub get_version {
  175. return;
  176. }
  177. =item my @dists = $entry->get_distributions()
  178. Return a list of target distributions for this version.
  179. =cut
  180. sub get_distributions {
  181. return;
  182. }
  183. =item $fields = $entry->get_optional_fields()
  184. Return a set of optional fields exposed by the changelog entry.
  185. It always returns a Dpkg::Control object (possibly empty though).
  186. =cut
  187. sub get_optional_fields {
  188. return Dpkg::Control::Changelog->new();
  189. }
  190. =item $urgency = $entry->get_urgency()
  191. Return the urgency of the associated upload.
  192. =cut
  193. sub get_urgency {
  194. return;
  195. }
  196. =item my $maint = $entry->get_maintainer()
  197. Return the string identifying the person who signed this changelog entry.
  198. =cut
  199. sub get_maintainer {
  200. return;
  201. }
  202. =item my $time = $entry->get_timestamp()
  203. Return the timestamp of the changelog entry.
  204. =cut
  205. sub get_timestamp {
  206. return;
  207. }
  208. =item my $str = $entry->get_dpkg_changes()
  209. Returns a string that is suitable for usage in a C<Changes> field
  210. in the output format of C<dpkg-parsechangelog>.
  211. =cut
  212. sub get_dpkg_changes {
  213. my ($self) = @_;
  214. my $header = $self->get_part('header') || '';
  215. $header =~ s/\s+$//;
  216. return "\n$header\n\n" . join("\n", @{$self->get_part('changes')});
  217. }
  218. =back
  219. =head1 AUTHOR
  220. Raphaël Hertzog <hertzog@debian.org>.
  221. =cut
  222. 1;