Entry.pm 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 output {
  61. my ($self, $fh) = @_;
  62. my $str = '';
  63. sub _block {
  64. my $lines = shift;
  65. return join('', map { $_ . "\n" } @{$lines});
  66. }
  67. $str .= $self->{header} . "\n" if defined($self->{header});
  68. $str .= _block($self->{blank_after_header});
  69. $str .= _block($self->{changes});
  70. $str .= _block($self->{blank_after_changes});
  71. $str .= $self->{trailer} . "\n" if defined($self->{trailer});
  72. $str .= _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 undef;
  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 undef;
  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 () if wantarray;
  182. return undef;
  183. }
  184. =item $fields = $entry->get_optional_fields()
  185. Return a set of optional fields exposed by the changelog entry.
  186. It always returns a Dpkg::Control object (possibly empty though).
  187. =cut
  188. sub get_optional_fields {
  189. return Dpkg::Control::Changelog->new();
  190. }
  191. =item $urgency = $entry->get_urgency()
  192. Return the urgency of the associated upload.
  193. =cut
  194. sub get_urgency {
  195. return undef;
  196. }
  197. =item my $maint = $entry->get_maintainer()
  198. Return the string identifying the person who signed this changelog entry.
  199. =cut
  200. sub get_maintainer {
  201. return undef;
  202. }
  203. =item my $time = $entry->get_timestamp()
  204. Return the timestamp of the changelog entry.
  205. =cut
  206. sub get_timestamp {
  207. return undef;
  208. }
  209. =item my $str = $entry->get_dpkg_changes()
  210. Returns a string that is suitable for usage in a C<Changes> field
  211. in the output format of C<dpkg-parsechangelog>.
  212. =cut
  213. sub get_dpkg_changes {
  214. my ($self) = @_;
  215. my $header = $self->get_part("header") || "";
  216. $header =~ s/\s+$//;
  217. return "\n$header\n\n" . join("\n", @{$self->get_part("changes")});
  218. }
  219. =back
  220. =head1 AUTHOR
  221. Raphaël Hertzog <hertzog@debian.org>.
  222. =cut
  223. 1;