dpkg-genchanges.pl 17 KB

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