Debian.pm 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. # Copyright © 2009 Raphaël Hertzog <hertzog@debian.org>
  2. # Copyright © 2012-2013 Guillem Jover <guillem@debian.org>
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package Dpkg::Changelog::Entry::Debian;
  17. use strict;
  18. use warnings;
  19. our $VERSION = '1.01';
  20. our @EXPORT_OK = qw(
  21. $regex_header
  22. $regex_trailer
  23. match_header
  24. match_trailer
  25. find_closes
  26. );
  27. use Exporter qw(import);
  28. use Time::Piece;
  29. use Dpkg::Gettext;
  30. use Dpkg::Control::Fields;
  31. use Dpkg::Control::Changelog;
  32. use Dpkg::Changelog::Entry;
  33. use Dpkg::Version;
  34. use parent qw(Dpkg::Changelog::Entry);
  35. =encoding utf8
  36. =head1 NAME
  37. Dpkg::Changelog::Entry::Debian - represents a Debian changelog entry
  38. =head1 DESCRIPTION
  39. This object represents a Debian changelog entry. It implements the
  40. generic interface Dpkg::Changelog::Entry. Only functions specific to this
  41. implementation are described below.
  42. =cut
  43. my $name_chars = qr/[-+0-9a-z.]/i;
  44. # XXX: Backwards compatibility, stop exporting on VERSION 2.00.
  45. ## no critic (Variables::ProhibitPackageVars)
  46. # The matched content is the source package name ($1), the version ($2),
  47. # the target distributions ($3) and the options on the rest of the line ($4).
  48. our $regex_header = qr{
  49. ^
  50. (\w$name_chars*) # Package name
  51. \ \(([^\(\) \t]+)\) # Package version
  52. ((?:\s+$name_chars+)+) # Target distribution
  53. \; # Separator
  54. (.*?) # Key=Value options
  55. \s*$ # Trailing space
  56. }xi;
  57. # The matched content is the maintainer name ($1), its email ($2),
  58. # some blanks ($3) and the timestamp ($4), which is decomposed into
  59. # day of week ($6), date-time ($7) and this into month name ($8).
  60. our $regex_trailer = qr<
  61. ^
  62. \ \-\- # Trailer marker
  63. \ (.*) # Maintainer name
  64. \ \<(.*)\> # Maintainer email
  65. (\ \ ?) # Blanks
  66. (
  67. ((\w+)\,\s*)? # Day of week (abbreviated)
  68. (
  69. \d{1,2}\s+ # Day of month
  70. (\w+)\s+ # Month name (abbreviated)
  71. \d{4}\s+ # Year
  72. \d{1,2}:\d\d:\d\d\s+[-+]\d{4} # ISO 8601 date
  73. )
  74. )
  75. \s*$ # Trailing space
  76. >xo;
  77. my %week_day = map { $_ => 1 } qw(Mon Tue Wed Thu Fri Sat Sun);
  78. my %month_abbrev = map { $_ => 1 } qw(
  79. Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
  80. );
  81. my %month_name = map { $_ => } qw(
  82. January February March April May June July
  83. August September October November December
  84. );
  85. ## use critic
  86. =head1 METHODS
  87. =over 4
  88. =item @items = $entry->get_change_items()
  89. Return a list of change items. Each item contains at least one line.
  90. A change line starting with an asterisk denotes the start of a new item.
  91. Any change line like "[ Raphaël Hertzog ]" is treated like an item of its
  92. own even if it starts a set of items attributed to this person (the
  93. following line necessarily starts a new item).
  94. =cut
  95. sub get_change_items {
  96. my $self = shift;
  97. my (@items, @blanks, $item);
  98. foreach my $line (@{$self->get_part('changes')}) {
  99. if ($line =~ /^\s*\*/) {
  100. push @items, $item if defined $item;
  101. $item = "$line\n";
  102. } elsif ($line =~ /^\s*\[\s[^\]]+\s\]\s*$/) {
  103. push @items, $item if defined $item;
  104. push @items, "$line\n";
  105. $item = undef;
  106. @blanks = ();
  107. } elsif ($line =~ /^\s*$/) {
  108. push @blanks, "$line\n";
  109. } else {
  110. if (defined $item) {
  111. $item .= "@blanks$line\n";
  112. } else {
  113. $item = "$line\n";
  114. }
  115. @blanks = ();
  116. }
  117. }
  118. push @items, $item if defined $item;
  119. return @items;
  120. }
  121. =item @errors = $entry->check_header()
  122. =item @errors = $entry->check_trailer()
  123. Return a list of errors. Each item in the list is an error message
  124. describing the problem. If the empty list is returned, no errors
  125. have been found.
  126. =cut
  127. sub check_header {
  128. my $self = shift;
  129. my @errors;
  130. if (defined($self->{header}) and $self->{header} =~ $regex_header) {
  131. my ($version, $options) = ($2, $4);
  132. $options =~ s/^\s+//;
  133. my %optdone;
  134. foreach my $opt (split(/\s*,\s*/, $options)) {
  135. unless ($opt =~ m/^([-0-9a-z]+)\=\s*(.*\S)$/i) {
  136. push @errors, sprintf(g_("bad key-value after ';': '%s'"), $opt);
  137. next;
  138. }
  139. my ($k, $v) = (field_capitalize($1), $2);
  140. if ($optdone{$k}) {
  141. push @errors, sprintf(g_('repeated key-value %s'), $k);
  142. }
  143. $optdone{$k} = 1;
  144. if ($k eq 'Urgency') {
  145. push @errors, sprintf(g_('badly formatted urgency value: %s'), $v)
  146. unless ($v =~ m/^([-0-9a-z]+)((\s+.*)?)$/i);
  147. } elsif ($k eq 'Binary-Only') {
  148. push @errors, sprintf(g_('bad binary-only value: %s'), $v)
  149. unless ($v eq 'yes');
  150. } elsif ($k =~ m/^X[BCS]+-/i) {
  151. } else {
  152. push @errors, sprintf(g_('unknown key-value %s'), $k);
  153. }
  154. }
  155. my ($ok, $msg) = version_check($version);
  156. unless ($ok) {
  157. push @errors, sprintf(g_("version '%s' is invalid: %s"), $version, $msg);
  158. }
  159. } else {
  160. push @errors, g_("the header doesn't match the expected regex");
  161. }
  162. return @errors;
  163. }
  164. sub check_trailer {
  165. my $self = shift;
  166. my @errors;
  167. if (defined($self->{trailer}) and $self->{trailer} =~ $regex_trailer) {
  168. if ($3 ne ' ') {
  169. push @errors, g_('badly formatted trailer line');
  170. }
  171. # Validate the week day. Date::Parse used to ignore it, but Time::Piece
  172. # is much more strict and it does not gracefully handle bogus values.
  173. if (defined $5 and not exists $week_day{$6}) {
  174. push @errors, sprintf(g_('ignoring invalid week day \'%s\''), $6);
  175. }
  176. # Ignore the week day ('%a, '), as we have validated it above.
  177. local $ENV{LC_ALL} = 'C';
  178. eval {
  179. Time::Piece->strptime($7, '%d %b %Y %T %z');
  180. } or do {
  181. # Validate the month. Date::Parse used to accept both abbreviated
  182. # and full months, but Time::Piece strptime() implementation only
  183. # matches the abbreviated one with %b, which is what we want anyway.
  184. if (exists $month_name{$8}) {
  185. push @errors, sprintf(g_('uses full instead of abbreviated month name \'%s\''),
  186. $8, $month_name{$8});
  187. } elsif (not exists $month_abbrev{$8}) {
  188. push @errors, sprintf(g_('invalid abbreviated month name \'%s\''), $8);
  189. }
  190. push @errors, sprintf(g_("cannot parse non-comformant date '%s'"), $7);
  191. };
  192. } else {
  193. push @errors, g_("the trailer doesn't match the expected regex");
  194. }
  195. return @errors;
  196. }
  197. =item $entry->normalize()
  198. Normalize the content. Strip whitespaces at end of lines, use a single
  199. empty line to separate each part.
  200. =cut
  201. sub normalize {
  202. my $self = shift;
  203. $self->SUPER::normalize();
  204. #XXX: recreate header/trailer
  205. }
  206. sub get_source {
  207. my $self = shift;
  208. if (defined($self->{header}) and $self->{header} =~ $regex_header) {
  209. return $1;
  210. }
  211. return;
  212. }
  213. sub get_version {
  214. my $self = shift;
  215. if (defined($self->{header}) and $self->{header} =~ $regex_header) {
  216. return Dpkg::Version->new($2);
  217. }
  218. return;
  219. }
  220. sub get_distributions {
  221. my $self = shift;
  222. if (defined($self->{header}) and $self->{header} =~ $regex_header) {
  223. my @dists = split ' ', $3;
  224. return @dists if wantarray;
  225. return $dists[0];
  226. }
  227. return;
  228. }
  229. sub get_optional_fields {
  230. my $self = shift;
  231. my $f = Dpkg::Control::Changelog->new();
  232. if (defined($self->{header}) and $self->{header} =~ $regex_header) {
  233. my $options = $4;
  234. $options =~ s/^\s+//;
  235. foreach my $opt (split(/\s*,\s*/, $options)) {
  236. if ($opt =~ m/^([-0-9a-z]+)\=\s*(.*\S)$/i) {
  237. $f->{$1} = $2;
  238. }
  239. }
  240. }
  241. my @closes = find_closes(join("\n", @{$self->{changes}}));
  242. if (@closes) {
  243. $f->{Closes} = join(' ', @closes);
  244. }
  245. return $f;
  246. }
  247. sub get_urgency {
  248. my $self = shift;
  249. my $f = $self->get_optional_fields();
  250. if (exists $f->{Urgency}) {
  251. $f->{Urgency} =~ s/\s.*$//;
  252. return lc($f->{Urgency});
  253. }
  254. return;
  255. }
  256. sub get_maintainer {
  257. my $self = shift;
  258. if (defined($self->{trailer}) and $self->{trailer} =~ $regex_trailer) {
  259. return "$1 <$2>";
  260. }
  261. return;
  262. }
  263. sub get_timestamp {
  264. my $self = shift;
  265. if (defined($self->{trailer}) and $self->{trailer} =~ $regex_trailer) {
  266. return $4;
  267. }
  268. return;
  269. }
  270. =back
  271. =head1 UTILITY FUNCTIONS
  272. =over 4
  273. =item $bool = match_header($line)
  274. Checks if the line matches a valid changelog header line.
  275. =cut
  276. sub match_header {
  277. my $line = shift;
  278. return $line =~ /$regex_header/;
  279. }
  280. =item $bool = match_trailer($line)
  281. Checks if the line matches a valid changelog trailing line.
  282. =cut
  283. sub match_trailer {
  284. my $line = shift;
  285. return $line =~ /$regex_trailer/;
  286. }
  287. =item @closed_bugs = find_closes($changes)
  288. Takes one string as argument and finds "Closes: #123456, #654321" statements
  289. as supported by the Debian Archive software in it. Returns all closed bug
  290. numbers in an array.
  291. =cut
  292. sub find_closes {
  293. my $changes = shift;
  294. my %closes;
  295. while ($changes && ($changes =~ m{
  296. closes:\s*
  297. (?:bug)?\#?\s?\d+
  298. (?:,\s*(?:bug)?\#?\s?\d+)*
  299. }pigx)) {
  300. $closes{$_} = 1 foreach (${^MATCH} =~ /\#?\s?(\d+)/g);
  301. }
  302. my @closes = sort { $a <=> $b } keys %closes;
  303. return @closes;
  304. }
  305. =back
  306. =head1 CHANGES
  307. =head2 Version 1.01 (dpkg 1.17.2)
  308. New functions: match_header(), match_trailer()
  309. Deprecated variables: $regex_header, $regex_trailer
  310. =head2 Version 1.00 (dpkg 1.15.6)
  311. Mark the module as public.
  312. =head1 AUTHOR
  313. Raphaël Hertzog <hertzog@debian.org>.
  314. =cut
  315. 1;