dpkg-genchanges.pl 16 KB

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