dpkg-genchanges.pl 17 KB

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