dpkg-genchanges.pl 15 KB

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