dpkg-genchanges.pl 17 KB

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