dpkg-genchanges.pl 17 KB

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