dpkg-genchanges.pl 16 KB

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