dpkg-genchanges.pl 17 KB

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