dpkg-genchanges.pl 17 KB

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