dpkg-genchanges.pl 16 KB

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