Debian.pm 10 KB

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