dpkg-genchanges.pl 16 KB

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