dpkg-genchanges.pl 16 KB

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