dpkg-genchanges.pl 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. #!/usr/bin/perl
  2. #
  3. # dpkg-genchanges
  4. #
  5. # Copyright © 1996 Ian Jackson
  6. # Copyright © 2000,2001 Wichert Akkerman
  7. # Copyright © 2006-2014 Guillem Jover <guillem@debian.org>
  8. #
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; either version 2 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. use strict;
  22. use warnings;
  23. use Encode;
  24. use POSIX qw(:errno_h :locale_h);
  25. use Dpkg ();
  26. use Dpkg::Gettext;
  27. use Dpkg::Util qw(:list);
  28. use Dpkg::File;
  29. use Dpkg::Checksums;
  30. use Dpkg::ErrorHandling;
  31. use Dpkg::Build::Types;
  32. use Dpkg::BuildProfiles qw(get_build_profiles parse_build_profiles
  33. evaluate_restriction_formula);
  34. use Dpkg::Arch qw(get_host_arch debarch_eq debarch_is debarch_list_parse);
  35. use Dpkg::Compression;
  36. use Dpkg::Control::Info;
  37. use Dpkg::Control::Fields;
  38. use Dpkg::Control;
  39. use Dpkg::Substvars;
  40. use Dpkg::Vars;
  41. use Dpkg::Changelog::Parse;
  42. use Dpkg::Dist::Files;
  43. use Dpkg::Version;
  44. textdomain('dpkg-dev');
  45. my $controlfile = 'debian/control';
  46. my $changelogfile = 'debian/changelog';
  47. my $changelogformat;
  48. my $fileslistfile = 'debian/files';
  49. my $uploadfilesdir = '..';
  50. my $sourcestyle = 'i';
  51. my $quiet = 0;
  52. my $host_arch = get_host_arch();
  53. my @profiles = get_build_profiles();
  54. my $changes_format = '1.8';
  55. my %p2f; # - package to file map, has entries for "packagename"
  56. my %f2seccf; # - package to section map, from control file
  57. my %f2pricf; # - package to priority map, from control file
  58. my %sourcedefault; # - default values as taken from source (used for Section,
  59. # Priority and Maintainer)
  60. my @descriptions;
  61. my $checksums = Dpkg::Checksums->new();
  62. my %remove; # - fields to remove
  63. my %override;
  64. my %archadded;
  65. my @archvalues;
  66. my $changesdescription;
  67. my $forcemaint;
  68. my $forcechangedby;
  69. my $since;
  70. my $substvars_loaded = 0;
  71. my $substvars = Dpkg::Substvars->new();
  72. $substvars->set_as_auto('Format', $changes_format);
  73. sub version {
  74. printf g_("Debian %s version %s.\n"), $Dpkg::PROGNAME, $Dpkg::PROGVERSION;
  75. printf g_('
  76. This is free software; see the GNU General Public License version 2 or
  77. later for copying conditions. There is NO warranty.
  78. ');
  79. }
  80. sub usage {
  81. printf g_(
  82. 'Usage: %s [<option>...]')
  83. . "\n\n" . g_(
  84. "Options:
  85. -g source and arch-indep build.
  86. -G source and arch-specific build.
  87. -b binary-only, no source files.
  88. -B binary-only, only arch-specific files.
  89. -A binary-only, only arch-indep files.
  90. -S source-only, no binary files.
  91. -c<control-file> get control info from this file.
  92. -l<changelog-file> get per-version info from this file.
  93. -f<files-list-file> get .deb files list from this file.
  94. -v<since-version> include all changes later than version.
  95. -C<changes-description> use change description from this file.
  96. -m<maintainer> override control's maintainer value.
  97. -e<maintainer> override changelog's maintainer value.
  98. -u<upload-files-dir> directory with files (default is '..').
  99. -si source includes orig, if new upstream (default).
  100. -sa source includes orig, always.
  101. -sd source is diff and .dsc only.
  102. -q quiet - no informational messages on stderr.
  103. -F<changelog-format> force changelog format.
  104. -V<name>=<value> set a substitution variable.
  105. -T<substvars-file> read variables here, not debian/substvars.
  106. -D<field>=<value> override or add a field and value.
  107. -U<field> remove a field.
  108. -?, --help show this help message.
  109. --version show the version.
  110. "), $Dpkg::PROGNAME;
  111. }
  112. while (@ARGV) {
  113. $_=shift(@ARGV);
  114. if (m/^-b$/) {
  115. set_build_type(BUILD_BINARY, $_);
  116. } elsif (m/^-B$/) {
  117. set_build_type(BUILD_ARCH_DEP, $_);
  118. } elsif (m/^-A$/) {
  119. set_build_type(BUILD_ARCH_INDEP, $_);
  120. } elsif (m/^-S$/) {
  121. set_build_type(BUILD_SOURCE, $_);
  122. } elsif (m/^-G$/) {
  123. set_build_type(BUILD_SOURCE | BUILD_ARCH_DEP, $_);
  124. } elsif (m/^-g$/) {
  125. set_build_type(BUILD_SOURCE | BUILD_ARCH_INDEP, $_);
  126. } elsif (m/^-s([iad])$/) {
  127. $sourcestyle= $1;
  128. } elsif (m/^-q$/) {
  129. $quiet= 1;
  130. } elsif (m/^-c(.*)$/) {
  131. $controlfile = $1;
  132. } elsif (m/^-l(.*)$/) {
  133. $changelogfile = $1;
  134. } elsif (m/^-C(.*)$/) {
  135. $changesdescription = $1;
  136. } elsif (m/^-f(.*)$/) {
  137. $fileslistfile = $1;
  138. } elsif (m/^-v(.*)$/) {
  139. $since = $1;
  140. } elsif (m/^-T(.*)$/) {
  141. $substvars->load($1) if -e $1;
  142. $substvars_loaded = 1;
  143. } elsif (m/^-m(.*)$/s) {
  144. $forcemaint = $1;
  145. } elsif (m/^-e(.*)$/s) {
  146. $forcechangedby = $1;
  147. } elsif (m/^-F([0-9a-z]+)$/) {
  148. $changelogformat = $1;
  149. } elsif (m/^-D([^\=:]+)[=:](.*)$/s) {
  150. $override{$1} = $2;
  151. } elsif (m/^-u(.*)$/) {
  152. $uploadfilesdir = $1;
  153. } elsif (m/^-U([^\=:]+)$/) {
  154. $remove{$1} = 1;
  155. } elsif (m/^-V(\w[-:0-9A-Za-z]*)[=:](.*)$/s) {
  156. $substvars->set($1, $2);
  157. } elsif (m/^-(?:\?|-help)$/) {
  158. usage();
  159. exit(0);
  160. } elsif (m/^--version$/) {
  161. version();
  162. exit(0);
  163. } else {
  164. usageerr(g_("unknown option '%s'"), $_);
  165. }
  166. }
  167. # Do not pollute STDOUT with info messages.
  168. report_options(info_fh => \*STDERR, quiet_warnings => $quiet);
  169. # Retrieve info from the current changelog entry
  170. my %options = (file => $changelogfile);
  171. $options{changelogformat} = $changelogformat if $changelogformat;
  172. $options{since} = $since if defined($since);
  173. my $changelog = changelog_parse(%options);
  174. # Change options to retrieve info of the former changelog entry
  175. delete $options{since};
  176. $options{count} = 1;
  177. $options{offset} = 1;
  178. my $prev_changelog = changelog_parse(%options);
  179. # Other initializations
  180. my $control = Dpkg::Control::Info->new($controlfile);
  181. my $fields = Dpkg::Control->new(type => CTRL_FILE_CHANGES);
  182. my $sourceversion = $changelog->{'Binary-Only'} ?
  183. $prev_changelog->{'Version'} : $changelog->{'Version'};
  184. my $binaryversion = $changelog->{'Version'};
  185. $substvars->set_version_substvars($sourceversion, $binaryversion);
  186. $substvars->set_arch_substvars();
  187. $substvars->load('debian/substvars') if -e 'debian/substvars' and not $substvars_loaded;
  188. if (defined($prev_changelog) and
  189. version_compare_relation($changelog->{'Version'}, REL_LT,
  190. $prev_changelog->{'Version'}))
  191. {
  192. warning(g_('the current version (%s) is earlier than the previous one (%s)'),
  193. $changelog->{'Version'}, $prev_changelog->{'Version'})
  194. # ~bpo and ~vola are backports and have lower version number by definition
  195. unless $changelog->{'Version'} =~ /~(?:bpo|vola)/;
  196. }
  197. # Scan control info of source package
  198. my $src_fields = $control->get_source();
  199. foreach (keys %{$src_fields}) {
  200. my $v = $src_fields->{$_};
  201. if (m/^Source$/) {
  202. set_source_package($v);
  203. } elsif (m/^Section$|^Priority$/i) {
  204. $sourcedefault{$_} = $v;
  205. } else {
  206. field_transfer_single($src_fields, $fields);
  207. }
  208. }
  209. my $dist = Dpkg::Dist::Files->new();
  210. my $origsrcmsg;
  211. if (build_has(BUILD_SOURCE)) {
  212. my $sec = $sourcedefault{'Section'} // '-';
  213. my $pri = $sourcedefault{'Priority'} // '-';
  214. warning(g_('missing Section for source files')) if $sec eq '-';
  215. warning(g_('missing Priority for source files')) if $pri eq '-';
  216. my $spackage = get_source_package();
  217. (my $sversion = $substvars->get('source:Version')) =~ s/^\d+://;
  218. my $dsc = "${spackage}_${sversion}.dsc";
  219. my $dsc_pathname = "$uploadfilesdir/$dsc";
  220. my $dsc_fields = Dpkg::Control->new(type => CTRL_PKG_SRC);
  221. $dsc_fields->load($dsc_pathname) or error(g_('%s is empty'), $dsc_pathname);
  222. $checksums->add_from_file($dsc_pathname, key => $dsc);
  223. $checksums->add_from_control($dsc_fields, use_files_for_md5 => 1);
  224. # Compare upstream version to previous upstream version to decide if
  225. # the .orig tarballs must be included
  226. my $include_tarball;
  227. if (defined($prev_changelog)) {
  228. my $cur = Dpkg::Version->new($changelog->{'Version'});
  229. my $prev = Dpkg::Version->new($prev_changelog->{'Version'});
  230. $include_tarball = ($cur->version() ne $prev->version()) ? 1 : 0;
  231. } else {
  232. # No previous entry means first upload, tarball required
  233. $include_tarball = 1;
  234. }
  235. my $ext = compression_get_file_extension_regex();
  236. if ((($sourcestyle =~ m/i/ && !$include_tarball) ||
  237. $sourcestyle =~ m/d/) &&
  238. any { m/\.(?:debian\.tar|diff)\.$ext$/ } $checksums->get_files())
  239. {
  240. $origsrcmsg = g_('not including original source code in upload');
  241. foreach my $f (grep { m/\.orig(-.+)?\.tar\.$ext$/ } $checksums->get_files()) {
  242. $checksums->remove_file($f);
  243. }
  244. } else {
  245. if ($sourcestyle =~ m/d/ &&
  246. none { m/\.(?:debian\.tar|diff)\.$ext$/ } $checksums->get_files()) {
  247. warning(g_('ignoring -sd option for native Debian package'));
  248. }
  249. $origsrcmsg = g_('including full source code in upload');
  250. }
  251. # Only add attributes for files being distributed.
  252. for my $f ($checksums->get_files()) {
  253. $dist->add_file($f, $sec, $pri);
  254. }
  255. } elsif (build_is(BUILD_ARCH_DEP)) {
  256. $origsrcmsg = g_('binary-only arch-specific upload ' .
  257. '(source code and arch-indep packages not included)');
  258. } elsif (build_is(BUILD_ARCH_INDEP)) {
  259. $origsrcmsg = g_('binary-only arch-indep upload ' .
  260. '(source code and arch-specific packages not included)');
  261. } else {
  262. $origsrcmsg = g_('binary-only upload (no source code included)');
  263. }
  264. if (build_has(BUILD_BINARY)) {
  265. my $dist_count = 0;
  266. $dist_count = $dist->load($fileslistfile) if -e $fileslistfile;
  267. error(g_('binary build with no binary artifacts found; cannot distribute'))
  268. if $dist_count == 0;
  269. foreach my $file ($dist->get_files()) {
  270. if (defined $file->{package} && $file->{package_type} =~ m/^u?deb$/) {
  271. $p2f{$file->{package}} //= [];
  272. push @{$p2f{$file->{package}}}, $file->{filename};
  273. }
  274. if (defined $file->{arch}) {
  275. push @archvalues, $file->{arch}
  276. if $file->{arch} and not $archadded{$file->{arch}}++;
  277. }
  278. }
  279. }
  280. # Scan control info of all binary packages
  281. foreach my $pkg ($control->get_packages()) {
  282. my $p = $pkg->{'Package'};
  283. my $a = $pkg->{'Architecture'};
  284. my $bp = $pkg->{'Build-Profiles'};
  285. my $d = $pkg->{'Description'} || 'no description available';
  286. $d = $1 if $d =~ /^(.*)\n/;
  287. my $pkg_type = $pkg->{'Package-Type'} ||
  288. $pkg->get_custom_field('Package-Type') || 'deb';
  289. my @f; # List of files for this binary package
  290. push @f, @{$p2f{$p}} if defined $p2f{$p};
  291. # Add description of all binary packages
  292. my $desc = encode_utf8(sprintf('%-10s - %-.65s', $p, decode_utf8($d)));
  293. $desc .= " ($pkg_type)" if $pkg_type ne 'deb';
  294. push @descriptions, $desc;
  295. my @restrictions;
  296. @restrictions = parse_build_profiles($bp) if defined $bp;
  297. if (not defined($p2f{$p})) {
  298. # No files for this package... warn if it's unexpected
  299. if (((build_has(BUILD_ARCH_INDEP) and debarch_eq('all', $a)) or
  300. (build_has(BUILD_ARCH_DEP) and
  301. (any { debarch_is($host_arch, $_) } debarch_list_parse($a)))) and
  302. (@restrictions == 0 or
  303. evaluate_restriction_formula(\@restrictions, \@profiles)))
  304. {
  305. warning(g_('package %s in control file but not in files list'),
  306. $p);
  307. }
  308. next; # and skip it
  309. }
  310. foreach (keys %{$pkg}) {
  311. my $v = $pkg->{$_};
  312. if (m/^Section$/) {
  313. $f2seccf{$_} = $v foreach (@f);
  314. } elsif (m/^Priority$/) {
  315. $f2pricf{$_} = $v foreach (@f);
  316. } elsif (m/^Architecture$/) {
  317. if (build_has(BUILD_ARCH_DEP) and
  318. (any { debarch_is($host_arch, $_) } debarch_list_parse($v))) {
  319. $v = $host_arch;
  320. } elsif (!debarch_eq('all', $v)) {
  321. $v = '';
  322. }
  323. push(@archvalues, $v) if $v and not $archadded{$v}++;
  324. } elsif (m/^Description$/) {
  325. # Description in changes is computed, do not copy this field
  326. } else {
  327. field_transfer_single($pkg, $fields);
  328. }
  329. }
  330. }
  331. # Scan fields of dpkg-parsechangelog
  332. foreach (keys %{$changelog}) {
  333. my $v = $changelog->{$_};
  334. if (m/^Source$/i) {
  335. set_source_package($v);
  336. } elsif (m/^Maintainer$/i) {
  337. $fields->{'Changed-By'} = $v;
  338. } else {
  339. field_transfer_single($changelog, $fields);
  340. }
  341. }
  342. if ($changesdescription) {
  343. open(my $changes_fh, '<', $changesdescription)
  344. or syserr(g_('cannot read %s'), $changesdescription);
  345. $fields->{'Changes'} = "\n" . file_slurp($changes_fh);
  346. close($changes_fh);
  347. }
  348. for my $p (keys %p2f) {
  349. if (not defined $control->get_pkg_by_name($p)) {
  350. warning(g_('package %s listed in files list but not in control info'), $p);
  351. next;
  352. }
  353. foreach my $f (@{$p2f{$p}}) {
  354. my $file = $dist->get_file($f);
  355. my $sec = $f2seccf{$f} || $sourcedefault{'Section'} // '-';
  356. if ($sec eq '-') {
  357. warning(g_("missing Section for binary package %s; using '-'"), $p);
  358. }
  359. if ($sec ne $file->{section}) {
  360. error(g_('package %s has section %s in control file but %s in ' .
  361. 'files list'), $p, $sec, $file->{section});
  362. }
  363. my $pri = $f2pricf{$f} || $sourcedefault{'Priority'} // '-';
  364. if ($pri eq '-') {
  365. warning(g_("missing Priority for binary package %s; using '-'"), $p);
  366. }
  367. if ($pri ne $file->{priority}) {
  368. error(g_('package %s has priority %s in control file but %s in ' .
  369. 'files list'), $p, $pri, $file->{priority});
  370. }
  371. }
  372. }
  373. info($origsrcmsg);
  374. $fields->{'Format'} = $substvars->get('Format');
  375. if (!defined($fields->{'Date'})) {
  376. setlocale(LC_TIME, 'C');
  377. $fields->{'Date'} = POSIX::strftime('%a, %d %b %Y %T %z', localtime);
  378. setlocale(LC_TIME, '');
  379. }
  380. $fields->{'Binary'} = join(' ', map { $_->{'Package'} } $control->get_packages());
  381. # Avoid overly long line by splitting over multiple lines
  382. if (length($fields->{'Binary'}) > 980) {
  383. $fields->{'Binary'} =~ s/(.{0,980}) /$1\n/g;
  384. }
  385. unshift @archvalues, 'source' if build_has(BUILD_SOURCE);
  386. @archvalues = ('all') if build_is(BUILD_ARCH_INDEP);
  387. @archvalues = grep { !debarch_eq('all', $_) } @archvalues
  388. if build_has_not(BUILD_ARCH_INDEP);
  389. @archvalues = grep { !debarch_eq($host_arch, $_) } @archvalues
  390. if build_has_not(BUILD_ARCH_DEP);
  391. $fields->{'Architecture'} = join ' ', @archvalues;
  392. $fields->{'Built-For-Profiles'} = join ' ', get_build_profiles();
  393. $fields->{'Description'} = "\n" . join("\n", sort @descriptions);
  394. $fields->{'Files'} = '';
  395. for my $file ($dist->get_files()) {
  396. my $f = $file->{filename};
  397. if (defined $file->{package} && $file->{package_type} =~ m/^u?deb$/) {
  398. my $arch_all = debarch_eq('all', $file->{arch});
  399. next if build_has_not(BUILD_ARCH_INDEP) and $arch_all;
  400. next if build_has_not(BUILD_ARCH_DEP) and not $arch_all;
  401. }
  402. $checksums->add_from_file("$uploadfilesdir/$f", key => $f);
  403. }
  404. foreach my $f ($checksums->get_files()) {
  405. my $file = $dist->get_file($f);
  406. $fields->{'Files'} .= "\n" . $checksums->get_checksum($f, 'md5') .
  407. ' ' . $checksums->get_size($f) .
  408. " $file->{section} $file->{priority} $f";
  409. }
  410. $checksums->export_to_control($fields);
  411. # redundant with the Files field
  412. delete $fields->{'Checksums-Md5'};
  413. $fields->{'Source'} = get_source_package();
  414. if ($fields->{'Version'} ne $substvars->get('source:Version')) {
  415. $fields->{'Source'} .= ' (' . $substvars->get('source:Version') . ')';
  416. }
  417. $fields->{'Maintainer'} = $forcemaint if defined($forcemaint);
  418. $fields->{'Changed-By'} = $forcechangedby if defined($forcechangedby);
  419. for my $f (qw(Version Distribution Maintainer Changes)) {
  420. error(g_('missing information for critical output field %s'), $f)
  421. unless defined $fields->{$f};
  422. }
  423. for my $f (qw(Urgency)) {
  424. warning(g_('missing information for output field %s'), $f)
  425. unless defined $fields->{$f};
  426. }
  427. for my $f (keys %override) {
  428. $fields->{$f} = $override{$f};
  429. }
  430. for my $f (keys %remove) {
  431. delete $fields->{$f};
  432. }
  433. # Note: do not perform substitution of variables, one of the reasons is that
  434. # they could interfere with field values, for example the Changes field.
  435. $fields->output(\*STDOUT);