dpkg-genchanges.pl 17 KB

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