dpkg-genchanges.pl 16 KB

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