dpkg-genchanges.pl 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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 qw(:DEFAULT unknown);
  11. use Dpkg::Arch qw(get_host_arch debarch_eq debarch_is);
  12. use Dpkg::Fields qw(:list capit);
  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. textdomain("dpkg-dev");
  21. my @changes_fields = qw(Format Date Source Binary Architecture Version
  22. Distribution Urgency Maintainer Changed-By
  23. Description Closes Changes Checksums-Md5
  24. Checksums-Sha1 Checksums-Sha256 Files);
  25. my $controlfile = 'debian/control';
  26. my $changelogfile = 'debian/changelog';
  27. my $changelogformat;
  28. my $fileslistfile = 'debian/files';
  29. my $varlistfile = 'debian/substvars';
  30. my $uploadfilesdir = '..';
  31. my $sourcestyle = 'i';
  32. my $quiet = 0;
  33. my $host_arch = get_host_arch();
  34. my $changes_format = "1.8";
  35. my %f2p; # - file to package map
  36. my %p2f; # - package to file map, has entries for "packagename"
  37. my %pa2f; # - likewise, has entries for "packagename architecture"
  38. my %p2ver; # - package to version map
  39. my %p2arch; # - package to arch map
  40. my %f2sec; # - file to section map
  41. my %f2seccf; # - likewise, from control file
  42. my %f2pri; # - file to priority map
  43. my %f2pricf; # - likewise, from control file
  44. my %sourcedefault; # - default values as taken from source (used for Section,
  45. # Priority and Maintainer)
  46. my @descriptions;
  47. my @sourcefiles;
  48. my @fileslistfiles;
  49. my %checksum; # - file to checksum map
  50. my %size; # - file to size map
  51. my %remove; # - fields to remove
  52. my %override;
  53. my %archadded;
  54. my @archvalues;
  55. my $dsc;
  56. my $changesdescription;
  57. my $forcemaint;
  58. my $forcechangedby;
  59. my $since;
  60. my $substvars = Dpkg::Substvars->new();
  61. $substvars->set("Format", $changes_format);
  62. use constant SOURCE => 1;
  63. use constant ARCH_DEP => 2;
  64. use constant ARCH_INDEP => 4;
  65. use constant BIN => ARCH_DEP | ARCH_INDEP;
  66. use constant ALL => BIN | SOURCE;
  67. my $include = ALL;
  68. sub is_sourceonly() { return $include == SOURCE; }
  69. sub is_binaryonly() { return !($include & SOURCE); }
  70. sub binary_opt() { return (($include == BIN) ? '-b' :
  71. (($include == ARCH_DEP) ? '-B' :
  72. (($include == ARCH_INDEP) ? '-A' :
  73. internerr("binary_opt called with include=$include"))));
  74. }
  75. sub version {
  76. printf _g("Debian %s version %s.\n"), $progname, $version;
  77. printf _g("
  78. Copyright (C) 1996 Ian Jackson.
  79. Copyright (C) 2000,2001 Wichert Akkerman.");
  80. printf _g("
  81. This is free software; see the GNU General Public Licence version 2 or
  82. later for copying conditions. There is NO warranty.
  83. ");
  84. }
  85. sub usage {
  86. printf _g(
  87. "Usage: %s [<option> ...]
  88. Options:
  89. -b binary-only build - no source files.
  90. -B arch-specific - no source or arch-indep files.
  91. -A only arch-indep - no source or arch-specific files.
  92. -S source-only upload.
  93. -c<controlfile> get control info from this file.
  94. -l<changelogfile> get per-version info from this file.
  95. -f<fileslistfile> get .deb files list from this file.
  96. -v<sinceversion> include all changes later than version.
  97. -C<changesdescription> use change description from this file.
  98. -m<maintainer> override control's maintainer value.
  99. -e<maintainer> override changelog's maintainer value.
  100. -u<uploadfilesdir> directory with files (default is \`..').
  101. -si (default) src includes orig if new upstream.
  102. -sa source includes orig src.
  103. -sd source is diff and .dsc only.
  104. -q quiet - no informational messages on stderr.
  105. -F<changelogformat> force change log format.
  106. -V<name>=<value> set a substitution variable.
  107. -T<varlistfile> read variables here, not debian/substvars.
  108. -D<field>=<value> override or add a field and value.
  109. -U<field> remove a field.
  110. -h, --help show this help message.
  111. --version show the version.
  112. "), $progname;
  113. }
  114. while (@ARGV) {
  115. $_=shift(@ARGV);
  116. if (m/^-b$/) {
  117. is_sourceonly && usageerr(_g("cannot combine %s and -S"), $_);
  118. $include = BIN;
  119. } elsif (m/^-B$/) {
  120. is_sourceonly && usageerr(_g("cannot combine %s and -S"), $_);
  121. $include = ARCH_DEP;
  122. printf STDERR _g("%s: arch-specific upload - not including arch-independent packages")."\n", $progname;
  123. } elsif (m/^-A$/) {
  124. is_sourceonly && usageerr(_g("cannot combine %s and -S"), $_);
  125. $include = ARCH_INDEP;
  126. printf STDERR _g("%s: arch-indep upload - not including arch-specific packages")."\n", $progname;
  127. } elsif (m/^-S$/) {
  128. is_binaryonly && usageerr(_g("cannot combine %s and -S"), binary_opt);
  129. $include = SOURCE;
  130. } elsif (m/^-s([iad])$/) {
  131. $sourcestyle= $1;
  132. } elsif (m/^-q$/) {
  133. $quiet= 1;
  134. } elsif (m/^-c/) {
  135. $controlfile= $POSTMATCH;
  136. } elsif (m/^-l/) {
  137. $changelogfile= $POSTMATCH;
  138. } elsif (m/^-C/) {
  139. $changesdescription= $POSTMATCH;
  140. } elsif (m/^-f/) {
  141. $fileslistfile= $POSTMATCH;
  142. } elsif (m/^-v/) {
  143. $since= $POSTMATCH;
  144. } elsif (m/^-T/) {
  145. $varlistfile= $POSTMATCH;
  146. warning(_g("substvars support is deprecated (see README.feature-removal-schedule)"));
  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 $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->parse($varlistfile) if -e $varlistfile;
  190. if (defined($prev_changelog) and
  191. compare_versions($changelog->{"Version"}, '<', $prev_changelog->{"Version"})) {
  192. warning(_g("the current version (%s) is smaller than the previous one (%s)"),
  193. $changelog->{"Version"}, $prev_changelog->{"Version"});
  194. }
  195. if (not is_sourceonly) {
  196. open(FL, "<", $fileslistfile) || syserr(_g("cannot read files list file"));
  197. while(<FL>) {
  198. if (m/^(([-+.0-9a-z]+)_([^_]+)_([-\w]+)\.u?deb) (\S+) (\S+)$/) {
  199. defined($p2f{"$2 $4"}) &&
  200. warning(_g("duplicate files list entry for package %s (line %d)"),
  201. $2, $NR);
  202. $f2p{$1}= $2;
  203. $pa2f{"$2 $4"}= $1;
  204. $p2f{$2} ||= [];
  205. push @{$p2f{$2}}, $1;
  206. $p2ver{$2}= $3;
  207. defined($f2sec{$1}) &&
  208. warning(_g("duplicate files list entry for file %s (line %d)"),
  209. $1, $NR);
  210. $f2sec{$1}= $5;
  211. $f2pri{$1}= $6;
  212. push(@archvalues,$4) unless !$4 || $archadded{$4}++;
  213. push(@fileslistfiles,$1);
  214. } elsif (m/^([-+.0-9a-z]+_[^_]+_([-\w]+)\.[a-z0-9.]+) (\S+) (\S+)$/) {
  215. # A non-deb package
  216. $f2sec{$1}= $3;
  217. $f2pri{$1}= $4;
  218. push(@archvalues,$2) unless !$2 || $archadded{$2}++;
  219. push(@fileslistfiles,$1);
  220. } elsif (m/^([-+.,_0-9a-zA-Z]+) (\S+) (\S+)$/) {
  221. defined($f2sec{$1}) &&
  222. warning(_g("duplicate files list entry for file %s (line %d)"),
  223. $1, $NR);
  224. $f2sec{$1}= $2;
  225. $f2pri{$1}= $3;
  226. push(@fileslistfiles,$1);
  227. } else {
  228. error(_g("badly formed line in files list file, line %d"), $NR);
  229. }
  230. }
  231. close(FL);
  232. }
  233. # Scan control info of source package
  234. my $src_fields = $control->get_source();
  235. foreach $_ (keys %{$src_fields}) {
  236. my $v = $src_fields->{$_};
  237. if (m/^Source$/) {
  238. set_source_package($v);
  239. } elsif (m/^Section$|^Priority$/i) {
  240. $sourcedefault{$_} = $v;
  241. } elsif (m/^Maintainer$/i) {
  242. $fields->{$_} = $v;
  243. } elsif (s/^X[BS]*C[BS]*-//i) { # Include XC-* fields
  244. $fields->{$_} = $v;
  245. } elsif (m/^X[BS]+-/i || m/^$control_src_field_regex$/i) {
  246. # Silently ignore valid fields
  247. } else {
  248. unknown(_g('general section of control info file'));
  249. }
  250. }
  251. # Scan control info of all binary packages
  252. foreach my $pkg ($control->get_packages()) {
  253. my $p = $pkg->{"Package"};
  254. my $a = $pkg->{"Architecture"};
  255. my $d = $pkg->{"Description"} || "no description available";
  256. $d = $1 if $d =~ /^(.*)\n/;
  257. my $pkg_type = $pkg->{"Package-Type"} ||
  258. tied(%$pkg)->get_custom_field("Package-Type") || "deb";
  259. my @f; # List of files for this binary package
  260. push @f, @{$p2f{$p}} if defined $p2f{$p};
  261. # Add description of all binary packages
  262. my $desc = sprintf("%-10s - %-.65s", $p, $d);
  263. $desc .= " (udeb)" if $pkg_type eq "udeb";
  264. push @descriptions, $desc;
  265. if (not defined($p2f{$p})) {
  266. # No files for this package... warn if it's unexpected
  267. if ((debarch_eq('all', $a) and ($include & ARCH_INDEP)) ||
  268. (grep(debarch_is($host_arch, $_), split(/\s+/, $a))
  269. and ($include & ARCH_DEP))) {
  270. warning(_g("package %s in control file but not in files list"),
  271. $p);
  272. }
  273. next; # and skip it
  274. }
  275. $p2arch{$p} = $a;
  276. foreach $_ (keys %{$pkg}) {
  277. my $v = $pkg->{$_};
  278. if (m/^Section$/) {
  279. $f2seccf{$_} = $v foreach (@f);
  280. } elsif (m/^Priority$/) {
  281. $f2pricf{$_} = $v foreach (@f);
  282. } elsif (s/^X[BS]*C[BS]*-//i) { # Include XC-* fields
  283. $fields->{$_} = $v;
  284. } elsif (m/^Architecture$/) {
  285. if (grep(debarch_is($host_arch, $_), split(/\s+/, $v))
  286. and ($include & ARCH_DEP)) {
  287. $v = $host_arch;
  288. } elsif (!debarch_eq('all', $v)) {
  289. $v = '';
  290. }
  291. push(@archvalues,$v) unless !$v || $archadded{$v}++;
  292. } elsif (m/^$control_pkg_field_regex$/ || m/^X[BS]+-/i) {
  293. # Silently ignore valid fields
  294. } else {
  295. unknown(_g("package's section of control info file"));
  296. }
  297. }
  298. }
  299. # Scan fields of dpkg-parsechangelog
  300. foreach $_ (keys %{$changelog}) {
  301. my $v = $changelog->{$_};
  302. if (m/^Source$/i) {
  303. set_source_package($v);
  304. } elsif (m/^Maintainer$/i) {
  305. $fields->{"Changed-By"} = $v;
  306. } elsif (m/^(Version|Changes|Urgency|Distribution|Date|Closes)$/i) {
  307. $fields->{$_} = $v;
  308. } elsif (s/^X[BS]*C[BS]*-//i) {
  309. $fields->{$_} = $v;
  310. } elsif (!m/^X[BS]+-/i) {
  311. unknown(_g("parsed version of changelog"));
  312. }
  313. }
  314. if ($changesdescription) {
  315. $fields->{'Changes'} = '';
  316. open(X, "<", $changesdescription) || syserr(_g("read changesdescription"));
  317. while(<X>) {
  318. s/\s*\n$//;
  319. $_= '.' unless m/\S/;
  320. $fields->{'Changes'}.= "\n $_";
  321. }
  322. }
  323. for my $pa (keys %pa2f) {
  324. my ($pp, $aa) = (split / /, $pa);
  325. defined($control->get_pkg_by_name($pp)) ||
  326. warning(_g("package %s listed in files list but not in control info"),
  327. $pp);
  328. }
  329. for my $p (keys %p2f) {
  330. my @f = @{$p2f{$p}};
  331. foreach my $f (@f) {
  332. my $sec = $f2seccf{$f};
  333. $sec ||= $sourcedefault{'Section'};
  334. if (!defined($sec)) {
  335. $sec = '-';
  336. warning(_g("missing Section for binary package %s; using '-'"), $p);
  337. }
  338. $sec eq $f2sec{$f} || error(_g("package %s has section %s in " .
  339. "control file but %s in files list"),
  340. $p, $sec, $f2sec{$f});
  341. my $pri = $f2pricf{$f};
  342. $pri ||= $sourcedefault{'Priority'};
  343. if (!defined($pri)) {
  344. $pri = '-';
  345. warning(_g("missing Priority for binary package %s; using '-'"), $p);
  346. }
  347. $pri eq $f2pri{$f} || error(_g("package %s has priority %s in " .
  348. "control file but %s in files list"),
  349. $p, $pri, $f2pri{$f});
  350. }
  351. }
  352. my $origsrcmsg;
  353. if (!is_binaryonly) {
  354. my $sec = $sourcedefault{'Section'};
  355. if (!defined($sec)) {
  356. $sec = '-';
  357. warning(_g("missing Section for source files"));
  358. }
  359. my $pri = $sourcedefault{'Priority'};
  360. if (!defined($pri)) {
  361. $pri = '-';
  362. warning(_g("missing Priority for source files"));
  363. }
  364. (my $sversion = $substvars->get('source:Version')) =~ s/^\d+://;
  365. $dsc= "$uploadfilesdir/${sourcepackage}_${sversion}.dsc";
  366. open(CDATA, "<", $dsc) || syserr(_g("cannot open .dsc file %s"), $dsc);
  367. push(@sourcefiles,"${sourcepackage}_${sversion}.dsc");
  368. my $dsc_fields = parsecdata(\*CDATA, sprintf(_g("source control file %s"), $dsc),
  369. allow_pgp => 1);
  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. $origsrcmsg= _g("not including original source code in upload");
  417. @sourcefiles= grep(!m/\.orig\.tar\.$comp_regex$/,@sourcefiles);
  418. } else {
  419. if ($sourcestyle =~ m/d/ &&
  420. !grep(m/\.(debian\.tar|diff)\.$comp_regex$/,@sourcefiles)) {
  421. warning(_g("ignoring -sd option for native Debian package"));
  422. }
  423. $origsrcmsg= _g("including full source code in upload");
  424. }
  425. } else {
  426. $origsrcmsg= _g("binary-only upload - not including any source code");
  427. }
  428. print(STDERR "$progname: $origsrcmsg\n") ||
  429. syserr(_g("write original source message")) unless $quiet;
  430. $fields->{'Format'} = $substvars->get("Format");
  431. if (!defined($fields->{'Date'})) {
  432. chomp(my $date822 = `date -R`);
  433. $? && subprocerr("date -R");
  434. $fields->{'Date'}= $date822;
  435. }
  436. $fields->{'Binary'} = join(' ', map { $_->{'Package'} } $control->get_packages());
  437. unshift(@archvalues,'source') unless is_binaryonly;
  438. @archvalues = ('all') if $include == ARCH_INDEP;
  439. @archvalues = grep {!debarch_eq('all',$_)} @archvalues
  440. unless $include & ARCH_INDEP;
  441. $fields->{'Architecture'} = join(' ',@archvalues);
  442. $fields->{'Description'} = "\n ".join("\n ",sort @descriptions);
  443. $fields->{'Files'} = '';
  444. my %filedone;
  445. for my $f (@sourcefiles, @fileslistfiles) {
  446. next if ($include == ARCH_DEP and debarch_eq('all', $p2arch{$f2p{$f}}));
  447. next if ($include == ARCH_INDEP and not debarch_eq('all', $p2arch{$f2p{$f}}));
  448. next if $filedone{$f}++;
  449. my $uf = "$uploadfilesdir/$f";
  450. $checksum{$f} ||= {};
  451. getchecksums($uf, $checksum{$f}, \$size{$f});
  452. foreach my $alg (sort keys %{$checksum{$f}}) {
  453. $fields->{"Checksums-$alg"} .= "\n $checksum{$f}{$alg} $size{$f} $f";
  454. }
  455. $fields->{'Files'} .= "\n $checksum{$f}{md5} $size{$f} $f2sec{$f} $f2pri{$f} $f";
  456. }
  457. # redundant with the Files field
  458. delete $fields->{"Checksums-Md5"};
  459. $fields->{'Source'}= $sourcepackage;
  460. if ($fields->{'Version'} ne $substvars->get('source:Version')) {
  461. $fields->{'Source'} .= " (" . $substvars->get('source:Version') . ")";
  462. }
  463. $fields->{'Maintainer'} = $forcemaint if defined($forcemaint);
  464. $fields->{'Changed-By'} = $forcechangedby if defined($forcechangedby);
  465. for my $f (qw(Version Distribution Maintainer Changes)) {
  466. defined($fields->{$f}) ||
  467. error(_g("missing information for critical output field %s"), $f);
  468. }
  469. for my $f (qw(Urgency)) {
  470. defined($fields->{$f}) ||
  471. warning(_g("missing information for output field %s"), $f);
  472. }
  473. for my $f (keys %override) {
  474. $fields->{$f} = $override{$f};
  475. }
  476. for my $f (keys %remove) {
  477. delete $fields->{$f};
  478. }
  479. tied(%{$fields})->set_field_importance(@changes_fields);
  480. tied(%{$fields})->output(\*STDOUT); # Note: no substitution of variables