dpkg-genchanges.pl 16 KB

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