dpkg-genchanges.pl 15 KB

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