dpkg-genchanges.pl 17 KB

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