dpkg-genchanges.pl 16 KB

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