Entry.pm 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. =head1 NAME
  27. Dpkg::Changelog::Entry - represents a changelog entry
  28. =head1 DESCRIPTION
  29. This object represents a changelog entry. It is composed
  30. of a set of lines with specific purpose: an header line, changes lines, a
  31. trailer line. Blank lines can be between those kind of lines.
  32. =head1 FUNCTIONS
  33. =over 4
  34. =item my $entry = Dpkg::Changelog::Entry->new()
  35. Creates a new object. It doesn't represent a real changelog entry
  36. until one has been successfully parsed or built from scratch.
  37. =cut
  38. sub new {
  39. my ($this) = @_;
  40. my $class = ref($this) || $this;
  41. my $self = {
  42. 'header' => undef,
  43. 'changes' => [],
  44. 'trailer' => undef,
  45. 'blank_after_header' => [],
  46. 'blank_after_changes' => [],
  47. 'blank_after_trailer' => [],
  48. };
  49. bless $self, $class;
  50. return $self;
  51. }
  52. =item my $str = $entry->output()
  53. =item "$entry"
  54. Get a string representation of the changelog entry.
  55. =item $entry->output($fh)
  56. Print the string representation of the changelog entry to a
  57. filehandle.
  58. =cut
  59. sub output {
  60. my ($self, $fh) = @_;
  61. my $str = '';
  62. sub _block {
  63. my $lines = shift;
  64. return join('', map { $_ . "\n" } @{$lines});
  65. }
  66. $str .= $self->{header} . "\n" if defined($self->{header});
  67. $str .= _block($self->{blank_after_header});
  68. $str .= _block($self->{changes});
  69. $str .= _block($self->{blank_after_changes});
  70. $str .= $self->{trailer} . "\n" if defined($self->{trailer});
  71. $str .= _block($self->{blank_after_trailer});
  72. print $fh $str if defined $fh;
  73. return $str;
  74. }
  75. =item $entry->get_part($part)
  76. Return either a string (for a single line) or an array ref (for multiple
  77. lines) corresponding to the requested part. $part can be
  78. "header, "changes", "trailer", "blank_after_header",
  79. "blank_after_changes", "blank_after_trailer".
  80. =cut
  81. sub get_part {
  82. my ($self, $part) = @_;
  83. internerr("invalid part of changelog entry: %s") unless exists $self->{$part};
  84. return $self->{$part};
  85. }
  86. =item $entry->set_part($part, $value)
  87. Set the value of the corresponding part. $value can be a string
  88. or an array ref.
  89. =cut
  90. sub set_part {
  91. my ($self, $part, $value) = @_;
  92. internerr("invalid part of changelog entry: %s") unless exists $self->{$part};
  93. if (ref($self->{$part})) {
  94. if (ref($value)) {
  95. $self->{$part} = $value;
  96. } else {
  97. $self->{$part} = [ $value ];
  98. }
  99. } else {
  100. $self->{$part} = $value;
  101. }
  102. }
  103. =item $entry->extend_part($part, $value)
  104. Concatenate $value at the end of the part. If the part is already a
  105. multi-line value, $value is added as a new line otherwise it's
  106. concatenated at the end of the current line.
  107. =cut
  108. sub extend_part {
  109. my ($self, $part, $value, @rest) = @_;
  110. internerr("invalid part of changelog entry: %s") unless exists $self->{$part};
  111. if (ref($self->{$part})) {
  112. if (ref($value)) {
  113. push @{$self->{$part}}, @$value;
  114. } else {
  115. push @{$self->{$part}}, $value;
  116. }
  117. } else {
  118. if (defined($self->{$part})) {
  119. if (ref($value)) {
  120. $self->{$part} = [ $self->{$part}, @$value ];
  121. } else {
  122. $self->{$part} .= $value;
  123. }
  124. } else {
  125. $self->{$part} = $value;
  126. }
  127. }
  128. }
  129. =item $is_empty = $entry->is_empty()
  130. Returns 1 if the changelog entry doesn't contain anything at all.
  131. Returns 0 as soon as it contains something in any of its non-blank
  132. parts.
  133. =cut
  134. sub is_empty {
  135. my ($self) = @_;
  136. return !(defined($self->{header}) || defined($self->{trailer}) ||
  137. scalar(@{$self->{changes}}));
  138. }
  139. =item $entry->normalize()
  140. Normalize the content. Strip whitespaces at end of lines, use a single
  141. empty line to separate each part.
  142. =cut
  143. sub normalize {
  144. my ($self) = @_;
  145. if (defined($self->{header})) {
  146. $self->{header} =~ s/\s+$//g;
  147. $self->{blank_after_header} = [''];
  148. } else {
  149. $self->{blank_after_header} = [];
  150. }
  151. if (scalar(@{$self->{changes}})) {
  152. s/\s+$//g foreach @{$self->{changes}};
  153. $self->{blank_after_changes} = [''];
  154. } else {
  155. $self->{blank_after_changes} = [];
  156. }
  157. if (defined($self->{trailer})) {
  158. $self->{trailer} =~ s/\s+$//g;
  159. $self->{blank_after_trailer} = [''];
  160. } else {
  161. $self->{blank_after_trailer} = [];
  162. }
  163. }
  164. =item my $src = $entry->get_source()
  165. Return the name of the source package associated to the changelog entry.
  166. =cut
  167. sub get_source {
  168. return undef;
  169. }
  170. =item my $ver = $entry->get_version()
  171. Return the version associated to the changelog entry.
  172. =cut
  173. sub get_version {
  174. return undef;
  175. }
  176. =item my @dists = $entry->get_distributions()
  177. Return a list of target distributions for this version.
  178. =cut
  179. sub get_distributions {
  180. return () if wantarray;
  181. return undef;
  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 undef;
  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 undef;
  201. }
  202. =item my $time = $entry->get_timestamp()
  203. Return the timestamp of the changelog entry.
  204. =cut
  205. sub get_timestamp {
  206. return undef;
  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;