dpkg-genchanges.pl 15 KB

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